Main Menu

How I'm calculating RPM

Started by Rx7man, June 17, 2015, 10:57:21 PM

Rx7man

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
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles