Sunday, January 4, 2009

iRobot C Sharp Create interface

I've found this great interface for the iRobot Create.
Here is the link

CreateOI interface


I've spent tonight extending the interface by adding a command that was left out that allows the iRobot Create to have each of its wheels controlled independently instead of dealing with having to figure out the weird arch system.

So first download the code from sourceforge, it will unzip into a folder named CreateOI, with CreateOI_Examples, iRobot, Kevin_Logging, and Visualizer as subfolders.
In order to add a command to the irobot.dll interface we need to go down a few levels to iRobot\iRobot folder

Now open irobot.csproj in Visual Studio.

The dll is broken down very hierarchically and is meant to work for both the roomba and the irobot create. In order to add my DirectDrive function I went to the Types.cs
under the CreateOI folder and added
public const byte DriveDirect = 145;
after
public const byte Pause_Resume_Stream = 145;

Then under Create.cs under Chasis folder

I added my DriveDirect function which is as following

///
/// This command lets you control the forward and backward
/// motion of Create’s drive wheels independently. It takes
/// four data bytes, which are interpreted as two 16-bit signed
/// values using two’s complement. The first two bytes specify
/// the velocity of the right wheel in millimeters per second
/// (mm/s), with the high byte sent first. The next two bytes
/// specify the velocity of the left wheel, in the same format.
/// A positive velocity makes that wheel drive forward, while a
/// negative velocity makes it drive backward.
/// • Serial sequence: [145] [Right velocity high byte]
/// [Right velocity low byte] [Left velocity high byte]
/// [Left velocity low byte]
/// • Available in modes: Safe or Full
/// • Changes mode to: No Change
/// • Drive Direct data byte 1: Right wheel velocity
/// (-500 – 500 mm/s)
/// • Drive Direct data byte 1: Left wheel velocity
/// (-500 – 500 mm/s)
///

///
///
///
public bool DriveDirect(Int16 leftVelocity, Int16 rightVelocity)
{
this.ErrorText = "";

bool bSafe = this.Mode == OI_Mode.Safe;
bool bFull = this.Mode == OI_Mode.Full;
bool bError = (!bSafe) & (!bFull);

//Push back at the user
if (bError)
{
throw new RoombaException("Roomba must be in Safe or Full mode before running Calling the Drive Function: ");
}

//Sample from the SCI Spec:
//Serial sequence: [145] [Right velocity high byte][Right velocity low byte] [Left velocity high byte][Left velocity low byte]


//divide up leftVelocity & rightVelocity into 2 bytes each
byte leftVelocityHi = (byte)( leftVelocity >> 8);
byte leftVelocityLo = (byte)( leftVelocity & 255);

byte rightVelocityHi = (byte)(rightVelocity >> 8);
byte rightVelocityLo = (byte)(rightVelocity & 255);

bool bSuccess = false;

List lSend = new List();
lSend.Add(CreateOI.OpCode.DriveDirect);
lSend.Add(rightVelocityHi);
lSend.Add(rightVelocityLo);
lSend.Add(leftVelocityHi);
lSend.Add(leftVelocityLo);

string sDebugSend = "[" + lSend[0].ToString() + "][" + lSend[1].ToString() + "][" + lSend[2].ToString() + "][" + lSend[3].ToString() + "][" + lSend[4].ToString() + "]";

try
{
Log.This("Drive Action: " + sDebugSend, c_sCommonChassis, this.Log_OICommands);

this.IO.RtsEnable = false;
this.IO.Write(lSend.ToArray(), 0, lSend.Count);
//this.Macro.SetAction("DRIVE\t" + sDebugSend);

this.p_leftVelocity = leftVelocity;
this.p_rightVelocity = rightVelocity;
//this.p_rRadius = rAngle; //may need to add equivalent for this class

bSuccess = true;
Log.This("Drive Action Success " + bSuccess.ToString() + " leftVelocity: " + this.p_leftVelocity.ToString() + " rightVelocity: " + this.p_rightVelocity.ToString(), c_sCommonChassis, this.Log_OICommands);
}
catch (Exception ex)
{
Log.This("Drive Action Fail: " + ex.Message, c_sCommonChassis, this.Log_OICommands);
}

return bSuccess;
}


Then save all changes and goto the iRobot project descriptor at the very top of the Solution Explorer, right click it and click rebuild. The iRobot.dll will be located in
CreateOI\CreateOI\iRobot\iRobot\bin\Release

add this .dll under References in solution explorer for whatever project you may have and it will allow you to use the DirectDrive function I implemented

No comments: