58 #include <gtest/gtest.h> 59 #include <sr_utilities/sr_math_utils.hpp> 62 #define DEG2RAD (M_PI / 180.0) 63 #define RAD2DEG (180.0 / M_PI) 66 #define MAX_DELTA 0.01 74 return sr_math_utils::Random::instance().generate<
double>(min_n, max_n);
80 std::string tipName = prefix +
"tip";
81 bool isLittleFinger = (prefix ==
"lf");
82 bool isThumb = (prefix ==
"th");
83 std::string hand_prefix =
"rh_";
85 std::vector<std::string> jointNames;
86 jointNames.push_back(hand_prefix + PREFIX +
"J1");
87 jointNames.push_back(hand_prefix + PREFIX +
"J2");
88 jointNames.push_back(hand_prefix + PREFIX +
"J3");
89 jointNames.push_back(hand_prefix + PREFIX +
"J4");
90 if (isLittleFinger || isThumb)
92 jointNames.push_back(hand_prefix + PREFIX +
"J5");
98 std::map<std::string, int> jointIndexMap;
99 for (
unsigned int j = 0; j < jointNames.size(); j++)
101 jointIndexMap[jointNames[j]] = j;
105 ROS_INFO(
"waiting for FK/IK service for finger %s", prefix.c_str());
114 moveit_msgs::GetPositionFK fkdata;
121 std::vector<double> jj;
122 for (
int i = 0; i < n_tests; i++)
148 fkdata.request.header.frame_id = hand_prefix +
"palm";
150 fkdata.request.fk_link_names.resize(1);
151 fkdata.request.fk_link_names.push_back(tipName);
152 fkdata.request.robot_state.joint_state.header.stamp =
ros::Time::now();
153 fkdata.request.robot_state.joint_state.header.frame_id =
"";
154 fkdata.request.robot_state.joint_state.name.resize(jointNames.size());
155 fkdata.request.robot_state.joint_state.position.resize(jointNames.size());
158 ROS_INFO(
"FK req fk_link_names[0] is %s", tipName.c_str());
159 for (
unsigned int j = 0; j < jointNames.size(); j++)
161 fkdata.request.robot_state.joint_state.name[j] = jointNames[j];
162 fkdata.request.robot_state.joint_state.position[j] = jj[j];
164 ROS_INFO(
"FK req %d joint %d %s radians %6.2f", i, j, jointNames[j].c_str(), jj[j]);
169 int status = fkdata.response.error_code.val;
171 ROS_INFO(
"FK returned status %d", static_cast<int>(status));
173 std::vector<geometry_msgs::PoseStamped> pp = fkdata.response.pose_stamped;
174 geometry_msgs::Pose pose = pp[0].pose;
176 if (status == fkdata.response.error_code.SUCCESS)
179 ROS_INFO(
"FK success, pose is %8.4f %8.4f %8.4f",
190 moveit_msgs::GetPositionIK::Request ikreq;
191 moveit_msgs::GetPositionIK::Response ikres;
193 ikreq.ik_request.ik_link_name = tipName;
194 ikreq.ik_request.pose_stamped.header.frame_id = hand_prefix +
"palm";
195 ikreq.ik_request.pose_stamped.pose.position.x = pose.position.x;
196 ikreq.ik_request.pose_stamped.pose.position.y = pose.position.y;
197 ikreq.ik_request.pose_stamped.pose.position.z = pose.position.z;
200 ikreq.ik_request.pose_stamped.pose.orientation.x = 0.0;
201 ikreq.ik_request.pose_stamped.pose.orientation.y = 0.0;
202 ikreq.ik_request.pose_stamped.pose.orientation.z = 0.0;
203 ikreq.ik_request.pose_stamped.pose.orientation.w = 1.0;
205 ikreq.ik_request.robot_state.joint_state.position.resize(jointNames.size());
206 ikreq.ik_request.robot_state.joint_state.name.resize(jointNames.size());
207 for (
unsigned int j = 0; j < jointNames.size(); j++)
209 ikreq.ik_request.robot_state.joint_state.name[j] = jointNames[j];
212 ik.call(ikreq, ikres);
213 if (ikres.error_code.val == ikres.error_code.SUCCESS)
218 for (
unsigned int j = 0; j < ikres.solution.joint_state.name.size(); j++)
221 ROS_INFO(
"Joint: %s %f", ikres.solution.joint_state.name[j].c_str(),
222 ikres.solution.joint_state.position[j]);
225 for (
unsigned int j = 0; j < ikres.solution.joint_state.name.size(); j++)
227 std::string name = ikres.solution.joint_state.name[j];
228 int ix = jointIndexMap[name];
229 double x = ikres.solution.joint_state.position[j];
233 ROS_INFO(
"FK/IK mismatch for joint %d %s, %6.4f %6.4f", j, name.c_str(), x, jj[ix]);
242 ROS_INFO(
"FK/IK comparison %s", (ok ?
"matched" :
"failed"));
247 ROS_WARN(
"IK Not solved for FK req %d", i);
248 for (
unsigned int j = 0; j < jointNames.size(); j++)
250 ROS_WARN(
" joint %d %s radians %6.2f", j, jointNames[j].c_str(), jj[j]);
252 ROS_WARN(
"Pose was %f, %f, %f", pose.position.x, pose.position.y, pose.position.z);
256 fkdata.request.header.frame_id = hand_prefix +
"palm";
258 fkdata.request.fk_link_names.resize(1);
259 fkdata.request.fk_link_names.push_back(tipName);
260 fkdata.request.robot_state.joint_state.header.stamp =
ros::Time::now();
261 fkdata.request.robot_state.joint_state.header.frame_id =
"";
262 fkdata.request.robot_state.joint_state.name.resize(jointNames.size());
263 fkdata.request.robot_state.joint_state.position.resize(jointNames.size());
265 ROS_INFO(
"FK req with IK result fk_link_names[0] is %s", tipName.c_str());
266 for (
unsigned int j = 0; j < ikres.solution.joint_state.name.size(); j++)
268 std::string name = ikres.solution.joint_state.name[j];
269 int ix = jointIndexMap[name];
270 double x = ikres.solution.joint_state.position[j];
272 fkdata.request.robot_state.joint_state.name[ix] = jointNames[ix];
273 fkdata.request.robot_state.joint_state.position[ix] = x;
275 ROS_INFO(
"FK req %d with IK result, joint %d %s radians %6.2f", i, j, jointNames[ix].c_str(), x);
279 status = fkdata.response.error_code.val;
281 ROS_INFO(
"FK returned status %d", static_cast<int>(status));
283 std::vector<geometry_msgs::PoseStamped> ppp = fkdata.response.pose_stamped;
284 geometry_msgs::Pose pppose = ppp[0].pose;
286 if (status == fkdata.response.error_code.SUCCESS)
289 ROS_INFO(
"FK pose with IK result is %8.4f %8.4f %8.4f",
301 ROS_INFO(
"-#- tested %d random positions, %d IK solved, %d matched", n_tests, n_iksolved, n_matched);
346 int main(
int argc,
char **argv)
348 testing::InitGoogleTest(&argc, argv);
355 char argument =
static_cast<char>((argv[1][0]));
371 ROS_INFO(
"-#- FK/IK test started, random seed is %d", seed);
374 return RUN_ALL_TESTS();
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
#define EXPECT_NEAR(a, b, prec)
double rand_range(double min_n, double max_n)
int main(int argc, char **argv)
void random_test_finger_fkik(std::string PREFIX, std::string prefix, int n_tests, bool verbose=false)
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)