plcopen 0.22.0
载入中...
搜索中...
未找到
scara.h
浏览该文件的文档.
1#pragma once
2
3// Reference kinematics #2 (approved matrix, decision #7): SCARA — a planar
4// 2R arm (theta1, theta2) plus an optional prismatic Z joint. Closed-form
5// analytic inverse with the elbow branch selected by the seed (decision #4):
6// the solution keeps the seed's elbow sign; a pose only reachable on the
7// other branch reports `infeasible`. Singularities sit at the stretched
8// (theta2 = 0) and folded (theta2 = +-pi) configurations; the margin is the
9// angular distance to the nearest one (decision #3).
10
11#include <cmath>
12#include <cstddef>
13
14#include "kin/kinematics.h"
15
16namespace plcopen::core::kin
17{
18
19class Scara final : public Kinematics
20{
21public:
22 // with_z selects the 3-joint (theta1, theta2, z) variant; link lengths
23 // must be positive.
24 Scara(double link1, double link2, bool with_z)
25 : link1_(link1)
26 , link2_(link2)
27 , with_z_(with_z)
28 {
29 }
30
31 std::size_t joint_count() const override
32 {
33 return with_z_ ? 3 : 2;
34 }
35
36 std::size_t cartesian_count() const override
37 {
38 return with_z_ ? 3 : 2;
39 }
40
41 rt::ErrorCode forward(const double *joints,
42 std::size_t joint_count,
43 geom::Vec3 &cartesian) const override
44 {
45 if(joint_count != this->joint_count()) {
47 }
48 const double reach = joints[0] + joints[1];
49 cartesian = geom::Vec3{link1_ * std::cos(joints[0]) + link2_ * std::cos(reach),
50 link1_ * std::sin(joints[0]) + link2_ * std::sin(reach),
51 with_z_ ? joints[2] : 0.0};
52 return rt::ErrorCode::ok;
53 }
54
56 const double *seed_joints,
57 std::size_t joint_count,
58 double *joints_out) const override
59 {
60 if(joint_count != this->joint_count() || !std::isfinite(cartesian.x) ||
61 !std::isfinite(cartesian.y)) {
63 }
64
65 const double radius_squared = cartesian.x * cartesian.x + cartesian.y * cartesian.y;
66 const double cos_theta2 =
67 (radius_squared - link1_ * link1_ - link2_ * link2_) / (2.0 * link1_ * link2_);
68 if(cos_theta2 > 1.0 + 1e-12 || cos_theta2 < -1.0 - 1e-12) {
69 return rt::ErrorCode::infeasible; // outside the annular workspace
70 }
71 const double clamped = cos_theta2 > 1.0 ? 1.0 : (cos_theta2 < -1.0 ? -1.0 : cos_theta2);
72 const double magnitude = std::acos(clamped);
73
74 // Seed-branch selection: keep the seed's elbow sign; a zero seed
75 // takes the positive elbow by declaration.
76 const double elbow = seed_joints[1] < 0.0 ? -magnitude : magnitude;
77 const double theta2 = elbow;
78 double theta1 =
79 std::atan2(cartesian.y, cartesian.x) -
80 std::atan2(link2_ * std::sin(theta2), link1_ + link2_ * std::cos(theta2));
81 // Revolute continuity: wrap theta1 into the seed's turn so the
82 // atan2 branch cut never produces a 2*pi jump against the seed.
83 const double two_pi = 6.28318530717958647692;
84 theta1 += two_pi * std::floor((seed_joints[0] - theta1) / two_pi + 0.5);
85
86 joints_out[0] = theta1;
87 joints_out[1] = theta2;
88 if(with_z_) {
89 if(!std::isfinite(cartesian.z)) {
91 }
92 joints_out[2] = cartesian.z;
93 }
94 return rt::ErrorCode::ok;
95 }
96
97 double singularity_margin(const double *joints, std::size_t joint_count) const override
98 {
99 if(joint_count != this->joint_count()) {
100 return 0.0;
101 }
102 const double pi = 3.14159265358979323846;
103 double folded = std::fmod(std::fabs(joints[1]), 2.0 * pi);
104 if(folded > pi) {
105 folded = 2.0 * pi - folded;
106 }
107 const double stretched_margin = folded; // distance to theta2 = 0
108 const double folded_margin = pi - folded; // distance to theta2 = +-pi
109 return stretched_margin < folded_margin ? stretched_margin : folded_margin;
110 }
111
112private:
113 double link1_ = 1.0;
114 double link2_ = 1.0;
115 bool with_z_ = false;
116};
117
118} // namespace plcopen::core::kin
Scara(double link1, double link2, bool with_z)
定义 scara.h:24
rt::ErrorCode inverse(geom::Vec3 cartesian, const double *seed_joints, std::size_t joint_count, double *joints_out) const override
定义 scara.h:55
rt::ErrorCode forward(const double *joints, std::size_t joint_count, geom::Vec3 &cartesian) const override
定义 scara.h:41
double singularity_margin(const double *joints, std::size_t joint_count) const override
定义 scara.h:97
std::size_t joint_count() const override
定义 scara.h:31
std::size_t cartesian_count() const override
定义 scara.h:36