# "Updated" Help in iRobot create serial communication in Arduino

I am programming an iRobot Create to follow serial commands using Arduino Uno. I have written the library, and found the serial commands to move the robot forward in the iRobot manual, but I couldn't find the bytes for other movements (backward, right and left). Could you please help me with this. How can I move the robot backward, right and left. I will upload my code library.

#include "iRobot.h"

#if defined(ARDUINO) && ARDUINO >= 100  //to check if the arduino is plugged and the its number is above 100

#include "Arduino.h"

#include "SoftwareSerial.h"  // so we can use all pins
SoftwareSerial softSerial = SoftwareSerial(10, 11);

#endif

iRobot::iRobot() //constructor to set the pins
{
_rxPin = 10;
_txPin = 11;
}

void iRobot::begin()  //needs to be called inside setup function
{

delay(2000); // Needed to initialize the iRobot, the delay is to ensure that each command before this is excucted or there will be overlap

// define pin modes for software tx, rx pins for iRobot
pinMode(_rxPin, INPUT);
pinMode(_txPin, OUTPUT);

softSerial.begin(19200); //we set the data rate received by the irobot

Serial.begin(19200); // set the data rate sent from the arduino

//these two line are necessary from the irobot manual
softSerial.write(128); // This command starts the communication.
softSerial.write(131); // set mode to safe, it will stop of there is a cliff or a wheel drops or

Serial.write("Enter Command: "); // here, if we start serial monitor, we can enter the command
}

void iRobot::runIt()   //needs to be called inside loop function
{
if (Serial.available())
{
String data = String(Serial.read()); //this will read the command, each word will call a function

if(data == "forward")
goForward();
if(data == "backward")
goBackward();
if(data == "left")
goLeft();
if(data == "right")
goRight();
}
}

void iRobot::goForward()
{
softSerial.write(137); // Opcode number for DRIVE, it's understood by the irobot that 137 means drive

// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);

softSerial.write((byte)128); // we should adjust this to make the robot go straight or slightly right or left
softSerial.write((byte)0); // we should adjust this to make the robot go straight or slightly right or left
}

void iRobot::goBackward()
{
softSerial.write(137);
//we should change the bytes to make the robot drive backward
//negative vaule of velocity drive the robot forward
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);

softSerial.write((byte)128); // we should adjust this to make the robot go straight or slightly right or left
softSerial.write((byte)0); // we should adjust this to make the robot go straight or slightly right or left

}

void iRobot::goLeft()
{
softSerial.write(137);
//we should change the bytes to make the robot drive left
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);

softSerial.write((byte)128); // we should adjust this to make the robot go straight or slightly right or left
softSerial.write((byte)0); // we should adjust this to make the robot go straight or slightly right or left
}

void iRobot::goRight()
{
softSerial.write(137);
//we should change the bytes to make the robot drive right
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);

softSerial.write((byte)128); // we should adjust this to make the robot go straight or slightly right or left
softSerial.write((byte)0); // we should adjust this to make the robot go straight or slightly right or left
}


Update: I have connected the robot to the Arduino and tried the code. Unfortunately, the robot didn't move This is my Arduino code:

 #include <Arduino.h>
#include <iRobot.h>
iRobot irobot;

void setup() {
irobot.begin();
}

void loop() {
irobot.runIt();
}


I have connected the the pins 10, 11 and GND on the Arduino to pins 3, 4 and 7 on the robot.

Page 9 of the Open Interface specification gives the following instructions:

It then gives special cases for the radius:

Special cases:

Straight = 32768 or 32767 = hex 8000 or 7FFF

Turn in place clockwise = hex FFFF

Turn in place counter-clockwise = hex 0001

Each portion, speed and radius, are two 16-bit signed values. Your robot does the same thing for forward/backward/left/right because you have copy/pasted the same code in each section. You don't have anything different in any of the code segments.

Here is a converter that will do the decimal to hex conversion for you - be sure to select 'signed 16-bit' from the dropdown box. Once you have the four digit hex number, the 'high' byte is the first two digits and the 'low' byte is the last two digits. Plug each pair into this converter to get the decimal value you should write.

So, for example, let's say you want to use a speed of 200 mm/s for all cases. Your code should look like the following:

void iRobot::goForward()
{
// Drive op code
softSerial.write(137);
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);
softSerial.write((byte)128);
softSerial.write((byte)0);
}
void iRobot::goBackward()
{
// Drive op code
softSerial.write(137);
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)255);
softSerial.write((byte)56);
softSerial.write((byte)128);
softSerial.write((byte)0);
}
void iRobot::goLeft()
{
// Drive op code
softSerial.write(137);
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);
softSerial.write((byte)0);
softSerial.write((byte)1);
}
void iRobot::goRight()
{
// Drive op code
softSerial.write(137);
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);