# Roomba 500 Series - Raw encoder counts readings

I have been trying to read Roomba raw encoder counts I am receiving values, but I have a sudden increase/decrease in values as can be seen over here.

142 152

153 166

167 180

181 196

50101 33280

207 223

222 238

236 252

#include <SoftwareSerial.h>
#include <Wire.h>
// Roomba Create2 connection
int rxPin = 10;
int txPin = 11;
SoftwareSerial Roomba(rxPin, txPin);
void setup() {

pinMode(10, INPUT);
pinMode(11, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(115200);
Roomba.begin(19200);
delay(1000);
Roomba.write(128);  //Start
Roomba.write(132);  //Full mode
delay(1000);
}
char command;
void loop() {

if (Serial.available()){
switch (command)
{
case '8':
Roomba.write(byte(145));
Roomba.write(byte(0));
Roomba.write(byte(0x28));
Roomba.write(byte(0));
Roomba.write(byte(0x28));
delay(50);
encoder_counts();
break;
case '0':
Roomba.write(byte(145));
Roomba.write(byte(0));
Roomba.write(byte(0));
Roomba.write(byte(0));
Roomba.write(byte(0));
break;
}
}

void encoder_counts()
{
unsigned int  right_encoder;
unsigned int   left_encoder;
byte  bytes[4];

Roomba.write(byte(149));
Roomba.write(byte(2));
Roomba.write(byte(43));
Roomba.write(byte(44));
delay(50);
int i=0;
while(Roomba.available()) {
left_encoder = (unsigned int)(bytes[0] << 8)|(unsigned int)(bytes[1]&0xFF);
right_encoder =(unsigned  int)(bytes[2] << 8)|(unsigned int)(bytes[3]&0xFF);
}

Serial.print(right_encoder);
Serial.print("  ");
Serial.println(left_encoder);

}


Welcome to Robotics, Ahmed. You have posted the code you use to build the encoder counts, but not the code you used to display the counts. I mention this because you have the encoder counts displayed in your post as:

13786.00 13938.00

as in, with a trailing decimal, but your encoder counts are defined in your code to be integers. This could be problematic if the print function is treating your two-byte integer as the first two bytes of a floating point number.

The second issue it looks like you've got is that you're defining your encoder variables as int instead of unsigned int. Per the manual,

encoder [count] is returned as an unsigned 16-bit number, high byte first

Try redefining your right_encoder and left_encoder as unsigned int, and then also try changing the formatting in your print command to something like Serial.print(right_encoder) instead of Serial.print(right_encoder, 2).

If that doesn't work, please edit your question to post the code you're using to display the values.

• first of all, I really appreciate your time in replying to my post. Thank you! thank you so much for pointing out the correct manual to use, I have been using completely different manual mentioning it returns signed values from -32k to 32k. Now I edited the question with the code I'm using – Ahmed Sep 5 '18 at 20:16