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
}