-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsabertooth.py
executable file
·52 lines (44 loc) · 1.15 KB
/
sabertooth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import serial
import struct
import time
commands = {'motor1fwd':0,'motor1bwd':1,'vmin':2,'vmax':3,'motor2fwd':4,'motor2bwd':5,'motor1drive':6,'motor2drive':7,'timeout':14,'baud':15}
baudcodes = {2400:1, 9600:2, 19200:3, 38400:4}
def set_baud(address, baudrate):
packet = make_packet(address, commands['baud'], baudcodes[baudrate])
ser.write(packet)
def drive(address, motor, speed):
if motor==1:
command = 0
elif motor==2:
command = 4
else:
return false
if speed<0:
command = command + 1
speed = -speed
if speed >127:
speed = 127
packet = make_packet(address, command, speed)
ser.write(packet)
def make_packet(address,command,data):
return struct.pack('BBBB', address, command, data, (127&(address+command+data)))
addr = 130
baud = 9600
ser = serial.Serial('/dev/ttyUSB0',baud)
#set_baud(130,baud)
print "Sending commands at 9600 baud\n"
print "Motor 1 forward\n"
drive(addr, 1, 127)
time.sleep(5)
print "Motor 1 reverse\n"
drive(addr, 1, -127)
time.sleep(5)
print "Motor 2 forward\n"
drive(addr, 1, 0)
drive(addr, 2, 127)
time.sleep(5)
print "Motor 2 reverse\n"
drive(addr, 2, -127)
time.sleep(5)
print "Test complete.\n"
drive(addr, 2, 0)