-
Notifications
You must be signed in to change notification settings - Fork 7
/
moveit_opw_kinematics_plugin.cpp
473 lines (404 loc) · 18.2 KB
/
moveit_opw_kinematics_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <class_loader/class_loader.hpp>
#include <moveit_opw_kinematics_plugin/moveit_opw_kinematics_plugin.h>
// URDF, SRDF
#include <srdfdom/model.h>
#include <urdf_model/model.h>
#include <moveit/kinematics_base/kinematics_base.h>
#include <moveit/rdf_loader/rdf_loader.h>
#include <moveit/robot_state/conversions.h>
// Eigen
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <eigen_conversions/eigen_msg.h>
// OPW kinematics
#include "opw_kinematics/opw_io.h"
#include "opw_kinematics/opw_kinematics.h"
#include "opw_kinematics/opw_utilities.h"
// register OPWKinematics as a KinematicsBase implementation
CLASS_LOADER_REGISTER_CLASS(moveit_opw_kinematics_plugin::MoveItOPWKinematicsPlugin, kinematics::KinematicsBase)
namespace moveit_opw_kinematics_plugin
{
using kinematics::KinematicsResult;
MoveItOPWKinematicsPlugin::MoveItOPWKinematicsPlugin() : active_(false)
{
}
bool MoveItOPWKinematicsPlugin::initialize(const std::string& robot_description, const std::string& group_name,
const std::string& base_frame, const std::vector<std::string>& tip_frames,
double search_discretization)
{
bool debug = false;
ROS_INFO_STREAM_NAMED("opw", "MoveItOPWKinematicsPlugin initializing");
setValues(robot_description, group_name, base_frame, tip_frames, search_discretization);
rdf_loader::RDFLoader rdf_loader(robot_description_);
const srdf::ModelSharedPtr& srdf = rdf_loader.getSRDF();
const urdf::ModelInterfaceSharedPtr& urdf_model = rdf_loader.getURDF();
if (!urdf_model || !srdf)
{
ROS_ERROR_NAMED("opw", "URDF and SRDF must be loaded for SRV kinematics "
"solver to work."); // TODO: is this true?
return false;
}
robot_model_.reset(new robot_model::RobotModel(urdf_model, srdf));
joint_model_group_ = robot_model_->getJointModelGroup(group_name);
if (!joint_model_group_)
return false;
if (debug)
{
std::cout << std::endl
<< "Joint Model Variable Names: "
"------------------------------------------- "
<< std::endl;
const std::vector<std::string> jm_names = joint_model_group_->getVariableNames();
std::copy(jm_names.begin(), jm_names.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << std::endl;
}
// Get the dimension of the planning group
dimension_ = joint_model_group_->getVariableCount();
ROS_INFO_STREAM_NAMED("opw", "Dimension planning group '"
<< group_name << "': " << dimension_
<< ". Active Joints Models: " << joint_model_group_->getActiveJointModels().size()
<< ". Mimic Joint Models: " << joint_model_group_->getMimicJointModels().size());
// Copy joint names
for (std::size_t i = 0; i < joint_model_group_->getJointModels().size(); ++i)
{
ik_group_info_.joint_names.push_back(joint_model_group_->getJointModelNames()[i]);
}
if (debug)
{
ROS_ERROR_STREAM_NAMED("opw", "tip links available:");
std::copy(tip_frames_.begin(), tip_frames_.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
// Make sure all the tip links are in the link_names vector
for (std::size_t i = 0; i < tip_frames_.size(); ++i)
{
if (!joint_model_group_->hasLinkModel(tip_frames_[i]))
{
ROS_ERROR_NAMED("opw", "Could not find tip name '%s' in joint group '%s'", tip_frames_[i].c_str(),
group_name.c_str());
return false;
}
ik_group_info_.link_names.push_back(tip_frames_[i]);
}
// Choose what ROS service to send IK requests to
ROS_DEBUG_STREAM_NAMED("opw", "Looking for ROS service name on rosparam server with param: "
<< "/kinematics_solver_service_name");
std::string ik_service_name;
lookupParam("kinematics_solver_service_name", ik_service_name, std::string("solve_ik"));
// Setup the joint state groups that we need
robot_state_.reset(new robot_state::RobotState(robot_model_));
robot_state_->setToDefaultValues();
// set geometric parameters for opw model
if (!setOPWParameters())
{
ROS_ERROR_STREAM_NAMED("opw", "Could not load opw parameters. Check kinematics.yaml.");
return false;
}
active_ = true;
ROS_DEBUG_NAMED("opw", "ROS service-based kinematics solver initialized");
return true;
}
bool MoveItOPWKinematicsPlugin::setRedundantJoints(const std::vector<unsigned int>& redundant_joints)
{
if (num_possible_redundant_joints_ < 0)
{
ROS_ERROR_NAMED("srv", "This group cannot have redundant joints");
return false;
}
if (redundant_joints.size() > num_possible_redundant_joints_)
{
ROS_ERROR_NAMED("srv", "This group can only have %d redundant joints", num_possible_redundant_joints_);
return false;
}
return true;
}
bool MoveItOPWKinematicsPlugin::isRedundantJoint(unsigned int index) const
{
for (std::size_t j = 0; j < redundant_joint_indices_.size(); ++j)
if (redundant_joint_indices_[j] == index)
return true;
return false;
}
int MoveItOPWKinematicsPlugin::getJointIndex(const std::string& name) const
{
for (unsigned int i = 0; i < ik_group_info_.joint_names.size(); i++)
{
if (ik_group_info_.joint_names[i] == name)
return i;
}
return -1;
}
bool MoveItOPWKinematicsPlugin::timedOut(const ros::WallTime& start_time, double duration) const
{
return ((ros::WallTime::now() - start_time).toSec() >= duration);
}
bool MoveItOPWKinematicsPlugin::getPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, std::vector<double>& solution,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
const IKCallbackFn solution_callback = 0;
std::vector<double> consistency_limits;
return searchPositionIK(ik_pose, ik_seed_state, default_timeout_, solution, solution_callback, error_code,
consistency_limits, options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, double timeout,
std::vector<double>& solution,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
const IKCallbackFn solution_callback = 0;
std::vector<double> consistency_limits;
return searchPositionIK(ik_pose, ik_seed_state, timeout, solution, solution_callback, error_code, consistency_limits,
options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, double timeout,
const std::vector<double>& consistency_limits,
std::vector<double>& solution,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
const IKCallbackFn solution_callback = 0;
return searchPositionIK(ik_pose, ik_seed_state, timeout, solution, solution_callback, error_code, consistency_limits,
options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, double timeout,
std::vector<double>& solution, const IKCallbackFn& solution_callback,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
std::vector<double> consistency_limits;
return searchPositionIK(ik_pose, ik_seed_state, timeout, solution, solution_callback, error_code, consistency_limits,
options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, double timeout,
const std::vector<double>& consistency_limits,
std::vector<double>& solution, const IKCallbackFn& solution_callback,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
return searchPositionIK(ik_pose, ik_seed_state, timeout, solution, solution_callback, error_code, consistency_limits,
options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const geometry_msgs::Pose& ik_pose,
const std::vector<double>& ik_seed_state, double timeout,
std::vector<double>& solution, const IKCallbackFn& solution_callback,
moveit_msgs::MoveItErrorCodes& error_code,
const std::vector<double>& consistency_limits,
const kinematics::KinematicsQueryOptions& options) const
{
// Convert single pose into a vector of one pose
std::vector<geometry_msgs::Pose> ik_poses;
ik_poses.push_back(ik_pose);
return searchPositionIK(ik_poses, ik_seed_state, timeout, consistency_limits, solution, solution_callback, error_code,
options);
}
bool MoveItOPWKinematicsPlugin::searchPositionIK(const std::vector<geometry_msgs::Pose>& ik_poses,
const std::vector<double>& ik_seed_state, double timeout,
const std::vector<double>& consistency_limits,
std::vector<double>& solution, const IKCallbackFn& solution_callback,
moveit_msgs::MoveItErrorCodes& error_code,
const kinematics::KinematicsQueryOptions& options) const
{
// Check if active
if (!active_)
{
ROS_ERROR_NAMED("opw", "kinematics not active");
error_code.val = error_code.NO_IK_SOLUTION;
return false;
}
// Check if seed state correct
if (ik_seed_state.size() != dimension_)
{
ROS_ERROR_STREAM_NAMED("opw", "Seed state must have size " << dimension_ << " instead of size "
<< ik_seed_state.size());
error_code.val = error_code.NO_IK_SOLUTION;
return false;
}
// Check that we have the same number of poses as tips
if (tip_frames_.size() != ik_poses.size())
{
ROS_ERROR_STREAM_NAMED("opw", "Mismatched number of pose requests (" << ik_poses.size() << ") to tip frames ("
<< tip_frames_.size()
<< ") in searchPositionIK");
error_code.val = error_code.NO_IK_SOLUTION;
return false;
}
Eigen::Affine3d pose;
tf::poseMsgToEigen(ik_poses[0], pose);
if (!getIK(pose, ik_seed_state, solution))
{
ROS_ERROR_STREAM_NAMED("opw", "Failed to find IK solution");
error_code.val = error_code.NO_IK_SOLUTION;
return false;
}
return true;
}
bool MoveItOPWKinematicsPlugin::getPositionIK(const std::vector<geometry_msgs::Pose>& ik_poses,
const std::vector<double>& ik_seed_state,
std::vector<std::vector<double>>& solutions, KinematicsResult& result,
const kinematics::KinematicsQueryOptions& options) const
{
if (ik_poses.size() > 1 || ik_poses.size() == 0)
{
ROS_ERROR_STREAM_NAMED("opw", "You can only get all solutions for a single pose.");
return false;
}
Eigen::Affine3d pose;
tf::poseMsgToEigen(ik_poses[0], pose);
return getAllIK(pose, solutions);
}
bool MoveItOPWKinematicsPlugin::getPositionFK(const std::vector<std::string>& link_names,
const std::vector<double>& joint_angles,
std::vector<geometry_msgs::Pose>& poses) const
{
if (!active_)
{
ROS_ERROR_NAMED("opw", "kinematics not active");
return false;
}
poses.resize(link_names.size());
if (joint_angles.size() != dimension_)
{
ROS_ERROR_NAMED("opw", "Joint angles vector must have size: %d", dimension_);
return false;
}
// Check that we have the same number of poses as tips
if (tip_frames_.size() != poses.size())
{
ROS_ERROR_STREAM_NAMED("opw", "Mismatched number of pose requests (" << poses.size() << ") to tip frames ("
<< tip_frames_.size()
<< ") in searchPositionFK");
return false;
}
// forward function expect pointer to first element of array of joint values
// that is why &joint_angles[0] is passed
tf::poseEigenToMsg(opw_kinematics::forward(opw_parameters_, &joint_angles[0]), poses[0]);
return true;
}
const std::vector<std::string>& MoveItOPWKinematicsPlugin::getJointNames() const
{
return ik_group_info_.joint_names;
}
const std::vector<std::string>& MoveItOPWKinematicsPlugin::getLinkNames() const
{
return ik_group_info_.link_names;
}
const std::vector<std::string>& MoveItOPWKinematicsPlugin::getVariableNames() const
{
return joint_model_group_->getVariableNames();
}
bool MoveItOPWKinematicsPlugin::setOPWParameters()
{
ROS_INFO_STREAM("Getting kinematic parameters from parameter server.");
// Using the full parameter name at the moment because the leading slash
// in front of robot_description_kinematics is missing
// in lookupParam function, but I'm not sure if this is a bug or I do not
// understand the interface.
std::string prefix = "/robot_description_kinematics/" + group_name_ + "/";
ros::NodeHandle nh;
std::map<std::string, double> geometric_parameters, dummy;
if (!lookupParam(prefix + "kinematics_solver_geometric_parameters", geometric_parameters, dummy))
{
ROS_ERROR_STREAM("Failed to load geometric parameters for ik solver.");
return false;
}
std::vector<double> joint_offsets, dummy2;
if (!lookupParam(prefix + "kinematics_solver_joint_offsets", joint_offsets, dummy2))
{
ROS_ERROR_STREAM("Failed to load joint offsets for ik solver.");
return false;
}
std::vector<int> joint_sign_corrections, dummy3;
if (!lookupParam(prefix + "kinematics_solver_joint_sign_corrections", joint_sign_corrections, dummy3))
{
ROS_ERROR_STREAM("Failed to load joint sign corrections for ik solver.");
return false;
}
opw_parameters_.a1 = geometric_parameters["a1"];
opw_parameters_.a2 = geometric_parameters["a2"];
opw_parameters_.b = geometric_parameters["b"];
opw_parameters_.c1 = geometric_parameters["c1"];
opw_parameters_.c2 = geometric_parameters["c2"];
opw_parameters_.c3 = geometric_parameters["c3"];
opw_parameters_.c4 = geometric_parameters["c4"];
for (std::size_t i = 0; i < joint_offsets.size(); ++i)
{
opw_parameters_.offsets[i] = joint_offsets[i];
opw_parameters_.sign_corrections[i] = joint_sign_corrections[i];
}
ROS_INFO_STREAM("Loaded parameters for ik solver:\n" << opw_parameters_);
return true;
}
double MoveItOPWKinematicsPlugin::distance(const std::vector<double>& a, const std::vector<double>& b) const
{
double cost = 0.0;
for (size_t i = 0; i < a.size(); ++i)
cost += std::abs(b[i] - a[i]);
return cost;
}
// Compute the index of the closest joint pose in 'candidates' from 'target'
std::size_t MoveItOPWKinematicsPlugin::closestJointPose(const std::vector<double>& target,
const std::vector<std::vector<double>>& candidates) const
{
size_t closest = 0; // index into candidates
double lowest_cost = std::numeric_limits<double>::max();
for (size_t i = 0; i < candidates.size(); ++i)
{
assert(target.size() == candidates[i].size());
double c = distance(target, candidates[i]);
if (c < lowest_cost)
{
closest = i;
lowest_cost = c;
}
}
return closest;
}
bool MoveItOPWKinematicsPlugin::getAllIK(const Eigen::Affine3d& pose,
std::vector<std::vector<double>>& joint_poses) const
{
joint_poses.clear();
// Transform input pose
// needed if we introduce a tip frame different from tool0
// or a different base frame
// Eigen::Affine3d tool_pose = diff_base.inverse() * pose *
// tip_frame.inverse();
// convert Eigen::Affine3d to Eigen::Isometry3d for opw_kinematics
Eigen::Isometry3d pose_isometry;
pose_isometry = pose.matrix();
std::array<double, 6 * 8> sols;
opw_kinematics::inverse(opw_parameters_, pose_isometry, sols.data());
// Check the output
std::vector<double> tmp(6); // temporary storage for API reasons
for (int i = 0; i < 8; i++)
{
double* sol = sols.data() + 6 * i;
if (opw_kinematics::isValid(sol))
{
opw_kinematics::harmonizeTowardZero(sol);
// TODO: make this better...
std::copy(sol, sol + 6, tmp.data());
// if (isValid(tmp))
// {
joint_poses.push_back(tmp);
// }
}
}
return joint_poses.size() > 0;
}
bool MoveItOPWKinematicsPlugin::getIK(const Eigen::Affine3d& pose, const std::vector<double>& seed_state,
std::vector<double>& joint_pose) const
{
// Descartes Robot Model interface calls for 'closest' point to seed position
std::vector<std::vector<double>> joint_poses;
if (!getAllIK(pose, joint_poses))
return false;
// Find closest joint pose; getAllIK() does isValid checks already
joint_pose = joint_poses[closestJointPose(seed_state, joint_poses)];
return true;
}
} // namespace moveit_opw_kinematics_plugin