forked from robotstreamer/robotstreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsbot_interface.py
290 lines (214 loc) · 9.37 KB
/
rsbot_interface.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
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
from robot_util import times
import time
import atexit
import json
import _thread
import vibrate
import robot_util
commandArgs = None
turningSpeedActuallyUsed = 255
#drivingSpeedAcutallyUsed = 255
straightDelay = 0.6
movementSystemActive = False
pingPongNumActive = 0
freePongActive = False
mh = Adafruit_MotorHAT(addr=0x60)
def getMotorHAT():
global mh
return mh
# occationally do this reinitialization in case it has stopped working
#if random.randint(0, 50) == 0:
# print()
# print("******** GETTING MOTOR HAT OBJECT AGAIN ********")
# print()
# mh = Adafruit_MotorHAT(addr=0x60)
#return mh
import random
def runMotor(motorIndex, direction):
global mh
mh = getMotorHAT()
motor = mh.getMotor(motorIndex+1)
if direction == 1:
motor.setSpeed(drivingSpeed)
motor.run(Adafruit_MotorHAT.FORWARD)
if direction == -1:
motor.setSpeed(drivingSpeed)
motor.run(Adafruit_MotorHAT.BACKWARD)
if direction == 0.5:
motor.setSpeed(128)
motor.run(Adafruit_MotorHAT.FORWARD)
if direction == -0.5:
motor.setSpeed(128)
motor.run(Adafruit_MotorHAT.BACKWARD)
def init(cArgs, forwardDefinition, leftDefinition, pEnabled):
global mh
global commandArgs
global drivingSpeed
global speed1
global left
global right
global forward
global backward
global motorA
global motorB
global pingPongEnabled
global mhPingPong
pingPongEnabled = pEnabled
forward = forwardDefinition
backward = times(forward, -1)
left = leftDefinition
right = times(left, -1)
print("directions", forward, backward, left, right)
commandArgs = cArgs
drivingSpeed = commandArgs.straight_speed
speed1 = drivingSpeed
_thread.start_new_thread(demoShots, ())
atexit.register(turnOffMotors)
motorA = mh.getMotor(1)
motorB = mh.getMotor(2)
atexit.register(turnOffMotors)
if pingPongEnabled:
mhPingPong = Adafruit_MotorHAT(addr=0x61)
def turnOffMotors():
global mh
mh = getMotorHAT()
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
def demoShots():
while True:
time.sleep(3600)
#handleCommand('FIRE')
#todo: should be called process command
def handleCommand(command, keyPosition, price=0):
global movementSystemActive
global pingPongNumActive
global freePongActive
if keyPosition != "down":
return
d = 255
motorA.setSpeed(speed1)
motorB.setSpeed(speed1)
robot_util.handleSoundCommand(command, keyPosition, price)
if command == 'F':
if movementSystemActive:
print("skip")
else:
drivingSpeed = d
for motorIndex in range(4):
runMotor(motorIndex, forward[motorIndex])
movementSystemActive = True
time.sleep(straightDelay)
turnOffMotors()
movementSystemActive = False
if command == 'B':
if movementSystemActive:
print("skip")
else:
drivingSpeed = d
for motorIndex in range(4):
runMotor(motorIndex, backward[motorIndex])
movementSystemActive = True
time.sleep(straightDelay)
turnOffMotors()
movementSystemActive = False
if command == 'L':
if movementSystemActive:
print("skip")
else:
drivingSpeed = turningSpeedActuallyUsed
for motorIndex in range(4):
runMotor(motorIndex, left[motorIndex])
movementSystemActive = True
print("starting")
time.sleep(commandArgs.turn_delay)
print("finished")
turnOffMotors()
movementSystemActive = False
if command == 'R':
if movementSystemActive:
print("skip")
else:
drivingSpeed = turningSpeedActuallyUsed
for motorIndex in range(4):
runMotor(motorIndex, right[motorIndex])
movementSystemActive = True
time.sleep(commandArgs.turn_delay)
turnOffMotors()
movementSystemActive = False
if command == 'U':
#mhArm.getMotor(1).setSpeed(127)
#mhArm.getMotor(1).run(Adafruit_MotorHAT.BACKWARD)
incrementArmServo(1, 10)
time.sleep(0.05)
if command == 'D':
#mhArm.getMotor(1).setSpeed(127)
#mhArm.getMotor(1).run(Adafruit_MotorHAT.FORWARD)
incrementArmServo(1, -10)
time.sleep(0.05)
if command == 'O':
#mhArm.getMotor(2).setSpeed(127)
#mhArm.getMotor(2).run(Adafruit_MotorHAT.BACKWARD)
incrementArmServo(2, -10)
time.sleep(0.05)
if command == 'C':
#mhArm.getMotor(2).setSpeed(127)
#mhArm.getMotor(2).run(Adafruit_MotorHAT.FORWARD)
incrementArmServo(2, 10)
time.sleep(0.05)
if command == 'FIRE':
print("processing fire")
if pingPongEnabled:
print("fire was enabled")
pingPongNumActive += 1
print("ping pong number active", pingPongNumActive)
pingPongMotor = mhPingPong.getMotor(1)
pingPongMotor.setSpeed(255)
pingPongMotor.run(Adafruit_MotorHAT.FORWARD)
time.sleep(3.1)
pingPongNumActive -= 1
print("ping pong number active", pingPongNumActive)
# if nobody has the cannon active anymore, release
if pingPongNumActive == 0:
pingPongMotor.run(Adafruit_MotorHAT.RELEASE)
else:
print("ping pong not enabled")
if command == 'FREE_FIRE':
return
print("processing fire")
if not freePongActive:
freePongActive = True
print("free pong fire was enabled")
pingPongMotor = mhPingPong.getMotor(1)
pingPongMotor.setSpeed(255)
pingPongMotor.run(Adafruit_MotorHAT.FORWARD)
time.sleep(2.8)
print("free ping pong", freePongActive)
pingPongMotor.run(Adafruit_MotorHAT.RELEASE)
time.sleep(180)
freePongActive = False
else:
print("ping pong not enabled")
if command == 'FIRE_ALL':
print("processing fire")
if pingPongEnabled:
print("fire was enabled")
pingPongNumActive += 1
print("ping pong number active", pingPongNumActive)
pingPongMotor = mhPingPong.getMotor(1)
pingPongMotor.setSpeed(255)
pingPongMotor.run(Adafruit_MotorHAT.FORWARD)
time.sleep(50)
pingPongNumActive -= 1
print("ping pong number active", pingPongNumActive)
# if nobody has the cannon active anymore, release
if pingPongNumActive == 0:
pingPongMotor.run(Adafruit_MotorHAT.RELEASE)
else:
print("ping pong not enabled")
if command == 'VIBRATE':
vibrate.vibrate(mh, forward)
# routing
#{"F": forward, "B": back, "L": left, "R": right}