WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

getDurationOfLastTick

Returns the duration, in μS, of the last received tick.
Among other uses - this could be used to calculate the current RPM of a motor.
Syntax
Encoder. getDurationOfLastTick()
Where Encoder is the name you have given to the device in Project Designer.
Parameters
None
Returns
TICK_COUNT
Example
To calculate the current RPM of a motor then:-
// Read encoder
myEncoder.read();
 
// Can only do this if interpolation is turned on and we have a tick
TICK_COUNT usPerTick = getDurationOfLastTick();
if( myEncoder.isInterpolating() && usPerTick != 0 ){
    uint32_t uSPerMinute = 60000000; // 60 milllion
    uint32_t ticksPerMinute = usPerMinute / usPerTick;
    uint32_t revsPerMinute = ticksPerMinute / ticksPerRevolution();
    cout << "RPM:" << revsPerMinute << '\n';
}
The following code will guess the current fraction of a tick. This is only useful for low resolution encoders. The guess is only accurate if the current speed is fairly constant. The logic is: if the previous tick tool 100μS and happend 10μS ago then we are about 10% into the next tick.
TICK_COUNT guess = myEncoder.getTimeSinceLastTick();
guess *= 100; // Coz we calc a percentage
guess /= myEncoder.getDurationOfLastTick();
uint8_t percent = (guess > 100) ? 100 : guess;
This will return the fraction as a percentage from 0 to 100%.

 

 

Valid XHTML 1.0 Transitional