-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMotion.py
299 lines (239 loc) · 14 KB
/
Motion.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from Component import *
import math
class Motion(Component):
def __init__(self, hardware, identifiers):
super().__init__(hardware, identifiers)
self._eventCallBack = None
# ===========CONSTANTS========
self.PWMNORMAL = 305
self.PWMRANGE = 165
self.ANGLE45 = 0.785398 # 45 deg to rad
self.ANGLE90 = 1.5708 # 90 deg to rad
self.ANGLE225 = 3.92699 # 225 deg to rad
self.FULL_PWM_RANGE_COEFFICIENT = self.PWMRANGE / 100.0 # PWMRANGE/100
self.MOTORS_BASE_PWM = 305
self.FULL_ROTATION_COEFFICIENT = 0.3 * 1.65
self.MAXTHRUST = 0.5
self.VMAXTHRUST = 0.5
self.prev_value = self.PWMNORMAL
# =========VARIABLES==========
self._verticalMotors = {}
self._horizontalMotors = {}
self._servos = {}
self._lights = {}
# ========SET MOTORS TO DEFAULTS=====
self._stopHorizontalMotors()
self._stopVerticalMotors()
# self._stopVerticalMotors_()
self._setCamToNormalPosition()
self._turnLightOff()
self._setFromMyLocalToDevice()
def _calculateHorizontalMotors_Mustafa(self):
_x = self._valueMap['x']
_y = self._valueMap['y']
_r = self._valueMap['r'] * 0.4
n = (abs(_x) + abs(_y)) / 100.0
# n = (abs(self._valueMap['x']) + abs(self._valueMap['y']) + abs(self._valueMap['r'])) / 100.0
if n < 1:
n = 1
right_front_thruster_value = self.PWMNORMAL + self.PWMRANGE * (
float(_x / -100) + float(_y / 100) + float(_r / -100)) * (1.0 / n)
left_front_thruster_value = self.PWMNORMAL + self.PWMRANGE * (float(_x / 100)
+ float(_y / 100) + float(_r / 100)) * (1.0 / n)
right_rear_thruster_value = self.PWMNORMAL + self.PWMRANGE * (float(_x / 100)
+ float(_y / 100) + float(_r / -100)) * (1.0 / n)
left_rear_thruster_value = self.PWMNORMAL + self.PWMRANGE * (float(_x / -100)
+ float(_y / 100) + float(_r / 100)) * (1.0 / n)
self._horizontalMotors["right_front_thruster"] = int(right_front_thruster_value)
self._horizontalMotors["left_front_thruster"] = int(left_front_thruster_value)
self._horizontalMotors["right_rear_thruster"] = int(right_rear_thruster_value)
self._horizontalMotors["left_rear_thruster"] = int(left_rear_thruster_value)
def _calculateHorizontalMotors_17(self):
_x = self._valueMap['x']
_y = self._valueMap['y']
_r = self._valueMap['r']
theta = math.atan2(_x, _y)
circle_factor = max(abs(math.cos(theta)), abs(math.sin(theta)))
resultant = math.hypot(_x, _y) * circle_factor
# alpha = 45 deg - theta
# alpha = theta - self.ANGLE225
alpha = self.ANGLE45 - theta
maximum_factor = 1 / (math.cos(self.ANGLE45 - abs(theta) + (int(abs(theta) / self.ANGLE90) * self.ANGLE90)))
RightComponent = resultant * math.cos(alpha) * maximum_factor
LeftComponent = resultant * math.sin(alpha) * maximum_factor
front_right_thruster_value = int(self.MOTORS_BASE_PWM + (LeftComponent * self.FULL_PWM_RANGE_COEFFICIENT))
front_left_thruster_value = int(self.MOTORS_BASE_PWM + (RightComponent * self.FULL_PWM_RANGE_COEFFICIENT*0.64))
back_right_thruster_value = int(self.MOTORS_BASE_PWM + (RightComponent * self.FULL_PWM_RANGE_COEFFICIENT))
back_left_thruster_value = int(self.MOTORS_BASE_PWM + (LeftComponent * self.FULL_PWM_RANGE_COEFFICIENT*0.64))
front_right_thruster_value -= _r * self.FULL_ROTATION_COEFFICIENT
front_left_thruster_value += _r * self.FULL_ROTATION_COEFFICIENT
back_left_thruster_value += _r * self.FULL_ROTATION_COEFFICIENT
back_right_thruster_value -= _r * self.FULL_ROTATION_COEFFICIENT
right_front_thruster_value = self.MAXTHRUST * (front_right_thruster_value - self.PWMNORMAL) + self.PWMNORMAL
left_front_thruster_value = self.MAXTHRUST * (front_left_thruster_value - self.PWMNORMAL) + self.PWMNORMAL
right_rear_thruster_value = self.MAXTHRUST * (back_right_thruster_value - self.PWMNORMAL) + self.PWMNORMAL
left_rear_thruster_value = self.MAXTHRUST * (back_left_thruster_value - self.PWMNORMAL) + self.PWMNORMAL
self._horizontalMotors["right_front_thruster"] = int(right_front_thruster_value)
self._horizontalMotors["left_front_thruster"] = int(left_front_thruster_value)
self._horizontalMotors["right_rear_thruster"] = int(right_rear_thruster_value)
self._horizontalMotors["left_rear_thruster"] = int(left_rear_thruster_value)
def _calculateHorizontalMotors_Local(self):
_x = self._valueMap['x']
_y = self._valueMap['y']
_r = self._valueMap['r']
if _r > 0:
front_right_thruster_value = 260
front_left_thruster_value = self.PWMNORMAL
back_right_thruster_value = self.PWMNORMAL
back_left_thruster_value = 260
elif _r < 0:
front_right_thruster_value = self.PWMNORMAL
front_left_thruster_value = 328
back_right_thruster_value = 328
back_left_thruster_value = self.PWMNORMAL
elif abs(_y) > abs(_x) and _y > 0:
front_right_thruster_value = 352
front_left_thruster_value = 239
back_right_thruster_value = 352
back_left_thruster_value = 239
elif abs(_y) > abs(_x) and _y < 0:
front_right_thruster_value = 239
front_left_thruster_value = 338
back_right_thruster_value = 239
back_left_thruster_value = 338
else:
back_left_thruster_value = self.PWMNORMAL
back_right_thruster_value = self.PWMNORMAL
front_right_thruster_value = self.PWMNORMAL
front_left_thruster_value = self.PWMNORMAL
right_front_thruster_value = front_right_thruster_value
left_front_thruster_value = front_left_thruster_value
right_rear_thruster_value = back_right_thruster_value
left_rear_thruster_value = back_left_thruster_value
self._horizontalMotors["right_front_thruster"] = int(right_front_thruster_value)
self._horizontalMotors["left_front_thruster"] = int(left_front_thruster_value)
self._horizontalMotors["right_rear_thruster"] = int(right_rear_thruster_value)
self._horizontalMotors["left_rear_thruster"] = int(left_rear_thruster_value)
def _calculateVerticalMotors_NormalMode(self):
# top_front_thruster_value = self._hardware.getDeviceValue('top_front_thruster')
# top_rear_thruster_value = self._hardware.getDeviceValue('top_rear_thruster')
if self._valueMap['up']:
top_front_thruster_value = int(self.PWMNORMAL + (self._valueMap['z']*self.VMAXTHRUST*(self.PWMRANGE/100)))
top_rear_thruster_value = int(self.PWMNORMAL + (self._valueMap['z']*self.VMAXTHRUST*(self.PWMRANGE/100)))
elif self._valueMap['down']:
top_front_thruster_value = int(self.PWMNORMAL - (self._valueMap['z']*self.VMAXTHRUST*(self.PWMRANGE/100)))
top_rear_thruster_value = int(self.PWMNORMAL - (self._valueMap['z']*self.VMAXTHRUST*(self.PWMRANGE/100)))
self._verticalMotors["top_front_thruster"] = int(top_front_thruster_value)
self._verticalMotors["top_rear_thruster"] = int(top_rear_thruster_value)
def _calculateVerticalMotors_HomeMode(self):
# top_front_thruster_value = self._hardware.getDeviceValue('top_front_thruster')
# top_rear_thruster_value = self._hardware.getDeviceValue('top_rear_thruster')
if self._valueMap['down']:
top_front_thruster_value = int(self.PWMNORMAL + (self._valueMap['z']*(self.PWMRANGE/100)))
top_rear_thruster_value = int(self.PWMNORMAL + (self._valueMap['z']*(self.PWMRANGE/100)))
elif self._valueMap['up']:
top_front_thruster_value = int(self.PWMNORMAL - (self._valueMap['z']*(self.PWMRANGE/100)))
top_rear_thruster_value = int(self.PWMNORMAL - (self._valueMap['z']*(self.PWMRANGE/100)))
self._verticalMotors["top_front_thruster"] = int(top_front_thruster_value)
self._verticalMotors["top_rear_thruster"] = int(top_rear_thruster_value)
def _stopVerticalMotors(self):
self._verticalMotors["top_front_thruster"] = self._hardware.getDeviceBaseValue("top_front_thruster")
# self._verticalMotors["top_front_thruster"] = 324
# self._verticalMotors["top_rear_thruster"] = 324
self._verticalMotors["top_rear_thruster"] = self._hardware.getDeviceBaseValue("top_rear_thruster")
def _stopVerticalMotors_(self):
self._verticalMotors["top_front_thruster"] = self._hardware.getDeviceBaseValue("top_front_thruster")
# self._verticalMotors["top_front_thruster"] = 324
# self._verticalMotors["top_rear_thruster"] = 324
self._verticalMotors["top_rear_thruster"] = self._hardware.getDeviceBaseValue("top_rear_thruster")
def _setCamToNormalPosition(self):
self._servos["camera_servo"] = self._hardware.getDeviceBaseValue("camera_servo")
def _stopHorizontalMotors(self):
self._horizontalMotors["right_front_thruster"] = self._hardware.getDeviceBaseValue("right_front_thruster")
self._horizontalMotors["left_front_thruster"] = self._hardware.getDeviceBaseValue("left_front_thruster")
self._horizontalMotors["right_rear_thruster"] = self._hardware.getDeviceBaseValue("right_rear_thruster")
self._horizontalMotors["left_rear_thruster"] = self._hardware.getDeviceBaseValue("left_rear_thruster")
def _printPWMValues(self):
for motor in self._horizontalMotors:
print(motor, " pwm: ", self._horizontalMotors[motor])
for motor in self._verticalMotors:
print(motor, " pwm: ", self._verticalMotors[motor])
def _turnLightOff(self):
self._lights['light'] = 0
def _moveCamera(self):
camera_servo_value = self._hardware.getDeviceValue('camera_servo')
if self._valueMap['cam_up'] and camera_servo_value <= 500:
camera_servo_value = camera_servo_value + 10
elif self._valueMap['cam_down'] and camera_servo_value >= 310:
camera_servo_value = camera_servo_value - 10
self._servos["camera_servo"] = camera_servo_value
def _light(self):
light_pwm_value = self._hardware.getDeviceValue('light')
if light_pwm_value < 1800:
light_pwm_value = light_pwm_value + 500
else:
light_pwm_value = 0
self._lights["light"] = light_pwm_value
def _setFromMyLocalToDevice(self):
self._hardware.setDeviceValue("right_front_thruster", self._horizontalMotors["right_front_thruster"])
self._hardware.setDeviceValue("left_front_thruster", self._horizontalMotors["left_front_thruster"])
self._hardware.setDeviceValue("right_rear_thruster", self._horizontalMotors["right_rear_thruster"])
self._hardware.setDeviceValue("left_rear_thruster", self._horizontalMotors["left_rear_thruster"])
self._hardware.setDeviceValue("top_front_thruster", self._verticalMotors["top_front_thruster"])
self._hardware.setDeviceValue("top_rear_thruster", self._verticalMotors["top_rear_thruster"])
self._hardware.setDeviceValue("top_rear_thruster", self._verticalMotors["top_rear_thruster"])
self._hardware.setDeviceValue("camera_servo", self._servos["camera_servo"])
self._hardware.setDeviceValue("light", self._lights["light"])
def registerCallBack(self, callback):
self._eventCallBack = callback
def update(self, event, mail_map=None):
if event == "TCP ERROR":
self._stopHorizontalMotors()
self._stopVerticalMotors()
self._setFromMyLocalToDevice()
self._printPWMValues()
if event is "TCP":
self._valueMap = mail_map
for key in self._valueMap:
self._valueMap[key] = float(self._valueMap[key])
print("TCP Event")
if self._valueMap['mode'] == 0:
if self._valueMap["up"] == 1 or self._valueMap["down"] == 1:
print("calculating vertical motors")
self._calculateVerticalMotors_NormalMode()
else:
self._stopVerticalMotors()
if self._valueMap["cam_up"] == 1 or self._valueMap["cam_down"]:
print("moving camera")
self._moveCamera()
if self._valueMap['l'] == 1:
print("light event")
self._light()
print("calculating horizontal motors")
self._calculateHorizontalMotors_17()
# self._calculateHorizontalMotors_Local()
elif self._valueMap['mode'] == 1:
if self._valueMap["up"] == 1 or self._valueMap["down"] == 1:
print("calculating vertical motors")
self._stopHorizontalMotors()
self._calculateVerticalMotors_HomeMode()
else:
print("calculating horizontal motors")
self._stopVerticalMotors()
self._calculateHorizontalMotors_17()
# self._calculateHorizontalMotors_Local()
if self._valueMap["cam_up"] == 1 or self._valueMap["cam_down"]:
print("moving camera")
self._moveCamera()
if self._valueMap['l'] == 1:
print("light event")
self._light()
self._setFromMyLocalToDevice()
# self._printPWMValues()
if self._eventCallBack is not None:
self._eventCallBack("HAT")
# PWM Clock Interrupter Event Listener (deprecated)
# if event == "CLOCK":
# # print("PWM UPDATE")
# self._setFromMyLocalToDevice()
# self._eventcallback("HAT")