4

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)
  {
    smcSerial.write(0x86); 
    speed = -speed;
  }
  else
  {
    smcSerial.write(0x85);       // This Line
  }
  smcSerial.write(speed & 0x1F); // This Line
  smcSerial.write(speed >> 5);   // This Line
}
Monte Carlo
  • 265
  • 2
  • 4
  • 10

2 Answers2

7

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).

Chris Stratton
  • 33,282
  • 3
  • 43
  • 89
2

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.

Gossamer
  • 663
  • 6
  • 14