-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathMJScene.cpp
More file actions
412 lines (339 loc) · 13.9 KB
/
Copy pathMJScene.cpp
File metadata and controls
412 lines (339 loc) · 13.9 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
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
/*
ISC License
Copyright (c) 2025, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "MJScene.h"
#include "MJFwdKinematics.h"
#include "StatefulSysModel.h"
#include "simulation/dynamics/_GeneralModuleFiles/svIntegratorRK4.h"
#include "architecture/utilities/macroDefinitions.h"
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <cmath>
MJScene::MJScene(std::string xml, const std::vector<std::string>& files)
: spec(*this, xml, files)
{
this->AddFwdKinematicsToDynamicsTask(MJScene::FWD_KINEMATICS_PRIORITY);
this->integrator = new svIntegratorRK4(this);
}
MJScene MJScene::fromFile(const std::string& fileName)
{
std::stringstream os(std::stringstream::out);
os << std::ifstream(fileName).rdbuf();
return MJScene(os.str());
}
void MJScene::AddModelToDynamicsTask(SysModel* model, int32_t priority)
{
this->dynamicsTask.AddNewObject(model, priority);
}
void MJScene::AddFwdKinematicsToDynamicsTask(int32_t priority)
{
this->ownedSysModel.emplace_back(std::make_unique<MJFwdKinematics>(*this));
this->ownedSysModel.back()->ModelTag = "FwdKinematics" + std::to_string(this->ownedSysModel.size()-1);
this->AddModelToDynamicsTask(this->ownedSysModel.back().get(), priority);
}
void MJScene::SelfInit() { this->dynamicsTask.SelfInitTaskList(); }
void MJScene::Reset(uint64_t CurrentSimNanos)
{
this->timeBefore = CurrentSimNanos * NANO2SEC;
this->dynamicsTask.TaskName = "Dynamics:" + this->ModelTag;
this->dynamicsTask.ResetTaskList(CurrentSimNanos);
this->initializeDynamics();
this->writeOutputStateMessages(CurrentSimNanos);
}
void MJScene::initializeDynamics()
{
// We need to use a special StateData type for qpos
// since it is integrated using a mujoco function
this->qposState = this->dynManager.registerState<MJQPosStateData>(1, 1, "mujocoQpos");
this->qvelState = this->dynManager.registerState(1, 1, "mujocoQvel");
this->actState = this->dynManager.registerState(1, 1, "mujocoAct");
for (auto&& body : this->spec.getBodies()) {
body.registerStates(DynParamRegisterer(
this->dynManager,
"body_" + body.getName() + "_"
));
}
// Make sure the spec is compiled
bool recompiled = this->spec.recompileIfNeeded();
// Always call `configure`, which will reshape the states size
if (!recompiled) {
this->spec.configure();
}
// Register the states of the models in the dynamics task
for (auto[_, sysModelPtr] : this->dynamicsTask.TaskModels)
{
if (auto statefulSysModelPtr = dynamic_cast<StatefulSysModel*>(sysModelPtr))
{
statefulSysModelPtr->registerStates(DynParamRegisterer(
this->dynManager,
sysModelPtr->ModelTag.empty() ? std::string("model") : sysModelPtr->ModelTag
+ "_" + std::to_string(sysModelPtr->moduleID) + "_"
));
}
}
}
void MJScene::UpdateState(uint64_t CurrentSimNanos)
{
double t = CurrentSimNanos * NANO2SEC;
this->integrateState(t);
this->writeOutputStateMessages(CurrentSimNanos);
for (auto&& body : this->spec.getBodies()) {
body.writeStateDependentOutputMessages(CurrentSimNanos);
}
}
void MJScene::equationsOfMotion(double t, double timeStep)
{
auto nanos = static_cast<uint64_t>(t * SEC2NANO);
// Make sure the model is compiled
this->spec.recompileIfNeeded();
// Copy data from Basilisk state objects to MuJoCo structs
updateMujocoArraysFromStates();
for (auto&& body : this->spec.getBodies()) {
// The mass of bodies is stored as a state, which may evolve in time.
// Mujoco expects mass properties to be stored in mjModel, so we need
// to update the mjModel with the mass properties of the bodies.
body.updateMujocoModelFromMassProps();
body.writeStateDependentOutputMessages(nanos);
}
// Mujoco models cache certain computations that depend on values that are
// supposed to be constant during mujoco simulations (like body mass). However,
// we need to alter some of them, in which case we need to update the 'constants'.
if (areMujocoModelConstStale()) {
mj_setConst(this->spec.getMujocoModel(), this->spec.getMujocoData());
}
// Execute the dynamics task!
// (One of) The first module in this task should be a MJFwdKinematics
// which will take the recently updated qpos and qvel data,
// do the fwd kinematics, and update the state messages.
// These messages can then be read by the rest of modules.
this->dynamicsTask.ExecuteTaskList(nanos);
// TODO: When allowing to prescribe the mass, remember to mj_setConst.
// This is similar to how prescribing joint states should be followed
// by running forward kinematics.
// If the kinematics have become stale while running the dynamics
// modules, we need to update them so that the accelerations
// calculations are correct. We do not need to write the messages
// though, since nothing needs them (only running mujoco now)
if (areKinematicsStale()) {
mj_fwdPosition(this->spec.getMujocoModel(), this->spec.getMujocoData());
mj_fwdVelocity(this->spec.getMujocoModel(), this->spec.getMujocoData());
}
// Update the ctrl array in mjData from the inputs in the actuators
for (auto&& actuator : this->spec.getActuators()) {
actuator->updateCtrl(this->spec.getMujocoData());
}
// Update the prescribed joints equalities
for (auto&& body : this->spec.getBodies()) {
body.updateConstrainedEqualityJoints();
}
// These methods will compute the accelerations
mj_fwdActuation(this->spec.getMujocoModel(), this->spec.getMujocoData());
mj_fwdAcceleration(this->spec.getMujocoModel(), this->spec.getMujocoData());
mj_fwdConstraint(this->spec.getMujocoModel(), this->spec.getMujocoData());
// Sanity check the produced accelerations
auto qacc = this->spec.getMujocoData()->qacc;
if (std::any_of(qacc, qacc + this->spec.getMujocoModel()->nv, [](mjtNum v){return std::isnan(v);})) {
logAndThrow<std::runtime_error>("Encountered NaN acceleration at time " +
std::to_string(t) +
"s in MJScene with ID: " + std::to_string(moduleID));
}
// The derivative of the position is simply the state of the velocity
this->qposState->setDerivative(this->qvelState->getState());
// Copy the computed accelerations into the derivative of the velocity
auto qvelDeriv = this->qvelState->stateDeriv.data();
std::copy_n(qacc, this->spec.getMujocoModel()->nv, qvelDeriv);
// Also copy the derivative of the actuator states, if we have them
if (this->spec.getMujocoModel()->na > 0) {
auto actDeriv = this->actState->stateDeriv.data();
std::copy_n(this->spec.getMujocoData()->act_dot, this->spec.getMujocoModel()->na, actDeriv);
}
// Update the derivative of the body mass property states
for (auto&& body : this->spec.getBodies()) {
body.updateMassPropsDerivative();
}
}
void MJScene::preIntegration(double callTime) { this->timeStep = callTime - this->timeBefore; }
void MJScene::postIntegration(double callTime)
{
this->timeBefore = callTime;
// Copy data from Basilisk state objects to MuJoCo structs
updateMujocoArraysFromStates();
if (extraEoMCall)
{
// If asked, this will call the equations of motion one last
// time with the final/integrated state. This also calls
// MJFwdKinematics::fwdKinematics
equationsOfMotion(callTime, 0);
}
else
{
// Always forward the kinematics with the final state
MJFwdKinematics::fwdKinematics(*this, static_cast<uint64_t>(callTime * SEC2NANO));
}
}
void MJScene::writeFwdKinematicsMessages(uint64_t CurrentSimNanos)
{
for (auto&& body : this->spec.getBodies()) {
body.writeFwdKinematicsMessages(this->spec.getMujocoModel(), this->spec.getMujocoData(), CurrentSimNanos);
}
this->forwardKinematicsStale = false;
}
void MJScene::saveToFile(std::string filename)
{
std::string suffix = ".mjb";
bool binary_file = filename.size() >= suffix.size() &&
filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0;
if (binary_file)
{
mj_saveModel(this->getMujocoModel(), filename.c_str(), NULL, 0);
}
else
{
char error[1024];
mj_saveXML(this->spec.getMujocoSpec(), filename.c_str(), error, sizeof(error));
}
}
MJQPosStateData* MJScene::getQposState()
{
if (!this->qposState) {
logAndThrow<std::runtime_error>("Tried to get qpos state before initialization.");
}
return this->qposState;
}
StateData* MJScene::getQvelState()
{
if (!this->qvelState) {
logAndThrow<std::runtime_error>("Tried to get qvel state before initialization.");
}
return this->qvelState;
}
StateData* MJScene::getActState()
{
if (!this->actState) {
logAndThrow<std::runtime_error>("Tried to get qpos state before initialization.");
}
return this->actState;
}
void MJScene::printMujocoModelDebugInfo(const std::string& path)
{
mj_printModel(this->getMujocoModel(), path.c_str());
}
MJBody& MJScene::getBody(const std::string& name)
{
auto& bodies = this->spec.getBodies();
auto bodyPtr = std::find_if(std::begin(bodies),
std::end(bodies),
[&](auto&& obj) { return obj.getName() == name; });
if (bodyPtr == std::end(bodies)) {
logAndThrow("Unknown body '" + name + "' in MJScene");
}
return *bodyPtr;
}
MJSite& MJScene::getSite(const std::string& name)
{
for (auto&& body : this->spec.getBodies()) {
if (body.hasSite(name)) return body.getSite(name);
}
logAndThrow("Unknown site '" + name + "' in MJScene");
}
MJEquality&
MJScene::getEquality(const std::string& name)
{
auto& equalities = this->spec.getEqualities();
auto equalityPtr = std::find_if(std::begin(equalities),
std::end(equalities),
[&](auto&& obj) { return obj.getName() == name; });
if (equalityPtr == std::end(equalities)) {
logAndThrow("Unknown equality '" + name + "' in MJScene");
}
return *equalityPtr;
}
MJSingleActuator& MJScene::getSingleActuator(const std::string& name)
{
return this->spec.getActuator<MJSingleActuator>(name);
}
MJForceActuator& MJScene::getForceActuator(const std::string& name)
{
return this->spec.getActuator<MJForceActuator>(name);
}
MJTorqueActuator& MJScene::getTorqueActuator(const std::string& name)
{
return this->spec.getActuator<MJTorqueActuator>(name);
}
MJForceTorqueActuator& MJScene::getForceTorqueActuator(const std::string& name)
{
return this->spec.getActuator<MJForceTorqueActuator>(name);
}
MJSingleActuator& MJScene::addSingleActuator(const std::string& name,
const std::string& site,
const Eigen::Vector6d& gear)
{
return this->spec.addSingleActuator(name, site, gear);
}
MJSingleActuator&
MJScene::addSingleActuator(const std::string& name, const MJSite& site, const Eigen::Vector6d& gear)
{
return this->addSingleActuator(name, site.getName(), gear);
}
MJForceActuator& MJScene::addForceActuator(const std::string& name, const std::string& site)
{
return this->spec.addCompositeActuator<MJForceActuator>(name, site);
}
MJForceActuator & MJScene::addForceActuator(const std::string & name, const MJSite & site)
{
return this->addForceActuator(name, site.getName());
}
MJTorqueActuator& MJScene::addTorqueActuator(const std::string& name, const std::string& site)
{
return this->spec.addCompositeActuator<MJTorqueActuator>(name, site);
}
MJTorqueActuator&
MJScene::addTorqueActuator(const std::string& name, const MJSite& site)
{
return this->addTorqueActuator(name, site.getName());
}
MJForceTorqueActuator& MJScene::addForceTorqueActuator(const std::string& name,
const std::string& site)
{
return this->spec.addCompositeActuator<MJForceTorqueActuator>(name, site);
}
MJForceTorqueActuator&
MJScene::addForceTorqueActuator(const std::string& name, const MJSite& site)
{
return this->addForceTorqueActuator(name, site.getName());
}
void MJScene::updateMujocoArraysFromStates()
{
auto mujocoModel = this->getMujocoModel();
auto mujocoData = this->getMujocoData();
// Copy the joint positions and velocity stored in the states
// into the mujoco arrays
std::copy_n(this->qposState->state.data(), mujocoModel->nq, mujocoData->qpos);
std::copy_n(this->qvelState->state.data(), mujocoModel->nv, mujocoData->qvel);
if (mujocoModel->na > 0) {
std::copy_n(this->actState->state.data(), mujocoModel->na, mujocoData->act);
}
markKinematicsAsStale();
}
void MJScene::writeOutputStateMessages(uint64_t CurrentSimNanos)
{
MJSceneStateMsgPayload stateOutMsgPayload{this->qposState->getState(),
this->qvelState->getState(),
this->actState->getState()};
stateOutMsg.write(&stateOutMsgPayload, this->moduleID, CurrentSimNanos);
}