moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
pr2_arm_ik.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Sachin Chitta, E. Gil Jones */
36
37#include <angles/angles.h>
39#include "pr2_arm_ik.hpp"
40
41/**** List of angles (for reference) *******
42 th1 = shoulder/turret pan
43 th2 = shoulder/turret lift/pitch
44 th3 = shoulder/turret roll
45 th4 = elbow pitch
46 th5 = elbow roll
47 th6 = wrist pitch
48 th7 = wrist roll
49*****/
50using namespace angles;
52{
53namespace
54{
55rclcpp::Logger getLogger()
56{
57 return moveit::getLogger("moveit.core.moveit_constaint_samplers.test.pr2_arm_ik");
58}
59} // namespace
60
64
65bool PR2ArmIK::init(const urdf::ModelInterface& robot_model, const std::string& root_name, const std::string& tip_name)
66{
67 std::vector<urdf::Pose> link_offset;
68 int num_joints = 0;
69 urdf::LinkConstSharedPtr link = robot_model.getLink(tip_name);
70 while (link && num_joints < 7)
71 {
72 urdf::JointConstSharedPtr joint;
73 if (link->parent_joint)
74 joint = robot_model.getJoint(link->parent_joint->name);
75 if (!joint)
76 {
77 if (link->parent_joint)
78 {
79 RCLCPP_ERROR(getLogger(), "Could not find joint: %s", link->parent_joint->name.c_str());
80 }
81 else
82 {
83 RCLCPP_ERROR(getLogger(), "Link %s has no parent joint", link->name.c_str());
84 }
85 return false;
86 }
87 if (joint->type != urdf::Joint::UNKNOWN && joint->type != urdf::Joint::FIXED)
88 {
89 link_offset.push_back(link->parent_joint->parent_to_joint_origin_transform);
90 angle_multipliers_.push_back(joint->axis.x * fabs(joint->axis.x) + joint->axis.y * fabs(joint->axis.y) +
91 joint->axis.z * fabs(joint->axis.z));
92 RCLCPP_DEBUG(getLogger(), "Joint axis: %d, %f, %f, %f", 6 - num_joints, joint->axis.x, joint->axis.y,
93 joint->axis.z);
94 if (joint->type != urdf::Joint::CONTINUOUS)
95 {
96 if (joint->safety)
97 {
98 min_angles_.push_back(joint->safety->soft_lower_limit);
99 max_angles_.push_back(joint->safety->soft_upper_limit);
100 }
101 else
102 {
103 if (joint->limits)
104 {
105 min_angles_.push_back(joint->limits->lower);
106 max_angles_.push_back(joint->limits->upper);
107 }
108 else
109 {
110 min_angles_.push_back(0.0);
111 max_angles_.push_back(0.0);
112 RCLCPP_WARN(getLogger(), "No joint limits or joint '%s'", joint->name.c_str());
113 }
114 }
115 continuous_joint_.push_back(false);
116 }
117 else
118 {
119 min_angles_.push_back(-M_PI);
120 max_angles_.push_back(M_PI);
121 continuous_joint_.push_back(true);
122 }
123 addJointToChainInfo(link->parent_joint, solver_info_);
124 num_joints++;
125 }
126 link = robot_model.getLink(link->getParent()->name);
127 }
128
129 solver_info_.link_names.push_back(tip_name);
130
131 // solver_info_.link_names.push_back(tip_name);
132 // We expect order from root to tip, so reverse the order
133 std::reverse(angle_multipliers_.begin(), angle_multipliers_.end());
134 std::reverse(min_angles_.begin(), min_angles_.end());
135 std::reverse(max_angles_.begin(), max_angles_.end());
136 std::reverse(link_offset.begin(), link_offset.end());
137 std::reverse(solver_info_.limits.begin(), solver_info_.limits.end());
138 std::reverse(solver_info_.joint_names.begin(), solver_info_.joint_names.end());
139 std::reverse(solver_info_.link_names.begin(), solver_info_.link_names.end());
140 std::reverse(continuous_joint_.begin(), continuous_joint_.end());
141
142 if (num_joints != 7)
143 {
144 RCLCPP_ERROR(getLogger(), "PR2ArmIK:: Chain from %s to %s does not have 7 joints", root_name.c_str(),
145 tip_name.c_str());
146 return false;
147 }
148
149 torso_shoulder_offset_x_ = link_offset[0].position.x;
150 torso_shoulder_offset_y_ = link_offset[0].position.y;
151 torso_shoulder_offset_z_ = link_offset[0].position.z;
152 shoulder_upperarm_offset_ = distance(link_offset[1]);
153 upperarm_elbow_offset_ = distance(link_offset[3]);
154 elbow_wrist_offset_ = distance(link_offset[5]);
155 shoulder_elbow_offset_ = shoulder_upperarm_offset_ + upperarm_elbow_offset_;
156 shoulder_wrist_offset_ = shoulder_upperarm_offset_ + upperarm_elbow_offset_ + elbow_wrist_offset_;
157
158 Eigen::Isometry3f home = Eigen::Isometry3f::Identity();
159 home(0, 3) = shoulder_upperarm_offset_ + upperarm_elbow_offset_ + elbow_wrist_offset_;
160 home_inv_ = home.inverse();
161 grhs_ = home;
162 gf_ = home_inv_;
163 solution_.resize(NUM_JOINTS_ARM7DOF);
164 return true;
165}
166
167void PR2ArmIK::addJointToChainInfo(const urdf::JointConstSharedPtr& joint, moveit_msgs::msg::KinematicSolverInfo& info)
168{
169 moveit_msgs::msg::JointLimits limit;
170 info.joint_names.push_back(joint->name); // Joints are coming in reverse order
171
172 if (joint->type != urdf::Joint::CONTINUOUS)
173 {
174 if (joint->safety)
175 {
176 limit.min_position = joint->safety->soft_lower_limit;
177 limit.max_position = joint->safety->soft_upper_limit;
178 limit.has_position_limits = true;
179 }
180 else if (joint->limits)
181 {
182 limit.min_position = joint->limits->lower;
183 limit.max_position = joint->limits->upper;
184 limit.has_position_limits = true;
185 }
186 else
187 {
188 limit.has_position_limits = false;
189 }
190 }
191 else
192 {
193 limit.min_position = -M_PI;
194 limit.max_position = M_PI;
195 limit.has_position_limits = false;
196 }
197 if (joint->limits)
198 {
199 limit.max_velocity = joint->limits->velocity;
200 limit.has_velocity_limits = 1;
201 }
202 else
203 {
204 limit.has_velocity_limits = 0;
205 }
206 info.limits.push_back(limit);
207}
208
209void PR2ArmIK::getSolverInfo(moveit_msgs::msg::KinematicSolverInfo& info)
210{
211 info = solver_info_;
212}
213
214void PR2ArmIK::computeIKShoulderPan(const Eigen::Isometry3f& g_in, double t1_in,
215 std::vector<std::vector<double> >& solution) const
216{
217 // t1 = shoulder/turret pan is specified
218 // solution_ik_.resize(0);
219 std::vector<double> solution_ik(NUM_JOINTS_ARM7DOF, 0.0);
220 Eigen::Isometry3f g = g_in;
221 Eigen::Isometry3f gf_local = home_inv_;
222 Eigen::Isometry3f grhs_local = home_inv_;
223 // First bring everything into the arm frame
224 g(0, 3) = g_in(0, 3) - torso_shoulder_offset_x_;
225 g(1, 3) = g_in(1, 3) - torso_shoulder_offset_y_;
226 g(2, 3) = g_in(2, 3) - torso_shoulder_offset_z_;
227
228 double t1 = angles::normalize_angle(t1_in);
229 if (!checkJointLimits(t1, 0))
230 return;
231
232 double cost1, cost2, cost3, cost4;
233 double sint1, sint2, sint3, sint4;
234
235 gf_local = g * home_inv_;
236
237 cost1 = cos(t1);
238 sint1 = sin(t1);
239
240 double t2(0), t3(0), t4(0), t5(0), t6(0), t7(0);
241
242 double at(0), bt(0), ct(0);
243
244 double theta2[2], theta3[2], theta4[2], theta5[2], theta6[4], theta7[2];
245
246 double sopx = shoulder_upperarm_offset_ * cost1;
247 double sopy = shoulder_upperarm_offset_ * sint1;
248 double sopz = 0;
249
250 double x = g(0, 3);
251 double y = g(1, 3);
252 double z = g(2, 3);
253
254 double dx = x - sopx;
255 double dy = y - sopy;
256 double dz = z - sopz;
257
258 double dd = dx * dx + dy * dy + dz * dz;
259
260 double numerator =
261 dd - shoulder_upperarm_offset_ * shoulder_upperarm_offset_ +
262 2 * shoulder_upperarm_offset_ * shoulder_elbow_offset_ - 2 * shoulder_elbow_offset_ * shoulder_elbow_offset_ +
263 2 * shoulder_elbow_offset_ * shoulder_wrist_offset_ - shoulder_wrist_offset_ * shoulder_wrist_offset_;
264 double denominator =
265 2 * (shoulder_upperarm_offset_ - shoulder_elbow_offset_) * (shoulder_elbow_offset_ - shoulder_wrist_offset_);
266
267 double acos_term = numerator / denominator;
268
269 if (acos_term > 1.0 || acos_term < -1.0)
270 return;
271
272 double acos_angle = acos(acos_term);
273
274 theta4[0] = acos_angle;
275 theta4[1] = -acos_angle;
276
277#ifdef DEBUG
278 std::cout << "ComputeIK::theta3:" << numerator << ',' << denominator << ",\n" << theta4[0] << '\n';
279#endif
280
281 for (double theta : theta4)
282 {
283 t4 = theta;
284 cost4 = cos(t4);
285 sint4 = sin(t4);
286
287#ifdef DEBUG
288 std::cout << "t4 " << t4 << '\n';
289#endif
290 if (std::isnan(t4))
291 continue;
292
293 if (!checkJointLimits(t4, 3))
294 continue;
295
296 at = x * cost1 + y * sint1 - shoulder_upperarm_offset_;
297 bt = -z;
298 ct = -shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
299 (shoulder_wrist_offset_ - shoulder_elbow_offset_) * cos(t4);
300
301 if (!solveCosineEqn(at, bt, ct, theta2[0], theta2[1]))
302 continue;
303
304 for (double theta : theta2)
305 {
306 t2 = theta;
307 if (!checkJointLimits(t2, 1))
308 continue;
309
310#ifdef DEBUG
311 std::cout << "t2 " << t2 << '\n';
312#endif
313 sint2 = sin(t2);
314 cost2 = cos(t2);
315
316 at = sint1 * (shoulder_elbow_offset_ - shoulder_wrist_offset_) * sint2 * sint4;
317 bt = (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cost1 * sint4;
318 ct = y - (shoulder_upperarm_offset_ + cost2 * (-shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
319 (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cos(t4))) *
320 sint1;
321 if (!solveCosineEqn(at, bt, ct, theta3[0], theta3[1]))
322 continue;
323
324 for (double theta : theta3)
325 {
326 t3 = theta;
327
328 if (!checkJointLimits(angles::normalize_angle(t3), 2))
329 continue;
330
331 sint3 = sin(t3);
332 cost3 = cos(t3);
333#ifdef DEBUG
334 std::cout << "t3 " << t3 << '\n';
335#endif
336 if (fabs((shoulder_upperarm_offset_ - shoulder_elbow_offset_ +
337 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost4) *
338 sint2 +
339 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost2 * cost3 * sint4 - z) > IK_EPS)
340 continue;
341
342 if (fabs((shoulder_elbow_offset_ - shoulder_wrist_offset_) * sint1 * sint3 * sint4 +
343 cost1 * (shoulder_upperarm_offset_ +
344 cost2 * (-shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
345 (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cost4) +
346 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost3 * sint2 * sint4) -
347 x) > IK_EPS)
348 continue;
349
350 grhs_local(0, 0) =
351 cost4 * (gf_local(0, 0) * cost1 * cost2 + gf_local(1, 0) * cost2 * sint1 - gf_local(2, 0) * sint2) -
352 (gf_local(2, 0) * cost2 * cost3 + cost3 * (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 +
353 (-(gf_local(1, 0) * cost1) + gf_local(0, 0) * sint1) * sint3) *
354 sint4;
355
356 grhs_local(0, 1) =
357 cost4 * (gf_local(0, 1) * cost1 * cost2 + gf_local(1, 1) * cost2 * sint1 - gf_local(2, 1) * sint2) -
358 (gf_local(2, 1) * cost2 * cost3 + cost3 * (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 +
359 (-(gf_local(1, 1) * cost1) + gf_local(0, 1) * sint1) * sint3) *
360 sint4;
361
362 grhs_local(0, 2) =
363 cost4 * (gf_local(0, 2) * cost1 * cost2 + gf_local(1, 2) * cost2 * sint1 - gf_local(2, 2) * sint2) -
364 (gf_local(2, 2) * cost2 * cost3 + cost3 * (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 +
365 (-(gf_local(1, 2) * cost1) + gf_local(0, 2) * sint1) * sint3) *
366 sint4;
367
368 grhs_local(1, 0) = cost3 * (gf_local(1, 0) * cost1 - gf_local(0, 0) * sint1) + gf_local(2, 0) * cost2 * sint3 +
369 (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 * sint3;
370
371 grhs_local(1, 1) = cost3 * (gf_local(1, 1) * cost1 - gf_local(0, 1) * sint1) + gf_local(2, 1) * cost2 * sint3 +
372 (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 * sint3;
373
374 grhs_local(1, 2) = cost3 * (gf_local(1, 2) * cost1 - gf_local(0, 2) * sint1) + gf_local(2, 2) * cost2 * sint3 +
375 (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 * sint3;
376
377 grhs_local(2, 0) =
378 cost4 *
379 (gf_local(2, 0) * cost2 * cost3 + cost3 * (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 +
380 (-(gf_local(1, 0) * cost1) + gf_local(0, 0) * sint1) * sint3) +
381 (gf_local(0, 0) * cost1 * cost2 + gf_local(1, 0) * cost2 * sint1 - gf_local(2, 0) * sint2) * sint4;
382
383 grhs_local(2, 1) =
384 cost4 *
385 (gf_local(2, 1) * cost2 * cost3 + cost3 * (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 +
386 (-(gf_local(1, 1) * cost1) + gf_local(0, 1) * sint1) * sint3) +
387 (gf_local(0, 1) * cost1 * cost2 + gf_local(1, 1) * cost2 * sint1 - gf_local(2, 1) * sint2) * sint4;
388
389 grhs_local(2, 2) =
390 cost4 *
391 (gf_local(2, 2) * cost2 * cost3 + cost3 * (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 +
392 (-(gf_local(1, 2) * cost1) + gf_local(0, 2) * sint1) * sint3) +
393 (gf_local(0, 2) * cost1 * cost2 + gf_local(1, 2) * cost2 * sint1 - gf_local(2, 2) * sint2) * sint4;
394
395 double val1 = sqrt(grhs_local(0, 1) * grhs_local(0, 1) + grhs_local(0, 2) * grhs_local(0, 2));
396 double val2 = grhs_local(0, 0);
397
398 theta6[0] = atan2(val1, val2);
399 theta6[1] = atan2(-val1, val2);
400
401 // theta6[3] = M_PI + theta6[0];
402 // theta6[4] = M_PI + theta6[1];
403
404 for (int mm = 0; mm < 2; ++mm)
405 {
406 t6 = theta6[mm];
407 if (!checkJointLimits(angles::normalize_angle(t6), 5))
408 continue;
409
410#ifdef DEBUG
411 std::cout << "t6 " << t6 << '\n';
412#endif
413 if (fabs(cos(t6) - grhs_local(0, 0)) > IK_EPS)
414 continue;
415
416 if (fabs(sin(t6)) < IK_EPS)
417 {
418 // std::cout << "Singularity" << '\n';
419 theta5[0] = acos(grhs_local(1, 1)) / 2.0;
420 theta7[0] = theta7[0];
421 theta7[1] = M_PI + theta7[0];
422 theta5[1] = theta7[1];
423 }
424 else
425 {
426 theta7[0] = atan2(grhs_local(0, 1), grhs_local(0, 2));
427 theta5[0] = atan2(grhs_local(1, 0), -grhs_local(2, 0));
428 theta7[1] = M_PI + theta7[0];
429 theta5[1] = M_PI + theta5[0];
430 }
431#ifdef DEBUG
432 std::cout << "theta1: " << t1 << '\n';
433 std::cout << "theta2: " << t2 << '\n';
434 std::cout << "theta3: " << t3 << '\n';
435 std::cout << "theta4: " << t4 << '\n';
436 std::cout << "theta5: " << t5 << '\n';
437 std::cout << "theta6: " << t6 << '\n';
438 std::cout << "theta7: " << t7 << '\n' << '\n' << '\n';
439#endif
440 for (int lll = 0; lll < 2; ++lll)
441 {
442 t5 = theta5[lll];
443 t7 = theta7[lll];
444 if (!checkJointLimits(t5, 4))
445 continue;
446 if (!checkJointLimits(t7, 6))
447 continue;
448
449#ifdef DEBUG
450 std::cout << "t5" << t5 << '\n';
451 std::cout << "t7" << t7 << '\n';
452#endif
453 if (fabs(sin(t6) * sin(t7) - grhs_local(0, 1)) > IK_EPS ||
454 fabs(cos(t7) * sin(t6) - grhs_local(0, 2)) > IK_EPS)
455 continue;
456
457 solution_ik[0] = normalize_angle(t1) * angle_multipliers_[0];
458 solution_ik[1] = normalize_angle(t2) * angle_multipliers_[1];
459 solution_ik[2] = normalize_angle(t3) * angle_multipliers_[2];
460 solution_ik[3] = normalize_angle(t4) * angle_multipliers_[3];
461 solution_ik[4] = normalize_angle(t5) * angle_multipliers_[4];
462 solution_ik[5] = normalize_angle(t6) * angle_multipliers_[5];
463 solution_ik[6] = normalize_angle(t7) * angle_multipliers_[6];
464 solution.push_back(solution_ik);
465
466#ifdef DEBUG
467 std::cout << "SOLN " << solution_ik[0] << ' ' << solution_ik[1] << ' ' << solution_ik[2] << ' '
468 << solution_ik[3] << ' ' << solution_ik[4] << ' ' << solution_ik[5] << ' ' << solution_ik[6]
469 << '\n'
470 << '\n';
471#endif
472 }
473 }
474 }
475 }
476 }
477}
478
479void PR2ArmIK::computeIKShoulderRoll(const Eigen::Isometry3f& g_in, const double t3,
480 std::vector<std::vector<double> >& solution) const
481{
482 std::vector<double> solution_ik(NUM_JOINTS_ARM7DOF, 0.0);
483 // ROS_INFO(" ");
484 // solution_ik_.clear();
485 // ROS_INFO("Solution IK size: %d",solution_ik_.size());
486 // for(unsigned int i=0; i < solution_ik_.size(); ++i)
487 // {
488 // solution_ik_[i].clear();
489 // }
490 // if(!solution_ik_.empty())
491 // solution_ik_.resize(0);
492 // t3 = shoulder/turret roll is specified
493 Eigen::Isometry3f g = g_in;
494 Eigen::Isometry3f gf_local = home_inv_;
495 Eigen::Isometry3f grhs_local = home_inv_;
496 // First bring everything into the arm frame
497 g(0, 3) = g_in(0, 3) - torso_shoulder_offset_x_;
498 g(1, 3) = g_in(1, 3) - torso_shoulder_offset_y_;
499 g(2, 3) = g_in(2, 3) - torso_shoulder_offset_z_;
500
501 if (!checkJointLimits(t3, 2))
502 {
503 return;
504 }
505 double x = g(0, 3);
506 double y = g(1, 3);
507 double z = g(2, 3);
508 double cost1, cost2, cost3, cost4;
509 double sint1, sint2, sint3, sint4;
510
511 gf_local = g * home_inv_;
512
513 cost3 = cos(t3);
514 sint3 = sin(t3);
515
516 double t1(0), t2(0), t4(0), t5(0), t6(0), t7(0);
517
518 double at(0), bt(0), ct(0);
519
520 double theta1[2], theta2[2], theta4[4], theta5[2], theta6[4], theta7[2];
521
522 double c0 = -sin(-t3) * elbow_wrist_offset_;
523 double c1 = -cos(-t3) * elbow_wrist_offset_;
524
525 double d0 = 4 * shoulder_upperarm_offset_ * shoulder_upperarm_offset_ *
526 (upperarm_elbow_offset_ * upperarm_elbow_offset_ + c1 * c1 - z * z);
527 double d1 = 8 * shoulder_upperarm_offset_ * shoulder_upperarm_offset_ * upperarm_elbow_offset_ * elbow_wrist_offset_;
528 double d2 =
529 4 * shoulder_upperarm_offset_ * shoulder_upperarm_offset_ * (elbow_wrist_offset_ * elbow_wrist_offset_ - c1 * c1);
530
531 double b0 = x * x + y * y + z * z - shoulder_upperarm_offset_ * shoulder_upperarm_offset_ -
532 upperarm_elbow_offset_ * upperarm_elbow_offset_ - c0 * c0 - c1 * c1;
533 double b1 = -2 * upperarm_elbow_offset_ * elbow_wrist_offset_;
534
535 if (!solveQuadratic(b1 * b1 - d2, 2 * b0 * b1 - d1, b0 * b0 - d0, &theta4[0], &theta4[1]))
536 {
537#ifdef DEBUG
538 printf("No solution to quadratic eqn\n");
539#endif
540 return;
541 }
542 theta4[0] = acos(theta4[0]);
543 theta4[2] = acos(theta4[1]);
544 theta4[1] = -theta4[0];
545 theta4[3] = -theta4[2];
546
547 for (double theta : theta4)
548 {
549 t4 = theta;
550
551 if (!checkJointLimits(t4, 3))
552 {
553 continue;
554 }
555 cost4 = cos(t4);
556 sint4 = sin(t4);
557#ifdef DEBUG
558 std::cout << "t4 " << t4 << '\n';
559#endif
560 if (std::isnan(t4))
561 continue;
562 at = cos(t3) * sin(t4) * (shoulder_elbow_offset_ - shoulder_wrist_offset_);
563 bt = (shoulder_upperarm_offset_ - shoulder_elbow_offset_ +
564 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cos(t4));
565 ct = z;
566
567 if (!solveCosineEqn(at, bt, ct, theta2[0], theta2[1]))
568 continue;
569
570 for (double theta : theta2)
571 {
572 t2 = theta;
573#ifdef DEBUG
574 std::cout << "t2 " << t2 << '\n';
575#endif
576 if (!checkJointLimits(t2, 1))
577 {
578 continue;
579 }
580
581 sint2 = sin(t2);
582 cost2 = cos(t2);
583
584 at = -y;
585 bt = x;
586 ct = (shoulder_elbow_offset_ - shoulder_wrist_offset_) * sin(t3) * sin(t4);
587 if (!solveCosineEqn(at, bt, ct, theta1[0], theta1[1]))
588 {
589#ifdef DEBUG
590 std::cout << "could not solve cosine equation for t1" << '\n';
591#endif
592 continue;
593 }
594
595 for (double theta : theta1)
596 {
597 t1 = theta;
598#ifdef DEBUG
599 std::cout << "t1 " << t1 << '\n';
600#endif
601 if (!checkJointLimits(t1, 0))
602 {
603 continue;
604 }
605 sint1 = sin(t1);
606 cost1 = cos(t1);
607 if (fabs((shoulder_upperarm_offset_ - shoulder_elbow_offset_ +
608 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost4) *
609 sint2 +
610 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost2 * cost3 * sint4 - z) > IK_EPS)
611 {
612#ifdef DEBUG
613 printf("z value not matched %f\n",
614 fabs((shoulder_upperarm_offset_ - shoulder_elbow_offset_ +
615 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost4) *
616 sint2 +
617 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost2 * cost3 * sint4 - z));
618#endif
619 continue;
620 }
621 if (fabs((shoulder_elbow_offset_ - shoulder_wrist_offset_) * sint1 * sint3 * sint4 +
622 cost1 * (shoulder_upperarm_offset_ +
623 cost2 * (-shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
624 (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cost4) +
625 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost3 * sint2 * sint4) -
626 x) > IK_EPS)
627 {
628#ifdef DEBUG
629 printf("x value not matched by %f\n",
630 fabs((shoulder_elbow_offset_ - shoulder_wrist_offset_) * sint1 * sint3 * sint4 +
631 cost1 * (shoulder_upperarm_offset_ +
632 cost2 * (-shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
633 (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cost4) +
634 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost3 * sint2 * sint4) -
635 x));
636#endif
637 continue;
638 }
639 if (fabs(-(shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost1 * sint3 * sint4 +
640 sint1 * (shoulder_upperarm_offset_ +
641 cost2 * (-shoulder_upperarm_offset_ + shoulder_elbow_offset_ +
642 (-shoulder_elbow_offset_ + shoulder_wrist_offset_) * cost4) +
643 (shoulder_elbow_offset_ - shoulder_wrist_offset_) * cost3 * sint2 * sint4) -
644 y) > IK_EPS)
645 {
646#ifdef DEBUG
647 printf("y value not matched\n");
648#endif
649 continue;
650 }
651 grhs_local(0, 0) =
652 cost4 * (gf_local(0, 0) * cost1 * cost2 + gf_local(1, 0) * cost2 * sint1 - gf_local(2, 0) * sint2) -
653 (gf_local(2, 0) * cost2 * cost3 + cost3 * (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 +
654 (-(gf_local(1, 0) * cost1) + gf_local(0, 0) * sint1) * sint3) *
655 sint4;
656
657 grhs_local(0, 1) =
658 cost4 * (gf_local(0, 1) * cost1 * cost2 + gf_local(1, 1) * cost2 * sint1 - gf_local(2, 1) * sint2) -
659 (gf_local(2, 1) * cost2 * cost3 + cost3 * (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 +
660 (-(gf_local(1, 1) * cost1) + gf_local(0, 1) * sint1) * sint3) *
661 sint4;
662
663 grhs_local(0, 2) =
664 cost4 * (gf_local(0, 2) * cost1 * cost2 + gf_local(1, 2) * cost2 * sint1 - gf_local(2, 2) * sint2) -
665 (gf_local(2, 2) * cost2 * cost3 + cost3 * (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 +
666 (-(gf_local(1, 2) * cost1) + gf_local(0, 2) * sint1) * sint3) *
667 sint4;
668
669 grhs_local(1, 0) = cost3 * (gf_local(1, 0) * cost1 - gf_local(0, 0) * sint1) + gf_local(2, 0) * cost2 * sint3 +
670 (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 * sint3;
671
672 grhs_local(1, 1) = cost3 * (gf_local(1, 1) * cost1 - gf_local(0, 1) * sint1) + gf_local(2, 1) * cost2 * sint3 +
673 (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 * sint3;
674
675 grhs_local(1, 2) = cost3 * (gf_local(1, 2) * cost1 - gf_local(0, 2) * sint1) + gf_local(2, 2) * cost2 * sint3 +
676 (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 * sint3;
677
678 grhs_local(2, 0) =
679 cost4 *
680 (gf_local(2, 0) * cost2 * cost3 + cost3 * (gf_local(0, 0) * cost1 + gf_local(1, 0) * sint1) * sint2 +
681 (-(gf_local(1, 0) * cost1) + gf_local(0, 0) * sint1) * sint3) +
682 (gf_local(0, 0) * cost1 * cost2 + gf_local(1, 0) * cost2 * sint1 - gf_local(2, 0) * sint2) * sint4;
683
684 grhs_local(2, 1) =
685 cost4 *
686 (gf_local(2, 1) * cost2 * cost3 + cost3 * (gf_local(0, 1) * cost1 + gf_local(1, 1) * sint1) * sint2 +
687 (-(gf_local(1, 1) * cost1) + gf_local(0, 1) * sint1) * sint3) +
688 (gf_local(0, 1) * cost1 * cost2 + gf_local(1, 1) * cost2 * sint1 - gf_local(2, 1) * sint2) * sint4;
689
690 grhs_local(2, 2) =
691 cost4 *
692 (gf_local(2, 2) * cost2 * cost3 + cost3 * (gf_local(0, 2) * cost1 + gf_local(1, 2) * sint1) * sint2 +
693 (-(gf_local(1, 2) * cost1) + gf_local(0, 2) * sint1) * sint3) +
694 (gf_local(0, 2) * cost1 * cost2 + gf_local(1, 2) * cost2 * sint1 - gf_local(2, 2) * sint2) * sint4;
695
696 double val1 = sqrt(grhs_local(0, 1) * grhs_local(0, 1) + grhs_local(0, 2) * grhs_local(0, 2));
697 double val2 = grhs_local(0, 0);
698
699 theta6[0] = atan2(val1, val2);
700 theta6[1] = atan2(-val1, val2);
701
702 for (int mm = 0; mm < 2; ++mm)
703 {
704 t6 = theta6[mm];
705#ifdef DEBUG
706 std::cout << "t6 " << t6 << '\n';
707#endif
708 if (!checkJointLimits(t6, 5))
709 {
710 continue;
711 }
712
713 if (fabs(cos(t6) - grhs_local(0, 0)) > IK_EPS)
714 continue;
715
716 if (fabs(sin(t6)) < IK_EPS)
717 {
718 // std::cout << "Singularity" << '\n';
719 theta5[0] = acos(grhs_local(1, 1)) / 2.0;
720 theta7[0] = theta5[0];
721 // theta7[1] = M_PI+theta7[0];
722 // theta5[1] = theta7[1];
723 }
724 else
725 {
726 theta7[0] = atan2(grhs_local(0, 1) / sin(t6), grhs_local(0, 2) / sin(t6));
727 theta5[0] = atan2(grhs_local(1, 0) / sin(t6), -grhs_local(2, 0) / sin(t6));
728 // theta7[1] = M_PI+theta7[0];
729 // theta5[1] = M_PI+theta5[0];
730 }
731 for (int lll = 0; lll < 1; ++lll)
732 {
733 t5 = theta5[lll];
734 t7 = theta7[lll];
735
736 if (!checkJointLimits(t5, 4))
737 {
738 continue;
739 }
740 if (!checkJointLimits(t7, 6))
741 {
742 continue;
743 }
744
745#ifdef DEBUG
746 std::cout << "t5 " << t5 << '\n';
747 std::cout << "t7 " << t7 << '\n';
748#endif
749 // if(fabs(sin(t6)*sin(t7)-grhs_local(0,1)) > IK_EPS || fabs(cos(t7)*sin(t6)-grhs_local(0,2)) > IK_EPS)
750 // continue;
751
752#ifdef DEBUG
753 std::cout << "theta1: " << t1 << '\n';
754 std::cout << "theta2: " << t2 << '\n';
755 std::cout << "theta3: " << t3 << '\n';
756 std::cout << "theta4: " << t4 << '\n';
757 std::cout << "theta5: " << t5 << '\n';
758 std::cout << "theta6: " << t6 << '\n';
759 std::cout << "theta7: " << t7 << '\n' << '\n' << '\n';
760#endif
761
762 solution_ik[0] = normalize_angle(t1 * angle_multipliers_[0]);
763 solution_ik[1] = normalize_angle(t2 * angle_multipliers_[1]);
764 solution_ik[2] = t3 * angle_multipliers_[2];
765 solution_ik[3] = normalize_angle(t4 * angle_multipliers_[3]);
766 solution_ik[4] = normalize_angle(t5 * angle_multipliers_[4]);
767 solution_ik[5] = normalize_angle(t6 * angle_multipliers_[5]);
768 solution_ik[6] = normalize_angle(t7 * angle_multipliers_[6]);
769 solution.push_back(solution_ik);
770#ifdef DEBUG
771 std::cout << "SOLN " << solution_ik[0] << ' ' << solution_ik[1] << ' ' << solution_ik[2] << ' '
772 << solution_ik[3] << ' ' << solution_ik[4] << ' ' << solution_ik[5] << ' ' << solution_ik[6]
773 << '\n'
774 << '\n';
775#endif
776 }
777 }
778 }
779 }
780 }
781}
782
783bool PR2ArmIK::checkJointLimits(const std::vector<double>& joint_values) const
784{
785 for (int i = 0; i < NUM_JOINTS_ARM7DOF; ++i)
786 {
787 if (!checkJointLimits(angles::normalize_angle(joint_values[i] * angle_multipliers_[i]), i))
788 {
789 return false;
790 }
791 }
792 return true;
793}
794
795bool PR2ArmIK::checkJointLimits(const double joint_value, const int joint_num) const
796{
797 double jv;
798 if (continuous_joint_[joint_num])
799 {
800 jv = angles::normalize_angle(joint_value * angle_multipliers_[joint_num]);
801 }
802 else if (joint_num == 2)
803 {
804 jv = joint_value * angle_multipliers_[joint_num];
805 }
806 else
807 {
808 jv = angles::normalize_angle(joint_value * angle_multipliers_[joint_num]);
809 }
810
811 return jv >= min_angles_[joint_num] && jv <= max_angles_[joint_num];
812}
813} // namespace pr2_arm_kinematics
void computeIKShoulderRoll(const Eigen::Isometry3f &g_in, double shoulder_roll_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder roll angle. h
bool init(const urdf::ModelInterface &robot_model, const std::string &root_name, const std::string &tip_name)
Initialize the solver by providing a urdf::Model and a root and tip name.
void computeIKShoulderPan(const Eigen::Isometry3f &g_in, double shoulder_pan_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder pan angle.
moveit_msgs::msg::KinematicSolverInfo solver_info_
get chain information about the arm.
void getSolverInfo(moveit_msgs::msg::KinematicSolverInfo &info)
get chain information about the arm. This populates the IK query response, filling in joint level inf...
PR2ArmIK()
Inverse kinematics for the PR2 arm.
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition logger.cpp:106
double distance(const urdf::Pose &transform)
bool solveQuadratic(double a, double b, double c, double *x1, double *x2)
bool solveCosineEqn(double a, double b, double c, double &soln1, double &soln2)