-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmotorControl.py
More file actions
35 lines (27 loc) · 815 Bytes
/
motorControl.py
File metadata and controls
35 lines (27 loc) · 815 Bytes
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
#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
#import time
#import atexit
import sys, json
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr = 0x60)
def runMotor(n, spd):
myMotor = mh.getMotor(n)
if spd < 0 and spd >= -255:
myMotor.run(Adafruit_MotorHAT.BACKWARD)
elif spd > 0 and spd <= 255:
myMotor.run(Adafruit_MotorHAT.FORWARD)
else:
myMotor.run(Adafruit_MotorHAT.RELEASE)
absSpeed = abs(spd)
myMotor.setSpeed(absSpeed)
# accept commands
for line in sys.stdin:
command = json.loads(line[:-1])
lmotor = command['left']['motor']
lspeed = command['left']['speed']
rmotor = command['right']['motor']
rspeed = command['right']['speed']
runMotor(lmotor, lspeed)
runMotor(rmotor, rspeed)
print ('ya did it')