-
Notifications
You must be signed in to change notification settings - Fork 1
/
CarControl.py
76 lines (63 loc) · 2.57 KB
/
CarControl.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# CarControl.py
#
from serial import Serial
from threading import Timer
class CarControl:
def __init__(self, comport):
self.comport = comport
self.cars = {
1: {'x-servo':1, 'y-servo':2},
2: {'x-servo':3, 'y-servo':4},
3: {'x-servo':5, 'y-servo':6}
}
self.directions = {
'S': {'x-servo-val':90, 'y-servo-val':90},
'F': {'x-servo-val':120, 'y-servo-val':90},
'B': {'x-servo-val':60, 'y-servo-val':90},
'FL': {'x-servo-val':120, 'y-servo-val':70},
'FR': {'x-servo-val':120, 'y-servo-val':100},
'BL': {'x-servo-val':60, 'y-servo-val':100},
'BR': {'x-servo-val':60, 'y-servo-val':70}
}
self.arduino = Serial(self.comport, 115200, timeout=1)
def move(self, car, direction, duration=500):
xservo = self.cars[car]['x-servo']
yservo = self.cars[car]['y-servo']
xval = self.directions[direction]['x-servo-val']
yval = self.directions[direction]['y-servo-val']
# steer first...
#print "xservo %d %d" % (xservo, xval)
self.moveServo(xservo, xval)
# then hit the gas!
#print "yservo %d %d" % (yservo, yval)
self.moveServo(yservo, yval)
# stop the car after given duration
print "stopping after %d" % (duration)
t = Timer(10 + float(duration)/1000, self.stop, [car])
t.start()
def stop(self, car):
print "stopping car %d" % car
xservo = self.cars[car]['x-servo']
yservo = self.cars[car]['y-servo']
xcentre = self.directions['S']['x-servo-val']
ycentre = self.directions['S']['y-servo-val']
self.moveServo(yservo, ycentre)
self.moveServo(xservo, xcentre)
def moveServo(self, servo, angle):
'''
Moves the specified servo to the supplied angle.
Arguments:
servo
the servo number to command, an integer from 1-6
angle
the desired servo angle, an integer from 0 to 180
(e.g.) >>> servo.move(2, 90)
... # "move servo #2 to 90 degrees"
'''
if (0 <= angle <= 180):
self.arduino.write(chr(255))
self.arduino.write(chr(servo))
self.arduino.write(chr(angle))
print "servo %d: %d" % (servo,angle)
else:
print "Servo angle must be an integer between 0 and 180.\n"