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


tibi_dabo_arm_node
Author(s): Sergi Hernandez Juan
autogenerated on Fri Sep 27 2013 10:24:22