How can i modify an orangutan library function?

Hi everyone,
I’m using Orangutan SVP 1284 with the Pololu 131:1 Gearmotor with encoder. I wrote a program to read the position of the encoder. The problems come when the encoder counts raises the number 32767(or -32768). I think that this happens because the function that i used (declared in OrangutanSVP.h), returns an ‘int’ variable. I would like to know how i can modify the libpolol-avr library to have a return ‘long’ variable instead.
Here’s my code:

#include <pololu/orangutan.h>

long encoder0Pos = 0;

int main()
{
  svp_set_mode(SVP_MODE_ENCODERS);
  while(1)
  {
    encoder0Pos = svp_get_counts_ab();
    lcd_goto_xy(0,0);
    print_long(encoder0Pos);
    print_from_program_space(PSTR("     "));
  }
}

Thanks for your help

Hello,

Instead of modifying the library function, you could keep track of the total encoder count yourself using a long, and reset the measured count every time you take a reading, by using the svp_get_counts_and_reset_ab() function:

long encoder0Pos = 0;
...
encoder0Pos += svp_get_counts_and_reset_ab();

However, all this means is that the total encoder count will take longer to overflow (2147483647 or –2147483648). Maybe that increase is enough to prevent the overflow from happening within the time that you expect your robot to be running, but a better solution might be to find a way to avoid having to keep track of the total count at all. For example, if you are doing PID control of two motors based on the difference between the encoder counts, all you really need to know is how much each encoder has changed since you last read them (which you could use the “reset” versions of the functions for), not how much they have changed since your robot was turned on.

- Kevin

Kevin thank you very much for your help :smiley:
This evening i’m going to fix my code

Perfect!! My program works really fine! Thank you again :smiley: