This post is a follows from an earlier post (iRobot Create 2: Angle Measurement). I have been trying to use the wheel encoders to calculate the angle of the Create 2. I am using an Arduino Uno to interface with the robot.
I use the following code to obtain the encoder values. A serial monitor is used to view the encoder counts.
void updateSensors() {
Roomba.write(byte(149)); // request encoder counts
Roomba.write(byte(2));
Roomba.write(byte(43));
Roomba.write(byte(44));
delay(100); // wait for sensors
int i=0;
while(Roomba.available()) {
sensorbytes[i++] = Roomba.read(); //read values into signed char array
}
//merge upper and lower bytes
right_encoder=(int)(sensorbytes[2] << 8)|(int)(sensorbytes[3]&0xFF);
left_encoder=int((sensorbytes[0] << 8))|(int(sensorbytes[1])&0xFF);
angle=((right_encoder*72*3.14/508.8)-(left_encoder*72*3.14/508.8))/235;
}
The code above prints out the encoder counts; however, when the wheels are spun backwards, the count increases and will never decrement. Tethered connection to the Create 2 using RealTerm exhibits the same behavior; this suggests that the encoders do not keep track of the direction of the spin. Is this true?