-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotSimulatorGreen.py
433 lines (397 loc) · 13 KB
/
robotSimulatorGreen.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# file robotSimulator.py simulates a robot in an arena
from sensorPlanTCP import SensorPlanTCP
from robotSimIX import SimpleRobotSim,RobotSimInterface
from joy import JoyApp, progress
from joy.decl import *
from joy.plans import Plan
from waypointShared import WAYPOINT_HOST, APRIL_DATA_PORT, lineSensorResponse, lineDist
from socket import (
socket, AF_INET,SOCK_DGRAM, IPPROTO_UDP, error as SocketError,
)
from pylab import randn,dot,mean,exp,newaxis,rand,ones,asarray,find,prctile,linspace,arange,argmax
from joy.plans import AnimatorPlan
import math
current_angle = 0
class yMovePlan(Plan):
"""
Plan simulates robot moving forward or back over a period of time.
(MODIFY THIS FOR YOUR ROBOT)
"""
def __init__(self,app,simIX):
Plan.__init__(self,app)
self.simIX = simIX
# Angle to turn [rad]
self.ang = 1+0j
# Distance to travel
self.dist = 10
# Duration of travel [sec]
self.dur = 3
# Number of intermediate steps
self.N = 10
def behavior(self):
s = self.simIX
progress("Pos: %s" % (s.tagPos))
s.ang = self.ang
s.yVis = True
s.xVis = False
# Compute step along the forward direction
step = self.dist / float(self.N)
dt = self.dur / float(self.N)
for k in range(self.N):
s.move(step)
yield self.forDuration(dt)
class xMovePlan(Plan):
"""
Plan simulates robot turning over a period of time.
(MODIFY THIS FOR YOUR ROBOT)
"""
def __init__(self,app,simIX):
Plan.__init__(self,app)
self.simIX = simIX
# Angle to turn [rad]
self.ang = 1j
# Duration of travel [sec]
self.dur = 3
# Number of intermediate steps
self.N = 10
# Distance to travel RIGHT/LEFT
self.dist = 10
def behavior(self):
s = self.simIX
s.ang = self.ang
s.yVis = False
s.xVis = True
# Compute rotation step
step = self.dist / float(self.N)
dt = self.dur / float(self.N)
for k in range(self.N):
s.move(step)
yield self.forDuration(dt)
class Auto(Plan):
"""
Autonomous mode for simulation of robot
"""
def __init__(self,app,simIX,yMoveP,xMoveP):
Plan.__init__(self,app)
self.simIX = simIX
self.sensor = self.app.sensor
# Duration of travel [sec]
self.dur = 3.0
# Number of intermediate steps
self.N = 10
# Distance to travel
self.dist = 10
#Plans
self.yMove = yMoveP
self.xMove = xMoveP
def negY(self):
self.yMove.dist = 50
yield self.yMove.start()
yield self.forDuration(2.5)
yield self.yMove.stop()
def posY(self):
self.yMove.dist = -50
yield self.yMove.start()
yield self.forDuration(2.5)
yield self.yMove.stop()
def posX(self):
self.xMove.dist = 50
yield self.xMove.start()
yield self.forDuration(2.5)
yield self.xMove.stop()
def negX(self):
self.xMove.dist = -50
yield self.xMove.start()
yield self.forDuration(2.5)
yield self.xMove.stop()
def behavior(self):
dictBacktrack = {'negY': self.posY, 'posY': self.negY, 'posX': self.negX, 'negX': self.posX}
history = []
ts,f,b = self.sensor.lastSensor
key1 = 'headDown' #Default dictionary
while True:
s = self.simIX
ts, f, b = self.sensor.lastSensor
old_f = f
old_b = b
ts, w = self.sensor.lastWaypoints
old_wx = w[0][0]
old_wy = w[0][1]
new_wx = w[1][0]
new_wy = w[1][1]
old_w = dot(asarray(s.matTransform),asarray([[old_wx],[old_wy],[1]]))
old_wx = old_w[0]
old_wy = old_w[1]
new_w = dot(asarray(s.matTransform),asarray([[new_wx],[new_wy],[1]]))
new_wx = new_w[0]
new_wy = new_w[1]
diffy = abs(new_wy - old_wy)
diffx = abs(new_wx - old_wx)
if(diffy > diffx and old_wy > new_wy): #new waypoint is in -y dir
key1 = 'headDown'
elif(diffy > diffx and old_wy < new_wy): #new waypoint is in +y dir
key1 = 'headUp'
elif(diffy < diffx and old_wx < new_wx): #new waypoint is in +x dir
if key1 == 'headRight':
key1 = 'headRight'
elif key1 == 'headRightAlt':
key1 = 'headRightAlt'
else:
key1 = 'headRight'
elif(diffy < diffx and old_wx > new_wx): #new waypoint is in +x dir
if key1 == 'headLeft':
key1 = 'headLeft'
elif key1 == 'headLeftAlt':
key1 = 'headLeftAlt'
else:
key1 = 'headLeft'
else: #TODO figure out what to do if new waypoint is in 45 deg from old waypoint
continue
if(key1 == 'headDown'):
progress('Dict = %s ' % (key1))
yield self.negY()
history.append('negY')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.posX()
history.append('posX');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.negX()
history.append('negX')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.negY()
history.append('negY')
elif(key1 == 'headUp'):
progress('Dict = %s ' % (key1))
yield self.posY()
history.append('posY')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.posX()
history.append('posX');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.negX()
history.append('negX')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.posY()
history.append('posY')
elif(key1 == 'headRight'):
progress('Dict = %s ' % (key1))
yield self.posX()
history.append('posX')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.negY()
history.append('negY');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.posY()
history.append('posY')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.posX()
history.append('posX')
ts, f, b = self.sensor.lastSensor
if (f < old_f) or (b < old_b):
key1 = 'headRightAlt'
elif(key1 == 'headRightAlt'):
progress('Dict = %s ' % (key1))
yield self.posX()
history.append('posX')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.posY()
history.append('posY');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.negY()
history.append('negY')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.posX()
history.append('posX')
ts, f, b = self.sensor.lastSensor
if (f < old_f) or (b < old_b):
key1 = 'headRight'
elif(key1 == 'headLeft'):
progress('Dict = %s ' % (key1))
yield self.negX()
history.append('negX')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.negY()
history.append('negY');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.posY()
history.append('posY')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.negX()
history.append('negX')
ts, f, b = self.sensor.lastSensor
if (f < old_f) or (b < old_b):
key1 = 'headLeftAlt'
elif(key1 == 'headLeftAlt'):
progress('Dict = %s ' % (key1))
yield self.negX()
history.append('negX')
if f > b and ((f < 150) or (b < 150)):
progress("F = %d > B = %d" % (f,b))
yield self.posY()
history.append('posY');
elif b > f and ((f < 150) or (b < 150)):
progress("F = %d < B = %d" % (f,b))
yield self.negY()
history.append('negY')
elif b < 10 or f < 10:
progress("BACKTRACK")
yield dictBacktrack[history.pop()]()
else:
progress("F = %d, B = %d" % (f,b))
yield self.negX()
history.append('negX')
ts, f, b = self.sensor.lastSensor
if (f < old_f) or (b < old_b):
key1 = 'headLeft'
else:
progress('Dict = %s ' % (key1))
continue
class RobotSimulatorApp( JoyApp ):
"""Concrete class RobotSimulatorApp <<singleton>>
A JoyApp which runs the DummyRobotSim robot model in simulation, and
emits regular simulated tagStreamer message to the desired waypoint host.
Used in conjection with waypointServer.py to provide a complete simulation
environment for Project 1
"""
def __init__(self,wphAddr=WAYPOINT_HOST,*arg,**kw):
"""
Initialize the simulator
"""
JoyApp.__init__( self,
confPath="$/cfg/JoyApp.yml", *arg, **kw
)
self.srvAddr = (wphAddr, APRIL_DATA_PORT)
# ADD pre-startup initialization here, if you need it
def onStart( self ):
"""
Sets up the JoyApp and configures the simulation
"""
### DO NOT MODIFY ------------------------------------------
# Set up socket for emitting fake tag messages
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
s.bind(("",0))
self.sock = s
# Set up the sensor receiver plan
self.sensor = SensorPlanTCP(self,server=self.srvAddr[0])
self.sensor.start()
self.timeForStatus = self.onceEvery(1)
self.timeForLaser = self.onceEvery(1/15.0)
self.timeForFrame = self.onceEvery(1/20.0)
progress("Using %s:%d as the waypoint host" % self.srvAddr)
self.T0 = self.now
### MODIFY FROM HERE ------------------------------------------
self.robSim = SimpleRobotSim(fn=None)
self.yMove = yMovePlan(self,self.robSim)
self.xMove = xMovePlan(self,self.robSim)
self.auto = Auto(self, self. robSim, self.yMove, self.xMove)
def showSensors( self ):
"""
Display sensor readings
"""
# This code should help you understand how you access sensor information
ts,f,b = self.sensor.lastSensor
if ts:
progress( "Sensor: %4d f %d b %d" % (ts-self.T0,f,b) )
else:
progress( "Sensor: << no reading >>" )
ts,w = self.sensor.lastWaypoints
if ts:
progress( "Waypoints: %4d " % (ts-self.T0) + str(w))
else:
progress( "Waypoints: << no reading >>" )
def emitTagMessage( self ):
"""Generate and emit and update simulated tagStreamer message"""
#### DO NOT MODIFY --- it WILL break the simulator
self.robSim.refreshState()
# Get the simulated tag message
msg = self.robSim.getTagMsg()
# Send message to waypointServer "as if" we were tagStreamer
self.sock.sendto(msg.encode("ascii"), self.srvAddr)
def onEvent( self, evt ):
#### DO NOT MODIFY --------------------------------------------
# periodically, show the sensor reading we got from the waypointServer
if self.timeForStatus():
#self.showSensors()
progress( self.robSim.logLaserValue(self.now) )
# generate simulated laser readings
elif self.timeForLaser():
self.robSim.logLaserValue(self.now)
# update the robot and simulate the tagStreamer
if self.timeForFrame():
self.emitTagMessage()
#### MODIFY FROM HERE ON ----------------------------------------
if evt.type == KEYDOWN:
if evt.key == K_UP and not self.yMove.isRunning():
self.yMove.dist = 100.0
self.yMove.start()
return progress("(say) Move forward")
elif evt.key == K_DOWN and not self.yMove.isRunning():
self.yMove.dist = -100.0
self.yMove.start()
return progress("(say) Move back")
if evt.key == K_LEFT and not self.xMove.isRunning():
self.xMove.dist = 100.0
self.xMove.start()
return progress("(say) Turn left")
if evt.key == K_RIGHT and not self.xMove.isRunning():
self.xMove.dist = -100.0
self.xMove.start()
return progress("(say) Turn right")
if evt.key == K_a and not self.auto.isRunning():
self.auto.start()
if evt.key == K_s:
self.yMove.stop()
self.xMove.stop()
self.auto.stop()
return progress("(say) Stopped")
if evt.key != K_s and evt.key != K_a and evt.type == KEYUP:
self.yMove.stop()
self.xMove.stop()
return progress("(say) Stopped")
### DO NOT MODIFY -----------------------------------------------
else:# Use superclass to show any other events
return JoyApp.onEvent(self,evt)
return # ignoring non-KEYDOWN events
if __name__=="__main__":
print("""
Running the robot simulator
Listens on local port 0xBAA (2986) for incoming waypointServer
information, and also transmits simulated tagStreamer messages to
the waypointServer.
""")
import sys
if len(sys.argv)>1:
app=RobotSimulatorApp(wphAddr=sys.argv[1], cfg={'windowSize' : [160,120]})
else:
app=RobotSimulatorApp(wphAddr=WAYPOINT_HOST, cfg={'windowSize' : [160,120]})
app.run()