forked from robotstreamer/robotstreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbcshop_interface.py
198 lines (168 loc) · 3.83 KB
/
sbcshop_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
import atexit
import robot_util
import PiMotor
import RPi.GPIO as GPIO
import os
import time
#how long a forward or reverse movement lasts in seconds
straightDelay=0.5
#how long a turn movement lasts in seconds
turnDelay=0.25
#tracks the state of the movement system globally
movementSystemActive=False
#gets called to turn on the motors and lights to turn right
def turnRight():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
ar.on()
mfl.forward(100)
mfr.reverse(100)
mbl.forward(100)
mbr.reverse(100)
return
#gets called to turn on the motors and lights to turn left
def turnLeft():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
al.on()
mfl.reverse(100)
mfr.forward(100)
mbl.reverse(100)
mbr.forward(100)
return
#gets called to turn on the motors and lights to go forward
def goForward():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
af.on()
mfl.forward(100)
mfr.forward(100)
mbl.forward(100)
mbr.forward(100)
return
#gets called to turn on the motors and lights to go backward
def goBackward():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
ab.on()
mfl.reverse(100)
mfr.reverse(100)
mbl.reverse(100)
mbr.reverse(100)
return
#gets called to turn off all motors and lights
def releaseMotors():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
af.off()
al.off()
ab.off()
ar.off()
mfl.stop()
mfr.stop()
mbl.stop()
mbr.stop()
return
#special function called only when the program stops using python's atexit feature
def motorshieldShutdown():
releaseMotors()
return
#gets called once by controller.py to initialize the interface
def init():
global af
global al
global ab
global ar
global mfl
global mfr
global mbl
global mbr
ab = PiMotor.Arrow(1)
al = PiMotor.Arrow(2)
af = PiMotor.Arrow(3)
ar = PiMotor.Arrow(4)
#front left motor
mfl=PiMotor.Motor("MOTOR2", 1)
#front right motor
mfr=PiMotor.Motor("MOTOR3", 1)
#back left motor
mbl=PiMotor.Motor("MOTOR1", 1)
#back right motor
mbr=PiMotor.Motor("MOTOR4", 1)
atexit.register(motorshieldShutdown)
return
#called by controller.py every time a command is sent
def handleCommand(command, keyPosition, price=0):
global movementSystemActive
print("\n\n\n\nhandleCommand called.\n")
if keyPosition != "down":
return
robot_util.handleSoundCommand(command, keyPosition, price)
if command == 'F':
if movementSystemActive:
print("skip")
else:
movementSystemActive=True
goForward()
time.sleep(straightDelay)
releaseMotors()
movementSystemActive=False
if command == 'B':
if movementSystemActive:
print("skip")
else:
movementSystemActive=True
goBackward()
time.sleep(straightDelay)
releaseMotors()
movementSystemActive=False
if command == 'L':
if movementSystemActive:
print("skip")
else:
movementSystemActive=True
turnLeft()
time.sleep(turnDelay)
releaseMotors()
movementSystemActive=False
if command == 'R':
if movementSystemActive:
print("skip")
else:
print("onright")
movementSystemActive=True
turnRight()
time.sleep(turnDelay)
releaseMotors()
movementSystemActive=False
return