46 : spec_(spec), valid_(validate_spec(spec))
50 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
52 spatial_inertias_[joint] = make_spatial_inertia(spec_.bodies[joint]);
57 bool valid()
const {
return valid_; }
59 std::size_t
joint_count()
const {
return valid_ ? spec_.joint_count : 0; }
62 const double gravity[3],
double *tau_out)
const
64 if (q ==
nullptr || dq ==
nullptr || ddq ==
nullptr || gravity ==
nullptr ||
65 tau_out ==
nullptr || !valid_inputs(q, dq, ddq, gravity))
70 std::array<SpatialMatrix, MaxJoints> parent_motion_to_body{};
71 std::array<SpatialVector, MaxJoints> velocities{};
72 std::array<SpatialVector, MaxJoints> accelerations{};
73 std::array<SpatialVector, MaxJoints> forces{};
74 std::array<double, MaxJoints> torques{};
76 SpatialVector parent_velocity{};
77 SpatialVector parent_acceleration{};
78 parent_acceleration[3] = -gravity[0];
79 parent_acceleration[4] = -gravity[1];
80 parent_acceleration[5] = -gravity[2];
82 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
85 parent_motion_to_body[joint] = motion_transform(body, q[joint]);
87 const SpatialVector transformed_velocity =
88 multiply(parent_motion_to_body[joint], parent_velocity);
89 SpatialVector joint_velocity{};
93 velocities[joint] = add(transformed_velocity, joint_velocity);
95 const SpatialVector transformed_acceleration =
96 multiply(parent_motion_to_body[joint], parent_acceleration);
97 SpatialVector joint_acceleration{};
98 joint_acceleration[0] = body.
joint_axis.
x * ddq[joint];
99 joint_acceleration[1] = body.
joint_axis.
y * ddq[joint];
100 joint_acceleration[2] = body.
joint_axis.
z * ddq[joint];
101 accelerations[joint] =
102 add(add(transformed_acceleration, joint_acceleration),
103 motion_cross(velocities[joint], joint_velocity));
105 const SpatialVector momentum =
106 multiply(spatial_inertias_[joint], velocities[joint]);
108 add(multiply(spatial_inertias_[joint], accelerations[joint]),
109 force_cross(velocities[joint], momentum));
111 parent_velocity = velocities[joint];
112 parent_acceleration = accelerations[joint];
115 for (std::size_t joint = spec_.joint_count; joint-- > 0;)
119 axis.x * forces[joint][0] +
axis.y * forces[joint][1] +
120 axis.z * forces[joint][2];
121 if (!std::isfinite(torques[joint]))
128 add(forces[joint - 1],
129 transpose_multiply(parent_motion_to_body[joint], forces[joint]));
133 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
135 tau_out[joint] = torques[joint];
141 using SpatialVector = std::array<double, 6>;
142 using SpatialMatrix = std::array<SpatialVector, 6>;
144 static constexpr double RotationTolerance = 1e-9;
145 static constexpr double AxisTolerance = 1e-9;
149 return std::isfinite(value.
x) && std::isfinite(value.
y) && std::isfinite(value.
z);
152 static double determinant3(
const double matrix[3][3])
154 return matrix[0][0] *
155 (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
157 (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
159 (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
162 static bool valid_rotation(
const double rotation[3][3])
164 for (
int row = 0; row < 3; ++row)
166 for (
int column = 0; column < 3; ++column)
168 if (!std::isfinite(rotation[row][column]))
174 for (
int row = 0; row < 3; ++row)
176 for (
int column = 0; column < 3; ++column)
178 const double dot = rotation[0][row] * rotation[0][column] +
179 rotation[1][row] * rotation[1][column] +
180 rotation[2][row] * rotation[2][column];
181 const double expected = row == column ? 1.0 : 0.0;
182 if (std::fabs(dot - expected) > RotationTolerance)
188 return std::fabs(determinant3(rotation) - 1.0) <= RotationTolerance;
191 static bool valid_inertia(
const double inertia[3][3])
193 for (
int row = 0; row < 3; ++row)
195 for (
int column = 0; column < 3; ++column)
197 if (!std::isfinite(inertia[row][column]))
201 if (inertia[row][column] != inertia[column][row])
208 const double leading2 =
209 inertia[0][0] * inertia[1][1] - inertia[0][1] * inertia[1][0];
210 const double determinant = determinant3(inertia);
211 if (!(inertia[0][0] > 0.0) || !(leading2 > 0.0) ||
212 !(determinant > 0.0))
219 double triangle[3][3] = {};
220 const double trace = inertia[0][0] + inertia[1][1] + inertia[2][2];
222 for (
int row = 0; row < 3; ++row)
224 for (
int column = 0; column < 3; ++column)
226 const double magnitude = std::fabs(inertia[row][column]);
227 scale = magnitude > scale ? magnitude : scale;
228 triangle[row][column] = -2.0 * inertia[row][column];
231 triangle[row][column] += trace;
235 const double tolerance1 = 1e-12 * scale;
236 const double tolerance2 = tolerance1 * scale;
237 const double tolerance3 = tolerance2 * scale;
238 for (
int index = 0; index < 3; ++index)
240 if (triangle[index][index] < -tolerance1)
245 const double minor01 =
246 triangle[0][0] * triangle[1][1] - triangle[0][1] * triangle[1][0];
247 const double minor02 =
248 triangle[0][0] * triangle[2][2] - triangle[0][2] * triangle[2][0];
249 const double minor12 =
250 triangle[1][1] * triangle[2][2] - triangle[1][2] * triangle[2][1];
251 return minor01 >= -tolerance2 && minor02 >= -tolerance2 &&
252 minor12 >= -tolerance2 && determinant3(triangle) >= -tolerance3;
255 static bool validate_spec(
const FixedBaseChainSpec &spec)
257 if (spec.joint_count == 0 || spec.joint_count >
MaxJoints)
261 for (std::size_t joint = 0;
joint < spec.joint_count; ++
joint)
263 const RevoluteBody &body = spec.bodies[
joint];
264 if (!valid_rotation(body.parent_from_joint_zero.rotation) ||
265 !finite(body.parent_from_joint_zero.translation) || !finite(body.joint_axis) ||
266 !std::isfinite(body.mass) || body.mass <= 0.0 ||
267 !finite(body.center_of_mass) || !valid_inertia(body.inertia_com))
271 const double axis_squared =
geom::dot(body.joint_axis, body.joint_axis);
272 if (std::fabs(axis_squared - 1.0) > AxisTolerance)
280 bool valid_inputs(
const double *q,
const double *dq,
const double *ddq,
281 const double gravity[3])
const
287 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
289 if (!std::isfinite(q[joint]) || !std::isfinite(dq[joint]) ||
290 !std::isfinite(ddq[joint]))
295 return std::isfinite(gravity[0]) && std::isfinite(gravity[1]) &&
296 std::isfinite(gravity[2]);
299 static SpatialMatrix make_spatial_inertia(
const RevoluteBody &body)
301 SpatialMatrix inertia{};
302 const double c[3] = {body.center_of_mass.x, body.center_of_mass.y,
303 body.center_of_mass.z};
304 const double squared = c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
305 for (
int row = 0; row < 3; ++row)
307 for (
int column = 0; column < 3; ++column)
309 inertia[row][column] =
310 body.inertia_com[row][column] +
311 body.mass * ((row == column ? squared : 0.0) - c[row] * c[column]);
314 const double cross[3][3] = {
315 {0.0, -c[2], c[1]}, {c[2], 0.0, -c[0]}, {-c[1], c[0], 0.0}};
316 for (
int row = 0; row < 3; ++row)
318 for (
int column = 0; column < 3; ++column)
320 inertia[row][column + 3] = body.mass *
cross[row][column];
321 inertia[row + 3][column] = -body.mass *
cross[row][column];
322 inertia[row + 3][column + 3] =
323 row == column ? body.mass : 0.0;
329 static SpatialMatrix motion_transform(
const RevoluteBody &body,
double q)
331 const double axis[3] = {body.joint_axis.x, body.joint_axis.y,
333 double joint_rotation[3][3];
335 double child_to_parent[3][3];
339 const geom::Vec3
p = body.parent_from_joint_zero.translation;
340 const double p_cross[3][3] = {
341 {0.0, -
p.z,
p.y}, {
p.z, 0.0, -
p.x}, {-
p.y,
p.x, 0.0}};
342 SpatialMatrix transform{};
343 for (
int row = 0; row < 3; ++row)
345 for (
int column = 0; column < 3; ++column)
347 const double transpose_rotation = child_to_parent[column][row];
348 transform[row][column] = transpose_rotation;
349 transform[row + 3][column + 3] = transpose_rotation;
350 transform[row + 3][column] =
351 -(child_to_parent[0][row] * p_cross[0][column] +
352 child_to_parent[1][row] * p_cross[1][column] +
353 child_to_parent[2][row] * p_cross[2][column]);
359 static SpatialVector add(
const SpatialVector &lhs,
const SpatialVector &rhs)
361 SpatialVector result{};
362 for (std::size_t index = 0; index < result.size(); ++index)
364 result[index] = lhs[index] + rhs[index];
369 static SpatialVector multiply(
const SpatialMatrix &matrix,
const SpatialVector &vector)
371 SpatialVector result{};
372 for (std::size_t row = 0; row < result.size(); ++row)
374 for (std::size_t column = 0; column < result.size(); ++column)
376 result[row] += matrix[row][column] * vector[column];
382 static SpatialVector transpose_multiply(
const SpatialMatrix &matrix,
383 const SpatialVector &vector)
385 SpatialVector result{};
386 for (std::size_t row = 0; row < result.size(); ++row)
388 for (std::size_t column = 0; column < result.size(); ++column)
390 result[row] += matrix[column][row] * vector[column];
396 static void cross3(
const double lhs[3],
const double rhs[3],
double result[3])
398 result[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
399 result[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
400 result[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
403 static SpatialVector motion_cross(
const SpatialVector &lhs,
const SpatialVector &rhs)
405 SpatialVector result{};
406 cross3(lhs.data(), rhs.data(), result.data());
409 cross3(lhs.data() + 3, rhs.data(), first);
410 cross3(lhs.data(), rhs.data() + 3, second);
411 for (
int index = 0; index < 3; ++index)
413 result[index + 3] = first[index] + second[index];
418 static SpatialVector force_cross(
const SpatialVector &motion,
419 const SpatialVector &force)
421 SpatialVector result{};
424 cross3(
motion.data(), force.data(), angular);
425 cross3(
motion.data() + 3, force.data() + 3, offset);
426 cross3(
motion.data(), force.data() + 3, result.data() + 3);
427 for (
int index = 0; index < 3; ++index)
429 result[index] = angular[index] + offset[index];
434 FixedBaseChainSpec spec_{};
435 std::array<SpatialMatrix, MaxJoints> spatial_inertias_{};