-
Notifications
You must be signed in to change notification settings - Fork 0
/
head.py
191 lines (171 loc) · 6.81 KB
/
head.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import maestro
import random
# Servo channels
CH_YAW = 2
CH_PITCH = 3
CH_BROW = 4
# Left/Right yaw constants
YAW_SPEED = 35
YAW_ACCELERATION = 5
YAW_L = 4378
YAW_CL = 5933
YAW_C = 6496
YAW_CR = 6895
YAW_R = 8200
# Up/Down pitch constants
PITCH_SPEED = 15
PITCH_ACCELERATION = 5
PITCH_U = 7007
PITCH_CU = 6591
PITCH_C = 6343
PITCH_CD = 6144
PITCH_D = 5706
# Brow Up/Down constants
BROW_SPEED = 30
BROW_U = 4032
BROW_C = 5100
BROW_D = 5952
#
# FUNCTIONS
#
# Test if x is within or on two points of a range
def between(x,limit1,limit2):
if limit1 < limit2:
return x >= limit1 and x <= limit2
else:
return x >= limit2 and x <= limit1
#
# HEAD CLASS
#
class Head():
# Provide maestro controller obj
def __init__(self, maestro):
self.maestro = maestro
#maestro.setRange(CH_YAW, YAW_R, YAW_L)
#maestro.setRange(CH_PITCH, PITCH_U, PITCH_D)
#maestro.setRange(CH_BROW, BROW_U, BROW_D)
maestro.setAccel(CH_YAW, YAW_ACCELERATION)
maestro.setAccel(CH_PITCH, PITCH_ACCELERATION)
def _speedCalc(self, pitch, yaw,throttle=1.0):
yaw_delta = abs(yaw - self.maestro.getPosition(CH_YAW))
pitch_delta = abs(pitch - self.maestro.getPosition(CH_PITCH))
if yaw_delta == 0:
yaw_delta += 1
if pitch_delta == 0:
pitch_delta += 1
yaw_time = float(yaw_delta) / YAW_SPEED * throttle
pitch_time = float(pitch_delta) / PITCH_SPEED * throttle
if yaw_time >= pitch_time:
spd_ratio = pitch_time / yaw_time
pitch_spd = int(spd_ratio * PITCH_SPEED * throttle)
return (int(pitch_spd), int(YAW_SPEED * throttle))
else:
spd_ratio = yaw_time / pitch_time
yaw_spd = int(spd_ratio * YAW_SPEED * throttle)
return (int(PITCH_SPEED * throttle), int(yaw_spd))
def isUp(self):
return between(self.maestro.getPosition(CH_PITCH),PITCH_CU,PITCH_U)
def isDown(self):
return between(self.maestro.getPosition(CH_PITCH),PITCH_CD,PITCH_D)
def isPitchCentered(self):
return between(self.maestro.getPosition(CH_PITCH),PITCH_CU,PITCH_CD)
def isLeft(self):
return between(self.maestro.getPosition(CH_YAW),YAW_L,YAW_CL)
def isRight(self):
return between(self.maestro.getPosition(CH_YAW),YAW_R,YAW_CR)
def isYawCentered(self):
return between(self.maestro.getPosition(CH_YAW),YAW_CL,YAW_CR)
def isBrowUp(self):
return self.maestro.getPosition(CH_BROW) >= BROW_U
def isBrowDown(self):
return self.maestro.getPosition(CH_BROW) <= BROW_D
def isBrowCentered(self):
return self.maestro.getPosition(CH_BROW) == BROW_C
def isPitchMoving(self):
pass
def isYawMoving(self):
pass
def isBrowMoving(self):
pass
def isHeadMoving(self):
pass
def movePitch(self, position, speed):
self.maestro.setSpeed(CH_PITCH, speed)
self.maestro.setTarget(CH_PITCH, position)
def moveYaw(self, position, speed):
self.maestro.setSpeed(CH_YAW, speed)
self.maestro.setTarget(CH_YAW, position)
def moveBrow(self, position):
self.maestro.setSpeed(CH_BROW, BROW_SPEED)
self.maestro.setTarget(CH_BROW, position)
def browUp(self):
self.moveBrow(BROW_U)
def browDown(self):
self.moveBrow(BROW_D)
def browCenter(self):
self.moveBrow(BROW_C)
def lookUp(self, throttle = 1.0):
yawChange = random.randint(-600, 600)
yaw = self.maestro.getPosition(CH_YAW) + yawChange
pitch = random.randint(PITCH_CU, PITCH_U)
(p, y) = self._speedCalc(pitch, yaw, throttle)
print p, y
self.maestro.setSpeed(CH_YAW, y)
self.maestro.setSpeed(CH_PITCH, p)
self.maestro.setTarget(CH_YAW, yaw)
self.maestro.setTarget(CH_PITCH, pitch)
def lookDown(self, throttle = 1.0):
yawChange = random.randint(-600, 600)
yaw = self.maestro.getPosition(CH_YAW) + yawChange
pitch = random.randint(PITCH_D, PITCH_CD)
(p, y) = self._speedCalc(pitch, yaw, throttle)
print p, y
self.maestro.setSpeed(CH_YAW, y)
self.maestro.setSpeed(CH_PITCH, p)
self.maestro.setTarget(CH_YAW, yaw)
self.maestro.setTarget(CH_PITCH, pitch)
def lookLeft(self, throttle = 1.0):
pitchChange = random.randint(-600, 600)
pitch = self.maestro.getPosition(CH_PITCH) + pitchChange
yaw = random.randint(YAW_L, YAW_CL)
(p, y) = self._speedCalc(pitch, yaw, throttle)
print p, y
self.maestro.setSpeed(CH_YAW, y)
self.maestro.setSpeed(CH_PITCH, p)
self.maestro.setTarget(CH_YAW, yaw)
self.maestro.setTarget(CH_PITCH, pitch)
def lookRight(self, throttle = 1.0):
pitchChange = random.randint(-600, 600)
pitch = self.maestro.getPosition(CH_PITCH) + pitchChange
yaw = random.randint(YAW_CR, YAW_R)
(p, y) = self._speedCalc(pitch, yaw, throttle)
print p, y
self.maestro.setSpeed(CH_YAW, y)
self.maestro.setSpeed(CH_PITCH, p)
self.maestro.setTarget(CH_YAW, yaw)
self.maestro.setTarget(CH_PITCH, pitch)
def lookCentered(self, throttle = 0.5):
pitch = random.randint(PITCH_D, PITCH_U)
yaw = random.randint(YAW_CL, YAW_CR)
(p, y) = self._speedCalc(pitch, yaw, throttle)
print "lookCentered",p, y
self.maestro.setSpeed(CH_YAW, y)
self.maestro.setSpeed(CH_PITCH, p)
self.maestro.setTarget(CH_YAW, yaw)
self.maestro.setTarget(CH_PITCH, pitch)
def moveAbs(self, x, y):
self.maestro.setSpeed(CH_YAW,YAW_SPEED)
if x >= 0:
mx = int(x * (YAW_R - YAW_C) + YAW_C)
self.maestro.setTarget(CH_YAW, mx)
else:
mx = int(YAW_C + x * (YAW_C - YAW_L))
self.maestro.setTarget(CH_YAW, mx)
self.maestro.setSpeed(CH_PITCH,PITCH_SPEED)
if y >= 0:
my = int(y * (PITCH_U - PITCH_C) + PITCH_C)
self.maestro.setTarget(CH_PITCH, my)
else:
my = int(PITCH_C + y * (PITCH_C - PITCH_D))
self.maestro.setTarget(CH_PITCH, my)
#print self.maestro.getPosition(CH_YAW), self.maestro.getPosition(CH_PITCH)