plcopen 0.22.0
载入中...
搜索中...
未找到
wrist6r.h
浏览该文件的文档.
1#pragma once
2
3// BS3.5: spherical-wrist 6R analytic inverse kinematics (approved kinematics
4// matrix, decision #7 item 3 — the standalone batch). Pre-integration form:
5// full 6-DOF poses (position + rotation matrix) do not fit the v1 Kinematics
6// ABI (2/3 translational coordinates), so this mechanism ships as its own
7// class with the same contract discipline (analytic, seed-branch selection,
8// singularity margins, no allocation, no exceptions); the group wiring
9// arrives with the orientation (RPY) batch.
10//
11// Geometry (Pieper-compliant articulated arm):
12// q1 about the base Z; shoulder at height d1; upper arm a2 and forearm d4
13// swing about the shoulder Y axis (q2, q3 measured from the vertical);
14// spherical wrist ZYZ (q4 about Z3, q5 about Y, q6 about Z) at the wrist
15// center; tool point offset d6 along the tool Z.
16// R = Rz(q1) * Ry(q2+q3) * Rz(q4) * Ry(q5) * Rz(q6)
17// wc = [c1*r, s1*r, d1 + z], r = a2*s2 + d4*s23, z = a2*c2 + d4*c23
18// p = wc + d6 * R * ez
19//
20// Eight solution branches: shoulder (q1 vs q1+pi with the reach mirrored),
21// elbow (q3 sign), wrist (q5 sign). inverse() enumerates all eight and picks
22// the branch closest to the seed (largest joint deviation minimized, each
23// revolute wrapped into the seed's turn); no same-turn candidate inside the
24// step bound is `infeasible` — never an implicit branch flip.
25
26#include <cmath>
27#include <cstddef>
28
29#include "kin/pose.h"
30#include "rt/error.h"
31
32namespace plcopen::core::kin
33{
34
35class SphericalWrist6R final : public PoseKinematics
36{
37public:
38 SphericalWrist6R(double base_height, double upper_arm, double forearm, double tool)
39 : d1_(base_height)
40 , a2_(upper_arm)
41 , d4_(forearm)
42 , d6_(tool)
43 {
44 }
45
46 static constexpr std::size_t JointCount = 6;
47
48 std::size_t joint_count() const override
49 {
50 return JointCount;
51 }
52
53 void forward(const double *q, Pose6 &pose) const override
54 {
55 double rotation[3][3];
56 compose_rotation(q, rotation);
57 const double s2 = std::sin(q[1]);
58 const double c2 = std::cos(q[1]);
59 const double s23 = std::sin(q[1] + q[2]);
60 const double c23 = std::cos(q[1] + q[2]);
61 const double radius = a2_ * s2 + d4_ * s23;
62 const double height = a2_ * c2 + d4_ * c23;
63 const double c1 = std::cos(q[0]);
64 const double s1 = std::sin(q[0]);
65 const double wc[3] = {c1 * radius, s1 * radius, d1_ + height};
66 for(int i = 0; i < 3; ++i) {
67 pose.position[i] = wc[i] + d6_ * rotation[i][2];
68 for(int j = 0; j < 3; ++j) {
69 pose.rotation[i][j] = rotation[i][j];
70 }
71 }
72 }
73
74 // max_joint_step bounds the seed distance per joint (same-turn wrapped);
75 // it is the "no branch flip" gate, not a numeric tolerance.
77 const double *seed,
78 double max_joint_step,
79 double *joints_out) const override
80 {
81 // Wrist center from the tool pose.
82 const double wc[3] = {pose.position[0] - d6_ * pose.rotation[0][2],
83 pose.position[1] - d6_ * pose.rotation[1][2],
84 pose.position[2] - d6_ * pose.rotation[2][2]};
85 const double planar = std::sqrt(wc[0] * wc[0] + wc[1] * wc[1]);
86 const double lift = wc[2] - d1_;
87
88 double best[JointCount] = {};
89 double best_score = 1e300;
90 bool found = false;
91
92 for(int shoulder = 0; shoulder < 2; ++shoulder) {
93 // Shoulder branch: q1 faces the wrist center, or looks away with
94 // the reach mirrored through the base axis.
95 double q1 = std::atan2(wc[1], wc[0]);
96 double reach = planar;
97 if(shoulder == 1) {
98 q1 += Pi;
99 reach = -planar;
100 }
101
102 // Planar 2R (a2, d4) in the (reach, lift) plane, angles measured
103 // from the vertical: reach = a2*s2 + d4*s23, lift = a2*c2 + d4*c23.
104 const double radius_squared = reach * reach + lift * lift;
105 const double cos_elbow =
106 (radius_squared - a2_ * a2_ - d4_ * d4_) / (2.0 * a2_ * d4_);
107 if(cos_elbow > 1.0 + 1e-12 || cos_elbow < -1.0 - 1e-12) {
108 continue; // out of the annular workspace on this branch
109 }
110 const double clamped =
111 cos_elbow > 1.0 ? 1.0 : (cos_elbow < -1.0 ? -1.0 : cos_elbow);
112 const double elbow_magnitude = std::acos(clamped);
113
114 for(int elbow = 0; elbow < 2; ++elbow) {
115 const double q3 = elbow == 0 ? elbow_magnitude : -elbow_magnitude;
116 // From the vertical: q2 = atan2(reach, lift) - atan2(d4*s3, a2 + d4*c3)
117 const double q2 = std::atan2(reach, lift) -
118 std::atan2(d4_ * std::sin(q3),
119 a2_ + d4_ * std::cos(q3));
120
121 // Wrist: R36 = Ry(q2+q3)^T * Rz(q1)^T * R = Rz(q4) Ry(q5) Rz(q6).
122 double r36[3][3];
123 arm_transpose_times(q1, q2 + q3, pose.rotation, r36);
124 for(int wrist = 0; wrist < 2; ++wrist) {
125 double q4;
126 double q5;
127 double q6;
128 const double sin_q5 =
129 std::sqrt(r36[0][2] * r36[0][2] + r36[1][2] * r36[1][2]);
130 q5 = std::atan2(wrist == 0 ? sin_q5 : -sin_q5, r36[2][2]);
131 // Wrist-singularity band (approved cartesian v2-A,
132 // KB-045): widened from the exact-zero 1e-12 so a
133 // Cartesian sweep lands on the seed-locked convention
134 // instead of an ill-conditioned split. 1e-8 keeps the
135 // convention orientation error (~d6*pi*band) below the
136 // 1e-8 linearity gates (tightened from the approved
137 // 1e-6, recorded).
138 if(sin_q5 > 1e-8) {
139 const double sign = wrist == 0 ? 1.0 : -1.0;
140 q4 = std::atan2(sign * r36[1][2], sign * r36[0][2]);
141 q6 = std::atan2(sign * r36[2][1], -sign * r36[2][0]);
142 } else {
143 // Wrist singularity: q4+q6 is the only observable;
144 // keep the seed's q4 and put the rest into q6.
145 q4 = seed[3];
146 q6 = std::atan2(r36[1][0], r36[0][0]) - q4;
147 if(wrist == 1) {
148 continue; // both wrist branches coincide here
149 }
150 }
151
152 double candidate[JointCount] = {q1, q2, q3, q4, q5, q6};
153 double score = 0.0;
154 bool inside = true;
155 for(std::size_t j = 0; j < JointCount; ++j) {
156 candidate[j] = wrap_to_seed(candidate[j], seed[j]);
157 const double deviation = std::fabs(candidate[j] - seed[j]);
158 if(deviation > max_joint_step) {
159 inside = false;
160 break;
161 }
162 if(deviation > score) {
163 score = deviation;
164 }
165 }
166 if(inside && score < best_score) {
167 best_score = score;
168 for(std::size_t j = 0; j < JointCount; ++j) {
169 best[j] = candidate[j];
170 }
171 found = true;
172 }
173 }
174 }
175 }
176
177 if(!found) {
179 }
180 for(std::size_t j = 0; j < JointCount; ++j) {
181 joints_out[j] = best[j];
182 }
183 return rt::ErrorCode::ok;
184 }
185
186 // Angular distance to the nearest singular configuration: wrist
187 // (q5 = 0 or +-pi), elbow (q3 = 0 or +-pi), and shoulder (wrist center on
188 // the base axis, measured as the planar reach angle).
189 double singularity_margin(const double *q) const override
190 {
191 const double wrist = fold(q[4]);
192 const double elbow = fold(q[2]);
193 // Shoulder: reach angle of the wrist center off the base axis.
194 const double s2 = std::sin(q[1]);
195 const double s23 = std::sin(q[1] + q[2]);
196 const double radius = a2_ * s2 + d4_ * s23;
197 const double height = a2_ * std::cos(q[1]) + d4_ * std::cos(q[1] + q[2]);
198 const double shoulder = std::fabs(std::atan2(radius, std::fabs(height) + d1_));
199 double margin = wrist < elbow ? wrist : elbow;
200 return shoulder < margin ? shoulder : margin;
201 }
202
203private:
204 static constexpr double Pi = 3.14159265358979323846;
205
206 static double fold(double angle)
207 {
208 double magnitude = std::fmod(std::fabs(angle), 2.0 * Pi);
209 if(magnitude > Pi) {
210 magnitude = 2.0 * Pi - magnitude;
211 }
212 const double to_pi = Pi - magnitude;
213 return magnitude < to_pi ? magnitude : to_pi;
214 }
215
216 static double wrap_to_seed(double angle, double seed)
217 {
218 return angle + 2.0 * Pi * std::floor((seed - angle) / (2.0 * Pi) + 0.5);
219 }
220
221 // rotation = Rz(q1) * Ry(q2+q3) * Rz(q4) * Ry(q5) * Rz(q6)
222 static void compose_rotation(const double *q, double rotation[3][3])
223 {
224 double arm[3][3];
225 rotation_zy(q[0], q[1] + q[2], arm);
226 double wrist[3][3];
227 double zy[3][3];
228 rotation_zy(q[3], q[4], zy);
229 double z6[3][3];
230 rotation_z(q[5], z6);
231 multiply(zy, z6, wrist);
232 multiply(arm, wrist, rotation);
233 }
234
235 // R36 = Ry(theta)^T * Rz(q1)^T * R
236 static void arm_transpose_times(double q1, double theta, const double r[3][3],
237 double out[3][3])
238 {
239 double arm[3][3];
240 rotation_zy(q1, theta, arm);
241 for(int i = 0; i < 3; ++i) {
242 for(int j = 0; j < 3; ++j) {
243 out[i][j] =
244 arm[0][i] * r[0][j] + arm[1][i] * r[1][j] + arm[2][i] * r[2][j];
245 }
246 }
247 }
248
249 static void rotation_z(double angle, double out[3][3])
250 {
251 const double c = std::cos(angle);
252 const double s = std::sin(angle);
253 out[0][0] = c;
254 out[0][1] = -s;
255 out[0][2] = 0.0;
256 out[1][0] = s;
257 out[1][1] = c;
258 out[1][2] = 0.0;
259 out[2][0] = 0.0;
260 out[2][1] = 0.0;
261 out[2][2] = 1.0;
262 }
263
264 // Rz(a) * Ry(b)
265 static void rotation_zy(double a, double b, double out[3][3])
266 {
267 const double ca = std::cos(a);
268 const double sa = std::sin(a);
269 const double cb = std::cos(b);
270 const double sb = std::sin(b);
271 out[0][0] = ca * cb;
272 out[0][1] = -sa;
273 out[0][2] = ca * sb;
274 out[1][0] = sa * cb;
275 out[1][1] = ca;
276 out[1][2] = sa * sb;
277 out[2][0] = -sb;
278 out[2][1] = 0.0;
279 out[2][2] = cb;
280 }
281
282 static void multiply(const double a[3][3], const double b[3][3], double out[3][3])
283 {
284 for(int i = 0; i < 3; ++i) {
285 for(int j = 0; j < 3; ++j) {
286 out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
287 }
288 }
289 }
290
291 double d1_ = 0.0;
292 double a2_ = 1.0;
293 double d4_ = 1.0;
294 double d6_ = 0.0;
295};
296
297} // namespace plcopen::core::kin
static constexpr std::size_t JointCount
rt::ErrorCode inverse(const Pose6 &pose, const double *seed, double max_joint_step, double *joints_out) const override
double singularity_margin(const double *q) const override
void forward(const double *q, Pose6 &pose) const override
SphericalWrist6R(double base_height, double upper_arm, double forearm, double tool)
std::size_t joint_count() const override