I have a Raspberry Pi hooked up to a Roomba 560's serial port. While going over the spec, I noticed movement controls weren't as simple as I expected. I can't send bytes larger than 255, but, according to the spec, to go straight I have to send 8000. How does this work?

EDIT: My solution was the following three functions:

import serial
import time

def start():
    #Open serial connection
    global ser
    ser = serial.Serial('/dev/ttyUSB0', 115200)
    # this is the defualt Roomba baud rate
    # Start SCI - puts into safe mode
    # Enable the safe mode
    # this is required or the command may fail

# This makes the serial command fit the big-endian notation
def make4(num):
    num = num[2:]
    z2a = 4-len(num)
    for q in range(z2a):
        num = '0' + num
    return '\\x'+num

def move(vel, rad):
    # Init move command

    # velocity
    vhex = hex(vel)
    vhex = make4(vhex)

    vrad = hex(rad)
    vrad = make4(vrad)

    # send to roomba

The spec says hex 8000 as in hexadecimal (which I usually write in C/Python notation as 0x8000). Each byte is exactly two hexadecimal digits. So you can break 0x8000 into 0x80 and 0x00 and send them separately, which correspond to decimal numbers 128 and 0.

You also have to know which to send first (referred as little-endian vs big-endian). The spec says [radius high byte] [radius low byte], so high byte comes first (big-endian).

Therefore send the sequence [128, 0] in order to transmit hex 8000.

| improve this answer | |
  • $\begingroup$ Thanks! I was able to make some nice functions using Python's hex() function so I can put in easily-understandable numbers. $\endgroup$ – Ian Hyzy Mar 1 '14 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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