Reading RPM, and Timer?

What could be included in further releases, or for the forum.
User avatar
ahull
Posts: 1422
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Reading RPM, and Timer?

Postby ahull » Thu Aug 18, 2016 8:48 pm

Maybe I'm mistaken, but is it not...

Code: Select all

// RPM = 60 * (1/timediff in seconds)
RPM = 60*(1000000/(micros() - oldTime )) ; // i.e.  RPM = 60 seconds divided by (1000000/(timediff uS)) 

Also watch out for millis() and micros() wrapping around, there is no check in your code for this..

Actually you could create an array of RPM samples (say 10), and average all those that are within an order of magnitude of each other, add your latest sample in to the sample_number mod 10 position each time you sample, so you overwrite the oldest.. when the millis()/micros() wraps around, the spurious sample should be automatically discarded, and will only last for 10 samples anyway.
Last edited by ahull on Thu Aug 18, 2016 9:57 pm, edited 3 times in total.
- Andy Hull -

Nutsy
Posts: 209
Joined: Sun Jul 24, 2016 4:26 pm

Re: Reading RPM, and Timer?

Postby Nutsy » Thu Aug 18, 2016 9:47 pm

How often do millis and micros wrap round?

User avatar
ahull
Posts: 1422
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Reading RPM, and Timer?

Postby ahull » Thu Aug 18, 2016 9:56 pm

Nutsy wrote:How often do millis and micros wrap round?


https://www.arduino.cc/en/Reference/Millis
https://www.arduino.cc/en/Reference/Micros

The STM32duino version may be different.. however whatever the period is, you should be aware of it.

You may think that your users will never encounter the issue, but as the old adage goes, there is no such standard as "Idiot proof", because there is no such thing as a standard idiot.

Always design for the problems you are aware of, and be aware that your users will find most of the ones you were not aware of. :D
- Andy Hull -

User avatar
martinayotte
Posts: 1163
Joined: Mon Apr 27, 2015 1:45 pm

Re: Reading RPM, and Timer?

Postby martinayotte » Thu Aug 18, 2016 10:02 pm

it is quite easy to detect the overflow :

Code: Select all

if (micros() < oldTime) {
   // handle the overflow (or throw away this sample)
}

victor_pv
Posts: 1077
Joined: Mon Apr 27, 2015 12:12 pm

Re: Reading RPM, and Timer?

Postby victor_pv » Tue Aug 30, 2016 4:15 am

I would use a hardware timer in input capture mode.
You can either set the timer to generate an interrupt when it overflows, and compare the time between overflows, or just read the timer value every X milliseconds and compare the timer counter increase in that period. You can also set pre-escalers. It all depends how many RPMs you are measuring and how much variance is between the minimum and the maximum speed.
If you are measuring a high RPM value, polling a pin or serving and interrupt per revolution would consume a lot of cpu time, while with a hardware timer you dont consume any cpu time to count the cicles, only to check the timer value as often as you want, or to server the timer interrupt on overflow, which can be more or less frequently depending on the pre-escaler and the overflow value.

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Reading RPM, and Timer?

Postby RogerClark » Tue Aug 30, 2016 4:22 am

Welcome back @victor_pv ;-)

I did mention Input Capture in one of my replies to this thread viewtopic.php?f=17&t=1322#p17059 but I think @nutsy hoped he could do it just using an ISR

But it does now sound like the Input Capture method would give a better value that counting over a known time period (as the number of pulses is too few)

Nutsy
Posts: 209
Joined: Sun Jul 24, 2016 4:26 pm

Re: Reading RPM, and Timer?

Postby Nutsy » Tue Aug 30, 2016 10:48 pm

Sorry for my late reply been busy with all sorts. But not sure anyone noticed. I even put my project on hackaday...

Well the interrupt counting seems to be working well enough currently.

I thought I had a suitable protection transistor to test the real RPM signal but i might have wired it up wrong and pin 12 has died :p

I've designed and ordered parts for a opto isolator setup to read the rpm safely... Then ill test again with another pin...


Return to “Ideas & suggestions”

Who is online

Users browsing this forum: No registered users and 1 guest