-
Notifications
You must be signed in to change notification settings - Fork 0
/
redSimulator.py
279 lines (251 loc) · 9.08 KB
/
redSimulator.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
# file robotSimulator.py simulates a robot in an arena
from sensorPlanTCP import SensorPlanTCP
from robotSim import DummyRobotSim,RobotSimInterface
from joy import JoyApp, progress
from joy.decl import *
from joy.plans import Plan
from waypointShared import WAYPOINT_HOST, APRIL_DATA_PORT
from socket import (
socket, AF_INET,SOCK_DGRAM, IPPROTO_UDP, error as SocketError,
)
from pylab import randn,dot,mean,exp,newaxis
from autoPlan import AutoPlan
class MoveForward(Plan):
def __init__(self,app,simIX):
Plan.__init__(self,app)
self.simIX = simIX
# Distance to travel
self.dist = 10
# Duration of travel [sec]
self.dur = 3
# Number of intermediate steps
self.N = 10
# Noise level for forward motion
self.dNoise = 0.05
def behavior(self):
s = self.simIX
# Compute step along the forward direction
step = (dot([1,-1,-1,1],s.tagPos)*2.0/float(self.N)*self.dist)[newaxis,:]
dt = self.dur / float(self.N)
for k in xrange(self.N):
# Move all tag corners forward by distance, with some noise
s.tagPos = s.tagPos + step * (1+randn()*self.dNoise)
yield self.forDuration(dt)
class Turn(Plan):
def __init__(self,app,simIX):
Plan.__init__(self,app)
self.simIX = simIX
# Angle to turn [rad]
self.ang = 0.1
# Duration of travel [sec]
self.dur = 3.0
# Number of intermediate steps
self.N = 10
# Noise level for turn motion
self.aNoise = 0.005
def behavior(self):
s = self.simIX
# Compute rotation step
dt = self.dur / float(self.N)
rot = exp(1j * self.ang / float(self.N))
for k in xrange(self.N):
# Get current tag location
z = dot(s.tagPos,[1,1j])
c = mean(z)
# Rotate with angle noise
zr = c + (z-c) * rot * exp(1j*randn()*self.aNoise)
# Store as new tag
s.tagPos[:,0] = zr.real
s.tagPos[:,1] = zr.imag
yield self.forDuration(dt)
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):
JoyApp.__init__( self,
confPath="$/cfg/JoyApp.yml", *arg, **kw
)
self.srvAddr = (wphAddr, APRIL_DATA_PORT)
def onStart( self ):
# 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.robSim = DummyRobotSim(fn=None)
self.moveP = MoveForward(self,self.robSim)
self.turnP = Turn(self,self.robSim)
self.autoP = AutoPlan(self, self.sensor, self.moveP, self.turnP)
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
def showSensors( self ):
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"""
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, self.srvAddr)
def onEvent( self, evt ):
# 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()
if evt.type == KEYDOWN:
if evt.key == K_UP and not self.moveP.isRunning():
self.moveP.dist = 0.2
self.moveP.start()
return progress("(say) Move forward")
elif evt.key == K_DOWN and not self.moveP.isRunning():
self.moveP.dist = -0.2
self.moveP.start()
return progress("(say) Move back")
if evt.key == K_LEFT and not self.turnP.isRunning():
self.turnP.ang = 0.3
self.turnP.start()
return progress("(say) Turn left")
if evt.key == K_RIGHT and not self.turnP.isRunning():
self.turnP.ang = -0.3
self.turnP.start()
return progress("(say) Turn right")
elif evt.key == K_a:
self.autoP.start()
return progress("(say) Moving autonomously")
elif evt.key == K_s:
self.autoP.stop()
return progress("(say) Stop autonomous control")
# Use superclass to show any other events
else:
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()
'''
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):
JoyApp.__init__( self,
confPath="$/cfg/JoyApp.yml", *arg, **kw
)
self.srvAddr = (wphAddr, APRIL_DATA_PORT)
def onStart( self ):
# 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.robSim = RedRobotSim(self.sensor, fn=None)
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
def showSensors( self ):
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"""
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, self.srvAddr)
def onEvent( self, evt ):
# 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()
if evt.type == KEYDOWN:
if evt.key == K_UP:
self.robSim.move(0.5)
return progress("(say) Move forward")
elif evt.key == K_DOWN:
self.robSim.move(-0.5)
return progress("(say) Move back")
elif evt.key == K_LEFT:
self.robSim.turn(0.5)
return progress("(say) Turn left")
elif evt.key == K_RIGHT:
self.robSim.turn(-0.5)
return progress("(say) Turn right")
elif evt.key == K_a:
self.robSim.startAuto()
return progress("(say) Moving autonomously")
elif evt.key == K_s:
self.robSim.stopAuto()
return progress("(say) Stop autonomous control")
# Use superclass to show any other events
else:
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()
'''