Log in

View Full Version : Kevin Watson's encoder code with RPM output


MaxM
04-02-2005, 22:35
I'm the programmer on Team 473. I've been trying to use Kevin Watson's encoder code to measure the RPMs of our transmission in order to write a shift scheduler. I've been trying to do this by getting the number of encoder counts which occur over 25 cycles of Process_Data_From_Master_uP. Since Process_Data_From_Master_uP runs every 26.2 ms, I can convert from rpms into encoder cycles per Process_Data_From_Master_uP runs. However, this is not giving me anything near an accurate RPM output (I have a digital tachometer to check the RPMs with).

Here's my code from Process_Data_From_Master_uP:

if (++counter >= (24))
{
Left_Encoder_Count = Get_Left_Encoder_Count();
printf("RPM Left: %5d\n",(int)((Left_Encoder_Count - previous_count_left)*0.698666667));
previous_count_left = Left_Encoder_Count;
counter = 0;
}


0.698666667 is the conversion factor counts per 25 Process_Data_From_Master_uP cycle to RPMs. (1 rotation/1 min*1 min/60 sec*1 sec/1000ms*655 ms (25*26.2ms)/1 program cycle*65 counts/1 rotation)

Either my conversion factor is totally wrong or my method is totally wrong (or my tachometer is wrong, but since it's digital I don't think it is). Can anyone help?

Thanks,

Mike Betts
04-02-2005, 23:40
Two things...

1. The cycle time of 26.2 ms is approximate. To get accurate timing, write a timer interrupt routine.

2. Integer arithemetic! You are multiplying by 1... Try doing the math outside of the printf. Multiply by 6987 and store into a long int variable. Now divide by 10000 and store in an int variable. Now printf your int.

Regards,

Kevin Watson
05-02-2005, 00:06
...Integer arithemetic! You are multiplying by 1... Try doing the math outside of the printf. Multiply by 6987 and store into a long int variable. Now divide by 10000 and store in an int variable. Now printf your int.
Or multiply by 45788 (2^16 * 0.6986666...) and then right shift sixteen bits or just grab the high order sixteen bits of the thirty-two bit long. Man, I dig this stuff <grin>.

-Kevin