Question about code for RPM

Development & General Chat for the superb openxvario project.

Moderator: rainer

Post Reply
Christoph
Posts: 10
Joined: Tue Apr 14, 2015 5:56 pm
Country: Germany

Question about code for RPM

Post by Christoph »

Hi,
i've installed a oxs in my heli an tryed to get correct values from a rpm-sensor (hall-sensor TLE4905, two magnets on maingear).
Actual i have some problems to understand the code inside oXs_general.cpp

Code: Select all

ISR( TIMER1_CAPT_vect, ISR_NOBLOCK )
{
	uint16_t elapsed ;
	uint32_t eventTime ;
	uint32_t difference ;

	if ( ++RpmCounter > 3 )
	{
		cli() ;
		uint16_t time = ICR1 ;	// Read timer 1
		sei() ;
		elapsed = time - lastTimerValue ;
  #if F_CPU == 20000000L   // 20MHz clock 
   #error Unsupported clock speed
  #elif F_CPU == 16000000L  // 16MHz clock                                                  
    		eventTime = TotalMicros + ( elapsed >> 4 ) ;
  #elif F_CPU == 8000000L   // 8MHz clock
  		eventTime = TotalMicros + ( elapsed >> 3 ) ;
    #else
    #error Unsupported clock speed
  #endif

		//eventTime = TotalMicros + ( elapsed >> 4 ) ;
		RpmCounter = 0 ;
		difference = eventTime - lastEventTime ;
		lastEventTime = eventTime ;
		if ( difference > 200 )
		{
			RpmValue = 4000000 / difference ;
		}
		else
		{
			RpmValue = 0 ;
		}
		RpmSet = true ;
	}
}

#endif // MEASURE_RPM
I understand, wait for more than 3 (so 4 and more) counts on the "RpmCounter",
calculate the difference betwen now and the time of the former calculation,
when it is bigger than 200 then "RpmValue = 4000000 / difference".
Why 4.000.000? I've compared the shown value on Taranis screen and a frequence-meter (signal from hall-sensor) and thats (nearly) the same. But not rotations per minute, the value is in Hz.

I've tryed to understand this number, but it's to high for me. :?:

From my point of view this is my favorit code:
RpmValue = 60*1000/(difference)*RpmCounter

Best regards
Christoph (from Germany)

mstrens
Posts: 1435
Joined: Fri Dec 27, 2013 7:49 pm
Country: -

Re: Question about code for RPM

Post by mstrens »

I think the code works with following logic:
- each time the RPM sensor send a pulse, OXS get an interrupt (by the timer 1 capture vector)
- OXS wait for 4 interrupts (in order to get some kind of average)
- on the 4th interrupt, OXS calculates the number of enlapsed micro seconds. Because timer1 is only 16 bits, OXS uses also some variables in 32 bits (named TotalMicros and lastTimerValue that are used in micros())
- OXS does ( elapsed >> 4) because timer 1 runs at 16 mhz and increase by 16 for 1 microsecond. So in order to get microsecond, the value from timer 1 has to be divided by 16 (this is achieved by the instruction >>4)
- so, difference is the number of micro seconds between 2 sets of 4 pulses.
- rpm value (in fact the name is wrong because it is the number of pulses per second) should be 1000000 / (difference / 4) . It must be divided by 4 because oXs calcutes the delay for 4 pulses and not 1. This is the same as calculating 1000000 * 4 / difference; so finally the same as 4000000 / difference.
I presume that the original Frsky sensor sent the number of pulses per second and it is openTx that converts it into rotations per minute taking care of the number of blades (parameter to set up on Tx side).

I hope this explanation makes it clear.
Christoph
Posts: 10
Joined: Tue Apr 14, 2015 5:56 pm
Country: Germany

Re: Question about code for RPM

Post by Christoph »

Thanks mstrens.
Now i understand the value "1.000.000" --> it's from microsecond. and the difference is also in microsecond's.
I presume that the original Frsky sensor sent the number of pulses per second and it is openTx that converts it into rotations per minute taking care of the number of blades (parameter to set up on Tx side).
To see the right value on Taranis-screen i need to multiply with 30 (2 impulse per rotation). I think, in the new version of opneTX this calculation can be done in the Taranis.

Best regards
Christoph

Post Reply

Return to “OpenXVario - an open source vario supported by the open source firmwares!!”