pr2_fk_ref.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, 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 the 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 
36 
37 
38 #include <ros/console.h>
39 #include <ros/ros.h>
41 #include <pr2_calibration_launch/FkTest.h>
42 #include <kdl/tree.hpp>
43 #include <kdl/chainfksolverpos_recursive.hpp>
44 
45 using namespace std;
46 using namespace pr2_calibration_launch;
47 
48 bool fk(KDL::Tree* tree, FkTest::Request& req, FkTest::Response& resp)
49 {
50  KDL::Chain my_chain;
51  bool success;
52  success = tree->getChain(req.root, req.tip, my_chain);
53 
54  if (!success)
55  {
56  ROS_ERROR("Error extracting chain from [%s] to [%s]\n", req.root.c_str(), req.tip.c_str());
57  return false;
58  }
59 
60  KDL::ChainFkSolverPos_recursive solver(my_chain);
61 
62  KDL::JntArray joints(my_chain.getNrOfJoints());
63  for (unsigned int i=0; i<my_chain.getNrOfJoints(); i++)
64  joints(i) = req.joint_positions[i];
65 
66  KDL::Frame out_frame;
67  if (solver.JntToCart(joints, out_frame))
68  {
69  ROS_ERROR("Error running KDL solver");
70  return false;
71  }
72 
73  resp.pos.resize(3);
74 
75  // Copy over translation vector
76  resp.pos[0] = out_frame.p.data[0];
77  resp.pos[1] = out_frame.p.data[1];
78  resp.pos[2] = out_frame.p.data[2];
79 
80  // Copy over rotation matrix
81  resp.rot.resize(9);
82  for (unsigned int i=0; i<9; i++)
83  resp.rot[i] = out_frame.M.data[i];
84 
85  return true;
86 }
87 
88 int main(int argc, char** argv)
89 {
90  ros::init(argc, argv, "fk_reference");
92  ros::NodeHandle pn("~");
93 
94  KDL::Tree my_tree;
95  string robot_desc_string;
96  if (!n.getParam("robot_description", robot_desc_string))
97  ROS_FATAL("Couldn't get a robot_description from the param server");
98 
99  if (!kdl_parser::treeFromString(robot_desc_string, my_tree))
100  {
101  ROS_ERROR("Failed to construct kdl tree");
102  return false;
103  }
104 
105  boost::function< bool(FkTest::Request&, FkTest::Response&) > func = boost::bind(fk, &my_tree, _1, _2) ;
106  ros::ServiceServer fk_service = n.advertiseService("fk", func);
107 
108  ros::spin();
109 
110  return 0;
111 }
#define ROS_FATAL(...)
virtual int JntToCart(const JntArray &q_in, Frame &p_out, int segmentNr=-1)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ROSCPP_DECL void spin(Spinner &spinner)
Rotation M
double data[3]
unsigned int getNrOfJoints() const
KDL_PARSER_PUBLIC bool treeFromString(const std::string &xml, KDL::Tree &tree)
bool fk(KDL::Tree *tree, FkTest::Request &req, FkTest::Response &resp)
Definition: pr2_fk_ref.cpp:48
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
bool getParam(const std::string &key, std::string &s) const
int i
int main(int argc, char **argv)
Definition: pr2_fk_ref.cpp:88
#define ROS_ERROR(...)
double data[9]


pr2_calibration_launch
Author(s): Vijay Pradeep
autogenerated on Tue Jun 1 2021 02:50:59