Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #pragma once
00035
00036 #include "ros/ros.h"
00037 #include <realtime_tools/realtime_publisher.h>
00038 #include <tf/message_filter.h>
00039 #include <tf/transform_datatypes.h>
00040 #include <tf/transform_listener.h>
00041
00042 #include "kdl/tree.hpp"
00043 #include "kdl/chain.hpp"
00044 #include "kdl/jntarray.hpp"
00045 #include "kdl/jacobian.hpp"
00046 #include "kdl/chainjnttojacsolver.hpp"
00047 #include "kdl/chainfksolverpos_recursive.hpp"
00048 #include "kdl/chainfksolvervel_recursive.hpp"
00049 #include "kdl/chainidsolver_recursive_newton_euler.hpp"
00050 #include "treeidsolver_recursive_newton_euler.hpp"
00051 #include <vector>
00052
00053 #include <Eigen/Geometry>
00054 #include <boost/thread/mutex.hpp>
00055
00056 #include <message_filters/subscriber.h>
00057 #include <pr2_controller_interface/controller.h>
00058 #include <pr2_mechanism_model/chain.h>
00059 #include <realtime_tools/realtime_publisher.h>
00060 #include <algorithm>
00061
00063 #include <sensor_msgs/JointState.h>
00064 #include <geometry_msgs/PoseStamped.h>
00065 #include <geometry_msgs/Twist.h>
00066 #include <nasa_r2_common_msgs/Gains.h>
00067 #include <nasa_r2_common_msgs/SetTipName.h>
00068 #include <nasa_r2_common_msgs/SetJointMode.h>
00069 #include <nasa_r2_common_msgs/Power.h>
00070 #include <nasa_r2_common_msgs/Servo.h>
00071 #include <nasa_r2_common_msgs/PoseTwistStamped.h>
00072
00073 #include "TreeChain.h"
00074 #include "WholeBodyCalc.h"
00075
00076
00077
00078 namespace r2_controller_ns {
00079 class R2ImpedanceController: public pr2_controller_interface::Controller{
00080
00081
00082 ros::NodeHandle node;
00083 tf::TransformListener tfListener;
00084 boost::scoped_ptr<realtime_tools::RealtimePublisher<geometry_msgs::PoseStamped> > left_tip_pose_publisher;
00085 boost::scoped_ptr<realtime_tools::RealtimePublisher<geometry_msgs::Twist> > left_pose_error_publisher;
00086 ros::Subscriber joint_command_sub;
00087 ros::Subscriber left_joint_command_sub;
00088
00089 message_filters::Subscriber<geometry_msgs::PoseStamped> left_pose_command_sub;
00090 boost::scoped_ptr<tf::MessageFilter<geometry_msgs::PoseStamped> > left_pose_command_filter;
00091 message_filters::Subscriber<nasa_r2_common_msgs::PoseTwistStamped> left_pose_vel_command_sub;
00092 boost::scoped_ptr<tf::MessageFilter<nasa_r2_common_msgs::PoseTwistStamped> > left_pose_vel_command_filter;
00093
00094 boost::scoped_ptr<realtime_tools::RealtimePublisher<geometry_msgs::PoseStamped> > right_tip_pose_publisher;
00095 boost::scoped_ptr<realtime_tools::RealtimePublisher<geometry_msgs::Twist> > right_pose_error_publisher;
00096 ros::Subscriber right_joint_command_sub;
00097
00098 message_filters::Subscriber<geometry_msgs::PoseStamped> right_pose_command_sub;
00099 boost::scoped_ptr<tf::MessageFilter<geometry_msgs::PoseStamped> > right_pose_command_filter;
00100 message_filters::Subscriber<nasa_r2_common_msgs::PoseTwistStamped> right_pose_vel_command_sub;
00101 boost::scoped_ptr<tf::MessageFilter<nasa_r2_common_msgs::PoseTwistStamped> > right_pose_vel_command_filter;
00102
00103
00104
00105 ros::Subscriber neck_joint_command_sub;
00106 ros::Subscriber waist_joint_command_sub;
00107
00108 ros::Subscriber set_gains_sub;
00109 boost::scoped_ptr<realtime_tools::RealtimePublisher<nasa_r2_common_msgs::Gains> > gains_publisher;
00110
00111 ros::ServiceServer srv_set_joint_mode;
00112 ros::ServiceServer srv_set_tip_name;
00113 ros::ServiceServer srv_set_power;
00114 ros::ServiceServer srv_set_servo;
00115 public:
00116 class CtrlCalc{
00117 public:
00119 std::string root_name;
00120
00121
00122 KDL::Tree robot_tree;
00123 TreeChain left;
00124 TreeChain right;
00125 TreeChain neck;
00126
00128 Eigen::Matrix<double,7,1> leftCmd;
00129 Eigen::Matrix<double,7,1> rightCmd;
00130 Eigen::Matrix<double,7,1> neckCmd;
00131 KDL::Twist leftVelCmd;
00132 KDL::Twist rightVelCmd;
00133 KDL::Twist neckVelCmd;
00134
00135 bool left_cart;
00136 bool left_cart_vel;
00137 bool right_cart;
00138 bool right_cart_vel;
00139 bool neck_cart;
00140 bool neck_cart_vel;
00141 std::vector<int> joint_pos_control;
00142 std::vector<int> joint_vel_control;
00143
00144 WholeBodyCalc wbc;
00145
00146
00147 std::vector<double> D_high;
00148 std::vector<double> D_low;
00149 std::vector<double> K_high;
00150 std::vector<double> K_low;
00151 std::vector<double> D;
00152 std::vector<double> K;
00153 std::vector<double> desired;
00154 std::vector<double> desiredVel;
00155 int jnt_size;
00156
00157 std::vector<double> cartK_left;
00158 std::vector<double> cartK_right;
00159 std::vector<double> cartD_left;
00160 std::vector<double> cartD_right;
00161
00162
00164 template<int _N>
00165 class AvgV{
00166 double data[_N];
00167 mutable double sorted[_N];
00168 mutable bool update;
00169 static const int N = _N;
00170
00171 int idx;
00172 public:
00173 AvgV():update(false),idx(0){}
00174 operator double()const{
00175 if( update ){
00176 update = false;
00177 for(int x=0; x<N; ++x)
00178 sorted[x] = data[x];
00179 std::sort(sorted,sorted+N);
00180 }
00181 return sorted[N/2];
00182 }
00183 void operator=(double in){
00184 data[idx++] = in;
00185 if( idx >= N )
00186 idx =0;
00187 update = true;
00188 }
00189 };
00190 std::vector< AvgV<3> >treeJntsAvg;
00191 std::vector< AvgV<3> >treeJntsVelAvg;
00192
00193 std::vector<double> treeJnts;
00194 std::vector<double> treeJntsVel;
00195 std::vector<double> jntsUpperLimit;
00196 std::vector<double> jntsLowerLimit;
00197 std::vector<double> jntsCenterPoint;
00198 std::map< std::string, int> name2idx;
00199 std::vector<std::string> idx2name;
00200
00201 KDL::JntArray torques;
00202
00203 boost::scoped_ptr<KDL::TreeIdSolver> rne_calc;
00204
00205 KDL::JntArray jointKCmd(const std::vector<double>& q);
00206 KDL::JntArray jointDCmd(const std::vector<double>& qd);
00207
00209 void activate( TreeChain& tc, bool& flag, Eigen::Matrix<double,7,1>& pose_cmd );
00211 void reactivate();
00212
00213 void init(double gravity[3]);
00214 void calculate();
00215 };
00216 private:
00217 CtrlCalc cc;
00218
00219
00220
00221
00222 std::vector<pr2_mechanism_model::JointState*> robotStateJoints;
00223
00224
00225 pr2_mechanism_model::RobotState* robot_state;
00226 ros::Time time;
00227
00228
00229 void init_ros_msgs();
00230 void publish_msgs();
00231 void joint_left_command(const sensor_msgs::JointState::ConstPtr& msg );
00232 void joint_right_command(const sensor_msgs::JointState::ConstPtr& msg );
00233 void joint_neck_command(const sensor_msgs::JointState::ConstPtr& msg );
00234 void joint_waist_command(const sensor_msgs::JointState::ConstPtr& msg );
00235 void joint_command( const sensor_msgs::JointState::ConstPtr& msg );
00236
00237 void joint_command_entry( const std::string& name, double value, std::vector<double>& desired );
00238 void joint_command_entry( const std::string& name, bool value, std::vector<int>& desired );
00239
00240 void pose_left_command(const geometry_msgs::PoseStamped::ConstPtr& msg);
00241 void pose_right_command(const geometry_msgs::PoseStamped::ConstPtr& msg);
00242
00243 void pose_vel_left_command(const nasa_r2_common_msgs::PoseTwistStamped::ConstPtr& msg );
00244 void pose_vel_right_command(const nasa_r2_common_msgs::PoseTwistStamped::ConstPtr& msg );
00245 void pose_vel_command_inner( const nasa_r2_common_msgs::PoseTwistStamped::ConstPtr& msg,
00246 Eigen::Matrix<double,7,1>& cmd,
00247 KDL::Twist& velCmd,
00248 bool& cart_vel );
00249
00250 KDL::Frame transformPoseMsg(const geometry_msgs::PoseStamped::ConstPtr& msg);
00251 void set_gains(const nasa_r2_common_msgs::Gains::ConstPtr& msg );
00252
00253
00254 bool set_joint_mode(nasa_r2_common_msgs::SetJointMode::Request &req, nasa_r2_common_msgs::SetJointMode::Response &res );
00255 bool set_tip_name(nasa_r2_common_msgs::SetTipName::Request &req, nasa_r2_common_msgs::SetTipName::Response &res );
00256 bool set_power(nasa_r2_common_msgs::Power::Request &req, nasa_r2_common_msgs::Power::Response &res );
00257 bool set_servo(nasa_r2_common_msgs::Servo::Request &req, nasa_r2_common_msgs::Servo::Response &res );
00258
00259
00260
00262 void load_params();
00263
00265 std::vector<double> getGainParams(const std::vector<std::string>& param_names, const std::string& param_name);
00266
00267
00268
00269 boost::mutex thread_mutex;
00270
00271 public:
00272
00273
00274
00275 bool init(pr2_mechanism_model::RobotState* robot_state, ros::NodeHandle& n );
00276
00278 virtual void starting(){
00279 time = robot_state->getTime();
00280 }
00282 virtual void update();
00283 };
00284 }