-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservoTest.py
92 lines (74 loc) · 2.37 KB
/
servoTest.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
#!/usr/bin/python
# servoTest.py
from __future__ import print_function
import rover
# Define variables for servo and degrees
servo = 0 # currently active servo (0 to 15)
degrees = 0 # Current angle of servo. 0 degrees is centre (-90 to +90)
#======================================================================
# 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
#======================================================================
rover.init(0)
print ("Use Arrows or W-Up, Z-Down, A-Left, S-Right Space=Centre, ^C=Exit:")
try:
servo = 0
while True:
key = readkey()
if key == ' ':
degrees = 0
rover.setServo(servo, degrees)
print ('Servo ', servo, ': Centre',sep='')
if key == 'x' or key == '.':
rover.stopServos()
print ('Servo ', servo, ': Stop',sep='')
elif key == 'w' or ord(key) == 16:
servo += 1
servo %= 16
print ('Servo ', servo, ': Selected',sep='')
elif key == 'z' or ord(key) == 17:
servo -= 1
servo %= 16
print ('Servo ', servo, ': Selected',sep='')
elif key == 'a' or ord(key) == 19:
degrees -= 5
if (degrees < -90):
degrees = -90
rover.setServo(servo, degrees)
print ('Servo ', servo, ': Value:', degrees, sep='')
elif key == 's' or ord(key) == 18:
degrees += 5
if (degrees > 90):
degrees = 90
rover.setServo(servo, degrees)
print ('Servo ', servo, ': Value:', degrees, sep='')
elif ord(key) == 3:
break
except KeyboardInterrupt:
print()
finally:
rover.cleanup()