Author Topic: RPM Measurement  (Read 2421 times)

0 Members and 1 Guest are viewing this topic.

Offline yashrkTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: in
  • A MAKER, AN ENGINEER, A HOBBYIST FOR LIFE
    • My Personal Blog
RPM Measurement
« on: December 16, 2015, 06:39:32 pm »
Hey Guys,
         I am working on RMP measurement program  on arduino, most of the rpm measurement reference code runs for ever and I don't want that I want to implement it as function which I can call when ever RPM value is necessary. So I wrote the code and it did complied but I am waiting on my hall effect sensor to arrive so I cannot test it right now so I just want to show you guys the code and see if the general implementation is correct or not? Code certainly is not the finished as you will see I don't make any use of rpm value in the loop but right now I just want to figure out the rpm part is right.

Here is the code


boolean pool = 0;
unsigned long lastmillis = 0;
int rpm = 0;
volatile int rpmcount = 0;

void get_rpm(){
 
  lastmillis = millis(); //as its a funtion which can be called at any time we upadate current time
 
  attachInterrupt(0, rpm_fan, FALLING); //attach interrup

  while(pool==0){ //wait for 1 sec to complete in the ISR
    //do nothing
    }
  pool =0; //reset pool

  rpm = rpmcount *60; //convert to rpm
  rpmcount = 0;       //reset rpmcount
 
}


void rpm_fan(){ //things to do when there is an interrupt
 
  if (millis() - lastmillis == 1000){  //after one second
    detachInterrupt(0);    //Disable interrupt
    pool=1;
  }
  else rpmcount++;
 
}




void setup() {

}

void loop() {

  get_rpm();

}
Find me and things I'm working on - https://www.yashkudale.com/
 

Offline AndreasF

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: gb
    • mind-dump.net
Re: RPM Measurement
« Reply #1 on: December 16, 2015, 07:10:59 pm »
...
Code: [Select]
boolean pool = 0;
unsigned long lastmillis = 0;
int rpm = 0;
volatile int rpmcount = 0;

void get_rpm(){
 
  lastmillis = millis(); //as its a funtion which can be called at any time we upadate current time
 
  attachInterrupt(0, rpm_fan, FALLING); //attach interrup

  while(pool==0){ //wait for 1 sec to complete in the ISR
    //do nothing
    }
  pool =0; //reset pool

  rpm = rpmcount *60; //convert to rpm
  rpmcount = 0;       //reset rpmcount
 
}


void rpm_fan(){ //things to do when there is an interrupt
 
  if (millis() - lastmillis == 1000){  //after one second
    detachInterrupt(0);    //Disable interrupt
    pool=1;
  }
  else rpmcount++;
 
}




void setup() {

}

void loop() {

  get_rpm();

}
A few issues: since you only check if the the 1 second has elapsed in the interrupt, you might end up waiting forever for "pool" to turn to 1. Also since you test against equality between lastmillis and millis() in the interrupt, what if one interrupt triggers at millis()-lastmillis = 999 and the next one doesn't trigger until 1001?

Another problem you might encounter is that your interrupts take too long to process and thereby miss pulses from your hall effect sensor. If all you want to count such pulses there are specific modules to do that, not surprisingly called counters (often part of a timer peripheral). However, I don't know how well the Arduino environment supports it.

PS: use the [ code ] tags to make code easier to read.
my random ramblings mind-dump.net
 

Offline yashrkTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: in
  • A MAKER, AN ENGINEER, A HOBBYIST FOR LIFE
    • My Personal Blog
Re: RPM Measurement
« Reply #2 on: December 16, 2015, 07:38:24 pm »
For my application RPM is not that critical so I didn't bother trying to make counter work.

I guess the both problem can be avoided by using ">=" instead of "==" for comparing the millis with 1 sec

The max pulses estimated is around 90 pulses / sec

Will it still miss out on any pules while its executing if loop in ISR?
Find me and things I'm working on - https://www.yashkudale.com/
 

Offline AndreasF

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: gb
    • mind-dump.net
Re: RPM Measurement
« Reply #3 on: December 16, 2015, 09:21:37 pm »
Yes, I would definitely use ">=" or simply ">999"

With only about 90 pulses per second the interrupt routine probably works just fine. You may even consider continuously updating a pulse-to-pulse interval (PPI) in the ISR, which you can then use to calculate RPM on demand whenever you need it without having to wait for 1 second:


Code: [Select]
...
volatile long PPI = 0;  //the pulse-to-pulse interval in ms
unsigned int RPM;

...

//whenever you need it:
if PPI>0 {
    RPM = 60000/PPI;   //need to avoid division by 0
} else {
    RPM = 60000;  //or choose any other maximum value if pulses come in in less than 1ms intervals
}

void rpm_fan(){ //things to do when there is an interrupt

   static long lastmillis = 0;  //because of the static keyword this will only be initialized once, and will otherwise keep its last value
   long currentmillis;

   currentmillis = millis();  //you only want to call millis()
   PPI = currentmillis-lastmillis; 
   lastmillis = currentmillis;
 
}
For higher RPMs this may not be as accurate as counting pulses per second, for very low RPMs it might be more accurate.
« Last Edit: December 16, 2015, 09:28:33 pm by AndreasF »
my random ramblings mind-dump.net
 

Offline yashrkTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: in
  • A MAKER, AN ENGINEER, A HOBBYIST FOR LIFE
    • My Personal Blog
Re: RPM Measurement
« Reply #4 on: December 17, 2015, 04:12:59 pm »
Yeah thanx
Find me and things I'm working on - https://www.yashkudale.com/
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19280
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: RPM Measurement
« Reply #5 on: December 17, 2015, 11:52:00 pm »
So I wrote the code and it did complied but I am waiting on my hall effect sensor to arrive

That is a very common situation.

Quote
so I cannot test it right now

You need to think about what you mean by "it".

Clearly you cannot test the hall effect sensor.

You can test the algorithm if you simulate the sensor in software.

You can test the code if you use a signal generator where its signal is similar to that from the sensor.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf