-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalibrateServos.py
123 lines (111 loc) · 3.56 KB
/
calibrateServos.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Calibrates and saves Servo Offsets on MARS Rover
from __future__ import print_function
import rover,time
#======================================================================
# Reading single character by forcing stdin to raw mode
import sys
import tty
import termios
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if ch == '0x03':
raise KeyboardInterrupt
return ch
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return chr(0x10 + ord(c3) - 65) # 16=Up, 17=Down, 18=Right, 19=Left arrows
# End of single character reading
#======================================================================
print ("Calibrate the servos on M.A.R.S. Rover")
print ("Select Servo to claibrate with '1', '2', '3', '4' or '5'")
print ("Use the left and right arrow keys to straighten the servo")
print ("Press 's' to save the calibration data in the EEROM")
print ("Press Ctrl-C to exit without saving")
print ()
rover.init(40)
red = rover.fromRGB(255,0,0)
green = rover.fromRGB(0,255,0)
blue = rover.fromRGB(0,0,255)
rover.setColor(red)
rover.show()
print ('Existing servo offsets')
print (rover.offsets)
print ()
try:
servo = 9
print ('Servo: Front Left: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(1, green)
rover.show()
while True:
key = readkey()
if key == '1':
servo = 9
print ('Servo: Front Left: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(1, green)
rover.show()
elif key == '2':
servo = 11
print ('Servo: Rear Left: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(0, green)
rover.show()
elif key == '3':
servo = 15
print ('Servo: Front Right: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(2, green)
rover.show()
elif key == '4':
servo = 13
print ('Servo: Rear Right: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(3, green)
rover.show()
elif key == '5':
servo = 0
print ('Servo: Mast: Offset:', rover.offsets[servo], sep='')
rover.setColor(red)
rover.setPixel(1, blue)
rover.setPixel(2, blue)
rover.show()
elif key == 'x' or key == '.':
rover.stopServos()
print ('Servo ', servo, ': Stop',sep='')
elif ord(key) == 19:
rover.offsets[servo] -= 1
rover.setServo(servo, 0)
print ('Servo ', servo, ': Offset:', rover.offsets[servo], sep='')
elif ord(key) == 18:
rover.offsets[servo] += 1
rover.setServo(servo, 0)
print ('Servo ', servo, ': Offset:', rover.offsets[servo], sep='')
elif key == 's':
print ('Saving servo offsets')
rover.saveOffsets()
break
elif ord(key) == 3:
break
except KeyboardInterrupt:
print()
finally:
rover.loadOffsets()
print ()
print ('New servo offsets')
print (rover.offsets)
print ()
rover.cleanup()