-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathMJBody.cpp
More file actions
277 lines (223 loc) · 8.76 KB
/
Copy pathMJBody.cpp
File metadata and controls
277 lines (223 loc) · 8.76 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
/*
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 "MJBody.h"
#include "MJScene.h"
#include "MJSpec.h"
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include <iostream>
MJBody::MJBody(mjsBody* body, MJSpec& spec)
: MJObject(body), spec(spec)
{
// SITES
for (auto child = mjs_firstChild(body, mjOBJ_SITE, 0); child; child = mjs_nextChild(body, child, 0))
{
auto mjssite = mjs_asSite(child);
assert(mjssite != NULL);
this->sites.emplace_back(mjssite, *this);
}
if (!this->hasSite(this->name + "_com")) {
this->addSite(this->name + "_com", Eigen::Vector3d::Zero());
}
if (!this->hasSite(this->name + "_origin")) {
this->addSite(this->name + "_origin", Eigen::Vector3d::Zero());
}
// JOINTS
for (auto child = mjs_firstChild(body, mjOBJ_JOINT, 0); child; child = mjs_nextChild(body, child, 0))
{
auto mjsjoint = mjs_asJoint(child);
assert(mjsjoint != NULL);
switch (mjsjoint->type)
{
case mjJNT_HINGE:
case mjJNT_SLIDE:
this->scalarJoints.emplace_back(mjsjoint, *this);
break;
case mjJNT_BALL:
this->ballJoint.emplace(mjsjoint, *this);
break;
case mjJNT_FREE:
this->freeJoint.emplace(mjsjoint, *this);
break;
default:
throw std::runtime_error("Unknown joint type."); // should not happen unless MuJoCo adds new joint
}
}
}
void MJBody::configure(const mjModel* mujocoModel)
{
MJObject::configure(mujocoModel);
for (auto&& joint : this->scalarJoints) {
joint.configure(mujocoModel);
}
if (this->ballJoint.has_value()) {
this->ballJoint->configure(mujocoModel);
}
if (this->freeJoint.has_value()) {
this->freeJoint->configure(mujocoModel);
}
for (auto&& site : this->sites) {
site.configure(mujocoModel);
}
// Update the position of the center of mass
auto& com = this->getCenterOfMass();
auto bodyId = this->getId();
auto siteId = com.getId();
std::copy_n(mujocoModel->body_ipos + 3 * bodyId, 3, mujocoModel->site_pos + 3 * siteId);
// Update the mass property states
if (!this->massState) {
// Should not happen
this->getSpec().getScene().logAndThrow("Tried to configure MJBody before massState was created.");
}
this->massState->setState(Eigen::Matrix<double, 1, 1>{mujocoModel->body_mass[this->getId()]});
}
MJSite& MJBody::getSite(const std::string& name)
{
auto sitePtr = std::find_if(std::begin(sites), std::end(sites), [&](auto&& obj) {
return obj.getName() == name;
});
if (sitePtr == std::end(sites)) {
this->getSpec().getScene().logAndThrow("Unknown site '" + name + "' in body '" + this->name + "'");
}
return *sitePtr;
}
MJScalarJoint& MJBody::getScalarJoint(const std::string& name)
{
auto jointPtr = std::find_if(std::begin(scalarJoints), std::end(scalarJoints), [&](auto&& obj) {
return obj.getName() == name;
});
if (jointPtr != std::end(scalarJoints)) return *jointPtr;
this->getSpec().getScene().logAndThrow("Unknown scalar joint '" + name + "' in body '" + this->getName() + "'");
}
MJBallJoint& MJBody::getBallJoint()
{
if (!this->ballJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to get a ball joint for a body without ball joints: " +
name);
}
return this->ballJoint.value();
}
MJFreeJoint & MJBody::getFreeJoint()
{
if (!this->freeJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to get a free joint for a body without free joints: " +
name);
}
return this->freeJoint.value();
}
void MJBody::setPosition(const Eigen::Vector3d& position)
{
if (!this->freeJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to set position in non-free body " +
name);
}
this->freeJoint.value().setPosition(position);
}
void MJBody::setVelocity(const Eigen::Vector3d& velocity)
{
if (!this->freeJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to set velocity in non-free body " +
name);
}
this->freeJoint.value().setVelocity(velocity);
}
void MJBody::setAttitude(const Eigen::MRPd& attitude)
{
if (!this->freeJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to set attitude in non-free body " +
name);
}
this->freeJoint.value().setAttitude(attitude);
}
void MJBody::setAttitudeRate(const Eigen::Vector3d& attitudeRate)
{
if (!this->freeJoint.has_value()) {
this->getSpec().getScene().logAndThrow<std::runtime_error>("Tried to set attitude rate in non-free body " +
name);
}
this->freeJoint.value().setAttitudeRate(attitudeRate);
}
void MJBody::writeFwdKinematicsMessages(mjModel* m, mjData* d, uint64_t CurrentSimNanos)
{
for (auto&& site : this->sites) {
site.writeFwdKinematicsMessage(m, d, CurrentSimNanos);
}
}
void MJBody::writeStateDependentOutputMessages(uint64_t CurrentSimNanos)
{
SCMassPropsMsgPayload massPropertiesOutMsgPayload;
massPropertiesOutMsgPayload.massSC = this->massState->getState()(0);
this->massPropertiesOutMsg.write(&massPropertiesOutMsgPayload,
this->getSpec().getScene().moduleID,
CurrentSimNanos);
for (auto&& joint : this->scalarJoints) {
joint.writeJointStateMessage(CurrentSimNanos);
}
}
void MJBody::registerStates(DynParamRegisterer paramManager)
{
this->massState = paramManager.registerState(1, 1, "mass");
}
void MJBody::updateMujocoModelFromMassProps()
{
auto m = spec.getMujocoModel();
double newMass = this->massState->getState()(0, 0);
auto diff = abs(m->body_mass[this->getId()] - newMass);
if (diff > 10 * std::numeric_limits<double>::epsilon()) {
// Update the mass in the mjModel AND mjsBody
m->body_mass[this->getId()] = newMass;
this->mjsObject->mass = newMass;
// Update the inertia in the mjModel AND mjsBody
for (size_t i = 0; i < 3; i++) {
m->body_inertia[3 * this->getId() + i] *= newMass / m->body_mass[this->getId()];
this->mjsObject->inertia[i] = m->body_inertia[3 * this->getId() + i];
}
this->getSpec().getScene().markMujocoModelConstAsStale();
this->getSpec().getScene().markKinematicsAsStale();
}
}
void MJBody::updateMassPropsDerivative()
{
if (this->derivativeMassPropertiesInMsg.isLinked()) {
auto deriv = this->derivativeMassPropertiesInMsg();
this->massState->setDerivative(Eigen::Matrix<double, 1, 1>{deriv.massSC});
}
}
void MJBody::updateConstrainedEqualityJoints()
{
for (auto&& joint : this->scalarJoints) {
joint.updateConstrainedEquality();
}
}
void MJBody::addSite(std::string name, const Eigen::Vector3d& position, const Eigen::MRPd& attitude)
{
if (this->hasSite(name)) {
this->getSpec().getScene().logAndThrow("Tried to create site " + name + " twice for body " +
this->name);
}
auto mjssite = mjs_addSite(this->mjsObject, 0);
mjs_setString(mjssite->name, name.c_str());
auto& site = this->sites.emplace_back(mjssite, *this);
spec.markAsNeedingToRecompileModel(); // Any updates to the 'structure' (i.e. new elements), we need to recompile
site.setPositionRelativeToBody(position);
site.setAttitudeRelativeToBody(attitude);
}
bool MJBody::hasSite(const std::string& name) const
{
return std::find_if(std::begin(sites), std::end(sites), [&](auto&& obj) {
return obj.getName() == name;
}) != std::end(sites);
}