debug_evaluate_jointstates_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <string>
19 #include <ros/ros.h>
20 
21 #include <sensor_msgs/JointState.h>
22 #include <std_msgs/Float64.h>
23 #include <geometry_msgs/Twist.h>
24 
26 #include <kdl/chainfksolvervel_recursive.hpp>
27 #include <kdl/chainjnttojacsolver.hpp>
28 #include <kdl/frames.hpp>
29 #include <kdl/jntarray.hpp>
30 #include <kdl/jntarrayvel.hpp>
31 
32 #include <Eigen/Dense>
34 
36 {
41 
42  std::string chain_base_link_;
43  std::string chain_tip_link_;
44 
48 
49 public:
50  int init()
51  {
52  if (!nh_.getParam("chain_base_link", this->chain_base_link_))
53  {
54  ROS_ERROR("Failed to get parameter \"chain_base_link\".");
55  return -1;
56  }
57 
58  if (!nh_.getParam("chain_tip_link", this->chain_tip_link_))
59  {
60  ROS_ERROR("Failed to get parameter \"chain_tip_link\".");
61  return -2;
62  }
63 
65  KDL::Tree my_tree;
66  if (!kdl_parser::treeFromParam("/robot_description", my_tree))
67  {
68  ROS_ERROR("Failed to construct kdl tree");
69  return -3;
70  }
71 
72  my_tree.getChain(this->chain_base_link_, this->chain_tip_link_, chain_);
73  if (chain_.getNrOfJoints() == 0)
74  {
75  ROS_ERROR("Failed to initialize kinematic chain");
76  return -4;
77  }
78 
79  p_fksolver_vel_ = new KDL::ChainFkSolverVel_recursive(chain_);
80  p_jnt2jac_ = new KDL::ChainJntToJacSolver(chain_);
81 
83  jointstate_sub_ = nh_.subscribe("joint_states", 1, &DebugEvaluateJointStates::jointstateCallback, this);
84  manipulability_pub_ = nh_.advertise<std_msgs::Float64> ("debug/manipulability", 1);
85  twist_current_pub_ = nh_.advertise<geometry_msgs::Twist> ("debug/twist_current", 1);
86 
87  return 0;
88  }
89 
90  void jointstateCallback(const sensor_msgs::JointState::ConstPtr& msg)
91  {
93  KDL::JntArray q_dot = KDL::JntArray(chain_.getNrOfJoints());
94 
95  for (unsigned int i = 0; i < msg->name.size(); i++)
96  {
97  q(i) = msg->position[i];
98  q_dot(i) = msg->velocity[i];
99  }
100 
102  KDL::FrameVel FrameVel;
103  KDL::JntArrayVel jntArrayVel = KDL::JntArrayVel(q, q_dot);
104  if (p_fksolver_vel_->JntToCart(jntArrayVel, FrameVel, -1) >= 0)
105  {
106  geometry_msgs::Twist twist_msg;
107  tf::twistKDLToMsg(FrameVel.GetTwist(), twist_msg);
108  twist_current_pub_.publish(twist_msg);
109  }
110 
112  KDL::Jacobian jac(chain_.getNrOfJoints());
113  p_jnt2jac_->JntToJac(q, jac);
114  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> prod = jac.data * jac.data.transpose();
115  double d = prod.determinant();
116  double kappa = std::sqrt(std::abs(d));
117  std_msgs::Float64 manipulability_msg;
118  manipulability_msg.data = kappa;
119  manipulability_pub_.publish(manipulability_msg);
120  }
121 };
122 
123 
124 int main(int argc, char** argv)
125 {
126  ros::init(argc, argv, "debug_evaluate_jointstates_node");
127 
129  if (dejs.init() != 0)
130  {
131  ROS_ERROR("Failed to initialize DebugEvaluateJointStates.");
132  return -1;
133  }
134 
135  ros::spin();
136 }
d
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
IMETHOD Twist GetTwist() const
ROSCPP_DECL void spin(Spinner &spinner)
KDL::ChainFkSolverVel_recursive * p_fksolver_vel_
void twistKDLToMsg(const KDL::Twist &k, geometry_msgs::Twist &m)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
unsigned int getNrOfJoints() const
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
virtual int JntToCart(const JntArrayVel &q_in, FrameVel &out, int segmentNr=-1)
KDL_PARSER_PUBLIC bool treeFromParam(const std::string &param, KDL::Tree &tree)
bool getParam(const std::string &key, std::string &s) const
void jointstateCallback(const sensor_msgs::JointState::ConstPtr &msg)
virtual int JntToJac(const JntArray &q_in, Jacobian &jac, int seg_nr=-1)
#define ROS_ERROR(...)


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00