I'm trying to get into robotics, and I've found some tutorial code that was readily available on pololu's website for one of their motor controllers which I now own. Could someone with some background in Processing explain the highlighted segments please?

#include <SoftwareSerial.h>
#define rxPin 3  // pin 3 connects to smcSerial TX  (not used in this example, but needed for the smcSerial object)
#define txPin 4  // pin 4 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
  if (speed < 0)
    speed = -speed;
    smcSerial.write(0x85);       // This Line
  smcSerial.write(speed & 0x1F); // This Line
  smcSerial.write(speed >> 5);   // This Line
  • \$\begingroup\$ They write to the software UART. \$\endgroup\$ – Ignacio Vazquez-Abrams Sep 5 '13 at 21:12

The program sends 3 bytes to a serial peripheral:

1) What appears to be a forward or reverse "command" or mode.

2) the lower 5 bits of the speed value

3) some more significant bits of the speed value (likely 7 more given the stated range)

That split between the 2nd and 3rd bytes is being done a bit unusually - typically you'd send the entire lower 8 bits, and then however many bits are leftover in the next byte. It's possible it's done this way because the peripheral reserves the upper bit(s) of each byte for designating commands or framing, and so they aren't allowed to be used for data (at least, that's a scheme I've used on projects before).

  • \$\begingroup\$ Ascii Character 0x85 … \$\endgroup\$ – Passerby Sep 5 '13 at 22:21
  • 1
    \$\begingroup\$ No. Not ASCII, but binary data. Intended for consumption by machines, not humans. \$\endgroup\$ – Chris Stratton Sep 5 '13 at 23:16
  • \$\begingroup\$ Technically ASCII stops at 0x7f. \$\endgroup\$ – Ignacio Vazquez-Abrams Sep 6 '13 at 12:37
  • \$\begingroup\$ Yes, that's a strong clue. But even in the 7-bit range it would not be ASCII as it has an entirely different purpose. \$\endgroup\$ – Chris Stratton Sep 6 '13 at 13:06

Basically, pololu implemented serial protocol on their motor driver and you need to send series of command to be able to tell the driver what to do.

From example above, you are sending the command 0x85 which is probably address of device + something related to desired rotational direction, and two more bytes for speed. As Chris already explained.

You have to take a look at datasheet of the driver itself to be able to figure out what you have to send, to achieve what you want... There is no universal serial protocol for robotics. Every driver will have different interpretation of bytes you send.


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.