Getting Distance values from Roomba robot.

Hi,
I am facing problem in extracting the correct values from roomba. I am following iRobot_Roomba_500_Open_Interface_Spec manual and with that I am first sending 142 byte to roomba and then 19 packet for getting the distance value the roomba travel.

As per this manual the roomba gives distance values in milimeter But the problem I am facing is the when roomba moves forward I am getting negative values and when roomba move backward I am getting positive values but it should be other way around. ANd the next problem I am facing is that I am getting values which did not make any sense. Like I am getting when roomba move forward, -43 which I measure practicaly with scale that it travel 14 inches
here is my code.

#define clamp(value, min, max) (value < min ? min : value > max ? max : value)
void driveWheels(int right, int left)
{
  clamp(right, -500, 500);
  clamp(left, -500, 500);


  Roomba.write(145);
  Roomba.write(right >> 8);
  Roomba.write(right & 0xff);
  Roomba.write(left >> 8);
  Roomba.write(left & 0xff);
  //Serial.println("Chal bhai");
}

void Rdistance() {

  Roomba.write(142);
  delay(100);
  Roomba.write(19);
  delay(100);

  while ( Roomba.available() ) {
    
    k[vel] = Roomba.read();
    Serial.print(F("velovitz loop"));
    Serial.println(k[vel]);
        vel++;    

  }
  vel=0;
  val=((int)k[0] << 8 | (0xff & (int)k[1]));
  
  Serial.print(F("Distance is mili meters :"));
   Serial.println(val);

}

Void setup{
Serial.begin(115200);
Roomba.begin(115200);
}

void loop{
driveWheel(100,100)
if(x==100){
Rdistance(); // measure distance
}
x++
}
  clamp(right, -500, 500);
  clamp(left, -500, 500);

These two lines do nothing. Perhaps you meant:

  right = clamp(right, -500, 500);
  left = clamp(left, -500, 500);

There is also a built-in 'constrain()' function that does the same thing as your 'clamp' macro.

Your sketch is quite incomplete so it won't compile.

What is showing on Serial Monitor next to "velovitz loop"? That would tell you what characters arrived.

what is the data type of the k array?

Yeah here is the output. When I drive it with 100mm/sec .......................... velovitz loop 255 velovitz loop 193 Distance im mili meters :-63 .....................

The roomba sending 2 byte signed. Now First confusion of mine is that it should be positive, and the second thing is what this value means, as per the manual it should be mili meter but it is different when I measure practically. I also find the Diameter of the wheel which is 65mm and with that I find the distance cover by one rotation of wheel is equal to its circumference.

Juraj:
what is the data type of the k array?

k and vel are byte while val is int. for storing signed values.

255 is FF

193 is C1

FFC1 is 65473

this is more the int can take. try long

But I have to plot signed values rather then decimal, and the sensor value gives in the Range: -32768 – 32767

"Distance. Packet ID: 19. Data Bytes: 2, signed. The distance that Roomba has traveled in millimeters since the distance it was last requested".

Get the distance BEFORE you tell it to move so that when you ask again you get the distance it has moved.