plcopen 0.22.0
载入中...
搜索中...
未找到
fixed_base_chain.h
浏览该文件的文档.
1#pragma once
2
3// H3 fixed-base inverse dynamics (approved matrix:
4// doc/compliance/dynamics-feedforward-semantics.md v1).
5//
6// RT-SAFE: fixed storage and bounded O(n) loops; no allocation, locks,
7// exceptions, operating-system calls, or wall-clock access.
8
9#include <array>
10#include <cmath>
11#include <cstddef>
12
13#include "geom/frame.h"
14#include "rt/error.h"
15
17{
18
19inline constexpr std::size_t FixedBaseChainMaxJoints = 8;
20
22{
23 // Maps joint-local coordinates at q=0 into the parent coordinates.
25 // Unit rotation axis expressed in the joint-local coordinates.
27 double mass = 0.0;
28 // Center of mass and inertia at the center of mass, both expressed in
29 // joint-local coordinates.
31 double inertia_com[3][3] = {};
32};
33
35{
36 std::size_t joint_count = 0;
37 std::array<RevoluteBody, FixedBaseChainMaxJoints> bodies{};
38};
39
41{
42 public:
43 static constexpr std::size_t MaxJoints = FixedBaseChainMaxJoints;
44
45 explicit FixedBaseChain(const FixedBaseChainSpec &spec)
46 : spec_(spec), valid_(validate_spec(spec))
47 {
48 if (valid_)
49 {
50 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
51 {
52 spatial_inertias_[joint] = make_spatial_inertia(spec_.bodies[joint]);
53 }
54 }
55 }
56
57 bool valid() const { return valid_; }
58
59 std::size_t joint_count() const { return valid_ ? spec_.joint_count : 0; }
60
61 rt::ErrorCode inverse_dynamics(const double *q, const double *dq, const double *ddq,
62 const double gravity[3], double *tau_out) const
63 {
64 if (q == nullptr || dq == nullptr || ddq == nullptr || gravity == nullptr ||
65 tau_out == nullptr || !valid_inputs(q, dq, ddq, gravity))
66 {
68 }
69
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{};
75
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];
81
82 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
83 {
84 const RevoluteBody &body = spec_.bodies[joint];
85 parent_motion_to_body[joint] = motion_transform(body, q[joint]);
86
87 const SpatialVector transformed_velocity =
88 multiply(parent_motion_to_body[joint], parent_velocity);
89 SpatialVector joint_velocity{};
90 joint_velocity[0] = body.joint_axis.x * dq[joint];
91 joint_velocity[1] = body.joint_axis.y * dq[joint];
92 joint_velocity[2] = body.joint_axis.z * dq[joint];
93 velocities[joint] = add(transformed_velocity, joint_velocity);
94
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));
104
105 const SpatialVector momentum =
106 multiply(spatial_inertias_[joint], velocities[joint]);
107 forces[joint] =
108 add(multiply(spatial_inertias_[joint], accelerations[joint]),
109 force_cross(velocities[joint], momentum));
110
111 parent_velocity = velocities[joint];
112 parent_acceleration = accelerations[joint];
113 }
114
115 for (std::size_t joint = spec_.joint_count; joint-- > 0;)
116 {
117 const geom::Vec3 axis = spec_.bodies[joint].joint_axis;
118 torques[joint] =
119 axis.x * forces[joint][0] + axis.y * forces[joint][1] +
120 axis.z * forces[joint][2];
121 if (!std::isfinite(torques[joint]))
122 {
124 }
125 if (joint != 0)
126 {
127 forces[joint - 1] =
128 add(forces[joint - 1],
129 transpose_multiply(parent_motion_to_body[joint], forces[joint]));
130 }
131 }
132
133 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
134 {
135 tau_out[joint] = torques[joint];
136 }
137 return rt::ErrorCode::ok;
138 }
139
140 private:
141 using SpatialVector = std::array<double, 6>;
142 using SpatialMatrix = std::array<SpatialVector, 6>;
143
144 static constexpr double RotationTolerance = 1e-9;
145 static constexpr double AxisTolerance = 1e-9;
146
147 static bool finite(geom::Vec3 value)
148 {
149 return std::isfinite(value.x) && std::isfinite(value.y) && std::isfinite(value.z);
150 }
151
152 static double determinant3(const double matrix[3][3])
153 {
154 return matrix[0][0] *
155 (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
156 matrix[0][1] *
157 (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
158 matrix[0][2] *
159 (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
160 }
161
162 static bool valid_rotation(const double rotation[3][3])
163 {
164 for (int row = 0; row < 3; ++row)
165 {
166 for (int column = 0; column < 3; ++column)
167 {
168 if (!std::isfinite(rotation[row][column]))
169 {
170 return false;
171 }
172 }
173 }
174 for (int row = 0; row < 3; ++row)
175 {
176 for (int column = 0; column < 3; ++column)
177 {
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)
183 {
184 return false;
185 }
186 }
187 }
188 return std::fabs(determinant3(rotation) - 1.0) <= RotationTolerance;
189 }
190
191 static bool valid_inertia(const double inertia[3][3])
192 {
193 for (int row = 0; row < 3; ++row)
194 {
195 for (int column = 0; column < 3; ++column)
196 {
197 if (!std::isfinite(inertia[row][column]))
198 {
199 return false;
200 }
201 if (inertia[row][column] != inertia[column][row])
202 {
203 return false;
204 }
205 }
206 }
207
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))
213 {
214 return false;
215 }
216
217 // For principal moments J, triangle feasibility is J_i <= J_j + J_k.
218 // The coordinate-invariant equivalent is trace(I)E - 2I >= 0.
219 double triangle[3][3] = {};
220 const double trace = inertia[0][0] + inertia[1][1] + inertia[2][2];
221 double scale = 0.0;
222 for (int row = 0; row < 3; ++row)
223 {
224 for (int column = 0; column < 3; ++column)
225 {
226 const double magnitude = std::fabs(inertia[row][column]);
227 scale = magnitude > scale ? magnitude : scale;
228 triangle[row][column] = -2.0 * inertia[row][column];
229 if (row == column)
230 {
231 triangle[row][column] += trace;
232 }
233 }
234 }
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)
239 {
240 if (triangle[index][index] < -tolerance1)
241 {
242 return false;
243 }
244 }
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;
253 }
254
255 static bool validate_spec(const FixedBaseChainSpec &spec)
256 {
257 if (spec.joint_count == 0 || spec.joint_count > MaxJoints)
258 {
259 return false;
260 }
261 for (std::size_t joint = 0; joint < spec.joint_count; ++joint)
262 {
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))
268 {
269 return false;
270 }
271 const double axis_squared = geom::dot(body.joint_axis, body.joint_axis);
272 if (std::fabs(axis_squared - 1.0) > AxisTolerance)
273 {
274 return false;
275 }
276 }
277 return true;
278 }
279
280 bool valid_inputs(const double *q, const double *dq, const double *ddq,
281 const double gravity[3]) const
282 {
283 if (!valid_)
284 {
285 return false;
286 }
287 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
288 {
289 if (!std::isfinite(q[joint]) || !std::isfinite(dq[joint]) ||
290 !std::isfinite(ddq[joint]))
291 {
292 return false;
293 }
294 }
295 return std::isfinite(gravity[0]) && std::isfinite(gravity[1]) &&
296 std::isfinite(gravity[2]);
297 }
298
299 static SpatialMatrix make_spatial_inertia(const RevoluteBody &body)
300 {
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)
306 {
307 for (int column = 0; column < 3; ++column)
308 {
309 inertia[row][column] =
310 body.inertia_com[row][column] +
311 body.mass * ((row == column ? squared : 0.0) - c[row] * c[column]);
312 }
313 }
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)
317 {
318 for (int column = 0; column < 3; ++column)
319 {
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;
324 }
325 }
326 return inertia;
327 }
328
329 static SpatialMatrix motion_transform(const RevoluteBody &body, double q)
330 {
331 const double axis[3] = {body.joint_axis.x, body.joint_axis.y,
332 body.joint_axis.z};
333 double joint_rotation[3][3];
334 geom::rodrigues(axis, q, joint_rotation);
335 double child_to_parent[3][3];
336 geom::rotation_multiply(body.parent_from_joint_zero.rotation, joint_rotation,
337 child_to_parent);
338
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)
344 {
345 for (int column = 0; column < 3; ++column)
346 {
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]);
354 }
355 }
356 return transform;
357 }
358
359 static SpatialVector add(const SpatialVector &lhs, const SpatialVector &rhs)
360 {
361 SpatialVector result{};
362 for (std::size_t index = 0; index < result.size(); ++index)
363 {
364 result[index] = lhs[index] + rhs[index];
365 }
366 return result;
367 }
368
369 static SpatialVector multiply(const SpatialMatrix &matrix, const SpatialVector &vector)
370 {
371 SpatialVector result{};
372 for (std::size_t row = 0; row < result.size(); ++row)
373 {
374 for (std::size_t column = 0; column < result.size(); ++column)
375 {
376 result[row] += matrix[row][column] * vector[column];
377 }
378 }
379 return result;
380 }
381
382 static SpatialVector transpose_multiply(const SpatialMatrix &matrix,
383 const SpatialVector &vector)
384 {
385 SpatialVector result{};
386 for (std::size_t row = 0; row < result.size(); ++row)
387 {
388 for (std::size_t column = 0; column < result.size(); ++column)
389 {
390 result[row] += matrix[column][row] * vector[column];
391 }
392 }
393 return result;
394 }
395
396 static void cross3(const double lhs[3], const double rhs[3], double result[3])
397 {
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];
401 }
402
403 static SpatialVector motion_cross(const SpatialVector &lhs, const SpatialVector &rhs)
404 {
405 SpatialVector result{};
406 cross3(lhs.data(), rhs.data(), result.data());
407 double first[3];
408 double second[3];
409 cross3(lhs.data() + 3, rhs.data(), first);
410 cross3(lhs.data(), rhs.data() + 3, second);
411 for (int index = 0; index < 3; ++index)
412 {
413 result[index + 3] = first[index] + second[index];
414 }
415 return result;
416 }
417
418 static SpatialVector force_cross(const SpatialVector &motion,
419 const SpatialVector &force)
420 {
421 SpatialVector result{};
422 double angular[3];
423 double offset[3];
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)
428 {
429 result[index] = angular[index] + offset[index];
430 }
431 return result;
432 }
433
434 FixedBaseChainSpec spec_{};
435 std::array<SpatialMatrix, MaxJoints> spatial_inertias_{};
436 bool valid_ = false;
437};
438
439} // namespace plcopen::core::dyn
FixedBaseChain(const FixedBaseChainSpec &spec)
rt::ErrorCode inverse_dynamics(const double *q, const double *dq, const double *ddq, const double gravity[3], double *tau_out) const
static constexpr std::size_t MaxJoints
constexpr std::size_t FixedBaseChainMaxJoints
void rotation_multiply(const double a[3][3], const double b[3][3], double out[3][3])
Vec3 cross(Vec3 lhs, Vec3 rhs)
double dot(Vec3 lhs, Vec3 rhs)
void rodrigues(const double axis[3], double angle, double out[3][3])
std::array< RevoluteBody, FixedBaseChainMaxJoints > bodies