Lil' Blackbox

Coding => v1.0 - v1.1 => Topic started by: Rx7man on June 17, 2015, 10:57:21 PM

Title: How I'm calculating RPM
Post by: Rx7man on June 17, 2015, 10:57:21 PM
Here's how I'm calculating RPM...

'TSScounts' is a volatile variable that's incremented in the interrupt triggered by the falling edge of a pin connected to the Max9924 or whatever.

the TSS variable is a class I made that automagically smoothes the inputs, calculates slope, etc when you call the "Update" function on it... the "Reset" function will bypass the smoothing and reset everything to 0... the deep parts of it aren't of much interest to the actual calculations

'TSSminCounts' is the minimum number of revolutions before recalculating.. a moderate number like 3-10 should be good
'TSStimeout' is the maximum amount of time without having reached TSSminCounts before considering the RPM 0.. depending on the size of TSSminCounts, 100,000 microseconds should be sufficient.



void CalculateTSS() {
  static unsigned long LastMicros = micros();
  unsigned long Now = micros();
  unsigned long Timespan = Now - LastMicros;
  int counts = TSScounts; //Assign to a new variable in case it gets incremented unexpectedly here by the interrupt

    if (counts > TSSminCounts) {//only update value if we hae enough counts to do something with..
//rather increase the timespan (by waiting until the next go round) to get a closer approximation
    TSScounts = 0;
    LastMicros = Now;
   
    TSS.UpdateValue(60000000 / Timespan * counts);
  }
  else if (Timespan > TSStimeout) { // a second has elapsed without accumulating enough counts, thus the turbine is stopped
    TSS.Reset();
    LastMicros = Now;
    TSScounts = 0;
  }
}


I tested this by running a strobe on an output and it seemed to work really nicely