Some help with speedometer code

I wrote a basic speedometer code using the shaft encoders a while ago and am just now getting to try it out. The problem however is that the program doesn’t want to seem to update the printtoscreen at the end of the big while loop:


#include "UserAPI.h"

unsigned int opticalencoder1 = 0;
unsigned int opticalencoder2 = 0;
double rate = 0.0;
double speed = 0.0;
double period = 0.0;
double speedmph = 0.0;

void main(void)
{
   PresetEncoder(1,0);
   StartEncoder(1);
   while(1==1)
   {
      
      opticalencoder1 = GetEncoder(1);
      Wait(100);
      opticalencoder2 = GetEncoder(1);
      if(opticalencoder2 == opticalencoder1)
      {
         rate = 1;
      }
      else
      {
         rate = (opticalencoder2 - opticalencoder1);
      }
      rate = (opticalencoder2 - opticalencoder1);
      period = (rate * 10);
      speed = (((2*3.1415926535)*2.625)/period);
      speedmph = (speed*3600)/63360;
      PrintToScreen("Speed in Miles Per Hour =%d/n" . (int)speedmph);
      StopEncoder(1);
   }
}

I’ve tried several alterations including placing the preset, start, and stop encoder commands outside the while loop, but the same result. It’s also just giving me a “Speed in Miles Per Hour = 32767” instead of changing the values any.
Thanks for any help,
Matt

***[EDIT] I’ve discovered that it might give me a divide by 0 error of some sort if there’s no movement, but I changed the presetEncoder to be 1 so that shouldn’t happen now.

[EDIT2] I’ve discovered that the divide by zero error wouldn’t appear where I thought it would. The problem was in the subtraction so I’ve added an if-else to take care of that. The speed still isn’t updating yet so I may need still need some help.

Try rounding 3.1415926535 so that it has about 6 or 7 digits.

I’m not sure that’s the problem, but rather than rounding, 22/7 is so close to pi that it’s not going to make any difference.

1 (foot per second) = 0.681818182 miles per hour (thanks Google) so until you’re going about 1.5 fps your screen is going to display 0 MPH if I’m reading your code correctly. .99999 mph is still 0 mph as far as your code is concerned.

Should that be %d\n instead of %d/n ?

You don’t start the encoder again after stopping it.

If, at the end of the above, what you have doesn’t work, I suggest the following.

  1. Throw away code until you have GetEncoder(1) displaying on the screen.
  2. Add the wait and the second GetEncoder and display that on the screen.
  3. Display the difference between them on the screen.
  4. Add the math to display feet per second on the screen. Consider putting wheel diameter as a variable in case you decide to reuse this code later.

Actually, now that I describe that, I’m convinced you’ve lost me on the math. The greater the difference between the two encoders, the greater the rate. The period is 10 * rate, so the greater the difference, the greater the period. We then divide by the period to get the speed, so the greater the difference, the slower you’re going.

That doesn’t seem right to me, but I might have missed something.

  1. Add the math to display MPH.

Developing the code in chunks like that and testing every chunk as it’s written makes it a lot easier to understand where any errors are, ESPECIALLY if you save off working partial versions of the code to go back to if you need to.

In a way, it doesn’t matter if it’s code or a robot, making and testing a small piece allows you to focus on that piece and look for flaws. If you build the whole thing at once, little opportunities for improvement are often missed and little design errors get a chance to compound and become bigger problems.