0
$\begingroup$

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()){
    command=Serial.read();} 
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()) { 
  bytes[i++]=Roomba.read();
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);

}

so please kindly advice, Thank you

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Ahmed Sep 5 '18 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.