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