dabo_arm_node_driver_node.cpp
Go to the documentation of this file.
00001 #include "dabo_arm_node_driver_node.h"
00002 
00003 DaboArmNodeDriverNode::DaboArmNodeDriverNode(ros::NodeHandle &nh) : 
00004   iri_base_driver::IriBaseNodeDriver<DaboArmNodeDriver>(nh),
00005   joint_motion_aserver_(public_node_handle_, "joint_motion"),
00006   motion_sequence_aserver_(public_node_handle_, "motion_sequence")
00007 {
00008   XmlRpc::XmlRpcValue files_param_list;
00009   XmlRpc::XmlRpcValue param_list_item;
00010   unsigned int i=0;
00011 
00012   //init class attributes if necessary
00013   this->loop_rate_ = 100;//in [Hz]
00014 
00015   // [init publishers]
00016   this->joint_feedback_publisher_ = this->public_node_handle_.advertise<sensor_msgs::JointState>("joint_feedback", 10);
00017   
00018   // [init subscribers]
00019   
00020   // [init services]
00021   
00022   // [init clients]
00023   
00024   // [init action servers]
00025   joint_motion_aserver_.registerStartCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionStartCallback, this, _1)); 
00026   joint_motion_aserver_.registerStopCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionStopCallback, this)); 
00027   joint_motion_aserver_.registerIsFinishedCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionIsFinishedCallback, this)); 
00028   joint_motion_aserver_.registerHasSucceedCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionHasSucceedCallback, this)); 
00029   joint_motion_aserver_.registerGetResultCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionGetResultCallback, this, _1)); 
00030   joint_motion_aserver_.registerGetFeedbackCallback(boost::bind(&DaboArmNodeDriverNode::joint_motionGetFeedbackCallback, this, _1)); 
00031 
00032   motion_sequence_aserver_.registerStartCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceStartCallback, this, _1)); 
00033   motion_sequence_aserver_.registerStopCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceStopCallback, this)); 
00034   motion_sequence_aserver_.registerIsFinishedCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceIsFinishedCallback, this)); 
00035   motion_sequence_aserver_.registerHasSucceedCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceHasSucceedCallback, this)); 
00036   motion_sequence_aserver_.registerGetResultCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceGetResultCallback, this, _1)); 
00037   motion_sequence_aserver_.registerGetFeedbackCallback(boost::bind(&DaboArmNodeDriverNode::motion_sequenceGetFeedbackCallback, this, _1)); 
00038   
00039   // [init action clients]
00040   files_param_list.setSize(this->driver_.get_num_motion_seq_files());
00041   for(i=0;i<this->driver_.get_num_motion_seq_files();i++)
00042   {
00043     param_list_item=this->driver_.get_motion_seq_file(i);
00044     files_param_list[i]=param_list_item;
00045   }
00046   this->public_node_handle_.setParam("motion_files",files_param_list);
00047   files_param_list.clear();
00048   files_param_list.setSize(this->driver_.get_num_config_files());
00049   for(i=0;i<this->driver_.get_num_config_files();i++)
00050   {
00051     param_list_item=this->driver_.get_config_file(i);
00052     files_param_list[i]=param_list_item;
00053   }
00054   this->public_node_handle_.setParam("config_files",files_param_list);
00055   files_param_list.clear();
00056 
00057   this->public_node_handle_.getParam("side_id",this->side_id);
00058   this->JointState_msg_.name.clear();
00059   if(this->side_id=="left")
00060   {
00061     this->JointState_msg_.name.push_back("left_shoulder_yaw");
00062     this->JointState_msg_.name.push_back("left_shoulder_pitch");
00063     this->JointState_msg_.name.push_back("left_shoulder_roll");
00064     this->JointState_msg_.name.push_back("left_elbow");
00065   }
00066   else if(this->side_id=="right")
00067   {
00068     this->JointState_msg_.name.push_back("right_shoulder_yaw");
00069     this->JointState_msg_.name.push_back("right_shoulder_pitch");
00070     this->JointState_msg_.name.push_back("right_shoulder_roll");
00071     this->JointState_msg_.name.push_back("right_elbow");
00072   }
00073   else
00074   {
00075     this->JointState_msg_.name.resize(4);
00076   }
00077 }
00078 
00079 void DaboArmNodeDriverNode::mainNodeThread(void)
00080 {
00081   std::vector<double> position(4);
00082   std::vector<double> velocity(4);
00083   unsigned int i=0;
00084 
00085   //lock access to driver if necessary
00086   this->driver_.lock();
00087 
00088   // update the loop rate
00089   this->loop_rate_ = this->driver_.get_feedback_rate();//in [Hz]
00090 
00091   // [fill msg Header if necessary]
00092   this->JointState_msg_.header.stamp = ros::Time::now();
00093   //<publisher_name>.header.frame_id = "<publisher_topic_name>";
00094 
00095   // [fill msg structures]
00096   this->driver_.get_position(position);
00097   this->driver_.get_velocity(velocity);
00098   this->JointState_msg_.position.clear();
00099   this->JointState_msg_.velocity.clear();
00100   for(i=0;i<position.size();i++)
00101   {
00102     // convert from degrees to radians
00103     this->JointState_msg_.position.push_back(position[i]*3.14159/180.0);
00104     this->JointState_msg_.velocity.push_back(velocity[i]*3.14159/180.0);
00105   }
00106   // [fill srv structure and make request to the server]
00107 
00108   // [fill action structure and make request to the action server]
00109 
00110   // [publish messages]
00111   this->joint_feedback_publisher_.publish(this->JointState_msg_);
00112 
00113   //unlock access to driver if previously blocked
00114   this->driver_.unlock();
00115 }
00116 
00117 void DaboArmNodeDriverNode::check_motion_sequence_status(diagnostic_updater::DiagnosticStatusWrapper &stat)
00118 {
00119   CEventServer *event_server=CEventServer::instance();
00120 
00121   if(driver_.isRunning())
00122   {
00123     if(this->driver_.get_motion_sequence_complete_event_id()!="")
00124     {
00125       if(event_server->event_is_set(this->driver_.get_motion_sequence_complete_event_id()))
00126       {
00127         if(event_server->event_is_set(this->driver_.get_motion_sequence_error_event_id()))
00128         {
00129           stat.summary(1,"Sequence has finished with errors");
00130           stat.add("Reported error",this->driver_.get_motion_sequence_error_message());
00131         }
00132         else
00133         {
00134           stat.summary(0,"Sequence has finished ok");
00135         }
00136       }
00137       else
00138         stat.summary(0,"Sequence is in progress");
00139     }
00140     else
00141       stat.summary(0,"No sequence has been executed");
00142   }
00143 }
00144 
00145 /*  [subscriber callbacks] */
00146 
00147 /*  [service callbacks] */
00148 
00149 /*  [action callbacks] */
00150 void DaboArmNodeDriverNode::joint_motionStartCallback(const control_msgs::FollowJointTrajectoryGoalConstPtr& goal)
00151 {
00152   control_msgs::FollowJointTrajectoryResult result;
00153   std::vector<double> current_position(4);
00154   std::vector<TDynamixelMotionStep> seq;
00155   unsigned int i=0,j=0;
00156   TDynamixelMotionStep step;
00157 
00158   //lock access to driver if necessary
00159   this->driver_.lock();
00160 
00161   if( driver_.isRunning() )
00162   {
00163     if(this->motion_sequence_aserver_.isActive())
00164       this->joint_motion_aserver_.setAborted(result,std::string("A motion sequence has been started"));
00165     else
00166     {
00167       try{
00168         this->driver_.get_position(current_position);
00169         // generate and execute the motion sequence
00170         for(i=0;i<goal->trajectory.points.size();i++)
00171         {
00172           step.position=current_position;
00173           step.velocity.resize(4,0.0);
00174           for(j=0;j<goal->trajectory.joint_names.size();j++)
00175           {
00176             if(this->side_id=="left")
00177             {
00178               if(goal->trajectory.joint_names[j]=="left_shoulder_yaw")
00179               {
00180                 step.position[0]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00181                 step.velocity[0]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00182               }
00183               else if(goal->trajectory.joint_names[j]=="left_shoulder_pitch")
00184               {
00185                 step.position[1]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00186                 step.velocity[1]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00187               }
00188               else if(goal->trajectory.joint_names[j]=="left_shoulder_roll")
00189               {
00190                 step.position[2]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00191                 step.velocity[2]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00192               }
00193               else if(goal->trajectory.joint_names[j]=="left_elbow")
00194               {
00195                 step.position[3]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00196                 step.velocity[3]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00197               }
00198             }
00199             else if(this->side_id=="right")
00200             {
00201               if(goal->trajectory.joint_names[j]=="right_shoulder_yaw")
00202               {
00203                 step.position[0]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00204                 step.velocity[0]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00205               }
00206               else if(goal->trajectory.joint_names[j]=="right_shoulder_pitch")
00207               {
00208                 step.position[1]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00209                 step.velocity[1]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00210               }
00211               else if(goal->trajectory.joint_names[j]=="Right_shoulder_roll")
00212               {
00213                 step.position[2]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00214                 step.velocity[2]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00215               }
00216               else if(goal->trajectory.joint_names[j]=="right_elbow")
00217               {
00218                 step.position[3]=goal->trajectory.points[i].positions[j]*180.0/3.14159;
00219                 step.velocity[3]=goal->trajectory.points[i].velocities[j]*180.0/3.14159;
00220               }
00221             }
00222           }
00223           step.delay=goal->trajectory.points[i].time_from_start.toSec()*1000;
00224           seq.push_back(step);
00225           // set the next current position;
00226           current_position=step.position;
00227         }
00228         this->driver_.start_motion_sequence(seq);
00229       }catch(CException &e){
00230         this->joint_motion_aserver_.setAborted(result);
00231       }
00232     }
00233   }
00234 
00235   //unlock access to driver if necessary
00236   this->driver_.unlock();
00237 } 
00238 
00239 void DaboArmNodeDriverNode::joint_motionStopCallback(void) 
00240 {
00241   //lock access to driver if necessary
00242   this->driver_.lock();
00243 
00244   if( driver_.isRunning() )
00245   {
00246     try{
00247       this->driver_.stop();
00248     }catch(CException &e){
00249       this->driver_.unlock();
00250       throw;
00251     }
00252   }
00253 
00254   //unlock access to driver if necessary
00255   this->driver_.unlock();
00256 } 
00257 
00258 bool DaboArmNodeDriverNode::joint_motionIsFinishedCallback(void) 
00259 { 
00260   CEventServer *event_server=CEventServer::instance();
00261   bool finished=false;
00262 
00263   //lock access to driver if necessary
00264   this->driver_.lock();
00265 
00266   if( driver_.isRunning() )
00267   {
00268     finished=event_server->event_is_set(this->driver_.get_motion_sequence_complete_event_id());
00269   }
00270 
00271   //unlock access to driver if necessary
00272   this->driver_.unlock();
00273 
00274   return finished;
00275 } 
00276 
00277 bool DaboArmNodeDriverNode::joint_motionHasSucceedCallback(void) 
00278 { 
00279   CEventServer *event_server=CEventServer::instance();
00280 
00281   //lock access to driver if necessary
00282   this->driver_.lock();
00283   bool success=false;
00284 
00285   if( driver_.isRunning() )
00286   {
00287     success=!event_server->event_is_set(this->driver_.get_motion_sequence_error_event_id());
00288   }
00289 
00290   //unlock access to driver if necessary
00291   this->driver_.unlock();
00292 
00293   return success;
00294 } 
00295 
00296 void DaboArmNodeDriverNode::joint_motionGetResultCallback(control_msgs::FollowJointTrajectoryResultPtr& result) 
00297 { 
00298   CEventServer *event_server=CEventServer::instance();
00299 
00300   //lock access to driver if necessary
00301   this->driver_.lock();
00302 
00303   if( driver_.isRunning() )
00304   {
00305     if(event_server->event_is_set(this->driver_.get_motion_sequence_error_event_id()))
00306       result->error_code=-1;
00307     else
00308       result->error_code=0;
00309   }
00310   
00311   //unlock access to driver if necessary
00312   this->driver_.unlock();
00313 } 
00314 
00315 void DaboArmNodeDriverNode::joint_motionGetFeedbackCallback(control_msgs::FollowJointTrajectoryFeedbackPtr& feedback) 
00316 { 
00317   std::vector<double> position,velocity;
00318   unsigned int i=0;
00319 
00320   //lock access to driver if necessary
00321   this->driver_.lock();
00322 
00323   if( driver_.isRunning() )
00324   {
00325     try{
00326       this->driver_.get_position(position);
00327       this->driver_.get_velocity(velocity);
00328       for(i=0;i<position.size();i++)
00329       {
00330         feedback->actual.positions.push_back(position[i]);
00331         feedback->actual.velocities.push_back(velocity[i]);
00332       }
00333     }catch(CException &e){
00334       this->driver_.unlock();
00335       throw;
00336     }
00337   }
00338 
00339   //unlock access to driver if necessary
00340   this->driver_.unlock();
00341 }
00342 void DaboArmNodeDriverNode::motion_sequenceStartCallback(const tibi_dabo_msgs::sequenceGoalConstPtr& goal)
00343 { 
00344   std::string file;
00345   tibi_dabo_msgs::sequenceResult result;
00346 
00347   //lock access to driver if necessary
00348   this->driver_.lock();
00349 
00350   if( driver_.isRunning() )
00351   {
00352     if(this->joint_motion_aserver_.isActive())
00353     {
00354       result.successful.push_back(false);
00355       result.observations.push_back(std::string("A joint motion is in progress"));
00356       this->motion_sequence_aserver_.setAborted(result);
00357     }
00358     else
00359     {
00360       try{
00361         // add the full path to the incomming filename
00362         ROS_INFO("Arm motion sequence file %s",goal->sequence_file[0].c_str());
00363         file=goal->sequence_file[0];
00364         this->driver_.start_motion_sequence(file);
00365       }catch(CException &e){
00366         result.successful.push_back(false);
00367         result.observations.push_back(e.what());
00368         this->motion_sequence_aserver_.setAborted(result);
00369       }
00370     }
00371   }
00372 
00373   //unlock access to driver if necessary
00374   this->driver_.unlock();
00375 } 
00376 
00377 void DaboArmNodeDriverNode::motion_sequenceStopCallback(void) 
00378 { 
00379   //lock access to driver if necessary
00380   this->driver_.lock();
00381 
00382   if( driver_.isRunning() )
00383   {
00384     try{
00385       this->driver_.stop_motion_sequence();
00386     }catch(CException &e){
00387       this->driver_.unlock();
00388       throw;
00389     }
00390   }
00391 
00392   //unlock access to driver if necessary
00393   this->driver_.unlock();
00394 } 
00395 
00396 bool DaboArmNodeDriverNode::motion_sequenceIsFinishedCallback(void) 
00397 { 
00398   CEventServer *event_server=CEventServer::instance();
00399   bool finished=false;
00400 
00401   //lock access to driver if necessary
00402   this->driver_.lock();
00403 
00404   if( driver_.isRunning() )
00405   {
00406     finished=event_server->event_is_set(this->driver_.get_motion_sequence_complete_event_id());
00407   }
00408 
00409   //unlock access to driver if necessary
00410   this->driver_.unlock();
00411 
00412   return finished;
00413 } 
00414 
00415 bool DaboArmNodeDriverNode::motion_sequenceHasSucceedCallback(void) 
00416 { 
00417   CEventServer *event_server=CEventServer::instance();
00418   bool success=false;
00419 
00420   //lock access to driver if necessary
00421   this->driver_.lock();
00422 
00423   if( driver_.isRunning() )
00424   {
00425     success=!event_server->event_is_set(this->driver_.get_motion_sequence_error_event_id());
00426   }
00427 
00428   //unlock access to driver if necessary
00429   this->driver_.unlock();
00430 
00431   return success;
00432 } 
00433 
00434 void DaboArmNodeDriverNode::motion_sequenceGetResultCallback(tibi_dabo_msgs::sequenceResultPtr& result) 
00435 { 
00436   CEventServer *event_server=CEventServer::instance();
00437 
00438   //lock access to driver if necessary
00439   this->driver_.lock();
00440 
00441   if( driver_.isRunning() )
00442   {
00443     result->successful.clear();
00444     result->observations.clear();
00445     result->successful.push_back(!event_server->event_is_set(this->driver_.get_motion_sequence_error_event_id()));
00446     result->observations.push_back(this->driver_.get_motion_sequence_error_message());
00447   }
00448 
00449   //unlock access to driver if necessary
00450   this->driver_.unlock();
00451 } 
00452 
00453 void DaboArmNodeDriverNode::motion_sequenceGetFeedbackCallback(tibi_dabo_msgs::sequenceFeedbackPtr& feedback) 
00454 { 
00455   //lock access to driver if necessary
00456   this->driver_.lock();
00457 
00458   if( driver_.isRunning() )
00459   {
00460     try{
00461       feedback->percentage.clear();
00462       feedback->percentage.push_back(this->driver_.get_motion_sequence_completed_percentage());
00463     }catch(CException &e){
00464       this->driver_.unlock();
00465       throw;
00466     }
00467   }
00468 
00469   //unlock access to driver if necessary
00470   this->driver_.unlock();
00471 }
00472 
00473 /*  [action requests] */
00474 
00475 void DaboArmNodeDriverNode::postNodeOpenHook(void)
00476 {
00477   motion_sequence_aserver_.start();
00478   ROS_INFO("DaboArmDriverNode:: Motion sequence server started!");
00479   joint_motion_aserver_.start();
00480   ROS_INFO("DaboArmDriverNode:: Joint motion server started!");
00481 }
00482 
00483 void DaboArmNodeDriverNode::preNodeCloseHook(void)
00484 {
00485   if(this->motion_sequence_aserver_.isStarted())
00486   {
00487     this->motion_sequence_aserver_.shutdown();
00488     ROS_INFO("DaboHeadDriverNode:: Motion sequence server stopped!");
00489   }
00490   if(this->joint_motion_aserver_.isStarted())
00491   {
00492     this->joint_motion_aserver_.shutdown();
00493     ROS_INFO("DaboHeadDriverNode:: Joint motion server stopped!");
00494   }
00495 //  ros::shutdown();
00496 }
00497 
00498 void DaboArmNodeDriverNode::addNodeDiagnostics(void)
00499 {
00500   diagnostic_.add("Motion sequence status", this, &DaboArmNodeDriverNode::check_motion_sequence_status);
00501 }
00502 
00503 void DaboArmNodeDriverNode::addNodeOpenedTests(void)
00504 {
00505 }
00506 
00507 void DaboArmNodeDriverNode::addNodeStoppedTests(void)
00508 {
00509 }
00510 
00511 void DaboArmNodeDriverNode::addNodeRunningTests(void)
00512 {
00513 }
00514 
00515 void DaboArmNodeDriverNode::reconfigureNodeHook(int level)
00516 {
00517 }
00518 
00519 DaboArmNodeDriverNode::~DaboArmNodeDriverNode(void)
00520 {
00521   // [free dynamic memory]
00522   this->driver_.lock();
00523   // [free dynamic memory]
00524   if(this->motion_sequence_aserver_.isActive())
00525   {
00526     this->motion_sequence_aserver_.setAborted();
00527     this->driver_.stop_motion_sequence();
00528   }
00529   this->driver_.unlock();
00530 }
00531 
00532 /* main function */
00533 int main(int argc,char *argv[])
00534 {
00535   return driver_base::main<DaboArmNodeDriverNode>(argc, argv, "dabo_arm_node_driver_node");
00536 }


dabo_arm_node
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 21:40:30