-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescuex.py
238 lines (198 loc) · 8.14 KB
/
rescuex.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
#!/usr/bin/env python
'''
rescuex.py
This code is intended to control a rescue robot which main task is
to rescue a person in three different scenarios:
- A muddy inclined plane
- An obscure laberynth
- A high alttitude challenge
All tasks were tested and exectuted using a Raspberry Pi 3 B running Debian 8.
Bill of materials:
Servos 180 | 3
Continous servos | 1
DC motors | 4
Webcamera | 1
Playstation 3 controller | 1
Daniela A. Plascencia <[email protected]>'''
#-----LIBRARIES-----
import pygame #Among other options, it fetches information from the PS controller
import time #Module of time to make delays
import RPi.GPIO as GPIO #Gets all functionallity from RPi's GPIOs
#-----INITIAL SETUP-----
GPIO.setmode(GPIO.BOARD) #Sets a mode so you can use the number of a pin in Raspberry Pi board
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Welcome to RescueX. Initialized Joystick : %s' % j.get_name()
#-----ASSIGNING VARIABLES-----
MotorA0 = 16 #H-Bridge inputs for the left pair of motors
MotorA1 = 18
MotorB0 = 29 #H-Bridge inputs for the right pair of motors
MotorB1 = 31
Servo1 = 12 #Clamp servo
Servo2 = 10 #Rack gear clamp
Servo3 = 8 #Positioning clamp servo
Servo4 = 36 #Lifting crane servo
value = 0 #Control variable
max_val = 8 #This is the maximum duty cycle for every servo
min_val = 3 #Minimim duty cycle for every servo
A0 = False #Initialise variable in 0
A1 = False #Initialise variable in 0
B0 = False #Initialise variable in 0
B1 = False #Initialise variable in 0
threshold = 0.60 #Control variable. Pygame takes the value of a joystick and compares it to a treshold so it can establish a set of values where the joystick will be detected as up or down movement
LeftTrack = 0 #Control variables: flags that permit access to an specific function
RightTrack = 0
x = 0
y = 0
#-----SETTING GPIOS DIRECTION-----
GPIO.setup(MotorA0,GPIO.OUT) #Sets GPIOs as outputs
GPIO.setup(MotorA1,GPIO.OUT)
GPIO.setup(MotorAE,GPIO.OUT)
GPIO.setup(MotorB0,GPIO.OUT)
GPIO.setup(MotorB1,GPIO.OUT)
GPIO.setup(MotorBE,GPIO.OUT)
GPIO.setup(Servo1, GPIO.OUT)
GPIO.setup(Servo2, GPIO.OUT)
GPIO.setup(Servo3, GPIO.OUT)
GPIO.setup(Servo4, GPIO.OUT)
#-----PWM SETUP-----
S1 = GPIO.PWM(Servo1, 50) #Establishes an assignment to a variable with the module PWM (set), number of pin (ServoX) and frequency (50 Hz)
S2 = GPIO.PWM(Servo2, 50)
S3 = GPIO.PWM(Servo3, 50)
S4 = GPIO.PWM(Servo4, 50)
#-----GPIOS OFF-----
GPIO.output(MotorA0, A0) #Start all GPIOs in zero so they do not move during the processing
GPIO.output(MotorA1, A1)
GPIO.output(MotorAE, False)
GPIO.output(MotorBE, False)
GPIO.output(MotorB0, B0)
GPIO.output(MotorB1, B1)
S1.start(min_val) #Starts servos at their minimum duty cycle
S2.start(min_val)
S3.start(min_val)
S4.start(0)
#This function gives a value to a certain GPIO, then it sends it to an H-bridge so a combination of digital values are written in the H-bride.
def setmotors():
GPIO.output(MotorA0, A0)
GPIO.output(MotorA1, A1)
GPIO.output(MotorAE, True)
GPIO.output(MotorBE, True)
GPIO.output(MotorB0, B0)
GPIO.output(MotorB1, B1)
try:
# Turn on the motors
GPIO.output(MotorAE, True)
GPIO.output(MotorBE, True)
#-----MAIN LOOP-----
while True:
events = pygame.event.get() #Read a list of events the Playstation 3 controller makes
for event in events:
UpdateMotors = 0
#JOYSTICK1 & JOYSTICK2: Gets the value of joystick 1 and 2 from the Playstation 3 controller
if event.type == pygame.JOYAXISMOTION:
if event.axis == 1:
LeftTrack = event.value
UpdateMotors = 1
elif event.axis == 3:
RightTrack = event.value
UpdateMotors = 1
#L1 & L2: Gets the value of triggers R1 from the Playstation 3 controller
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 11:
x = 11
UpdateMotors = 1
elif event.type == pygame.JOYBUTTONUP:
if event.button == 11:
x = 0
UpdateMotors = 1
#L1 & L2: Gets the value of triggers L1 from the Playstation 3 controller
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 10:
x = 10
UpdateMotors = 1
elif event.type == pygame.JOYBUTTONUP:
if event.button == 10:
x = 0
UpdateMotors = 1
#TRIANGLE: Gets the state of button
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 12:
y = 12
UpdateMotors = 1
#CIRCLE: Gets the state of button
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 13:
y = 13
UpdateMotors = 1
#X: Gets the state of button
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 14:
y = 14
UpdateMotors = 1
#SQUARE: Gets the state of button
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 15:
y = 15
UpdateMotors = 1
#Main loop. Depending on the pressed button, Raspberry Pi will send a digital output to any of the specified GPIO
if UpdateMotors:
if (y==12): #When triangle is pressed, you can move a servo (S1) to rotate clockwise (R1) or counter clockwise(L1)
if (j.get_button(11) and value < max_val):
value = value + 0.5
S1.ChangeDutyCycle(value)
elif (j.get_button(10) and value > min_val):
value = value - 0.5
S1.ChangeDutyCycle(value)
if (y==13): #When circle is pressed, you can move a servo (S2) to to rotate clockwise (R1) or counter clockwise(L1)
if (j.get_button(11) and value < max_val):
value = value + 0.5
S2.ChangeDutyCycle(value)
elif (j.get_button(10) and value > min_val):
value = value - 0.5
S2.ChangeDutyCycle(value)
if (y==14): #When x is pressed, you can move a servo (S3) to rotate clockwise (R1) or counter clockwise(L1)
o if (j.get_button(11) and value < max_val):
value = value + 0.5
S3.ChangeDutyCycle(value)
elif (j.get_button(10) and value > min_val):
value = value - 0.5
S3.ChangeDutyCycle(value)
if (y==15): #When square is pressed, you can move a servo (S4) to rotate clockwise (R1) or counter clockwise (L1)
if (j.get_button(11)):
S4.ChangeDutyCycle(5)
elif (j.get_button(10)):
S4.ChangeDutyCycle(10)
else:
S4.ChangeDutyCycle(0)
# Move forwards
if (RightTrack > threshold):
A0 = False
A1 = True
# Move backwards
elif (RightTrack < -threshold):
A0 = True
A1 = False
# Stopping
else:
A0 = False
A1 = False
if (LeftTrack > threshold):
B0 = False
B1 = True
# Move backwards
elif (LeftTrack < -threshold):
B0 = True
B1 = False
# Otherwise stop
else:
B0 = False
B1 = False
setmotors()
#The script can be exited from
except KeyboardInterrupt:
# Turn off the motors
GPIO.output(MotorAE, False)
GPIO.output(MotorBE, False)
j.quit()#!/usr/bin/env python
GPIO.cleanup()