plcopen 0.22.0
载入中...
搜索中...
未找到
serial_chain.h
浏览该文件的文档.
1#pragma once
2
3// H2 numerical inverse kinematics (approved matrix:
4// doc/compliance/kinematics-plugin-semantics.md v2/v2.1). SerialChain is a
5// fixed-capacity PoseKinematics plugin for revolute standard-DH and
6// modified-DH chains. It owns only the base-to-flange model; L5 keeps the
7// workpiece and tool transforms.
8//
9// RT-SAFE: value math only, fixed storage, bounded iterations, no exceptions.
10
11#include <array>
12#include <cmath>
13#include <cstddef>
14
15#include "geom/frame.h"
16#include "kin/pose.h"
17#include "rt/error.h"
18
19namespace plcopen::core::kin
20{
21
22inline constexpr std::size_t SerialChainMaxJoints = 8;
23
24enum class DhConvention
25{
28};
29
30struct DhLink
31{
32 double a = 0.0;
33 double alpha = 0.0;
34 double d = 0.0;
35 double theta_offset = 0.0;
36 double min_position = 0.0;
37 double max_position = 0.0;
38};
39
41{
42 std::size_t joint_count = 0;
44 std::array<DhLink, SerialChainMaxJoints> links{};
45 // Y5 immutable nullspace posture preference (used by PoseKinematics::inverse
46 // when preference_weight > 0.0 on redundant chains).
47 std::array<double, SerialChainMaxJoints> preferred_joints{};
48 double preference_weight = 0.0;
49};
50
52{
53 double position_tolerance = 1e-9;
54 double orientation_tolerance = 1e-9;
55 const double *preferred_joints = nullptr;
56 double preference_weight = 0.0;
57};
58
60{
62 std::array<double, SerialChainMaxJoints> joints{};
63 double position_residual = 0.0;
65 std::size_t iterations = 0;
66};
67
68class SerialChain final : public PoseKinematics
69{
70 public:
71 static constexpr std::size_t MaxJoints = SerialChainMaxJoints;
72 static constexpr std::size_t MaxIterations = 32;
73
74 explicit SerialChain(const SerialChainSpec &spec) : spec_(spec), valid_(validate_spec(spec))
75 {
76 if (valid_)
77 {
78 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
79 {
80 alpha_cosines_[joint] = std::cos(spec_.links[joint].alpha);
81 alpha_sines_[joint] = std::sin(spec_.links[joint].alpha);
82 }
83 }
84 }
85
86 std::size_t joint_count() const override { return valid_ ? spec_.joint_count : 0; }
87
88 void forward(const double *joints, Pose6 &pose) const override
89 {
90 geom::RigidTransform transform{};
91 if (!valid_ || joints == nullptr)
92 {
93 copy_pose(transform, pose);
94 return;
95 }
96 for (std::size_t i = 0; i < spec_.joint_count; ++i)
97 {
98 // PoseKinematics carries no array extent: validate_spec bounds the
99 // loop, while callers provide joint_count initialized elements.
100 // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
101 const geom::RigidTransform link = link_transform(i, joints[i]);
102 transform = geom::compose(transform, link);
103 }
104 copy_pose(transform, pose);
105 }
106
107 rt::ErrorCode inverse(const Pose6 &pose, const double *seed, double max_joint_step,
108 double *joints_out) const override
109 {
110 SerialChainSolveOptions options{};
111 if (spec_.preference_weight > 0.0)
112 {
113 options.preferred_joints = spec_.preferred_joints.data();
114 options.preference_weight = spec_.preference_weight;
115 }
116 return solve(pose, seed, max_joint_step, options, joints_out);
117 }
118
119 rt::ErrorCode solve(const Pose6 &pose, const double *seed, double max_joint_step,
120 const SerialChainSolveOptions &options, double *joints_out) const
121 {
122 if (joints_out == nullptr)
123 {
125 }
126 const SerialChainSolveResult result = solve_internal(pose, seed, max_joint_step, options);
127 if (result.code == rt::ErrorCode::ok)
128 {
129 for (std::size_t i = 0; i < spec_.joint_count; ++i)
130 {
131 joints_out[i] = result.joints[i];
132 }
133 }
134 return result.code;
135 }
136
138 solve_best_effort(const Pose6 &pose, const double *seed, double max_joint_step,
139 const SerialChainSolveOptions &options = SerialChainSolveOptions{}) const
140 {
141 return solve_internal(pose, seed, max_joint_step, options);
142 }
143
144 double singularity_margin(const double *) const override
145 {
146 // Numerical chains use adaptive damping and the convergence gates in
147 // place of the analytic plugin's entry-ban margin.
148 return valid_ ? 1e300 : 0.0;
149 }
150
151 private:
152 static constexpr double DifferenceStep = 1e-7;
153 static constexpr double MinimumDamping = 1e-6;
154 static constexpr double MaximumDamping = 1e6;
155 static constexpr std::size_t DampingAttempts = 13;
156 static constexpr std::size_t LineSearchSteps = 10;
157
158 static bool validate_spec(const SerialChainSpec &spec)
159 {
160 if (spec.joint_count == 0 || spec.joint_count > MaxJoints)
161 {
162 return false;
163 }
165 {
166 return false;
167 }
168 if (!std::isfinite(spec.preference_weight) || spec.preference_weight < 0.0)
169 {
170 return false;
171 }
172 for (std::size_t i = 0; i < spec.joint_count; ++i)
173 {
174 const DhLink &link = spec.links[i];
175 if (!std::isfinite(link.a) || !std::isfinite(link.alpha) || !std::isfinite(link.d) ||
176 !std::isfinite(link.theta_offset) || !std::isfinite(link.min_position) ||
177 !std::isfinite(link.max_position) || link.min_position > link.max_position)
178 {
179 return false;
180 }
181 if (spec.preference_weight > 0.0)
182 {
183 const double pref = spec.preferred_joints[i];
184 if (!std::isfinite(pref) || pref < link.min_position || pref > link.max_position)
185 {
186 return false;
187 }
188 }
189 }
190 return true;
191 }
192
193 static bool valid_solve_options(double max_joint_step, const SerialChainSolveOptions &options)
194 {
195 constexpr double ToleranceFloor = 1e-12;
196 return std::isfinite(max_joint_step) && max_joint_step > 0.0 &&
197 std::isfinite(options.position_tolerance) &&
198 options.position_tolerance >= ToleranceFloor &&
199 std::isfinite(options.orientation_tolerance) &&
200 options.orientation_tolerance >= ToleranceFloor &&
201 std::isfinite(options.preference_weight) && options.preference_weight >= 0.0 &&
202 (options.preference_weight == 0.0 || options.preferred_joints != nullptr);
203 }
204
205 bool valid_solve_inputs(const Pose6 &pose, const double *seed, double max_joint_step,
206 const SerialChainSolveOptions &options) const
207 {
208 if (!valid_ || seed == nullptr || !valid_solve_options(max_joint_step, options))
209 {
210 return false;
211 }
212 for (int i = 0; i < 3; ++i)
213 {
214 if (!std::isfinite(pose.position[i]))
215 {
216 return false;
217 }
218 for (int j = 0; j < 3; ++j)
219 {
220 if (!std::isfinite(pose.rotation[i][j]))
221 {
222 return false;
223 }
224 }
225 }
226 for (std::size_t i = 0; i < spec_.joint_count; ++i)
227 {
228 if (!std::isfinite(seed[i]) ||
229 (options.preference_weight > 0.0 && !std::isfinite(options.preferred_joints[i])))
230 {
231 return false;
232 }
233 }
234 return true;
235 }
236
237 static double vector_norm(const double *values, std::size_t count)
238 {
239 double squared = 0.0;
240 for (std::size_t i = 0; i < count; ++i)
241 {
242 squared += values[i] * values[i];
243 }
244 return std::sqrt(squared);
245 }
246
247 static void pose_error(const Pose6 &current, const Pose6 &target, double error[6],
248 double &position_residual, double &orientation_residual)
249 {
250 for (int i = 0; i < 3; ++i)
251 {
252 error[i] = target.position[i] - current.position[i];
253 }
254 position_residual = vector_norm(error, 3);
255
256 relative_rotation_log(current.rotation, target.rotation, error + 3);
257 orientation_residual = vector_norm(error + 3, 3);
258 }
259
260 static void relative_rotation_log(const double from[3][3], const double to[3][3],
261 double base_log[3])
262 {
263 double relative[3][3];
264 for (int row = 0; row < 3; ++row)
265 {
266 for (int column = 0; column < 3; ++column)
267 {
268 relative[row][column] = from[0][row] * to[0][column] +
269 from[1][row] * to[1][column] + from[2][row] * to[2][column];
270 }
271 }
272 double local_log[3] = {0.5 * (relative[2][1] - relative[1][2]),
273 0.5 * (relative[0][2] - relative[2][0]),
274 0.5 * (relative[1][0] - relative[0][1])};
275 const double sine = vector_norm(local_log, 3);
276 double cosine = 0.5 * (relative[0][0] + relative[1][1] + relative[2][2] - 1.0);
277 cosine = cosine > 1.0 ? 1.0 : (cosine < -1.0 ? -1.0 : cosine);
278 if (sine > 1e-12)
279 {
280 const double scale = std::atan2(sine, cosine) / sine;
281 for (double &value : local_log)
282 {
283 value *= scale;
284 }
285 }
286 else if (cosine < 0.0)
287 {
288 double axis[3];
289 double angle = 0.0;
290 geom::relative_axis_angle(from, to, axis, angle);
291 for (int row = 0; row < 3; ++row)
292 {
293 local_log[row] = axis[row] * angle;
294 }
295 }
296 for (int row = 0; row < 3; ++row)
297 {
298 base_log[row] = from[row][0] * local_log[0] + from[row][1] * local_log[1] +
299 from[row][2] * local_log[2];
300 }
301 }
302
303 static void relative_rotation_increment(const double from[3][3], const double to[3][3],
304 double base_increment[3])
305 {
306 // Numerical Jacobian columns use a fixed 1e-7-rad perturbation. The
307 // skew part is the one-sided infinitesimal rotation increment; the
308 // full SO(3) log remains the convergence/error oracle.
309 double relative[3][3];
310 for (int row = 0; row < 3; ++row)
311 {
312 for (int column = 0; column < 3; ++column)
313 {
314 relative[row][column] = from[0][row] * to[0][column] +
315 from[1][row] * to[1][column] +
316 from[2][row] * to[2][column];
317 }
318 }
319 const double local_increment[3] = {
320 0.5 * (relative[2][1] - relative[1][2]),
321 0.5 * (relative[0][2] - relative[2][0]),
322 0.5 * (relative[1][0] - relative[0][1])};
323 for (int row = 0; row < 3; ++row)
324 {
325 base_increment[row] = from[row][0] * local_increment[0] +
326 from[row][1] * local_increment[1] +
327 from[row][2] * local_increment[2];
328 }
329 }
330
331 void forward_from(const double *joints, Pose6 &pose) const
332 {
333 geom::RigidTransform transform{};
334 for (std::size_t i = 0; i < spec_.joint_count; ++i)
335 {
336 const geom::RigidTransform link = link_transform(i, joints[i]);
337 transform = geom::compose(transform, link);
338 }
339 copy_pose(transform, pose);
340 }
341
342 void evaluate_chain(const double *joints, Pose6 &pose,
343 geom::RigidTransform link_transforms[MaxJoints],
344 geom::RigidTransform prefixes[MaxJoints + 1]) const
345 {
346 prefixes[0] = geom::RigidTransform{};
347 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
348 {
349 link_transforms[joint] = link_transform(joint, joints[joint]);
350 prefixes[joint + 1] = geom::compose(prefixes[joint], link_transforms[joint]);
351 }
352 copy_pose(prefixes[spec_.joint_count], pose);
353 }
354
355 void numerical_jacobian(
356 const double *joints, const Pose6 &current,
357 const geom::RigidTransform link_transforms[MaxJoints],
358 const geom::RigidTransform prefixes[MaxJoints + 1],
359 double jacobian[6][MaxJoints]) const
360 {
361 // A one-joint perturbation leaves every other local link transform
362 // unchanged. Reuse the current-pose products so each numerical-
363 // Jacobian column recomposes only the affected suffix.
364 for (std::size_t column = 0; column < spec_.joint_count; ++column)
365 {
366 geom::RigidTransform perturbed_transform =
367 geom::compose(prefixes[column],
368 link_transform(column, joints[column] + DifferenceStep));
369 for (std::size_t downstream = column + 1; downstream < spec_.joint_count;
370 ++downstream)
371 {
372 perturbed_transform =
373 geom::compose(perturbed_transform, link_transforms[downstream]);
374 }
375 Pose6 perturbed{};
376 copy_pose(perturbed_transform, perturbed);
377 for (int row = 0; row < 3; ++row)
378 {
379 jacobian[row][column] =
380 (perturbed.position[row] - current.position[row]) / DifferenceStep;
381 }
382 double rotation_delta[3];
383 relative_rotation_increment(current.rotation, perturbed.rotation, rotation_delta);
384 for (int row = 0; row < 3; ++row)
385 {
386 jacobian[row + 3][column] = rotation_delta[row] / DifferenceStep;
387 }
388 }
389 for (std::size_t column = spec_.joint_count; column < MaxJoints; ++column)
390 {
391 for (int row = 0; row < 6; ++row)
392 {
393 jacobian[row][column] = 0.0;
394 }
395 }
396 }
397
398 static bool solve_linear(double matrix[6][6], const double right_hand_side[6],
399 double solution[6])
400 {
401 double augmented[6][7] = {};
402 for (int row = 0; row < 6; ++row)
403 {
404 for (int column = 0; column < 6; ++column)
405 {
406 augmented[row][column] = matrix[row][column];
407 }
408 augmented[row][6] = right_hand_side[row];
409 }
410
411 for (int column = 0; column < 6; ++column)
412 {
413 int pivot_row = column;
414 double pivot_magnitude = std::fabs(augmented[column][column]);
415 for (int row = column + 1; row < 6; ++row)
416 {
417 const double candidate = std::fabs(augmented[row][column]);
418 if (candidate > pivot_magnitude)
419 {
420 pivot_magnitude = candidate;
421 pivot_row = row;
422 }
423 }
424 if (!std::isfinite(pivot_magnitude) || pivot_magnitude < 1e-24)
425 {
426 return false;
427 }
428 if (pivot_row != column)
429 {
430 for (int item = column; item < 7; ++item)
431 {
432 const double saved = augmented[column][item];
433 augmented[column][item] = augmented[pivot_row][item];
434 augmented[pivot_row][item] = saved;
435 }
436 }
437 for (int row = column + 1; row < 6; ++row)
438 {
439 const double factor = augmented[row][column] / augmented[column][column];
440 for (int item = column; item < 7; ++item)
441 {
442 augmented[row][item] -= factor * augmented[column][item];
443 }
444 }
445 }
446
447 for (int row = 5; row >= 0; --row)
448 {
449 double value = augmented[row][6];
450 for (int column = row + 1; column < 6; ++column)
451 {
452 value -= augmented[row][column] * solution[column];
453 }
454 const double pivot = augmented[row][row];
455 if (!std::isfinite(pivot) || std::fabs(pivot) < 1e-24)
456 {
457 return false;
458 }
459 solution[row] = value / pivot;
460 if (!std::isfinite(solution[row]))
461 {
462 return false;
463 }
464 }
465 return true;
466 }
467
468 void damped_matrix(const double jacobian[6][MaxJoints], double damping,
469 double matrix[6][6]) const
470 {
471 for (int row = 0; row < 6; ++row)
472 {
473 for (int column = 0; column < 6; ++column)
474 {
475 double value = 0.0;
476 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
477 {
478 value += jacobian[row][joint] * jacobian[column][joint];
479 }
480 if (row == column)
481 {
482 value += damping * damping;
483 }
484 matrix[row][column] = value;
485 }
486 }
487 }
488
489 bool damped_task_step(const double jacobian[6][MaxJoints], const double error[6],
490 double damping, double step[MaxJoints]) const
491 {
492 double matrix[6][6];
493 damped_matrix(jacobian, damping, matrix);
494 double task_solution[6] = {};
495 if (!solve_linear(matrix, error, task_solution))
496 {
497 return false;
498 }
499 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
500 {
501 step[joint] = 0.0;
502 for (int row = 0; row < 6; ++row)
503 {
504 step[joint] += jacobian[row][joint] * task_solution[row];
505 }
506 }
507 return true;
508 }
509
510 bool nullspace_step(const double jacobian[6][MaxJoints], const double *joints,
511 const SerialChainSolveOptions &options, double damping,
512 double step[MaxJoints]) const
513 {
514 double gradient[MaxJoints] = {};
515 double projected[6] = {};
516 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
517 {
518 gradient[joint] =
519 options.preference_weight * (options.preferred_joints[joint] - joints[joint]);
520 for (int row = 0; row < 6; ++row)
521 {
522 projected[row] += jacobian[row][joint] * gradient[joint];
523 }
524 }
525 double matrix[6][6];
526 damped_matrix(jacobian, damping, matrix);
527 double task_component[6] = {};
528 if (!solve_linear(matrix, projected, task_component))
529 {
530 return false;
531 }
532 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
533 {
534 step[joint] = gradient[joint];
535 for (int row = 0; row < 6; ++row)
536 {
537 step[joint] -= jacobian[row][joint] * task_component[row];
538 }
539 }
540 return true;
541 }
542
543 void project_candidate(const double *seed, double max_joint_step, double candidate[MaxJoints],
544 bool &limit_clipped, bool &step_clipped) const
545 {
546 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
547 {
548 const DhLink &link = spec_.links[joint];
549 if (candidate[joint] < link.min_position)
550 {
551 candidate[joint] = link.min_position;
552 limit_clipped = true;
553 }
554 else if (candidate[joint] > link.max_position)
555 {
556 candidate[joint] = link.max_position;
557 limit_clipped = true;
558 }
559 const double step_lower = seed[joint] - max_joint_step;
560 const double step_upper = seed[joint] + max_joint_step;
561 if (candidate[joint] < step_lower)
562 {
563 candidate[joint] = step_lower;
564 step_clipped = true;
565 }
566 else if (candidate[joint] > step_upper)
567 {
568 candidate[joint] = step_upper;
569 step_clipped = true;
570 }
571 if (candidate[joint] < link.min_position)
572 {
573 candidate[joint] = link.min_position;
574 limit_clipped = true;
575 }
576 else if (candidate[joint] > link.max_position)
577 {
578 candidate[joint] = link.max_position;
579 limit_clipped = true;
580 }
581 }
582 }
583
584 double preference_cost(const double *joints, const SerialChainSolveOptions &options) const
585 {
586 double cost = 0.0;
587 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
588 {
589 const double difference = options.preferred_joints[joint] - joints[joint];
590 cost += difference * difference;
591 }
592 return cost;
593 }
594
595 // Called only from the bounded outer loop. An accepted secondary step
596 // rechecks both primary gates and consumes one refinement from the same
597 // MaxIterations budget.
598 bool apply_in_loop_preference_step(const Pose6 &target, const double *seed,
599 double max_joint_step,
600 const SerialChainSolveOptions &options,
601 double joints[MaxJoints], const Pose6 &current,
602 const geom::RigidTransform link_transforms[MaxJoints],
603 const geom::RigidTransform prefixes[MaxJoints + 1],
604 double &position_residual,
605 double &orientation_residual) const
606 {
607 if (spec_.joint_count <= 6 || options.preference_weight == 0.0)
608 {
609 return false;
610 }
611 double jacobian[6][MaxJoints] = {};
612 numerical_jacobian(joints, current, link_transforms, prefixes, jacobian);
613 double secondary_step[MaxJoints] = {};
614 if (!nullspace_step(jacobian, joints, options, MinimumDamping, secondary_step))
615 {
616 return false;
617 }
618 const double current_cost = preference_cost(joints, options);
619 double scale = 1.0;
620 for (std::size_t line = 0; line < LineSearchSteps; ++line)
621 {
622 double candidate[MaxJoints] = {};
623 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
624 {
625 candidate[joint] = joints[joint] + scale * secondary_step[joint];
626 }
627 bool limit_clipped = false;
628 bool step_clipped = false;
629 project_candidate(seed, max_joint_step, candidate, limit_clipped, step_clipped);
630 Pose6 candidate_pose{};
631 forward_from(candidate, candidate_pose);
632 double error[6];
633 double candidate_position = 0.0;
634 double candidate_orientation = 0.0;
635 pose_error(candidate_pose, target, error, candidate_position, candidate_orientation);
636 if (candidate_position <= options.position_tolerance &&
637 candidate_orientation <= options.orientation_tolerance &&
638 preference_cost(candidate, options) < current_cost)
639 {
640 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
641 {
642 joints[joint] = candidate[joint];
643 }
644 position_residual = candidate_position;
645 orientation_residual = candidate_orientation;
646 return true;
647 }
648 scale *= 0.5;
649 }
650 return false;
651 }
652
653 SerialChainSolveResult solve_internal(const Pose6 &target, const double *seed,
654 double max_joint_step,
655 const SerialChainSolveOptions &options) const
656 {
657 SerialChainSolveResult result{};
658 if (!valid_solve_inputs(target, seed, max_joint_step, options))
659 {
661 return result;
662 }
663
664 double joints[MaxJoints] = {};
665 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
666 {
667 // PoseKinematics carries no array extent; callers provide one seed
668 // per validated joint_count element.
669 // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Assign)
670 const double seed_value = seed[joint];
671 if (seed_value < spec_.links[joint].min_position ||
672 seed_value > spec_.links[joint].max_position)
673 {
675 return result;
676 }
677 joints[joint] = seed_value;
678 result.joints[joint] = seed_value;
679 }
680
681 double best_score = 1e300;
682 double damping = MinimumDamping;
683 geom::RigidTransform link_transforms[MaxJoints] = {};
684 geom::RigidTransform prefixes[MaxJoints + 1] = {};
685 for (std::size_t iteration = 0; iteration < MaxIterations; ++iteration)
686 {
687 Pose6 current{};
688 evaluate_chain(joints, current, link_transforms, prefixes);
689 double error[6];
690 double position_residual = 0.0;
691 double orientation_residual = 0.0;
692 pose_error(current, target, error, position_residual, orientation_residual);
693 const double current_score = std::sqrt(position_residual * position_residual +
694 orientation_residual * orientation_residual);
695 if (current_score < best_score)
696 {
697 best_score = current_score;
698 result.position_residual = position_residual;
699 result.orientation_residual = orientation_residual;
700 result.iterations = iteration;
701 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
702 {
703 result.joints[joint] = joints[joint];
704 }
705 }
706
707 if (position_residual <= options.position_tolerance &&
708 orientation_residual <= options.orientation_tolerance)
709 {
710 double preference_position = 0.0;
711 double preference_orientation = 0.0;
712 if (apply_in_loop_preference_step(target, seed, max_joint_step, options, joints,
713 current, link_transforms, prefixes,
714 preference_position, preference_orientation))
715 {
716 result.code = rt::ErrorCode::ok;
717 result.position_residual = preference_position;
718 result.orientation_residual = preference_orientation;
719 result.iterations = iteration + 1;
720 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
721 {
722 result.joints[joint] = joints[joint];
723 }
724 return result;
725 }
726 result.code = rt::ErrorCode::ok;
727 result.position_residual = position_residual;
728 result.orientation_residual = orientation_residual;
729 result.iterations = iteration;
730 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
731 {
732 result.joints[joint] = joints[joint];
733 }
734 return result;
735 }
736
737 double jacobian[6][MaxJoints] = {};
738 numerical_jacobian(joints, current, link_transforms, prefixes, jacobian);
739 bool accepted = false;
740 bool any_limit_clipped = false;
741 bool any_step_clipped = false;
742 double attempt_damping = damping;
743 for (std::size_t damping_attempt = 0; damping_attempt < DampingAttempts;
744 ++damping_attempt)
745 {
746 double step[MaxJoints] = {};
747 if (!damped_task_step(jacobian, error, attempt_damping, step))
748 {
749 attempt_damping *= 10.0;
750 if (attempt_damping > MaximumDamping)
751 {
752 attempt_damping = MaximumDamping;
753 }
754 continue;
755 }
756 double scale = 1.0;
757 for (std::size_t line = 0; line < LineSearchSteps; ++line)
758 {
759 double candidate[MaxJoints] = {};
760 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
761 {
762 candidate[joint] = joints[joint] + scale * step[joint];
763 }
764 bool limit_clipped = false;
765 bool step_clipped = false;
766 project_candidate(seed, max_joint_step, candidate, limit_clipped, step_clipped);
767 any_limit_clipped = any_limit_clipped || limit_clipped;
768 any_step_clipped = any_step_clipped || step_clipped;
769
770 Pose6 candidate_pose{};
771 forward_from(candidate, candidate_pose);
772 double candidate_error[6];
773 double candidate_position = 0.0;
774 double candidate_orientation = 0.0;
775 pose_error(candidate_pose, target, candidate_error, candidate_position,
776 candidate_orientation);
777 const double candidate_score =
778 std::sqrt(candidate_position * candidate_position +
779 candidate_orientation * candidate_orientation);
780 if (candidate_score < current_score)
781 {
782 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
783 {
784 joints[joint] = candidate[joint];
785 }
786 damping = attempt_damping * 0.2;
787 if (damping < MinimumDamping)
788 {
789 damping = MinimumDamping;
790 }
791 accepted = true;
792 break;
793 }
794 scale *= 0.5;
795 }
796 if (accepted)
797 {
798 break;
799 }
800 attempt_damping *= 10.0;
801 if (attempt_damping > MaximumDamping)
802 {
803 attempt_damping = MaximumDamping;
804 }
805 }
806
807 if (!accepted)
808 {
809 result.code = any_limit_clipped
811 : (any_step_clipped ? rt::ErrorCode::infeasible
813 return result;
814 }
815 }
816
817 Pose6 final_pose{};
818 forward_from(joints, final_pose);
819 double final_error[6];
820 double final_position = 0.0;
821 double final_orientation = 0.0;
822 pose_error(final_pose, target, final_error, final_position, final_orientation);
823 const double final_score =
824 std::sqrt(final_position * final_position + final_orientation * final_orientation);
825 if (final_score < best_score)
826 {
827 result.position_residual = final_position;
828 result.orientation_residual = final_orientation;
829 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
830 {
831 result.joints[joint] = joints[joint];
832 }
833 }
834 if (final_position <= options.position_tolerance &&
835 final_orientation <= options.orientation_tolerance)
836 {
837 result.code = rt::ErrorCode::ok;
838 result.position_residual = final_position;
839 result.orientation_residual = final_orientation;
840 result.iterations = MaxIterations;
841 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
842 {
843 result.joints[joint] = joints[joint];
844 }
845 return result;
846 }
847 result.code = rt::ErrorCode::not_converged;
848 result.iterations = MaxIterations;
849 return result;
850 }
851
852 geom::RigidTransform link_transform(std::size_t index, double joint) const
853 {
854 const DhLink &link = spec_.links[index];
855 const double theta = joint + link.theta_offset;
856 const double ct = std::cos(theta);
857 const double st = std::sin(theta);
858 const double ca = alpha_cosines_[index];
859 const double sa = alpha_sines_[index];
860 geom::RigidTransform transform{};
861 if (spec_.convention == DhConvention::standard)
862 {
863 transform.rotation[0][0] = ct;
864 transform.rotation[0][1] = -st * ca;
865 transform.rotation[0][2] = st * sa;
866 transform.rotation[1][0] = st;
867 transform.rotation[1][1] = ct * ca;
868 transform.rotation[1][2] = -ct * sa;
869 transform.rotation[2][0] = 0.0;
870 transform.rotation[2][1] = sa;
871 transform.rotation[2][2] = ca;
872 transform.translation = geom::Vec3{link.a * ct, link.a * st, link.d};
873 return transform;
874 }
875
876 transform.rotation[0][0] = ct;
877 transform.rotation[0][1] = -st;
878 transform.rotation[0][2] = 0.0;
879 transform.rotation[1][0] = st * ca;
880 transform.rotation[1][1] = ct * ca;
881 transform.rotation[1][2] = -sa;
882 transform.rotation[2][0] = st * sa;
883 transform.rotation[2][1] = ct * sa;
884 transform.rotation[2][2] = ca;
885 transform.translation = geom::Vec3{link.a, -link.d * sa, link.d * ca};
886 return transform;
887 }
888
889 static void copy_pose(const geom::RigidTransform &transform, Pose6 &pose)
890 {
891 pose.position[0] = transform.translation.x;
892 pose.position[1] = transform.translation.y;
893 pose.position[2] = transform.translation.z;
894 for (int row = 0; row < 3; ++row)
895 {
896 for (int column = 0; column < 3; ++column)
897 {
898 pose.rotation[row][column] = transform.rotation[row][column];
899 }
900 }
901 }
902
903 SerialChainSpec spec_{};
904 std::array<double, MaxJoints> alpha_cosines_{};
905 std::array<double, MaxJoints> alpha_sines_{};
906 bool valid_ = false;
907};
908
909} // namespace plcopen::core::kin
static constexpr std::size_t MaxJoints
static constexpr std::size_t MaxIterations
std::size_t joint_count() const override
rt::ErrorCode solve(const Pose6 &pose, const double *seed, double max_joint_step, const SerialChainSolveOptions &options, double *joints_out) const
rt::ErrorCode inverse(const Pose6 &pose, const double *seed, double max_joint_step, double *joints_out) const override
SerialChainSolveResult solve_best_effort(const Pose6 &pose, const double *seed, double max_joint_step, const SerialChainSolveOptions &options=SerialChainSolveOptions{}) const
double singularity_margin(const double *) const override
void forward(const double *joints, Pose6 &pose) const override
SerialChain(const SerialChainSpec &spec)
void relative_axis_angle(const double a[3][3], const double b[3][3], double axis[3], double &angle)
RigidTransform compose(const RigidTransform &a, const RigidTransform &b)
constexpr std::size_t SerialChainMaxJoints
std::array< double, SerialChainMaxJoints > joints
std::array< DhLink, SerialChainMaxJoints > links
std::array< double, SerialChainMaxJoints > preferred_joints