wam_driver_node.cpp
Go to the documentation of this file.
00001 #include "wam_driver_node.h"
00002 
00003 using namespace Eigen;
00004 
00005 WamDriverNode::WamDriverNode(ros::NodeHandle &nh) :
00006  iri_base_driver::IriBaseNodeDriver<WamDriver>(nh),
00007  DMPTracker_aserver_(public_node_handle_, "DMPTracker"),
00008  lwpr_trajectory_server_aserver_(public_node_handle_, "lwpr_trajectory"),
00009  follow_joint_trajectory_server_(public_node_handle_, "follow_joint_trajectory")
00010 {
00011   //init class attributes if necessary
00012   //this->loop_rate_ = 2;//in [Hz]
00013 
00014   //ask wam for the numaxes
00015   this->JointState_msg.name.resize(7);
00016   this->JointState_msg.position.resize(7); 
00017 
00018   // [init publishers]
00019   this->pose_publisher = this->public_node_handle_.advertise<geometry_msgs::PoseStamped>("libbarrett_link_tcp", 1);
00020   this->joint_states_publisher = this->public_node_handle_.advertise<sensor_msgs::JointState>("/joint_states", 1);
00021 
00022   // [init subscribers]
00023   this->jnt_pos_cmd_subscriber_ = this->public_node_handle_.subscribe("jnt_pos_cmd", 1, &WamDriverNode::jnt_pos_cmd_callback, this);
00024   this->DMPTrackerNewGoal_subscriber_ = this->public_node_handle_.subscribe("DMPTrackerNewGoal", 1, &WamDriverNode::DMPTrackerNewGoal_callback, this);
00025 
00026   // [init services]
00027   wam_services_server_ = public_node_handle_.advertiseService("wam_services",
00028                                                               &WamDriverNode::wam_servicesCallback, this);
00029   joints_move_server   = public_node_handle_.advertiseService("joints_move",
00030                                                               &WamDriverNode::joints_moveCallback, this);
00031   pose_move_server     = public_node_handle_.advertiseService("pose_move",
00032                                                               &WamDriverNode::pose_moveCallback, this);
00033   // [init clients]
00034 
00035   // [init action servers]
00036   DMPTracker_aserver_.registerStartCallback(boost::bind(&WamDriverNode::DMPTrackerStartCallback, this, _1)); 
00037   DMPTracker_aserver_.registerStopCallback(boost::bind(&WamDriverNode::DMPTrackerStopCallback, this)); 
00038   DMPTracker_aserver_.registerIsFinishedCallback(boost::bind(&WamDriverNode::DMPTrackerIsFinishedCallback, this)); 
00039   DMPTracker_aserver_.registerHasSucceedCallback(boost::bind(&WamDriverNode::DMPTrackerHasSucceedCallback, this)); 
00040   DMPTracker_aserver_.registerGetResultCallback(boost::bind(&WamDriverNode::DMPTrackerGetResultCallback, this, _1)); 
00041   DMPTracker_aserver_.registerGetFeedbackCallback(boost::bind(&WamDriverNode::DMPTrackerGetFeedbackCallback, this, _1)); 
00042   DMPTracker_aserver_.start();
00043   
00044   lwpr_trajectory_server_aserver_.registerStartCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverStartCallback, this, _1)); 
00045   lwpr_trajectory_server_aserver_.registerStopCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverStopCallback, this)); 
00046   lwpr_trajectory_server_aserver_.registerIsFinishedCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverIsFinishedCallback, this)); 
00047   lwpr_trajectory_server_aserver_.registerHasSucceedCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverHasSucceedCallback, this)); 
00048   lwpr_trajectory_server_aserver_.registerGetResultCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverGetResultCallback, this, _1)); 
00049   lwpr_trajectory_server_aserver_.registerGetFeedbackCallback(boost::bind(&WamDriverNode::lwpr_trajectory_serverGetFeedbackCallback, this, _1)); 
00050   lwpr_trajectory_server_aserver_.start();
00051 
00052   follow_joint_trajectory_server_.registerStartCallback(boost::bind(&WamDriverNode::joint_trajectoryStartCallback, this, _1));
00053   follow_joint_trajectory_server_.registerStopCallback(boost::bind(&WamDriverNode::joint_trajectoryStopCallback, this)); 
00054   follow_joint_trajectory_server_.registerIsFinishedCallback(boost::bind(&WamDriverNode::joint_trajectoryIsFinishedCallback, this)); 
00055   follow_joint_trajectory_server_.registerHasSucceedCallback(boost::bind(&WamDriverNode::joint_trajectoryHasSucceedCallback, this)); 
00056   follow_joint_trajectory_server_.registerGetResultCallback(boost::bind(&WamDriverNode::joint_trajectoryGetResultCallback, this, _1)); 
00057   follow_joint_trajectory_server_.registerGetFeedbackCallback(boost::bind(&WamDriverNode::joint_trajectoryGetFeedbackCallback, this, _1)); 
00058   follow_joint_trajectory_server_.start();
00059 
00060   // [init action clients]
00061 
00062   ROS_INFO("Wam node started"); 
00063 }
00064 
00065 void WamDriverNode::mainNodeThread(void)
00066 {
00067   //lock access to driver if necessary
00068   //this->driver_.lock();
00069 
00070   std::vector<double> angles(7,0.1);
00071   std::vector<double> pose(16,0);
00072   Matrix3f rmat;
00073 
00074   // [fill msg Header if necessary]
00075   std::string robot_name = this->driver_.get_robot_name();
00076   std::stringstream ss_tcp_link_name;
00077   ss_tcp_link_name << robot_name << "_link_base";
00078 
00079   this->PoseStamped_msg.header.stamp = ros::Time::now();
00080   this->PoseStamped_msg.header.frame_id = ss_tcp_link_name.str().c_str();
00081 
00082   // [fill msg structures]
00083   this->driver_.lock();
00084   this->driver_.get_pose(&pose);
00085   this->driver_.get_joint_angles(&angles);
00086   this->driver_.unlock();
00087 
00088   rmat << pose.at(0), pose.at(1), pose.at(2), pose.at(4), pose.at(5), pose.at(6), pose.at(8), pose.at(9), pose.at(10);
00089 
00090   {
00091     Quaternion<float> quat(rmat);
00092 
00093     this->PoseStamped_msg.pose.position.x = pose.at(3);
00094     this->PoseStamped_msg.pose.position.y = pose.at(7);
00095     this->PoseStamped_msg.pose.position.z = pose.at(11);
00096     this->PoseStamped_msg.pose.orientation.x = quat.x();
00097     this->PoseStamped_msg.pose.orientation.y = quat.y();
00098     this->PoseStamped_msg.pose.orientation.z = quat.z();
00099     this->PoseStamped_msg.pose.orientation.w = quat.w();
00100   }
00101 
00102   JointState_msg.header.stamp = ros::Time::now();
00103   for(int i=0;i<(int)angles.size();i++){
00104       std::stringstream ss_jname;
00105       ss_jname << robot_name << "_joint_" << i+1;
00106       JointState_msg.name[i] = ss_jname.str().c_str();
00107       JointState_msg.position[i] = angles[i];
00108   }
00109 
00110   // [fill srv structure and make request to the server]
00111   
00112   // [fill action structure and make request to the action server]
00113 
00114   // [publish messages]
00115   this->joint_states_publisher.publish(this->JointState_msg);
00116   this->pose_publisher.publish(this->PoseStamped_msg);
00117 
00118   //unlock access to driver if previously blocked
00119   //this->driver_.unlock();
00120 }
00121 
00122 /*  [subscriber callbacks] */
00123 void WamDriverNode::jnt_pos_cmd_callback(const wam_msgs::RTJointPos::ConstPtr& msg) 
00124 { 
00125   ROS_INFO("WamDriverNode::jnt_pos_cmd_callback: New Message Received"); 
00126 
00127   //use appropiate mutex to shared variables if necessary 
00128   //this->driver_.lock(); 
00129   //this->jnt_pos_cmd_mutex_.enter(); 
00130 
00131   //std::cout << msg->data << std::endl; 
00132 
00133   driver_.jnt_pos_cmd_callback(&msg->joints,&msg->rate_limits);
00134   //unlock previously blocked shared variables 
00135   //this->driver_.unlock(); 
00136   //this->jnt_pos_cmd_mutex_.exit(); 
00137 }
00138 void WamDriverNode::DMPTrackerNewGoal_callback(const trajectory_msgs::JointTrajectoryPoint::ConstPtr& msg) 
00139 { 
00140   ROS_INFO("WamDriverNode::DMPTrackerNewGoal_callback: New Message Received"); 
00141 
00142   //use appropiate mutex to shared variables if necessary 
00143   //this->driver_.lock(); 
00144   //this->DMPTrackerNewGoal_mutex_.enter(); 
00145   driver_.dmp_tracker_new_goal(&msg->positions);
00146   //std::cout << msg->data << std::endl; 
00147 
00148   //unlock previously blocked shared variables 
00149   //this->driver_.unlock(); 
00150   //this->DMPTrackerNewGoal_mutex_.exit(); 
00151 }
00152 
00153 /*  [service callbacks] */
00154 bool WamDriverNode::wam_servicesCallback(iri_wam_common_msgs::wamdriver::Request &req, iri_wam_common_msgs::wamdriver::Response &res) 
00155 { 
00156   //lock access to driver if necessary 
00157   this->driver_.lock(); 
00158 
00159   if(this->driver_.isRunning()) 
00160   { 
00161     switch(req.call){
00162       case HOLDON:
00163         this->driver_.hold_current_position(true);
00164         break;
00165       case HOLDOFF:
00166         this->driver_.hold_current_position(false);
00167         break;
00168       default:
00169           ROS_ERROR("Invalid action id. Check wam_actions_node.h");
00170         break;
00171     }
00172     //do operations with req and output on res 
00173     //res.data2 = req.data1 + my_var; 
00174   } else { 
00175     std::cout << "ERROR: Driver is not on run mode yet." << std::endl; 
00176   } 
00177 
00178   //unlock driver if previously blocked 
00179   this->driver_.unlock(); 
00180 
00181   return true; 
00182 }
00183 bool WamDriverNode::joints_moveCallback(iri_wam_common_msgs::joints_move::Request  & req,
00184                                         iri_wam_common_msgs::joints_move::Response & res)
00185 {
00186   //lock access to driver if necessary
00187   this->driver_.lock();
00188 
00189   if (!this->driver_.isRunning())
00190   {
00191     ROS_ERROR("[JointsMoveCB] Driver is not running");
00192     //unlock driver if previously blocked 
00193     this->driver_.unlock();
00194     return false;
00195   }
00196 
00197   //this call blocks if the wam faults. The mutex is not freed...!   
00198   //unlock driver if previously blocked 
00199   if ((req.velocities.size() == 0) || (req.accelerations.size() == 0))
00200     this->driver_.move_in_joints(& req.joints); 
00201   else
00202     this->driver_.move_in_joints(& req.joints, &req.velocities, &req.accelerations); 
00203   this->driver_.unlock();
00204   this->driver_.wait_move_end();
00205 
00206   return true;
00207 }
00208 
00209 bool
00210 WamDriverNode::pose_moveCallback(iri_wam_common_msgs::pose_move::Request  & req,
00211                                  iri_wam_common_msgs::pose_move::Response & res)
00212 {
00213     bool result;
00214 
00215     //lock access to driver if necessary 
00216     this->driver_.lock();
00217     if (this->driver_.isRunning())
00218     {
00219 
00220         ROS_DEBUG("Received cartesian pose for movement: %f %f %f %f %f %f %f\n",
00221                 req.pose.position.x, req.pose.position.y, req.pose.position.z,
00222                 req.pose.orientation.x, req.pose.orientation.y, req.pose.orientation.z, req.pose.orientation.w);
00223 
00224         this->driver_.move_in_cartesian_pose(req.pose, req.vel, req.acc);
00225         this->driver_.wait_move_end();
00226         result = true;
00227     }
00228     else
00229     {
00230         ROS_ERROR("[PoseMoveCB] Driver is not running");
00231         result = false;
00232     }
00233     this->driver_.unlock();
00234 
00235     res.success = result;
00236     return result;
00237 }
00238 
00239 /*  [action callbacks] */
00240 void WamDriverNode::DMPTrackerStartCallback(const iri_wam_common_msgs::DMPTrackerGoalConstPtr& goal)
00241 { 
00242   driver_.lock(); 
00243     //check goal 
00244     //execute goal 
00245   driver_.start_dmp_tracker(&goal->initial.positions,&goal->goal.positions);
00246   driver_.unlock(); 
00247 } 
00248 
00249 void WamDriverNode::DMPTrackerStopCallback(void) 
00250 { 
00251   driver_.lock(); 
00252     //stop action 
00253   driver_.unlock(); 
00254 } 
00255 
00256 bool WamDriverNode::DMPTrackerIsFinishedCallback(void) 
00257 { 
00258   bool ret = false; 
00259 
00260   driver_.lock(); 
00261     //if action has finish for any reason 
00262     //ret = true; 
00263   driver_.unlock(); 
00264 
00265   return ret; 
00266 } 
00267 
00268 bool WamDriverNode::DMPTrackerHasSucceedCallback(void) 
00269 { 
00270   bool ret = false; 
00271 
00272   driver_.lock(); 
00273     //if goal was accomplished 
00274     //ret = true; 
00275   driver_.unlock(); 
00276 
00277   return ret; 
00278 } 
00279 
00280 void WamDriverNode::DMPTrackerGetResultCallback(iri_wam_common_msgs::DMPTrackerResultPtr& result) 
00281 { 
00282   driver_.lock(); 
00283     //update result data to be sent to client 
00284     //result->data = data; 
00285   driver_.unlock(); 
00286 } 
00287 
00288 void WamDriverNode::DMPTrackerGetFeedbackCallback(iri_wam_common_msgs::DMPTrackerFeedbackPtr& feedback) 
00289 { 
00290   driver_.lock(); 
00291     //keep track of feedback 
00292     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00293   driver_.unlock(); 
00294 }
00295 void WamDriverNode::lwpr_trajectory_serverStartCallback(const iri_wam_common_msgs::LWPRTrajectoryReturningForceEstimationGoalConstPtr& goal)
00296 {
00297     driver_.lock();
00298     driver_.move_trajectory_learnt_and_estimate_force(goal->model_filename, goal->points_filename);
00299     driver_.unlock();
00300 }
00301 
00302 void WamDriverNode::lwpr_trajectory_serverStopCallback(void) 
00303 {
00304     driver_.lock();
00305     // TODO: not implemented yet
00306     driver_.unlock();
00307 }
00308 
00309 bool WamDriverNode::lwpr_trajectory_serverIsFinishedCallback(void) 
00310 {
00311     bool ret = false;
00312 
00313     driver_.lock();
00314     if (driver_.get_force_request_info()->is_estimate_force_request_finish())
00315          ret = true;
00316     driver_.unlock();
00317 
00318     return ret;
00319 }
00320 
00321 bool WamDriverNode::lwpr_trajectory_serverHasSucceedCallback(void) 
00322 {
00323     bool ret = false;
00324 
00325     driver_.lock();
00326     ret = driver_.get_force_request_info()->was_estimate_force_request_succedded();
00327     driver_.unlock();
00328 
00329     return ret;
00330 }
00331 
00332 void WamDriverNode::lwpr_trajectory_serverGetResultCallback(iri_wam_common_msgs::LWPRTrajectoryReturningForceEstimationResultPtr& result)
00333 {
00334     driver_.lock();
00335     result->force = driver_.get_force_request_info()->force_value;
00336     driver_.unlock();
00337 }
00338 
00339 void WamDriverNode::lwpr_trajectory_serverGetFeedbackCallback(iri_wam_common_msgs::LWPRTrajectoryReturningForceEstimationFeedbackPtr& feedback) 
00340 {
00341     // TODO: not feedback provided at the moment
00342     driver_.lock();
00343    //keep track of feedback 
00344     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00345     driver_.unlock(); 
00346 }
00347 void
00348 WamDriverNode::joint_trajectoryStartCallback(const control_msgs::FollowJointTrajectoryGoalConstPtr& goal)
00349 {
00350     // Need to get the list of positions and send it to the driver
00351     ROS_INFO("[WamDriverNode]: New FollowJointTrajectoryGoal RECEIVED!"); 
00352     driver_.lock();
00353     driver_.move_trajectory_in_joints(goal->trajectory);
00354     driver_.unlock();
00355     ROS_INFO("[WamDriverNode]: FollowJointTrajectoryGoal SENT TO ROBOT!"); 
00356 }
00357 
00358 void
00359 WamDriverNode::joint_trajectoryStopCallback(void) 
00360 {
00361     ROS_INFO("[WamDriverNode]: New CancelFollowJointTrajectoryAction RECEIVED!"); 
00362     driver_.lock();
00363     driver_.stop_trajectory_in_joints();
00364     driver_.unlock();
00365     ROS_INFO("[WamDriverNode]: CancelFollowJointTrajectoryAction SENT TO ROBOT!"); 
00366 }
00367 
00368 bool
00369 WamDriverNode::joint_trajectoryIsFinishedCallback(void) 
00370 {
00371     bool ret = false;
00372     ROS_INFO("[WamDriverNode]: FollowJointTrajectory NOT FINISHED"); 
00373 
00374     // This sleep assures that the robot receives the trajectory and starts moving
00375     sleep(1);
00376 
00377     driver_.lock();
00378     if (! driver_.is_moving()){
00379         ret = true;
00380         ROS_INFO("[WamDriverNode]: FollowJointTrajectory FINISHED!"); 
00381     }
00382     driver_.unlock();
00383 
00384     return ret;
00385 }
00386 
00387 bool WamDriverNode::joint_trajectoryHasSucceedCallback(void) 
00388 { 
00389   bool ret = false; 
00390 
00391   driver_.lock();
00392   //This callback is called when the trajectory is Finished so by
00393   // default is always true
00394   ret = true;
00395   driver_.unlock(); 
00396 
00397   ROS_INFO("[WamDriverNode]: FollowJointTrajectory SUCCEEDED!");
00398 
00399   return ret; 
00400 } 
00401 
00402 void
00403 WamDriverNode::joint_trajectoryGetResultCallback(control_msgs::FollowJointTrajectoryResultPtr& result) 
00404 {
00405     // MSG has an empty result message
00406     driver_.lock();
00407     if (driver_.is_joint_trajectory_result_succeeded()) {
00408       result->error_code = 0;
00409     } else {
00410       result->error_code = -1;
00411     }
00412     driver_.unlock();
00413 }
00414 
00415 void WamDriverNode::joint_trajectoryGetFeedbackCallback(control_msgs::FollowJointTrajectoryFeedbackPtr& feedback) 
00416 { 
00417   driver_.lock(); 
00418     //keep track of feedback 
00419     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00420   feedback->desired = driver_.get_desired_joint_trajectory_point();
00421 
00422   driver_.unlock(); 
00423 }
00424 void WamDriverNode::goalCB(GoalHandle gh)
00425 {
00426     gh.setAccepted();
00427     bool state=false;
00428     trajectory_msgs::JointTrajectory traj = gh.getGoal()->trajectory;
00429     trajectory2follow(traj,state);
00430     if(state) gh.setSucceeded();
00431     else gh.setAborted();
00432 }
00433 void WamDriverNode::goalFollowCB(GoalHandleFollow gh)
00434 {
00435     gh.setAccepted();
00436     bool state=false;
00437     trajectory_msgs::JointTrajectory traj = gh.getGoal()->trajectory;
00438     trajectory2follow(traj,state);
00439     if(state) gh.setSucceeded();
00440     else gh.setAborted();
00441 }
00442 void WamDriverNode::trajectory2follow(trajectory_msgs::JointTrajectory traj, bool& state)
00443 {
00444         this->driver_.lock();
00445         for(unsigned int ii=0; ii < traj.points.size(); ++ii)
00446         {
00447         if(this->driver_.isRunning())
00448         {
00449             //ii=traj.points.size()-1;
00450             this->driver_.move_in_joints(&traj.points[ii].positions); //this call blocks if the wam faults. The mutex is not freed...!
00451             this->driver_.unlock();
00452             this->driver_.wait_move_end();
00453         }
00454         else
00455         {
00456                     ROS_FATAL("[Trajectory2follow] Driver is not running");
00457                     state=false;
00458             }
00459     }
00460    state=true;  
00461 } 
00462 
00463 
00464 
00465 /*  [action requests] */
00466 
00467 void WamDriverNode::postNodeOpenHook(void)
00468 {
00469 }
00470 
00471 void WamDriverNode::addNodeDiagnostics(void)
00472 {
00473 }
00474 
00475 void WamDriverNode::addNodeOpenedTests(void)
00476 {
00477 }
00478 
00479 void WamDriverNode::addNodeStoppedTests(void)
00480 {
00481 }
00482 
00483 void WamDriverNode::addNodeRunningTests(void)
00484 {
00485 }
00486 
00487 void WamDriverNode::reconfigureNodeHook(int level)
00488 {
00489 }
00490 
00491 WamDriverNode::~WamDriverNode()
00492 {
00493   //[free dynamic memory]
00494 }
00495 
00496 /* main function */
00497 int main(int argc,char *argv[])
00498 {
00499   return driver_base::main<WamDriverNode>(argc,argv,"wam_driver_node");
00500 }


iri_wam_wrapper
Author(s): Pol Monso
autogenerated on Fri Dec 6 2013 21:47:20