NXT Frequency counter program update

I added to the program a little feature, and it makes a huge difference with the stability of the output value. Before, it would check CurrentTick, and then let TachoCount count up until CurrentTick was greater than or equal to LastTick plus the measurement timeframe. However, CurrentTick only has resolution of 1ms, so what if it based it’s LastTick at an extreme side of the ms? For example, from 18394.0000 to 18394.9999, it would return 18394. Well, that would normally not be an issue. However, the until loop that waits until CurrentTick >= LastTick + TimeFrame will continue on at (ideally) the same instant as (CurrentTick >= LastTick + TimeFrame)==true.

So, lets say we are using a TimeFrame of 100 (0.1 seconds). If at 18394.0000, it sets LastTick to 18394, then the until statement waits until CurrentTick>= 18394 + 100, or 18494. That is good. 18494-18394=100, perfect. But, what if it sets LastTick when the CurrentTick is at 18394.9999? It will still set it to 18394, but then the until statement still waits until 18494. The problem, is that 18494-18394.9999 does not equal 100, it equals 99.0001. That value of potentially up to (but not including) 1ms, can cause a huge difference in the measurements (with a timeframe of 100, that is up to a 1% error). This causes jittery numbers, and is inaccurate.

The solution I came up with , is to use two until statements. One is to base the LastTick at the exact same point of the CurrentTick every time. What I mean, is it (ideally) always sets LastTick at the instant that CurrentTick equals 18394.0000. Here is how I do it:

LastTick=CurrentTick();
until(CurrentTick()==LastTick+2);
LastTick=CurrentTick();

And then I continue, resetting TachoCount etc. like this:

LastTick=CurrentTick();
until(CurrentTick()==LastTick+2);
LastTick=CurrentTick();
ResetTachoCount(OUT_A);
until(CurrentTick()>=LastTick+TimeFrame);
Frequency=MotorTachoCount(OUT_A);
Frequency*=2;

Now I get super stable values. They vary by very very little amounts. For example, I used a 555 with just typical resistors etc., and a pot to adjust the frequency. I set it at around 15kHz, and the NXT reads different values in the range of 15116-15120! What’s more, just breathing across the 555 circuit is enough to get the values up to 15220, that’s 100 Hz difference just by breathing on it! I am very impressed by the results.

Advertisements
This entry was posted in Electronics, Mindstorms, NXT and tagged , , , . Bookmark the permalink.

One Response to NXT Frequency counter program update

  1. SavedCoder says:

    WOW! That’s so cool!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s