Skip to content

RPiSabertooth

Alex Schendel edited this page Jun 27, 2019 · 5 revisions

This is the Python library we created to control Sabertooth DC Motor controllers. These are complex motor controllers with various modes of motor control. We are using the "Packetized Serial" mode. These motor controllers can be found here.

Packetized Serial

Packetized Serial is a mode of communication that uses standard serial communication (bytestream with a dedicated Tx and Rx wires) to send packets containing the data we want to send. Fortunately, most microcontrollers, including the RPi, has dedicated pins for Tx and Rx over serial. This means we just need to write the proper bytes to the Serial and that will make the Sabertooth run.

Packets

These packets are what we send to the Sabertooth to get it to run. On the datasheet, more info can be found on page 17.

The format of the packets is as follows:

  • Address
  • Command
  • Data
  • Checksum

What do these all mean? First is the address, which is the reason we like Packetized Serial so much. There are 8 addresses (128-135) that can be used with Sabertooths and these addresses allow us to have multiple Sabertooths on one pin without having all of them do the exact same thing. The address allows us to tell a particular Sabertooth to listen while the others ignore the message. You can learn to set the address of the Sabertooths on page 18.

Next is the command. This is a number from 0-17 and represents what we want the Sabertooth to do. 0 is drive motor 1 forward. More information on pages 19-21.

Then comes the Data. The meaning of the data depends on the command. If we told the Sabertooth to drive motor 1 forward, then the data is the speed. If we told the Sabertooth to change its baud rate, then the data is the new baud rate (DON'T ACTUALLY CHANGE THE BAUD RATE!)

Last is the Checksum. A checksum is popular in computer science because its and efficient way to figure out if you are reading the packet that was sent to you. If you apply an operation on the data you were sent (Address, Command, and Data) you should get the Checksum. If you don't then you either read something wrong or some noise messed up the packet. The format of this checksum is as follows: (Address + Command + Data) & 127. Let's break that down. Since the Address, Command, and Data are all ints, we can just add them up no problem. Then comes the "&". This is actually the binary AND operator. This means we take the sum and convert it into binary and also take the 127 and convert it into binary. After this, we apply the binary AND operator on each bit to get the checksum. More info on page 22.

API

This is where you can find all the capabilities of the API.

Functions:

drive(address=0, speed=30, motor=0):

This drives a particular motor. Parameters are the address of the Sabertooth you want to control (0-7), the speed that you wish to run it at (-127-127), and the motor on the Sabertooth you want to run (0 or 1). Examples:

  • drive() - runs motor 0 on Sabertooth 0 at a speed of 30.
  • drive(2, 50, 1) - runs motor 1 on Sabertooth 2 at a speed of 50. Return Values: Raises a ValueError if the address, speed, and/or motor is out of range. Otherwise, returns nothing.
Clone this wiki locally