-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathsimulation.py
363 lines (330 loc) · 14.4 KB
/
simulation.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
from ..robotsim import *
import simlog
import weakref
class SensorEmulator:
"""A generic sensor emulator. Translates from the physics simulation -> inputs to a Python controller.
The Python controller is assumed to have the structure of BaseController, where it is given as input
a dictionary of named items reflecting the most up-to-date readings on each control time-step.
"""
def __init__(self):
pass
def update(self):
"""Returns a dictionary mapping named sensors to their outputs."""
return {}
def drawGL(self):
"""Optional: for debugging"""
return
class DefaultSensorEmulator(SensorEmulator):
"""A sensor emulator that by default provides the robot's commanded position, velocity,
and other sensors defined in the robot or world XML file.
"""
def __init__(self,sim,controller):
self.sim = sim
self.controller = controller
def update(self):
measurements = {}
mode = self.controller.getControlType()
if mode == "PID":
measurements['qcmd'] = self.controller.getCommandedConfig()
measurements['dqcmd'] = self.controller.getCommandedVelocity()
k = 0
while True:
s = self.controller.sensor(k)
if s.type()=='':
break;
measurements[s.name()] = s.getMeasurements()
k+=1
return measurements
def drawGL(self):
if self.controller.getControlType() == "PID":
q = self.controller.getCommandedConfig()
r = self.controller.model()
r.setConfig(q)
colors = []
for j in range(r.numLinks()):
colors.append(r.link(j).appearance().getColor())
r.link(j).appearance().setColor(0,1,0,0.5)
r.drawGL()
for j in range(r.numLinks()):
r.link(j).appearance().setColor(*colors[j])
class ActuatorEmulator:
"""A generic actuator emulator. Translates outputs from the Python controller -> the physics simulation.
A variety of non-traditional actuators can be simulated here.
The Python controller is assumed to have the structure of BaseController, where outputs a dictionary
of commands every control time step. The emulator will read these with the process() methods
"""
def __init__(self):
pass
def process(self,commands,dt):
"""Processes the dictionary of commands, which are outputted by the controller.
This may involve applying commands to the low-level motor emulator,
or applying forces to the simulator.
Arguments:
- commands: a dictionary of commands produced by the controller
- dt: the control time step (not the underlying simulation time step)
To play nicely with other actuators in a nested steup, once a command is processed, the class should
remove it from the commands dictionary.
"""
pass
def substep(self,dt):
"""This is called every simulation substep, which occurs at a higher rate than
process() is called. dt is the simulation substep.
"""
pass
def drawGL(self):
"""Optional: for debugging"""
return
class DefaultActuatorEmulator(ActuatorEmulator):
"""This default emulator can take the commands
- torquecmd: torque comand
- qcmd: position command
- dqcmd: velocity command
- tcmd: time for a dqcmd
And will also pass any remaining commands to the low-level C controller.
It can also simulate forces, etc. at a higher rate than the control loop rate.
"""
def __init__(self,sim,controller):
self.sim = sim
self.controller = controller
def process(self,commands,dt):
"""Commands: a dictionary of values outputted from the controller module,
or None if no command was issued. """
if commands == None: return
c = self.controller
defaultVals = set(['torquecmd','qcmd','dqcmd','tcmd'])
if 'qcmd' in commands:
dqcmd = commands['dqcmd'] if 'dqcmd' in commands else [0.0]*len(commands['qcmd'])
if 'torquecmd' in commands:
c.setPIDCommand(commands['qcmd'],dqcmd,commands['torquecmd'])
else:
c.setPIDCommand(commands['qcmd'],dqcmd)
elif 'dqcmd' in commands:
assert 'tcmd' in commands
c.setVelocityCommand(commands['dqcmd'],commands['tcmd'])
elif 'torquecmd' in commands:
c.setTorque(commands['torquecmd'])
for (k,v) in commands.iteritems():
if k not in defaultVals:
print "Sending command",k,v,"to low level controller"
c.sendCommand(k,v)
return
class SimpleSimulator (Simulator):
"""A convenience class that enables easy logging, definition of simulation hooks, emulators
of sensors / actuators, and definition of robot controllers.
Note that for greatest compatibility you should NOT manually apply forces to the simulation
except for inside of hooks and emulators. This is because several simulation sub-steps will be
taken, and you will have no control over the forces applied except for the first time step.
"""
def __init__(self,world):
"""Arguments:
- world: a RobotWorld instance.
"""
Simulator.__init__(self,world)
#these are functions automatically called at each time step
self.robotControllers = [None]*world.numRobots()
self.sensorEmulators = [[DefaultSensorEmulator(weakref.proxy(self),self.controller(i))] for i in range(world.numRobots())]
self.actuatorEmulators = [[DefaultActuatorEmulator(weakref.proxy(self),self.controller(i))] for i in range(world.numRobots())]
self.hooks = []
self.hook_args = []
#the rate of applying simulation substeps. Hooks and actuator emulators are
#called at this rate. Note: this should be set at least as large as the simulation time step
self.substep_dt = 0.001
self.worst_status = Simulator.STATUS_NORMAL
#turn this on to save log to disk
self.logging = False
self.logger = None
self.log_state_fn="simulation_state.csv"
self.log_contact_fn="simulation_contact.csv"
#save state so controllers don't rely on world state
self.robotStates = []
self.objectStates = []
def getStatus(self):
return self.worst_status
def getStatusString(self, status = -1):
if status > 0:
return Simulator.getStatusString(self, status)
return Simulator.getStatusString(self, self.getStatus())
def beginLogging(self):
self.logging = True
self.logger = simlog.SimLogger(weakref.proxy(self),self.log_state_fn,self.log_contact_fn)
def endLogging(self):
self.logging = False
self.logger = None
def pauseLogging(self,paused=True):
self.logging=not paused
def toggleLogging(self):
if self.logging:
self.pauseLogging()
else:
if self.logger==None:
self.beginLogging()
else:
self.pauseLogging(False)
def setController(self,robot,function):
"""Sets a robot's controller function.
Arguments:
- robot: either an index, string, or RobotModel.
- function: either be 1) a one-argument function that takes the
robot's SimRobotController instance, or 2) an instance of a
BaseController class (see Python/control/controller.py)
"""
if isinstance(robot,int):
index = robot
elif isinstance(robot,str):
index = self.world.robot(robot).index
elif isinstance(robot,RobotModel):
index = robot.index
else:
raise ValueError("Invalid robot specified")
if not callable(function):
assert hasattr(function,'output_and_advance'),"setController takes either a 1-argument function or a BaseController instance"
self.robotControllers += [None]*(self.world.numRobots()-len(self.robotControllers))
self.robotControllers[index] = function
def addEmulator(self,robot,e):
"""Adds an emulator to the given robot. e must be of SensorEmulator or ActuatorEmulator type.
"""
if isinstance(e,SensorEmulator):
self.sensorEmulators[robot].append(e)
elif isinstance(e,ActuatorEmulator):
self.actuatorEmulators[robot] = [e] + self.actuatorEmulators[robot]
else:
raise ValueError("Invalid emulator type")
def addHook(self,objects,function):
"""For the world object or objects 'objects', applies a hook that gets called every
simulation loop. The objects may be certain identifiers, WorldModel items or SimBodies.
- Accepted names are: 'time', or any items in the world
- If they are individual bodies, the corresponding SimBody objects are passed to function.
- If they are RobotModel's, the corresponding SimRobotController objects are passed to function.
- Otherwise they are passed directly to function.
"""
if not hasattr(objects,'__iter__'):
objects = [objects]
args = []
for o in objects:
if isinstance(o,(RobotModelLink,RigidObjectModel,TerrainModel)):
args.append(self.body(o))
elif isinstance(o,RobotModel):
args.append(self.controller(o))
elif isinstance(o,str):
if o == 'time':
args.append(o)
elif self.world.robot(o).world >= 0:
args.append(self.world.robot(o))
elif self.world.terrain(o).world >= 0:
args.append(self.world.terrain(o))
elif self.world.rigidObject(o).world >= 0:
args.append(self.world.rigidObject(o))
else:
raise ValueError("String value "+o+" is unknown")
else:
args.append(o)
self.hooks.append(function)
self.hook_args.append(args)
def drawGL(self):
self.updateWorld()
self.world.drawGL()
self.drawEmulatorsGL()
self.drawControllersGL()
def drawEmulatorsGL(self):
#draw emulators
for elist in self.sensorEmulators:
for e in elist:
e.drawGL()
for elist in self.actuatorEmulators:
for e in elist:
e.drawGL()
def drawControllersGL(self):
#draw controllers
for i in xrange(self.world.numRobots()):
if self.robotControllers[i] == None:
continue
if not hasattr(self.robotControllers[i],'drawGL'):
continue
self.robotControllers[i].drawGL()
def simulate(self,dt):
"""Runs the simulation. Note that this should be called at the
rate of the controller. Simulation hooks and emulator substeps
will be called at the rate of substep_dt.
Arguments:
- dt: control timestep
"""
#Handle logging
if self.logger: self.logger.saveStep()
self.worst_status = Simulator.STATUS_NORMAL
#Advance controller, emulators
#restore state from previous call -- this is done so that simulation data doesn't leak into controllers
for i,(q,dq) in enumerate(self.robotStates):
self.world.robot(i).setConfig(q)
self.world.robot(i).setVelocity(dq)
for i,T in enumerate(self.objectStates):
self.world.rigidObject(i).setTransform(*T)
#advance controller
self.control_loop(dt)
#save post-controller state
self.robotStates = [(self.world.robot(i).getConfig(),self.world.robot(i).getVelocity()) for i in range(self.world.numRobots())]
self.objectStates = [self.world.rigidObject(i).getTransform() for i in range(self.world.numRigidObjects())]
#advance hooks and the physics simulation at the high rate
assert self.substep_dt > 0
t = 0
while True:
substep = min(self.substep_dt,dt-t)
for i in range(self.world.numRobots()):
for e in self.actuatorEmulators[i]:
e.substep(substep)
for (hook,args) in zip(self.hooks,self.hook_args):
resolvedArgs = []
for a in args:
if isinstance(a,str):
if a=='time':
resolvedArgs.append(sim.getTime())
elif a=='dt':
resolvedArgs.append(substep)
else:
raise ValueError("Invalid unresolved argument",a)
else:
resolvedArgs.append(a)
try:
hook(*resolvedArgs)
except Exception, e:
import traceback
print "Hook encountered error with arguments",resolvedArgs
traceback.print_exc()
raise
#Finally advance the physics simulation
Simulator.simulate(self,substep)
s = Simulator.getStatus(self)
if s > self.worst_status:
self.worst_status = s
t += self.substep_dt
if t >= dt:
break
#done
return
def control_loop(self,dt):
for i in range(self.world.numRobots()):
c = self.robotControllers[i]
if callable(c):
c(self.controller(i))
else:
#build measurement dict
measurements = {'t':self.getTime(),'dt':dt}
for e in self.sensorEmulators[i]:
measurements.update(e.update())
"""
#debug: print measurements
for (k,v) in measurements.iteritems():
print k,":",
if hasattr(v,'__iter__'):
print ' '.join("%.2f"%(vi,) for vi in v)
else:
print v
"""
if c:
#assume it's a BaseController instance
#compute controller output, advance controller
output = c.output_and_advance(**measurements)
else:
output = None
#process output => sim using actuator emulators
for e in self.actuatorEmulators[i]:
e.process(output,dt)