debug_evaluate_jointstates_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <string>
00019 #include <ros/ros.h>
00020 
00021 #include <sensor_msgs/JointState.h>
00022 #include <std_msgs/Float64.h>
00023 #include <geometry_msgs/Twist.h>
00024 
00025 #include <kdl_parser/kdl_parser.hpp>
00026 #include <kdl/chainfksolvervel_recursive.hpp>
00027 #include <kdl/chainjnttojacsolver.hpp>
00028 #include <kdl/frames.hpp>
00029 #include <kdl/jntarray.hpp>
00030 #include <kdl/jntarrayvel.hpp>
00031 
00032 #include <Eigen/Dense>
00033 #include <kdl_conversions/kdl_msg.h>
00034 
00035 class DebugEvaluateJointStates
00036 {
00037     ros::NodeHandle nh_;
00038     ros::Subscriber jointstate_sub_;
00039     ros::Publisher manipulability_pub_;
00040     ros::Publisher twist_current_pub_;
00041 
00042     std::string chain_base_link_;
00043     std::string chain_tip_link_;
00044 
00045     KDL::Chain chain_;
00046     KDL::ChainFkSolverVel_recursive* p_fksolver_vel_;
00047     KDL::ChainJntToJacSolver* p_jnt2jac_;
00048 
00049 public:
00050     int init()
00051     {
00052         if (!nh_.getParam("chain_base_link", this->chain_base_link_))
00053         {
00054             ROS_ERROR("Failed to get parameter \"chain_base_link\".");
00055             return -1;
00056         }
00057 
00058         if (!nh_.getParam("chain_tip_link", this->chain_tip_link_))
00059         {
00060             ROS_ERROR("Failed to get parameter \"chain_tip_link\".");
00061             return -2;
00062         }
00063 
00065         KDL::Tree my_tree;
00066         if (!kdl_parser::treeFromParam("/robot_description", my_tree))
00067         {
00068             ROS_ERROR("Failed to construct kdl tree");
00069             return -3;
00070         }
00071 
00072         my_tree.getChain(this->chain_base_link_, this->chain_tip_link_, chain_);
00073         if (chain_.getNrOfJoints() == 0)
00074         {
00075             ROS_ERROR("Failed to initialize kinematic chain");
00076             return -4;
00077         }
00078 
00079         p_fksolver_vel_ = new KDL::ChainFkSolverVel_recursive(chain_);
00080         p_jnt2jac_ = new KDL::ChainJntToJacSolver(chain_);
00081 
00083         jointstate_sub_ = nh_.subscribe("joint_states", 1, &DebugEvaluateJointStates::jointstateCallback, this);
00084         manipulability_pub_ = nh_.advertise<std_msgs::Float64> ("debug/manipulability", 1);
00085         twist_current_pub_ = nh_.advertise<geometry_msgs::Twist> ("debug/twist_current", 1);
00086 
00087         return 0;
00088     }
00089 
00090     void jointstateCallback(const sensor_msgs::JointState::ConstPtr& msg)
00091     {
00092         KDL::JntArray q = KDL::JntArray(chain_.getNrOfJoints());
00093         KDL::JntArray q_dot = KDL::JntArray(chain_.getNrOfJoints());
00094 
00095         for (unsigned int i = 0; i < msg->name.size(); i++)
00096         {
00097             q(i) = msg->position[i];
00098             q_dot(i) = msg->velocity[i];
00099         }
00100 
00102         KDL::FrameVel FrameVel;
00103         KDL::JntArrayVel jntArrayVel = KDL::JntArrayVel(q, q_dot);
00104         if (p_fksolver_vel_->JntToCart(jntArrayVel, FrameVel, -1) >= 0)
00105         {
00106             geometry_msgs::Twist twist_msg;
00107             tf::twistKDLToMsg(FrameVel.GetTwist(), twist_msg);
00108             twist_current_pub_.publish(twist_msg);
00109         }
00110 
00112         KDL::Jacobian jac(chain_.getNrOfJoints());
00113         p_jnt2jac_->JntToJac(q, jac);
00114         Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> prod = jac.data * jac.data.transpose();
00115         double d = prod.determinant();
00116         double kappa = std::sqrt(std::abs(d));
00117         std_msgs::Float64 manipulability_msg;
00118         manipulability_msg.data = kappa;
00119         manipulability_pub_.publish(manipulability_msg);
00120     }
00121 };
00122 
00123 
00124 int main(int argc, char** argv)
00125 {
00126     ros::init(argc, argv, "debug_evaluate_jointstates_node");
00127 
00128     DebugEvaluateJointStates dejs;
00129     if (dejs.init() != 0)
00130     {
00131         ROS_ERROR("Failed to initialize DebugEvaluateJointStates.");
00132         return -1;
00133     }
00134 
00135     ros::spin();
00136 }


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Jun 6 2019 21:19:26