Robot App Store Developer Program

robots-app-store-developer-program

Knowledge Base

How to program Roomba for C# .NET developers

  By Anna Sandler

Are you a .NET developer? Congratulations!

In the next 10 minutes, you are going to be amazed how easy it is to program Roomba and what awesome things you can do with the vacuum cleaner!

Make sure you've read the- "Introduction to Roomba Programming" chapter,
and how to connect to Roomba- "Serial Port Baud Rate Configuration" chapter.

Connecting to Roomba

All the communication between Roomba and .NET will be handled through SerialPort class http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
from System.IO.Ports namespace. http://msdn.microsoft.com/en-us/library/system.io.ports.aspx.

Once you connect the Roomba to your PC (using Serial to USB cable, or a Bluetooth device),
you will have to figure out which of the serial ports in your PC is being used.
You can ask the user to choose the port, but this is not user-friendly.
Alternatively, you can run a loop across all active ports and try to send a start command to each one of them, until you succeed.

Take a look at these methods:

using System.IO.Ports;
 
public class Roomba
{
    private SerialPort IO;

    public bool TryToConnect()
    {       var ports = SerialPort.GetPortNames();
      foreach (var port in ports)
      {
        var isPortSet = SetPort(port);
        if (isPortSet)
        {
          return true;
        }
      }
    return false;
  }

  private bool SetPort(string portNum)
  {
    try
    {
      if (IO != null)
      {
        IO.Close();//Just in case port is already taken
      }
      IO = new SerialPort(portNum, 57600, Parity.None, 8, StopBits.One);
      IO.DtrEnable = false;
      IO.Handshake = Handshake.None;
      IO.RtsEnable = false;

      IO.Open();

      return SendCommand(new[] { 128 });
    
    catch
    {
      this.portNum = String.Empty;
      IO.Close();
      return false;
    }
  }

  private bool SendCommand(IEnumerable<byte> commandCollection)
  {
    try
    {
      var commandArr = commandCollection.ToArray();
      IO.Write(commandArr, 0, commandArr.Length);
      return true;
    }
    catch
    {
      return false;
    }
  }
}

Explanation:
SerialPort.GetPortNames()- It will show all active ports in your PC as a string array.

After SerialPort is set up, SendCommand is being called with "start sending commands" ([128] byte command). We are going to use this method for all our commands that usually contain more than one byte, that’s why it gets a collection of bytes.

IO.Write- Send commands to Roomba. If Roomba is not connected, or a wrong port is used, an exception will be thrown. This can be used in the ‘searching for the right port’ phase in a loop that keeps looking for the next valid port, until we find the right port.

After the start command ([128] byte command), you can send the rest of the commands using the SendCommand method.

Be aware, the port that Roomba is using, can be closed for several reasons (cable is out, Bluetooth in off…), therefore, a check if the port is valid, is a must before you send a command (other than a start command).

  private bool IsValid()
  {
    return IO.IsOpen;
  }

At the end, you must close the port when you no longer need it or when the class is disposed.

    if (IO != null)
    {
      IO.Close();
    }

High-low bytes calculations

How to convert decimal to high-low bytes

Some Roomba commands, like the driving commands use high –low bytes.

For example: driving command ([137] byte command), requires 4 bytes to be followed.
First 2 bytes are the high byte and the low byte of the average velocity of the drive wheels in millimeters per second (mm/s), with the high byte sent first. The next two bytes specify the radius, in millimeters, at which Roomba should turn. For more information about the driving commands please refer to this chapter.

Just as a short example - to drive in reverse at a velocity of -200 mm/s while turning at a radius of 500mm, you would send the serial byte sequence: [137] [255] [56] [1] [244].

Explanation:
Velocity = -200 = hex FF38 = [hex FF] [hex 38] = [255] [56]
Radius = 500 = hex 01F4 = [hex 01] [hex F4] = [1] [244]

Converting decimal to high-low bytes:

  private static IEnumerable<byte> DecimalToHighLowBytes(int decimalNum)
  {
    byte highByte = (byte)(decimalNum >> 8);
    byte lowByte = (byte)(decimalNum & 255);
    var commands = new List<byte>() { highByte, lowByte };
    return commands;
  }

How to convert high-low bytes to decimal

Roomba can send data about its sensors.
Some of sensors' data is being sent as unsigned high-low bytes, like the current charge of Roomba’s battery in milliamp-hours sensors.
Use this method to convert unsigned high-low bytes to decimal:

  private static int UnsignedHighLowBytesToDecimal(byte highByte, byte lowByte)
  {
    return 256 * highByte + lowByte;
  }

Some of sensors' data is being sent as signed high-low bytes, like the current in milliamps (mA) flowing into or out of Roomba’s battery.
Use this method to convert signed high-low bytes to decimal:

  private static int SignedHighLowBytesToDecimal(byte highByte, byte lowByte)
  {
    uint u = (uint)highByte << 8 | lowByte;
    int num = (int)(u >= (1u << 15) ? u - (1u << 16) : u);
    return num;
  }

How to read data from serial port

Use the following method to read data from serial port:

  private void SensorDataWasReceived (object sender, SerialDataReceivedEventArgs e)
  {
    var numOfBytes = IO.BytesToRead;
     byte[] sensorsData = new byte[numOfBytes];
    IO.Read(sensorsData, 0, numOfBytes);
    //set sensors…
  }

will my wife cheat again click here how do i know if my wife has cheated
click wives that cheat married men having affairs
married men affairs My wife cheated on me infidelity in marriage
signs of a cheater what makes married men cheat married men that cheat
all about abortion side effects of the abortion pill misoprostol and abortion
wifes who cheat cheat women women who cheat on their husband
sumatriptan succ sumatriptan succ sumatriptan succ







Related Robot-Apps™

Comments