1

I'm trying to control a SaberTooth 2x12 connected to my Pi3. Based my Google research it seemed the best method was Simple Serial. I configured the Pi for Serial in raspi-config. Set the dip switches on the SaberTooth for simple serial and 9600 baud. I added a Level Shifter between the Pi and SaberTooth.

According to the Sabertooth documentation, with simple serial 1-127 controls the speed and direction of M1 and 128-255 speed and direction of M2. "0" shuts the motors off. When I run the code motor1 moves in one direction and one speed and will continue until power is removed.

As long as I'm sending a "number" of any value the same results: Motor 1 one speed and one direction. One thing the original code I was basing this off of use "ser.writeString". When I ran the code with that format it said Serial Object does not have "String". Can you advise what I'm doing wrong?

#! /user/bin/env python
# _*_ coding : utf-8 _*_

import time
import serial

ser = serial.Serial('/dev/serial0', 9600)
def main():
   print("Program Start")
   time.sleep(5)
   x=0
     while x < 4:
       ser.write("M1: 1\r\n")
       ser.write("M2: 128\r\n")
       time.sleep(2)
       ser.write("M1: 64\r\n")
       ser.write("M2: 192\r\n")
       time.sleep(2)
       ser.write("M1: 127\r\n")
       ser.write("M2: 255\r\n")
       time.sleep(2)
       ser.write("M1: 0\r\n")
       ser.write("M2: 0\r\n")
       time.sleep(2)
       x = x + 1

if __name__ == '_main_':
   main()

Here's the traceback of the error I was getting:

Traceback (most recent call last): 

File "MotorTest.py", line 27, in <module> main() 
File "MotorTest.py" , line 16 in main 
ser.writeString("M1: 1\r\n") 
AttributeError: 'Serial' object has no attribute 'writeString' 
------------ 
Program exited with code: 1
  • 1
    Welcome. Could you include the actual code you were using, in text form, if possible? We discourage images of text, as they can be difficult to read and can't be searched or copied so that we can test the code ourselves. In addition, when you get the error, please copy the full message including the stack trace into your post too; that'll help us find the error more quickly. – Aurora0001 Jan 9 '18 at 18:42
  • Here is the code in text form; – Brian McMillion Jan 10 '18 at 18:12
  • #!/usr/bin/env python # * coding: utf-8 * import time import serial ser = serial.Serial('/dev/serial0', 9600) def main(): print("Program Start") time.sleep(5) x=0 while x < 4: ser.write("M1: 1\r\n") ser.write("M2: 128\r\n") time.sleep(2) ser.write("M1: 64\r\n") ser.write("M2: 192\r\n") time.sleep(2) ser.write("M1: 127\r\n") ser.write("M2: 255\r\n") time.sleep(2) ser.write("M1: 0\r\n") ser.write("M2: 0\r\n") time.sleep(2) x = x + 1 if name == 'main_': main() – Brian McMillion Jan 10 '18 at 18:13
  • Best to edit with that instead of putting it in a comment, @Brian; the comment formatting interferes with the code and since only one line is supported in comments, it's difficult to read. You can indent with 4 spaces when editing and you'll get clearer code block formatting. – Aurora0001 Jan 10 '18 at 18:15
  • Ok I edited but I think the formatting got messed up because when I click on the "edit" it looks correct, however looking at it on the "main" screen it looks off. Must be doing something wrong. – Brian McMillion Jan 11 '18 at 20:06
1

Thanks to Aurora001 for pointing me to https://github.com/MomsFriendlyRobotCompany/pysabertooth. Using this library I'm able to get the motor control I wanted. A couple of side notes to add. As mentioned in the comments above, when I used the library as is I got an Attribute Error: Serial module does not include "is_open". I modified the installed PySabertooth.py code by commenting out the "if not self.saber.is_open:" line in the open(self) function. Un-indented the next line "self.saber.open() and my program worked. The second note is for a Pi3 instead of '/dev/tty.usbserial' I used '/dev/serial0' Thanks again for your help!!

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.