-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtankbot_KEYBOARD_2.6.py
293 lines (241 loc) · 8.16 KB
/
tankbot_KEYBOARD_2.6.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
#Improved KEYBOARD control with WASD.
#FORWARD plus TURN results in a slow turn.
#TURN results in faster turn.
#2.0 Keys can be held down
#2.5 Start implementing camera commands. Camera servos connected to GPIO pins
#2.6 Servo control moved over to Servo Controller board. Added laser to SPACEBAR and GPIO 7
from Adafruit_PWM_Servo_Driver import PWM
import pygame
import os, sys
import Robot
import threading
import RPi.GPIO as GPIO
import time
import sonarBot
GPIO.setmode(GPIO.BOARD)
#Servo setup:
#Connect PAN servo to slot 2 and TILT servo to slot 3
pwm = PWM(0x40)
panServo = 365 #Pan servo, center position
tiltServo = 420 #Tilt servo, center position
panMAX = 610 #Leftmost position
panMIN = 150 #Rightmost position
tiltMAX = 530 #Downmost position
tiltMIN = 220 #Upmost position
servoSpeed = 5 #Turning speed
pwm.setPWMFreq(60)
pwm.setPWM(3, 0, tiltServo) #Start servos centered
pwm.setPWM(2, 0, panServo) #Start servos centered
#END servo setup
speed = 255 #Speed multiplier, values 0-255
reducedSpeed = int(speed*0.50) #Alternate track speed for turning. The lower the percentage, the lower the turning radius and forward momentum.
robot = Robot.Robot(left_trim=0, right_trim=0)
sonarBot = sonarBot
#os.environ['SDL_VIDEODRIVER'] = 'dummy'
pygame.init()
myfont = pygame.font.SysFont("monospace", 24)
screen = pygame.display.set_mode((200, 200))
running = True
#Dictionary for command values.
#The use of a dictionary helps us to read multiple values at the same time,
#and to specify an action related to their value.
moveCommandValues = { 'W':0,
'A':0,
'S':0,
'D':0, }
cameraCommandValues = { 'K_UP':0,
'K_DOWN':0,
'K_LEFT':0,
'K_RIGHT':0,
}
functionCommandValues = { 'K_SPACE':0,
'K_F2':0, }
print ("********\n" + "*READY!*\n" + "********")
print("Press ESCAPE to QUIT")
readytext = myfont.render("READY!", 1, (0,255,0))
screen.blit(readytext, (60, 160))
pygame.draw.circle(screen, (0,255,0), (100,100), 50)
pygame.display.flip()
def checkKeyDown():
#Change values for Tank Movement
if event.key == pygame.K_w:
#print("forward!")
moveCommandValues['W'] = 1
if event.key == pygame.K_s:
#print("reverse")
moveCommandValues['S'] = 1
if event.key == pygame.K_a:
#print("turn left")
moveCommandValues['A'] = 1
if event.key == pygame.K_d:
#print("turn right")
moveCommandValues['D'] = 1
robot.stop()
#Change values for Camera Movement
if event.key == pygame.K_UP:
#print("Camera up")
cameraCommandValues['K_UP'] = 1
if event.key == pygame.K_DOWN:
#print("Camera down")
cameraCommandValues['K_DOWN'] = 1
if event.key == pygame.K_LEFT:
#print("Camera left")
cameraCommandValues['K_LEFT'] = 1
if event.key == pygame.K_RIGHT:
#print("Camera right")
cameraCommandValues['K_RIGHT'] = 1
if event.key == pygame.K_SPACE:
functionCommandValues['K_SPACE'] = 1
if event.key == pygame.K_F2:
functionCommandValues['K_F2'] = 1
if event.key == pygame.K_RCTRL: #Press right CTRL to center PanTilt-kit
panServo = 365
tiltServo = 420
pwm.setPWM(2, 0, panServo)
pwm.setPWM(3, 0, tiltServo)
#Press ESCAPE to QUIT
if event.key == pygame.K_ESCAPE:
print(moveCommandValues)
print(cameraCommandValues)
running = False
quit()
pygame.quit()
else:
pass
def checkKeyUp():
#Change values for Tank Movement
if event.key == pygame.K_w:
#print("stopFORWARD")
moveCommandValues['W'] = 0
if event.key == pygame.K_s:
#print("stopREVERSE")
moveCommandValues['S'] = 0
if event.key == pygame.K_a:
#print("stopLEFT")
moveCommandValues['A'] = 0
if event.key == pygame.K_d:
#print("stopRIGHT")
moveCommandValues['D'] = 0
#Change values for Camera Movement
if event.key == pygame.K_UP:
#print("Camera up STOP")
cameraCommandValues['K_UP'] = 0
if event.key == pygame.K_DOWN:
#print("Camera down STOP")
cameraCommandValues['K_DOWN'] = 0
if event.key == pygame.K_LEFT:
#print("Camera left STOP")
cameraCommandValues['K_LEFT'] = 0
if event.key == pygame.K_RIGHT:
#print("Camera right STOP")
cameraCommandValues['K_RIGHT'] = 0
if event.key == pygame.K_SPACE:
functionCommandValues['K_SPACE'] = 0
if event.key == pygame.K_F2:
functionCommandValues['K_F2'] = 0
else:
pass
while running:
for event in pygame.event.get():
#When a key is pressed down
if event.type == pygame.KEYDOWN:
checkKeyDown()
#When a key is released
if event.type == pygame.KEYUP:
checkKeyUp()
#Translate values into movement commands
#Only forward
if moveCommandValues['W'] == 1 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 0:
robot.RT_forward(speed)
robot.LT_forward(speed)
#Left turn while moving forward
if moveCommandValues['W'] == 1 \
and moveCommandValues['A'] == 1 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 0:
robot.RT_forward(speed)
robot.LT_forward(reducedSpeed)
#Right turn while moving forward
if moveCommandValues['W'] == 1 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 1:
robot.RT_forward(reducedSpeed)
robot.LT_forward(speed)
#Only reverse
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 1 \
and moveCommandValues['D'] == 0:
robot.RT_backward(speed)
robot.LT_backward(speed)
#Left turn while reversing
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 1 \
and moveCommandValues['S'] == 1 \
and moveCommandValues['D'] == 0:
robot.RT_backward(reducedSpeed)
robot.LT_backward(speed)
#Right turn while reversing
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 1 \
and moveCommandValues['D'] == 1:
robot.RT_backward(speed)
robot.LT_backward(reducedSpeed)
#Only left turn
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 1 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 0:
robot.RT_forward(speed)
robot.LT_backward(speed)
#Only right turn
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 1:
robot.RT_backward(speed)
robot.LT_forward(speed)
#Stop when no keys pressed
if moveCommandValues['W'] == 0 \
and moveCommandValues['A'] == 0 \
and moveCommandValues['S'] == 0 \
and moveCommandValues['D'] == 0:
robot.stop()
#Translate values into camera movement
if cameraCommandValues['K_UP'] == 1: #If the key is pressed down
#print("UP")
tiltServo -= servoSpeed #Change the servo position by the value of servoSpeed.
if tiltServo < tiltMIN: #If positional value exceeds the set limit,
tiltServo = tiltMIN #set it back to its limit value.
pwm.setPWM(3, 0, tiltServo) #Move the servo to the specified location
if cameraCommandValues['K_DOWN'] == 1:
#print("DOWN")
tiltServo += servoSpeed
if tiltServo > tiltMAX:
tiltServo = tiltMAX
pwm.setPWM(3, 0, tiltServo)
if cameraCommandValues['K_LEFT'] == 1:
#print("LEFT")
panServo += servoSpeed
if panServo > panMAX:
panServo = panMAX
pwm.setPWM(2, 0, panServo)
if cameraCommandValues['K_RIGHT'] == 1:
#print("RIGHT")
panServo -= servoSpeed
if panServo < panMIN:
panServo = panMIN
pwm.setPWM(2, 0, panServo)
if functionCommandValues['K_SPACE'] == 1: #Laser on
GPIO.setup(7, GPIO.OUT)
if functionCommandValues['K_SPACE'] == 0: #Laser off
GPIO.setup(7, GPIO.IN)
if functionCommandValues['K_F2'] == 1:
sonarBot.moveBot()
pygame.quit()
quit()