-
Notifications
You must be signed in to change notification settings - Fork 92
/
ikfast_moveit_state_adapter.cpp
185 lines (154 loc) · 6.28 KB
/
ikfast_moveit_state_adapter.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
/*
* Software License Agreement (Apache License)
*
* Copyright (c) 2016, Jonathan Meyer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "descartes_moveit/ikfast_moveit_state_adapter.h"
#include <eigen_conversions/eigen_msg.h>
#include <ros/node_handle.h>
const static std::string default_base_frame = "base_link";
const static std::string default_tool_frame = "tool0";
// Compute the 'joint distance' between two poses
static double distance(const std::vector<double>& a, const std::vector<double>& b)
{
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'
static size_t closestJointPose(const std::vector<double>& target, const std::vector<std::vector<double>>& candidates)
{
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 descartes_moveit::IkFastMoveitStateAdapter::initialize(const std::string& robot_description,
const std::string& group_name,
const std::string& world_frame,
const std::string& tcp_frame)
{
if (!MoveitStateAdapter::initialize(robot_description, group_name, world_frame, tcp_frame))
{
return false;
}
return computeIKFastTransforms();
}
bool descartes_moveit::IkFastMoveitStateAdapter::getAllIK(const Eigen::Affine3d& pose,
std::vector<std::vector<double>>& joint_poses) const
{
joint_poses.clear();
const auto& solver = joint_group_->getSolverInstance();
// Transform input pose
Eigen::Affine3d tool_pose = world_to_base_.frame_inv * pose * tool0_to_tip_.frame;
// convert to geometry_msgs ...
geometry_msgs::Pose geometry_pose;
tf::poseEigenToMsg(tool_pose, geometry_pose);
std::vector<geometry_msgs::Pose> poses = { geometry_pose };
std::vector<double> dummy_seed(getDOF(), 0.0);
std::vector<std::vector<double>> joint_results;
kinematics::KinematicsResult result;
kinematics::KinematicsQueryOptions options; // defaults are reasonable as of Indigo
if (!solver->getPositionIK(poses, dummy_seed, joint_results, result, options))
{
return false;
}
const auto last_index = 5;
const auto index4 = 3;
for (auto& sol : joint_results)
{
const double joint6 = sol[last_index];
const double joint4 = sol[index4];
for (int i = -1; i <= 1; ++i)
{
sol[last_index] = joint6 + i * 2.0 * M_PI;
for (int j = -1; j <= 1; ++j)
{
sol[index4] = joint4 + j * 2.0 * M_PI;
if (isValid(sol))
joint_poses.push_back(sol);
}
}
}
return joint_poses.size() > 0;
}
bool descartes_moveit::IkFastMoveitStateAdapter::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;
}
bool descartes_moveit::IkFastMoveitStateAdapter::getFK(const std::vector<double>& joint_pose,
Eigen::Affine3d& pose) const
{
const auto& solver = joint_group_->getSolverInstance();
std::vector<std::string> tip_frame = { solver->getTipFrame() };
std::vector<geometry_msgs::Pose> output;
if (!isValid(joint_pose))
return false;
if (!solver->getPositionFK(tip_frame, joint_pose, output))
return false;
tf::poseMsgToEigen(output[0], pose); // pose in frame of IkFast base
pose = world_to_base_.frame * pose * tool0_to_tip_.frame_inv;
return true;
}
void descartes_moveit::IkFastMoveitStateAdapter::setState(const moveit::core::RobotState& state)
{
descartes_moveit::MoveitStateAdapter::setState(state);
computeIKFastTransforms();
}
bool descartes_moveit::IkFastMoveitStateAdapter::computeIKFastTransforms()
{
// look up the IKFast base and tool frame
ros::NodeHandle nh;
std::string ikfast_base_frame, ikfast_tool_frame;
nh.param<std::string>("ikfast_base_frame", ikfast_base_frame, default_base_frame);
nh.param<std::string>("ikfast_tool_frame", ikfast_tool_frame, default_tool_frame);
if (!robot_state_->knowsFrameTransform(ikfast_base_frame))
{
logError("IkFastMoveitStateAdapter: Cannot find transformation to frame '%s' in group '%s'.",
ikfast_base_frame.c_str(), group_name_.c_str());
return false;
}
if (!robot_state_->knowsFrameTransform(ikfast_tool_frame))
{
logError("IkFastMoveitStateAdapter: Cannot find transformation to frame '%s' in group '%s'.",
ikfast_tool_frame.c_str(), group_name_.c_str());
return false;
}
// calculate frames
tool0_to_tip_ = descartes_core::Frame(robot_state_->getFrameTransform(tool_frame_).inverse() *
robot_state_->getFrameTransform(ikfast_tool_frame));
world_to_base_ = descartes_core::Frame(world_to_root_.frame * robot_state_->getFrameTransform(ikfast_base_frame));
logInform("IkFastMoveitStateAdapter: initialized with IKFast tool frame '%s' and base frame '%s'.",
ikfast_tool_frame.c_str(), ikfast_base_frame.c_str());
return true;
}