[794894]: / arm_model / simulation.py

Download this file

311 lines (248 with data), 9.7 kB

  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
import sympy as sp
import numpy as np
import pylab as plt
from scipy.integrate import odeint
from logger import Logger
# ------------------------------------------------------------------------
# Simulation
# ------------------------------------------------------------------------
class Simulation:
"""This class accepts a ArmModel and a Controller class that must define a
function control(x, t, x0) and performs a numerical integration.
"""
def __init__(self, model, controller):
""" Constructor.
Parameters
----------
model: a reference to ToyModel
controller: a reference to a tracking controller (forcing)
"""
self.logger = Logger('Simulation')
self.model = model
# initial state
self.x0 = model.state0
# check if simulation has finished
self.finished = False
self.controller = controller
def integrate(self, tf):
"""Numerical integration of the model for the given initial state the provided
controller and target.
Parameters
----------
tf: simulation end time
"""
# simulation parameters
self.fps = 30
self.tf = tf
self.t = np.linspace(0.0, self.tf, self.tf * self.fps)
def dxdt(x, t):
return self.model.rhs(x, t,
self.controller.controller(x, t, self.x0),
self.model.constants)
# integrate the system
self.logger.debug('Integrating ...')
self.finished = False
self.x = odeint(dxdt, self.x0, self.t)
self.finished = True
self.logger.debug('Integration finished: ' + str(self.x.shape))
def plot_simulation(self, ax=None):
"""Visualize initial and final pose along with the state variables.
Parameters
----------
ax: 1 x 3 axis
"""
if not self.finished:
self.logger('Simulation has not been performed')
return
if ax is None or ax.shape[0] < 3:
fig, ax = plt.subplots(1, 3, figsize=(15, 5))
# state
x = self.x
t = self.t
n = self.model.nd
ax[0].plot(t, np.rad2deg(x[:, :n]))
ax[0].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.coordinates])
ax[0].set_xlabel('$t \; (s)$')
ax[0].set_ylabel('$q \; (deg)$')
ax[0].set_title('Generalized Coordinates')
# generalized speeds u's
ax[1].plot(t, np.rad2deg(x[:, n:]))
ax[1].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.speeds])
ax[1].set_xlabel('$t \; (s)$')
ax[1].set_ylabel('$u \; (deg/s)$')
ax[1].set_title('Generalized Speeds')
# initial and final pose
poseA = x[0, :n].tolist()
poseB = x[-1, :n].tolist()
self.model.draw_model(poseA, False, ax=ax[2], scale=0.7,
use_axis_limits=True, alpha=0.3, text=False)
self.model.draw_model(poseB, False, ax=ax[2], scale=0.7,
use_axis_limits=True, alpha=1.0, text=False)
# ------------------------------------------------------------------------
# SimulationReporter
# ------------------------------------------------------------------------
class SimulationReporter:
"""A generic simulation reporter.
"""
def __init__(self, model):
"""Constructor
Parameters
----------
model: a reference to ToyModel
"""
self.logger = Logger('SimulationReporter')
self.model = model
self.t = []
self.q = []
self.u = []
self.qd = []
self.tau = []
self.x = []
self.xd = []
self.lm = []
self.lm_d = []
self.lm_del = []
self.lmd = []
self.lmd_d = []
self.fm = []
self.ft = []
def plot_joint_space_data(self, ax=None):
"""Plots joint space data. [Torques, Coordinates]
Parameters
----------
ax: axis must be of dim >= 2
"""
n = self.model.nd
t = np.asarray(self.t)
q = np.asarray(self.q)
qd = np.asarray(self.qd)
tau = np.asarray(self.tau)
if ax is None or ax.shape[0] < 2:
fig, ax = plt.subplots(nrows=1, ncols=2, sharex=True)
# torques
ax[0].plot(t, tau[:, :])
ax[0].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.specifieds])
ax[0].set_xlabel('$t \; (s)$')
ax[0].set_ylabel('$\\tau \; (Nm)$')
ax[0].set_title('Generalized Forces')
# coordinates
ax[1].plot(t, np.rad2deg(q[:, :]))
ax[1].set_title('Joint Space Goals')
ax[1].plot(t, np.rad2deg(qd[:, :]))
ax[1].legend(['$q_' + str(i) + '$' for i in range(1, n + 1)] +
['$q^d_' + str(i) + '$' for i in range(1, n + 1)])
ax[1].set_xlabel('$t \; (s)$')
ax[1].set_ylabel('$q \; (deg)$')
def plot_task_space_data(self, ax=None):
"""Plots task space data. [Torques, Task Positions]
Parameters
----------
ax: axis must be of dim >= 3
"""
t = np.asarray(self.t)
x = np.asarray(self.x)
xd = np.asarray(self.xd)
ft = np.asarray(self.ft)
tau = np.asarray(self.tau)
if ax is None or ax.shape[0] < 3:
fig, ax = plt.subplots(nrows=1, ncols=3, sharex=True)
# torques
ax[0].plot(t, tau[:, :])
ax[0].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.specifieds])
ax[0].set_xlabel('$t \; (s)$')
ax[0].set_ylabel('$\\tau \; (Nm)$')
ax[0].set_title('Generalized Forces')
# task positions
ax[1].plot(t, x[:, :])
ax[1].plot(t, xd[:, :])
ax[1].legend(['$x_s$', '$y_s$', '$x_d$', '$y_d$'])
ax[1].set_xlabel('$t \; (s)$')
ax[1].set_ylabel('$x \; (m)$')
ax[1].set_title('Task Space Goals')
# task space forces
ax[2].plot(t, ft[:, :])
ax[2].legend(['$f_{t' + str(i) + '}$' for i in ['x', 'y']])
ax[2].set_xlabel('$t \; (s)$')
ax[2].set_ylabel('$f_t \; (N)$')
ax[2].set_title('Task Space Forces')
def plot_muscle_space_data_js(self, ax=None):
"""Plots muscle space controller reporter. [Torques]
Parameters
----------
ax: axis must be of dim >= 3
"""
n = self.model.nd
m = self.model.md
t = np.asarray(self.t)
q = np.asarray(self.q)
qd = np.asarray(self.qd)
lmd = np.asarray(self.lmd)
lmd_d = np.asarray(self.lmd_d)
tau = np.asarray(self.tau)
if ax is None or ax.shape[0] < 3:
fig, ax = plt.subplots(nrows=1, ncols=3, sharex=True)
# torques
ax[0].plot(t, tau[:, :])
ax[0].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.specifieds])
ax[0].set_xlabel('$t \; (s)$')
ax[0].set_ylabel('$\\tau \; (Nm)$')
ax[0].set_title('Generalized Forces')
# coordinates
ax[1].plot(t, np.rad2deg(q[:, :]))
ax[1].set_title('Goals')
ax[1].plot(t, np.rad2deg(qd[:, :]))
ax[1].legend(['$q_' + str(i) + '$' for i in range(1, n + 1)] +
['$q^d_' + str(i) + '$' for i in range(1, n + 1)])
ax[1].set_xlabel('$t \; (s)$')
ax[1].set_ylabel('$q \; (deg)$')
# muscle lengths
ax[2].plot(t, lmd[:, :])
ax[2].plot(t, lmd_d[:, :])
ax[2].legend(['$\dot{l}_{m' + str(i) + '}$' for i in range(1, m + 1)] +
['$\dot{l}^d_{m' + str(i) + '}$' for i in range(1, m + 1)])
ax[2].set_xlabel('$t \; (s)$')
ax[2].set_ylabel('$\dot{l}_m \; (m / s)$')
ax[2].set_title('Muscle Lengthening (+) Shortening (-)')
def plot_postural_muscle_space_data(self, ax=None):
"""Plots postural muscle space controller reporter. [Torques, Muslce Lengths]
Parameters
----------
ax: axis must be of dim >= 3
"""
m = self.model.md
t = np.asarray(self.t)
lm = np.asarray(self.lm)
lm_d = np.asarray(self.lm_d)
fm = np.asarray(self.fm)
tau = np.asarray(self.tau)
if ax is None or ax.shape[0] < 3:
fig, ax = plt.subplots(nrows=1, ncols=3, sharex=True)
# torques
ax[0].plot(t, tau[:, :])
ax[0].legend(["${}$".format(sp.physics.vector.vlatex(temp))
for temp in self.model.specifieds])
ax[0].set_xlabel('$t \; (s)$')
ax[0].set_ylabel('$\\tau \; (Nm)$')
ax[0].set_title('Generalized Forces')
# muscle lengths
ax[1].plot(t, lm[:, :], '--')
ax[1].set_prop_cycle(None)
ax[1].plot(t, lm_d[:, :])
# ax[1].legend(['$l_{m' + str(i) + '}$' for i in range(1, m + 1)] +
# ['$l^d_{m' + str(i) + '}$' for i in range(1, m + 1)])
ax[1].set_xlabel('$t \; (s)$')
ax[1].set_ylabel('$l_m \; (m)$')
ax[1].set_title('Muscle Length Goals')
# muscle forces
ax[2].plot(t, fm[:, :])
ax[2].legend(['$f_{m_{' + str(i) + '}}$' for i in range(1, m + 1)]
# + ['$l^d_{m' + str(i) + '}(t-\tau)$' for i in range(1, m + 1)]
)
ax[2].set_xlabel('$t \; (s)$')
ax[2].set_ylabel('$f_m \; (N)$')
ax[2].set_title('Muscle Forces')