# iRobot Create 2: Encoder Counts

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?

## 1 Answer

This is true. The encoders on the Create are square-wave, not quadrature. Therefore, they rely on the commanded direction to figure out which way the wheel is spinning. When driving forward, they count up, and when driving backward they count down as expected. But if you move the wheel when the robot is not moving, or move the wheel opposite the commanded direction, you will get incorrect results. The latest OI Spec available at www.irobot.com/create has a small discussion of this in the section for Opcode 43 on page 30. This was only recently updated, and contains a number of notes regarding gotchas like this. So you may want to re-download.

• Thanks for the confirmation. This seems like a serious limitation of the Create 2. Are there any solutions to the situation when the robot wheels are moved by an external force? Without better sensors, I can't think of a way to close the loop for control of the Create 2. – JSycamore May 13 '15 at 20:29
• Well, as long as the robot is driving itself around, you won't notice the issue. – Ben May 14 '15 at 2:11
• That is an excellent point that I didn't get until I read your comment in another post (robotics.stackexchange.com/questions/7251/…). I didn't realize that the encoder count was different when the wheels were driven rather than pushed. Thanks for your help. – JSycamore May 14 '15 at 19:55