78 for (std::size_t joint = 0; joint < spec_.joint_count; ++joint)
80 alpha_cosines_[joint] = std::cos(spec_.links[joint].alpha);
81 alpha_sines_[joint] = std::sin(spec_.links[joint].alpha);
86 std::size_t
joint_count()
const override {
return valid_ ? spec_.joint_count : 0; }
91 if (!valid_ || joints ==
nullptr)
93 copy_pose(transform, pose);
96 for (std::size_t i = 0; i < spec_.joint_count; ++i)
104 copy_pose(transform, pose);
108 double *joints_out)
const override
111 if (spec_.preference_weight > 0.0)
116 return solve(pose, seed, max_joint_step, options, joints_out);
122 if (joints_out ==
nullptr)
129 for (std::size_t i = 0; i < spec_.joint_count; ++i)
131 joints_out[i] = result.
joints[i];
141 return solve_internal(pose, seed, max_joint_step, options);
148 return valid_ ? 1e300 : 0.0;
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;
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)
184 if (!std::isfinite(pref) || pref < link.min_position || pref > link.max_position)
193 static bool valid_solve_options(
double max_joint_step,
const SerialChainSolveOptions &options)
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);
205 bool valid_solve_inputs(
const Pose6 &pose,
const double *seed,
double max_joint_step,
206 const SerialChainSolveOptions &options)
const
208 if (!valid_ || seed ==
nullptr || !valid_solve_options(max_joint_step, options))
212 for (
int i = 0; i < 3; ++i)
214 if (!std::isfinite(
pose.position[i]))
218 for (
int j = 0; j < 3; ++j)
220 if (!std::isfinite(
pose.rotation[i][j]))
226 for (std::size_t i = 0; i < spec_.joint_count; ++i)
228 if (!std::isfinite(seed[i]) ||
229 (options.preference_weight > 0.0 && !std::isfinite(options.preferred_joints[i])))
237 static double vector_norm(
const double *values, std::size_t count)
239 double squared = 0.0;
240 for (std::size_t i = 0; i <
count; ++i)
242 squared += values[i] * values[i];
244 return std::sqrt(squared);
247 static void pose_error(
const Pose6 ¤t,
const Pose6 &target,
double error[6],
248 double &position_residual,
double &orientation_residual)
250 for (
int i = 0; i < 3; ++i)
254 position_residual = vector_norm(error, 3);
256 relative_rotation_log(
current.rotation, target.rotation, error + 3);
257 orientation_residual = vector_norm(error + 3, 3);
260 static void relative_rotation_log(
const double from[3][3],
const double to[3][3],
264 for (
int row = 0; row < 3; ++row)
266 for (
int column = 0; column < 3; ++column)
268 relative[row][column] = from[0][row] * to[0][column] +
269 from[1][row] * to[1][column] + from[2][row] * to[2][column];
273 0.5 * (relative[0][2] - relative[2][0]),
275 const double sine = vector_norm(local_log, 3);
277 cosine = cosine > 1.0 ? 1.0 : (cosine < -1.0 ? -1.0 : cosine);
280 const double scale = std::atan2(sine, cosine) / sine;
281 for (
double &value : local_log)
286 else if (cosine < 0.0)
291 for (
int row = 0; row < 3; ++row)
293 local_log[row] =
axis[row] * angle;
296 for (
int row = 0; row < 3; ++row)
298 base_log[row] = from[row][0] * local_log[0] + from[row][1] * local_log[1] +
299 from[row][2] * local_log[2];
303 static void relative_rotation_increment(
const double from[3][3],
const double to[3][3],
304 double base_increment[3])
310 for (
int row = 0; row < 3; ++row)
312 for (
int column = 0; column < 3; ++column)
314 relative[row][column] = from[0][row] * to[0][column] +
315 from[1][row] * to[1][column] +
316 from[2][row] * to[2][column];
319 const double local_increment[3] = {
321 0.5 * (relative[0][2] - relative[2][0]),
323 for (
int row = 0; row < 3; ++row)
325 base_increment[row] = from[row][0] * local_increment[0] +
326 from[row][1] * local_increment[1] +
327 from[row][2] * local_increment[2];
331 void forward_from(
const double *joints, Pose6 &pose)
const
333 geom::RigidTransform transform{};
334 for (std::size_t i = 0; i < spec_.joint_count; ++i)
336 const geom::RigidTransform link = link_transform(i, joints[i]);
339 copy_pose(transform, pose);
342 void evaluate_chain(
const double *joints, Pose6 &pose,
343 geom::RigidTransform link_transforms[
MaxJoints],
344 geom::RigidTransform prefixes[
MaxJoints + 1])
const
346 prefixes[0] = geom::RigidTransform{};
347 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
349 link_transforms[
joint] = link_transform(joint, joints[joint]);
352 copy_pose(prefixes[spec_.joint_count], pose);
355 void numerical_jacobian(
356 const double *joints,
const Pose6 ¤t,
357 const geom::RigidTransform link_transforms[
MaxJoints],
358 const geom::RigidTransform prefixes[
MaxJoints + 1],
364 for (std::size_t column = 0; column < spec_.joint_count; ++column)
366 geom::RigidTransform perturbed_transform =
368 link_transform(column, joints[column] + DifferenceStep));
369 for (std::size_t downstream = column + 1; downstream < spec_.joint_count;
372 perturbed_transform =
373 geom::compose(perturbed_transform, link_transforms[downstream]);
376 copy_pose(perturbed_transform, perturbed);
377 for (
int row = 0; row < 3; ++row)
379 jacobian[row][column] =
380 (perturbed.position[row] -
current.position[row]) / DifferenceStep;
382 double rotation_delta[3];
383 relative_rotation_increment(
current.rotation, perturbed.rotation, rotation_delta);
384 for (
int row = 0; row < 3; ++row)
386 jacobian[row + 3][column] = rotation_delta[row] / DifferenceStep;
389 for (std::size_t column = spec_.joint_count; column <
MaxJoints; ++column)
391 for (
int row = 0; row < 6; ++row)
393 jacobian[row][column] = 0.0;
398 static bool solve_linear(
double matrix[6][6],
const double right_hand_side[6],
401 double augmented[6][7] = {};
402 for (
int row = 0; row < 6; ++row)
404 for (
int column = 0; column < 6; ++column)
406 augmented[row][column] = matrix[row][column];
408 augmented[row][6] = right_hand_side[row];
411 for (
int column = 0; column < 6; ++column)
413 int pivot_row = column;
414 double pivot_magnitude = std::fabs(augmented[column][column]);
415 for (
int row = column + 1; row < 6; ++row)
417 const double candidate = std::fabs(augmented[row][column]);
418 if (candidate > pivot_magnitude)
420 pivot_magnitude = candidate;
424 if (!std::isfinite(pivot_magnitude) || pivot_magnitude < 1e-24)
428 if (pivot_row != column)
430 for (
int item = column; item < 7; ++item)
432 const double saved = augmented[column][item];
433 augmented[column][item] = augmented[pivot_row][item];
434 augmented[pivot_row][item] = saved;
437 for (
int row = column + 1; row < 6; ++row)
439 const double factor = augmented[row][column] / augmented[column][column];
440 for (
int item = column; item < 7; ++item)
442 augmented[row][item] -= factor * augmented[column][item];
447 for (
int row = 5; row >= 0; --row)
449 double value = augmented[row][6];
450 for (
int column = row + 1; column < 6; ++column)
452 value -= augmented[row][column] * solution[column];
454 const double pivot = augmented[row][row];
455 if (!std::isfinite(pivot) || std::fabs(pivot) < 1e-24)
459 solution[row] = value / pivot;
460 if (!std::isfinite(solution[row]))
468 void damped_matrix(
const double jacobian[6][
MaxJoints],
double damping,
469 double matrix[6][6])
const
471 for (
int row = 0; row < 6; ++row)
473 for (
int column = 0; column < 6; ++column)
476 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
478 value += jacobian[row][
joint] * jacobian[column][
joint];
482 value += damping * damping;
484 matrix[row][column] = value;
489 bool damped_task_step(
const double jacobian[6][
MaxJoints],
const double error[6],
490 double damping,
double step[
MaxJoints])
const
493 damped_matrix(jacobian, damping, matrix);
494 double task_solution[6] = {};
495 if (!solve_linear(matrix, error, task_solution))
499 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
502 for (
int row = 0; row < 6; ++row)
510 bool nullspace_step(
const double jacobian[6][
MaxJoints],
const double *joints,
511 const SerialChainSolveOptions &options,
double damping,
515 double projected[6] = {};
516 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
519 options.preference_weight * (options.preferred_joints[
joint] - joints[
joint]);
520 for (
int row = 0; row < 6; ++row)
522 projected[row] += jacobian[row][
joint] * gradient[
joint];
526 damped_matrix(jacobian, damping, matrix);
527 double task_component[6] = {};
528 if (!solve_linear(matrix, projected, task_component))
532 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
535 for (
int row = 0; row < 6; ++row)
543 void project_candidate(
const double *seed,
double max_joint_step,
double candidate[
MaxJoints],
544 bool &limit_clipped,
bool &step_clipped)
const
546 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
548 const DhLink &link = spec_.links[
joint];
549 if (candidate[joint] < link.min_position)
551 candidate[
joint] = link.min_position;
552 limit_clipped =
true;
554 else if (candidate[joint] > link.max_position)
556 candidate[
joint] = link.max_position;
557 limit_clipped =
true;
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)
563 candidate[
joint] = step_lower;
566 else if (candidate[joint] > step_upper)
568 candidate[
joint] = step_upper;
571 if (candidate[joint] < link.min_position)
573 candidate[
joint] = link.min_position;
574 limit_clipped =
true;
576 else if (candidate[joint] > link.max_position)
578 candidate[
joint] = link.max_position;
579 limit_clipped =
true;
584 double preference_cost(
const double *joints,
const SerialChainSolveOptions &options)
const
587 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
589 const double difference = options.preferred_joints[
joint] - joints[
joint];
590 cost += difference * difference;
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 ¤t,
602 const geom::RigidTransform link_transforms[
MaxJoints],
603 const geom::RigidTransform prefixes[
MaxJoints + 1],
604 double &position_residual,
605 double &orientation_residual)
const
607 if (spec_.joint_count <= 6 || options.preference_weight == 0.0)
612 numerical_jacobian(joints, current, link_transforms, prefixes, jacobian);
614 if (!nullspace_step(jacobian, joints, options, MinimumDamping, secondary_step))
618 const double current_cost = preference_cost(joints, options);
620 for (std::size_t line = 0;
line < LineSearchSteps; ++
line)
623 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
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);
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)
640 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
644 position_residual = candidate_position;
645 orientation_residual = candidate_orientation;
653 SerialChainSolveResult solve_internal(
const Pose6 &target,
const double *seed,
654 double max_joint_step,
655 const SerialChainSolveOptions &options)
const
657 SerialChainSolveResult result{};
658 if (!valid_solve_inputs(target, seed, max_joint_step, options))
665 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
670 const double seed_value = seed[
joint];
671 if (seed_value < spec_.links[joint].min_position ||
672 seed_value > spec_.links[joint].max_position)
677 joints[
joint] = seed_value;
678 result.joints[
joint] = seed_value;
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)
688 evaluate_chain(joints, current, link_transforms, prefixes);
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)
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)
707 if (position_residual <= options.position_tolerance &&
708 orientation_residual <= options.orientation_tolerance)
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))
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)
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)
738 numerical_jacobian(joints, current, link_transforms, prefixes, jacobian);
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;
747 if (!damped_task_step(jacobian, error, attempt_damping, step))
749 attempt_damping *= 10.0;
750 if (attempt_damping > MaximumDamping)
752 attempt_damping = MaximumDamping;
757 for (std::size_t line = 0;
line < LineSearchSteps; ++
line)
760 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
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;
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)
782 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
786 damping = attempt_damping * 0.2;
787 if (damping < MinimumDamping)
789 damping = MinimumDamping;
800 attempt_damping *= 10.0;
801 if (attempt_damping > MaximumDamping)
803 attempt_damping = MaximumDamping;
809 result.code = any_limit_clipped
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)
827 result.position_residual = final_position;
828 result.orientation_residual = final_orientation;
829 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
834 if (final_position <= options.position_tolerance &&
835 final_orientation <= options.orientation_tolerance)
838 result.position_residual = final_position;
839 result.orientation_residual = final_orientation;
841 for (std::size_t joint = 0;
joint < spec_.joint_count; ++
joint)
852 geom::RigidTransform link_transform(std::size_t index,
double joint)
const
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{};
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};
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};
889 static void copy_pose(
const geom::RigidTransform &transform, Pose6 &pose)
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)
896 for (
int column = 0; column < 3; ++column)
898 pose.rotation[row][column] = transform.rotation[row][column];
903 SerialChainSpec spec_{};
904 std::array<double, MaxJoints> alpha_cosines_{};
905 std::array<double, MaxJoints> alpha_sines_{};