Over the past few weeks, I have been attempting to interface the iRobot Create 2 with an Arduino Uno. As of yet, I have been unable to read sensor values back to the Arduino. I will describe by hardware setup and my Arduino code, then ask several questions; hopefully, answers to these questions will be helpful for future work with the Create 2.
Hardware: The iRobot Create 2 is connected to the Arduino Uno according to the suggestions given by iRobot. Instead of the diodes, a DC buck converter is used, and the transistor is not used because a software serial port is used instead of the UART port.
Software: The following is the code that I am implementing on the Arduino. The overall function is to stop spinning the robot once the angle of the robot exceeds some threshold. A software serial port is used, which runs at the default Create 2 Baud rate.
#include <SoftwareSerial.h>
int rxPin=3;
int txPin=4;
int ddPin=5; //device detect
int sensorbytes[2]; //array to store encoder counts
int angle;
const float pi=3.1415926;
#define left_encoder (sensorbytes[0])
#define right_encoder (sensorbytes[1])
SoftwareSerial Roomba(rxPin,txPin);
void setup() {
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(ledPin, OUTPUT);
Roomba.begin(19200);
// wake up the robot
digitalWrite(ddPin, HIGH);
delay(100);
digitalWrite(ddPin, LOW);
delay(500);
digitalWrite(ddPin, HIGH);
delay(2000);
Roomba.write(byte(128)); //Start
Roomba.write(byte(131)); //Safe mode
updateSensors();
// Spin slowly
Roomba.write(byte(145));
Roomba.write(byte(0x00));
Roomba.write(byte(0x0B));
Roomba.write(byte(0xFF));
Roomba.write(byte(0xF5));
}
void loop() {
updateSensors();
// stop if angle is greater than 360 degrees
if(abs(angle)>2*pi){
Roomba.write(173);
delay(100);
}
}
void updateSensors() {
// call for the left and right encoder counts
Roomba.write(byte(148));
Roomba.write(byte(2));
Roomba.write(byte(43));
Roomba.write(byte(44));
delay(100);
// load encoder counts into an array
int i = 0;
while(Roomba.available()) {
int c = Roomba.read();
sensorbytes[i] = c;
i++;
}
angle=((right_encoder*72*pi/508.8)-(left_encoder*72*pi/508.8))/235;
}
Questions:
- Am I loading the sensor values into the array correctly? This same code works when a bump and run program is implemented, but that requires knowing only one bit rather than two bytes.
- How many bytes can be read over the serial connection at a time? A previous post (Help sending serial command to Roomba) highlights that one byte can be sent at a time. Does this imply that the reverse is true? If so, would a solution be to use a char array to read the values instead and then to append two chars to form an signed int?
- Is serial communication synchronization a problem? I am assuming that synchronization is not a problem, but is it possible for the bytes to be split on the nibble boundaries? This would present a problem because there is not a nibble datatype.