1

Forgive my lack of understanding in this area and if my title is incorrect.

I am trying to control a Sabertooth motor driver board which I can connect my Raspberry Pi to in two ways. 1. USB to Micro USB on Motor Driver 2. GPIO - Logic Level Shifter - Motor Driver

My understanding is that I can control the motors by sending plain text serial commands so according to the Sabertooth manual to drive a motor forward at half speed I send the following command: M1 1023\r\n

So my test code would look like this:

#!/usr/bin/python3

import serial
from time import sleep
ser = serial.Serial('/dev/ttyAMA0', 9600) #enter correct port for serial

def main():
    # write data to serial port

    ser.write("M1 1023\r\n")
    sleep(1)
    ser.write("M1 0\r\n")
    sleep(1)
    ser.write("M2 1023\r\n")
    sleep(1)
    ser.write("M2 0\r\n")
    sleep(1)
    ser.write("M1 -1023\r\n")
    sleep(1)
    ser.write("M1 0\r\n")
    sleep(1)
    ser.write("M2 -1023\r\n")
    sleep(1)
    ser.write("M2 0\r\n")

main()

A couple of questions:

  1. I am struggling to understand the correct serial port syntax for the USB connection or the GPIO connection. My guess is that I need to plug the Sabertooth in via USB to find the correct USB name using 'ls usb' or 'dmesg'?

  2. What would the GPIO device name be?

  3. What would the correct baud rate be?

I will be using WebIOPi eventually but want to test using command line first. (can't quite get my head around how to use WebIOPi yet so will start a different thread on that) I have installed it and can switch an LED on connected to the GPIO using the GPIO GUI.

This is a Pi 2 (40 Pin)

  • What Pi? What OS? – Milliways Apr 29 '17 at 8:12
  • Welcome to Raspberry Pi SE. Be sure to take the tour to see how this works and to earn a badge: raspberrypi.stackexchange.com/Tour – SDsolar Apr 29 '17 at 8:18
  • This is a Pi 2 running Rasbian Jessie – Bill Apr 29 '17 at 12:41
1
  1. the first serial USB dongle normally gets the name /dev/ttyUSB0, the next /dev/ttyUSB1 etc,
  2. The GPIO based UART (pins 8/10, GPIO 14/15, TXD/RXD) on the Pi2 is normally /dev/ttyAMA0. If you want you can use different internal hardware in which case it will be /dev/ttyS0. It's safest to use the alias /dev/serial0 which will always be the UART on up to date Raspbian systems.
  3. People normally use the fastest reliable speed supported by both ends of the serial link.
  • Turns out even though I connected via USB the serial port was ttyacm0 and changed my code to use webiopi. – Bill Apr 29 '17 at 19:48
1

Once I got the DIP switches and wiring figured out sorted out my code I now have a working setup. Here's the code:

#!/usr/bin/python3

import webiopi
from webiopi.devices.serial import Serial
from time import sleep

serial = Serial('ttyACM0', 9600) #enter correct port for serial

def loop():
# write data to serial port

for i in range (1, 5):
    serial.writeString("M1: 1023\r\n")
    print("M1 Fwd")
    sleep(2)
    serial.writeString("M1: 0\r\n")
    print("M1 Off")
    sleep(2)
    serial.writeString("M2: 1023\r\n")
    print("M2 Fwd")
    sleep(2)
    serial.writeString("M2: 0\r\n")
    print("M2 Off")
    sleep(2)
    serial.writeString("M1: -1023\r\n")
    sleep(2)
    serial.writeString("M1: 0\r\n")
    sleep(2)
    serial.writeString("M2: -1023\r\n")
    sleep(2)
    serial.writeString("M2: 0\r\n")

    if (serial.available() > 0):
        data = serial.readString()
        print(data)

    webiopi.sleep(1)

loop()

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.