2
$\begingroup$

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);


//Radius (-2000 - 2000 mm)
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);


//Radius (-2000 - 2000 mm)
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
 //radius value should be positive
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);


//Radius (-2000 - 2000 mm)
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
 //radius value should be negative
// Velocity (-500 – 500 mm/s)
softSerial.write((byte)0);
softSerial.write((byte)200);


//Radius (-2000 - 2000 mm)
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. enter image description here

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

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

Serial sequence: [137][Velocity high byte][Velocity low byte][Radius high byte][Radius low byte]

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);
//Radius (-2000 - 2000 mm)
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);
//Radius (-2000 - 2000 mm)
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);
//Radius (-2000 - 2000 mm)
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);
//Radius (-2000 - 2000 mm)
softSerial.write((byte)255);
softSerial.write((byte)255);
}

I'll mention here that the Create won't "strafe" or move left/right while facing forwards. It only moves forwards. It can turn while moving forward, such that it follows an arc, but that's the only "goLeft" or "goRight" you get. As I've written it above, goLeft and goRight are both zero-radius turns.

$\endgroup$
1
  • $\begingroup$ Thank you for your help. I have tried this code but it the robot didn't move. I don't know what is missing. $\endgroup$ Commented Nov 26, 2016 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.