convert_pose_alg_node.cpp
Go to the documentation of this file.
00001 #include "convert_pose_alg_node.h"
00002 
00003 ConvertPoseAlgNode::ConvertPoseAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<ConvertPoseAlgorithm>(),
00005   tracking_head_client_("tracking_head", true),
00006   move_joints_client_("move_joints", true),
00007   move_right_arm_client_("move_right_arm", true),
00008   move_left_arm_client_("move_left_arm", true),
00009   tf_listener(ros::Duration(10.0))
00010 {
00011   //init class attributes if necessary
00012   this->loop_rate_ = 10;//in [Hz]
00013 
00014   // [init publishers]  
00015   this->cmd_vel_publisher_ = this->public_node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 100);
00016   this->head_target_publisher_ = this->public_node_handle_.advertise<std_msgs::Float64MultiArray>("head_target", 100);
00017   this->execute_action_publisher_ = this->public_node_handle_.advertise<std_msgs::Int32>("execute_action", 100);
00018   
00019   // [init subscribers]
00020   this->adc_channels_subscriber_ = this->public_node_handle_.subscribe("adc_channels", 100, &ConvertPoseAlgNode::adc_channels_callback, this);
00021   this->joint_state_subscriber_ = this->public_node_handle_.subscribe("joint_states", 100, &ConvertPoseAlgNode::joint_state_callback, this);
00022   this->marker_subscriber_ = this->public_node_handle_.subscribe("marker", 100, &ConvertPoseAlgNode::marker_callback, this);
00023   
00024   // [init services]
00025   
00026   // [init clients]
00027   get_ik_client_ = this->public_node_handle_.serviceClient<kinematics_msgs::GetPositionIK>("get_ik");
00028   
00029   // [init action servers]
00030   
00031   // [init action clients]
00032   this->ready=false;
00033   this->joints_moving=false;
00034   this->right_arm_moving=false;
00035   this->left_arm_moving=false;  
00036   this->ready_for_check=false;
00037   this->left_ok=false;
00038   this->right_ok=false;  
00039   this->prev_left_ok=false;
00040   this->prev_right_ok=false;
00041   this->left_done=false;  
00042   this->left_marker_found=false;
00043   this->right_marker_found=false;
00044   this->first_goal=true;
00045   this->current_pan=0.0;
00046   this->n=0;  
00047   this->left_data_ready=false;
00048   this->right_data_ready=false;
00049   this->left_ready_tf=false;
00050   this->right_ready_tf=false;
00051   this->success=true;
00052  
00053   this->state=initial_pose;    
00054 
00055   sleep(10);
00056 }
00057 
00058 ConvertPoseAlgNode::~ConvertPoseAlgNode(void)
00059 {
00060   // [free dynamic memory]
00061 }
00062 
00063 void ConvertPoseAlgNode::mainNodeThread(void)
00064 {
00065   int i=0;  
00066   
00067   // [fill msg structures]  
00068   
00069   // [fill srv structure and make request to the server]
00070      
00071   // [fill action structure and make request to the action server]  
00072 
00073   // [publish messages]  
00074   ROS_INFO("Main Thread");
00075   try{
00076   switch(this->state)
00077   {
00078     case initial_pose:  ROS_INFO("Current state: Initial pose");
00079                         this->Int32_msg_.data=15;
00080                         this->execute_action_publisher_.publish(this->Int32_msg_);
00081                         this->move_joints_goal_.trajectory.header.seq=0;
00082                         this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00083                         this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00084                         this->move_joints_goal_.trajectory.joint_names.resize(2);
00085                         this->move_joints_goal_.trajectory.joint_names[0]="j_high_arm_l";
00086                         this->move_joints_goal_.trajectory.joint_names[1]="j_high_arm_r";
00087                         this->move_joints_goal_.trajectory.points.resize(1);
00088                         this->move_joints_goal_.trajectory.points[0].positions.resize(2);
00089                         this->move_joints_goal_.trajectory.points[0].velocities.resize(2);
00090                         this->move_joints_goal_.trajectory.points[0].accelerations.resize(2);
00091                         this->move_joints_goal_.trajectory.points[0].positions[0]=0.0;
00092                         this->move_joints_goal_.trajectory.points[0].positions[1]=0.0;
00093                         for(i=0;i<2;i++){
00094                           this->move_joints_goal_.trajectory.points[0].velocities[i]=60.0;
00095                           this->move_joints_goal_.trajectory.points[0].accelerations[i]=100.0;
00096                         }
00097                         this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00098                         move_jointsMakeActionRequest();
00099                         this->success=true;
00100                         this->ready=false;
00101                         this->left_done=false;
00102                         this->left_marker_found=false;
00103                         this->right_marker_found=false;
00104                         this->state=initial_angle;                                                              
00105                         break;
00106 
00107     case initial_angle: ROS_INFO("Current state: Initial angle");
00108                         if(this->joints_moving)
00109                           this->state=initial_angle;
00110                         else{                                           
00111                           this->move_joints_goal_.trajectory.header.seq=0;
00112                           this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00113                           this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00114                           this->move_joints_goal_.trajectory.joint_names.resize(2);
00115                           this->move_joints_goal_.trajectory.joint_names[0]="j_tilt";
00116                           this->move_joints_goal_.trajectory.joint_names[1]="j_pan";
00117                           this->move_joints_goal_.trajectory.points.resize(1);
00118                           this->move_joints_goal_.trajectory.points[0].positions.resize(2);
00119                           this->move_joints_goal_.trajectory.points[0].velocities.resize(2);
00120                           this->move_joints_goal_.trajectory.points[0].accelerations.resize(2);
00121                           this->move_joints_goal_.trajectory.points[0].positions[0]=40.0-5.0*this->n;
00122                           this->move_joints_goal_.trajectory.points[0].positions[1]=0.0;
00123                           for(i=0;i<2;i++){
00124                             this->move_joints_goal_.trajectory.points[0].velocities[i]=60.0;
00125                             this->move_joints_goal_.trajectory.points[0].accelerations[i]=100.0;
00126                           }
00127                           this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00128                           move_jointsMakeActionRequest();                                               
00129                           if(this->n>=3)
00130                             this->state=move_back;
00131                           else{
00132                             if(this->left_done)
00133                               this->state=search_right_marker;
00134                             else
00135                               this->state=search_left_marker;                                                                                                   
00136                           }
00137                           this->n++;
00138                         }
00139                         break;
00140 
00141     case move_back:     ROS_INFO("Current state: Going back");
00142                         this->n=0;
00143                         this->ready=false;
00144                         this->Int32_msg_.data=16;
00145                         this->execute_action_publisher_.publish(this->Int32_msg_);
00146                         sleep(2);
00147                         this->Twist_msg_.linear.x=-0.01;
00148                         this->Twist_msg_.linear.y=0.0;
00149                         this->Twist_msg_.linear.z=0.0;
00150                         this->Twist_msg_.angular.x=0.0;
00151                         this->Twist_msg_.angular.y=0.0;
00152                         this->Twist_msg_.angular.z=0.0;
00153                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00154                         sleep(2);
00155                         this->state=initial_pose;                        
00156                         break;
00157 
00158     case search_left_marker:    ROS_INFO("Current state: Searching left marker");
00159                                 if(this->left_marker_found){
00160                                   ROS_WARN("Left marker found");
00161                                   move_joints_client_.cancelGoal();
00162                                   this->left_ready_tf=true;
00163                                   this->state=transform_left_pose;
00164                                 }else{
00165                                   if(this->joints_moving)
00166                                     this->state=search_left_marker;                                                                                             
00167                                   else{                                                          
00168                                     this->ready=true; 
00169                                     this->move_joints_goal_.trajectory.header.seq=0;
00170                                     this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00171                                     this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00172                                     this->move_joints_goal_.trajectory.joint_names.resize(1);
00173                                     this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00174                                     this->move_joints_goal_.trajectory.points.resize(1);
00175                                     this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00176                                     this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00177                                     this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00178                                     this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00179                                     this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00180                                     this->move_joints_goal_.trajectory.points[0].positions[0]=40.0;                        
00181                                     this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00182                                     move_jointsMakeActionRequest();                                                      
00183                                     if(this->current_state.position[18]*180.0/3.14159>=39.0)
00184                                       this->state=initial_angle; 
00185                                   }
00186                                 }                               
00187                                 break;
00188 
00189     case transform_left_pose:   ROS_INFO("Current state: Transforming left pose");                              
00190                                 if(this->joints_moving)
00191                                   this->state=transform_left_pose;
00192                                 else{                                                                                     
00193                                   this->move_joints_goal_.trajectory.header.seq=0;
00194                                   this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00195                                   this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00196                                   this->move_joints_goal_.trajectory.joint_names.resize(1);
00197                                   this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00198                                   this->move_joints_goal_.trajectory.points.resize(1);
00199                                   this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00200                                   this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00201                                   this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00202                                   this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00203                                   this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00204                                   this->move_joints_goal_.trajectory.points[0].positions[0]=this->current_pan;                            
00205                                   this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00206                                   move_jointsMakeActionRequest();
00207                                 }                               
00208                                 if(this->left_data_ready){
00209                                   ROS_WARN("Left pose transformed");                              
00210                                   this->state=search_right_marker;                                
00211                                 }else
00212                                   this->state=transform_left_pose;
00213                                 break;
00214    
00215     case search_right_marker:   ROS_INFO("Current state: Searching right marker");
00216                                 if(this->right_marker_found){
00217                                   ROS_WARN("Right marker found");
00218                                   move_joints_client_.cancelGoal();
00219                                   this->right_ready_tf=true;
00220                                   this->state=transform_right_pose;
00221                                 }else{
00222                                   if(this->joints_moving)
00223                                     this->state=search_right_marker;                                                              
00224                                   else{                           
00225                                     this->move_joints_goal_.trajectory.header.seq=0;
00226                                     this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00227                                     this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00228                                     this->move_joints_goal_.trajectory.joint_names.resize(1);
00229                                     this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00230                                     this->move_joints_goal_.trajectory.points.resize(1);
00231                                     this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00232                                     this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00233                                     this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00234                                     this->move_joints_goal_.trajectory.points[0].positions[0]=-40.0;
00235                                     this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00236                                     this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00237                                     this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00238                                     move_jointsMakeActionRequest();
00239                                     if(this->current_state.position[18]*180.0/3.14159<=-39.0)
00240                                       this->state=initial_angle;                                          
00241                                   }
00242                                 }                               
00243                                 break;
00244 
00245     case transform_right_pose:  ROS_INFO("Current state: Transforming right pose");                             
00246                                 if(this->joints_moving)
00247                                   this->state=transform_right_pose;
00248                                 else{                                                               
00249                                   this->move_joints_goal_.trajectory.header.seq=0;
00250                                   this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00251                                   this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00252                                   this->move_joints_goal_.trajectory.joint_names.resize(1);
00253                                   this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00254                                   this->move_joints_goal_.trajectory.points.resize(1);
00255                                   this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00256                                   this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00257                                   this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00258                                   this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00259                                   this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00260                                   this->move_joints_goal_.trajectory.points[0].positions[0]=this->current_pan;
00261                                   this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00262                                   move_jointsMakeActionRequest();                                                                 
00263                                 }                               
00264                                 if(this->right_data_ready){
00265                                   ROS_WARN("Right pose transformed");                                  
00266                                   this->ready_for_check=true;
00267                                   this->state=make_decision;
00268                                 }else
00269                                   this->state=transform_right_pose;
00270                                 break;
00271 
00272     case make_decision: ROS_INFO("Current state: Looking if the final pose have inverse kinematics solution");
00273                         if(this->joints_moving)
00274                           this->state=make_decision;
00275                         else{
00276                           //Mirar si hi ha solució o no i actuar en conseqüència.                     
00277                           this->get_ik_srv_.request.ik_request.ik_link_name="MP_ARM_GRIPPER_FIX_L";
00278                           // IK seed, get the current left arm position
00279                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(4);
00280                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="j_shoulder_l";
00281                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[1]="j_high_arm_l";
00282                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[2]="j_low_arm_l";
00283                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[3]="j_wrist_l";                     
00284                           this->joint_state_mutex_.enter();
00285                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(4);
00286                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=this->current_state.position[2];
00287                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[1]=this->current_state.position[4];
00288                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[2]=this->current_state.position[6];
00289                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[3]=this->current_state.position[23];
00290                           this->joint_state_mutex_.exit();
00291                           this->get_ik_srv_.request.ik_request.pose_stamped=left_pose_;
00292                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.x=left_pose_.pose.position.x+0.02;
00293                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.y=left_pose_.pose.position.y;
00294                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.z=left_pose_.pose.position.z-0.16;
00295                           // ignore the rest of the infomation for the moment
00296                           if(get_ik_client_.call(get_ik_srv_)){                                              
00297                             if(this->get_ik_srv_.response.error_code.val==this->get_ik_srv_.response.error_code.SUCCESS)// success
00298                               this->left_ok=true;
00299                             else this->left_ok=false;               
00300                           }else
00301                             ROS_INFO("ConvertPoseAlgNode:: Failed to Call Server on topic get_ik ");                                       
00302                           
00303                           this->get_ik_srv_.request.ik_request.ik_link_name="MP_ARM_GRIPPER_FIX_R";
00304                           // IK seed, get the current right arm position
00305                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(4);
00306                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="j_shoulder_r";
00307                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[1]="j_high_arm_r";
00308                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[2]="j_low_arm_r";
00309                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[3]="j_wrist_r";                    
00310                           this->joint_state_mutex_.enter();
00311                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(4);
00312                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=this->current_state.position[1];
00313                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[1]=this->current_state.position[3];
00314                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[2]=this->current_state.position[5];
00315                           this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[3]=this->current_state.position[21];
00316                           this->joint_state_mutex_.exit();
00317                           this->get_ik_srv_.request.ik_request.pose_stamped=right_pose_;
00318                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.x=right_pose_.pose.position.x+0.03;
00319                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.y=right_pose_.pose.position.y+0.02;
00320                           this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.z=right_pose_.pose.position.z-0.18;
00321                           // ignore the rest of the infomation for the moment
00322                           if(get_ik_client_.call(get_ik_srv_)){
00323                             if(this->get_ik_srv_.response.error_code.val==this->get_ik_srv_.response.error_code.SUCCESS)// success
00324                               this->right_ok=true;
00325 
00326                             else this->right_ok=false;
00327                           }else
00328                             ROS_INFO("ConvertPoseAlgNode:: Failed to Call Server on topic get_ik ");
00329  
00330                           if((this->left_ok)&&(this->right_ok)){
00331                             ROS_WARN("Darwin can take the connectors");
00332                             this->state=make_decision2;
00333                           }else{
00334                             ROS_WARN("Darwin need to correct its position");
00335                             this->state=correct_position;       
00336                           }                         
00337                         }               
00338                         break;
00339 
00340     case make_decision2:        ROS_INFO("Current state: Looking if the previous pose have inverse kinematics solution");                                                                                 
00341                                 // Mirar si hi ha solució o no i actuar en conseqüència.                        
00342                                 this->get_ik_srv_.request.ik_request.ik_link_name="MP_ARM_GRIPPER_FIX_L";
00343                                 // IK seed, get the current left arm position
00344                                 this->left_pose_.header.stamp=ros::Time::now();
00345                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(4);
00346                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="j_shoulder_l";
00347                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[1]="j_high_arm_l";
00348                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[2]="j_low_arm_l";
00349                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[3]="j_wrist_l";
00350                                 this->joint_state_mutex_.enter();
00351                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(4);
00352                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=this->current_state.position[2];
00353                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[1]=this->current_state.position[4];
00354                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[2]=this->current_state.position[6];
00355                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[3]=this->current_state.position[23];
00356                                 this->joint_state_mutex_.exit();
00357                                 this->get_ik_srv_.request.ik_request.pose_stamped=left_pose_;
00358                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.x=left_pose_.pose.position.x-0.01;
00359                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.y=left_pose_.pose.position.y+0.04;
00360                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.z=left_pose_.pose.position.z-0.20;
00361                                 // ignore the rest of the infomation for the moment
00362                                 if(get_ik_client_.call(get_ik_srv_)){
00363                                   if(this->get_ik_srv_.response.error_code.val==this->get_ik_srv_.response.error_code.SUCCESS)// success
00364                                     this->prev_left_ok=true;
00365                                   else this->prev_left_ok=false;
00366                                 }else
00367                                   ROS_INFO("ConvertPoseAlgNode:: Failed to Call Server on topic get_ik ");
00368 
00369                                 this->get_ik_srv_.request.ik_request.ik_link_name="MP_ARM_GRIPPER_FIX_R";
00370                                 // IK seed, get the current right arm position
00371                                 this->right_pose_.header.stamp=ros::Time::now();
00372                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(4);
00373                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="j_shoulder_r";
00374                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[1]="j_high_arm_r";
00375                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[2]="j_low_arm_r";
00376                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.name[3]="j_wrist_r";
00377                                 this->joint_state_mutex_.enter();
00378                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(4);
00379                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=this->current_state.position[1];
00380                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[1]=this->current_state.position[3];
00381                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[2]=this->current_state.position[5];
00382                                 this->get_ik_srv_.request.ik_request.ik_seed_state.joint_state.position[3]=this->current_state.position[21];
00383                                 this->joint_state_mutex_.exit();
00384                                 this->get_ik_srv_.request.ik_request.pose_stamped=right_pose_;
00385                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.x=right_pose_.pose.position.x;
00386                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.y=right_pose_.pose.position.y+0.03;
00387                                 this->get_ik_srv_.request.ik_request.pose_stamped.pose.position.z=right_pose_.pose.position.z-0.20;
00388                                 // ignore the rest of the infomation for the moment
00389                                 if(get_ik_client_.call(get_ik_srv_)){
00390                                   if(this->get_ik_srv_.response.error_code.val==this->get_ik_srv_.response.error_code.SUCCESS)// success
00391                                     this->prev_right_ok=true;
00392                                   else this->prev_right_ok=false;
00393                                 }else
00394                                   ROS_INFO("ConvertPoseAlgNode:: Failed to Call Server on topic get_ik ");                          
00395                                 this->state=open_gripper;                                                       
00396                                 break;
00397 
00398     case open_gripper:  ROS_INFO("Current state: Open gripper");
00399                         this->move_joints_goal_.trajectory.header.seq=0;
00400                         this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00401                         this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00402                         this->move_joints_goal_.trajectory.joint_names.resize(2);
00403                         this->move_joints_goal_.trajectory.joint_names[0]="j_gripper_l";
00404                         this->move_joints_goal_.trajectory.joint_names[1]="j_gripper_r";
00405                         this->move_joints_goal_.trajectory.points.resize(1);
00406                         this->move_joints_goal_.trajectory.points[0].positions.resize(2);
00407                         this->move_joints_goal_.trajectory.points[0].velocities.resize(2);
00408                         this->move_joints_goal_.trajectory.points[0].accelerations.resize(2);                           
00409                         this->move_joints_goal_.trajectory.points[0].positions[0] = -30.0;
00410                         this->move_joints_goal_.trajectory.points[0].positions[1] = 30.0;                       
00411                         for(i=0;i<2;i++){
00412                           this->move_joints_goal_.trajectory.points[0].velocities[i] = 60.0;
00413                           this->move_joints_goal_.trajectory.points[0].accelerations[i] = 100.0;
00414                         }
00415                         this->move_joints_goal_.trajectory.points[0].time_from_start = ros::Duration(1.0);
00416                         move_jointsMakeActionRequest();
00417                         if(this->success)
00418                           this->state=take_connectors;
00419                         else{
00420                           this->state=correct_position;
00421                           sleep(1);
00422                         }
00423                         break;
00424 
00425     case take_connectors:       ROS_INFO("Current state: Open gripper");
00426                                 if(this->joints_moving)
00427                                   this->state=take_connectors;
00428                                 else{
00429                                   if(this->prev_left_ok){
00430                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints.resize(1);
00431                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.seq=0;
00432                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.stamp=ros::Time::now();
00433                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.frame_id="MP_BODY";
00434 
00435                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.x=this->left_pose_.pose.position.x-0.01;
00436                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.y=this->left_pose_.pose.position.y+0.04;
00437                                     this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.z=this->left_pose_.pose.position.z-0.20;
00438 
00439                                     move_left_armMakeActionRequest();
00440                                   }
00441                                   if(this->prev_right_ok){
00442                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints.resize(1);
00443                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.seq=0;
00444                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.stamp=ros::Time::now();
00445                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.frame_id="MP_BODY";
00446 
00447                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.x=this->right_pose_.pose.position.x;
00448                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.y=this->right_pose_.pose.position.y+0.03;
00449                                     this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.z=this->right_pose_.pose.position.z-0.20;
00450                                   
00451                                     move_right_armMakeActionRequest();
00452                                   }
00453                                   this->state=take_connectors2;
00454                                 }
00455                                 break;
00456     
00457     case take_connectors2:      ROS_INFO("Current state: Moving arms to a previous pose");
00458                                 if((this->right_arm_moving)||(this->left_arm_moving))
00459                                   this->state=take_connectors2;
00460                                 else{
00461                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints.resize(1);
00462                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.seq=0;
00463                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.stamp=ros::Time::now();
00464                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.frame_id="MP_BODY";
00465 
00466                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.x=this->left_pose_.pose.position.x+0.02;
00467                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.y=this->left_pose_.pose.position.y;
00468                                   this->move_left_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.z=this->left_pose_.pose.position.z-0.16;
00469 
00470                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints.resize(1);
00471                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.seq=0;
00472                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.stamp=ros::Time::now();
00473                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].header.frame_id="MP_BODY";
00474 
00475                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.x=this->right_pose_.pose.position.x+0.03;
00476                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.y=this->right_pose_.pose.position.y+0.02;
00477                                   this->move_right_arm_goal_.motion_plan_request.goal_constraints.position_constraints[0].position.z=this->right_pose_.pose.position.z-0.18;
00478 
00479                                   move_left_armMakeActionRequest();
00480                                   move_right_armMakeActionRequest();
00481                                   this->state=close_gripper;
00482                                 }
00483                                 break;
00484 
00485     case close_gripper: ROS_INFO("Current state: Moving arms to the final pose");
00486                         if((this->left_arm_moving)||(this->right_arm_moving))
00487                           this->state=close_gripper;
00488                         else{                   
00489                           this->move_joints_goal_.trajectory.header.seq=0;
00490                           this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00491                           this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00492                           this->move_joints_goal_.trajectory.joint_names.resize(2);
00493                           this->move_joints_goal_.trajectory.joint_names[0]="j_gripper_l";
00494                           this->move_joints_goal_.trajectory.joint_names[1]="j_gripper_r";
00495                           this->move_joints_goal_.trajectory.points.resize(1);
00496                           this->move_joints_goal_.trajectory.points[0].positions.resize(2);
00497                           this->move_joints_goal_.trajectory.points[0].velocities.resize(2);
00498                           this->move_joints_goal_.trajectory.points[0].accelerations.resize(2);
00499                           this->move_joints_goal_.trajectory.points[0].positions[0]=45.0;
00500                           this->move_joints_goal_.trajectory.points[0].positions[1]=-45.0;
00501                           for(i=0;i<2;i++){
00502                             this->move_joints_goal_.trajectory.points[0].velocities[i]=60.0;
00503                             this->move_joints_goal_.trajectory.points[0].accelerations[i]=100.0;
00504                           }
00505                           this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00506                           move_jointsMakeActionRequest();
00507                           this->state=done;
00508                         }
00509                         break;
00510 
00511     case done:  ROS_INFO("Current state: Close gripper");
00512                 if(this->joints_moving)
00513                   this->state=done;
00514                 else{
00515                   ROS_WARN("Current voltage: %f", this->current_voltage);               
00516                   if(this->current_voltage>0.0)
00517                     ROS_WARN("Darwin has caught the connectors");
00518                   else{
00519                     ROS_WARN("Darwin hasn't caught the connectors");                
00520                     this->state=done;
00521                     //this->state=open_gripper;
00522                     this->success=false;
00523                   }
00524                 }
00525                 break;
00526 
00527     case correct_position: ROS_INFO("Current state: Correct position");
00528                            this->Int32_msg_.data=16;
00529                            this->execute_action_publisher_.publish(this->Int32_msg_);           
00530                            sleep(2);                               
00531                            this->ready_for_check=false;                   
00532                            this->left_done=false;
00533                            this->first_goal=true;                                                  
00534                            this->left_data_ready=false;
00535                            this->right_data_ready=false;                         
00536                            this->left_ready_tf=false;
00537                            this->right_ready_tf=false;  
00538                            this->state=tracking_angle;                     
00539                            break;
00540 
00541     case tracking_angle:        ROS_INFO("Current state: Tracking angle");
00542                                 this->left_marker_found=false;
00543                                 this->right_marker_found=false;                 
00544                                 this->move_joints_goal_.trajectory.header.seq=0;
00545                                 this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00546                                 this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00547                                 this->move_joints_goal_.trajectory.joint_names.resize(2);
00548                                 this->move_joints_goal_.trajectory.joint_names[0]="j_tilt";
00549                                 this->move_joints_goal_.trajectory.joint_names[1]="j_pan";
00550                                 this->move_joints_goal_.trajectory.points.resize(1);
00551                                 this->move_joints_goal_.trajectory.points[0].positions.resize(2);
00552                                 this->move_joints_goal_.trajectory.points[0].velocities.resize(2);
00553                                 this->move_joints_goal_.trajectory.points[0].accelerations.resize(2);
00554                                 this->move_joints_goal_.trajectory.points[0].positions[0]=15.0-5.0*this->n;
00555                                 this->move_joints_goal_.trajectory.points[0].positions[1]=0.0;
00556                                 for(i=0;i<2;i++){
00557                                   this->move_joints_goal_.trajectory.points[0].velocities[i]=60.0;
00558                                   this->move_joints_goal_.trajectory.points[0].accelerations[i]=100.0;
00559                                 }
00560                                 this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00561                                 move_jointsMakeActionRequest();
00562                                 if(this->n>=4)
00563                                   this->state=move_back;
00564                                 if((this->right_pose_.pose.position.z>=0.12)&&(this->left_pose_.pose.position.z<0.12)){
00565                                   this->left_done=true;
00566                                   this->state=tracking_right;
00567                                 }else
00568                                   this->state=tracking_left;
00569                                 this->n++;                              
00570                                 break;
00571 
00572     case tracking_left: ROS_INFO("Current state: Tracking left marker");
00573                         if(this->left_marker_found){
00574                           ROS_WARN("Left marker found");
00575                           move_joints_client_.cancelGoal();
00576                           this->state=new_goal;
00577                         }else{
00578                           if(this->joints_moving)
00579                             this->state=tracking_left;
00580                           else{                          
00581                             this->move_joints_goal_.trajectory.header.seq=0;
00582                             this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00583                             this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00584                             this->move_joints_goal_.trajectory.joint_names.resize(1);
00585                             this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00586                             this->move_joints_goal_.trajectory.points.resize(1);
00587                             this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00588                             this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00589                             this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00590                             this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00591                             this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00592                             this->move_joints_goal_.trajectory.points[0].positions[0]=40.0;
00593                             this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00594                             move_jointsMakeActionRequest();                          
00595                             if(this->current_state.position[18]*180.0/3.14159>=39.0)
00596                               this->state=tracking_angle;
00597                           }
00598                         }                        
00599                         break;
00600 
00601     case tracking_right:        ROS_INFO("Current state: Tracking right marker");
00602                                 if(this->right_marker_found){
00603                                   ROS_WARN("Right marker found");
00604                                   move_joints_client_.cancelGoal();
00605                                   this->state=new_goal;
00606                                 }else{                            
00607                                   if(this->joints_moving)
00608                                     this->state=tracking_right;
00609                                   else{
00610                                     this->move_joints_goal_.trajectory.header.seq=0;
00611                                     this->move_joints_goal_.trajectory.header.stamp=ros::Time::now();
00612                                     this->move_joints_goal_.trajectory.header.frame_id="MP_BODY";
00613                                     this->move_joints_goal_.trajectory.joint_names.resize(1);
00614                                     this->move_joints_goal_.trajectory.joint_names[0]="j_pan";
00615                                     this->move_joints_goal_.trajectory.points.resize(1);
00616                                     this->move_joints_goal_.trajectory.points[0].positions.resize(1);
00617                                     this->move_joints_goal_.trajectory.points[0].velocities.resize(1);
00618                                     this->move_joints_goal_.trajectory.points[0].accelerations.resize(1);
00619                                     this->move_joints_goal_.trajectory.points[0].velocities[0]=20.0;
00620                                     this->move_joints_goal_.trajectory.points[0].accelerations[0]=30.0;
00621                                     this->move_joints_goal_.trajectory.points[0].positions[0]=-40.0;
00622                                     this->move_joints_goal_.trajectory.points[0].time_from_start=ros::Duration(1.0);
00623                                     move_jointsMakeActionRequest();
00624                                     if(this->current_state.position[18]*180.0/3.14159<=-39.0)
00625                                      this->state=tracking_angle;
00626                                   }
00627                                 }                        
00628                                 break;
00629 
00630     case tracking:      // The node breaks if it doesn't have a goal when a tracking action is requested.
00631                         ROS_INFO("Current state: Start tracking");
00632                         tracking_headMakeActionRequest();
00633                         sleep(1);
00634                         this->first_goal=false;
00635                         this->state=new_goal;                        
00636                         break;
00637 
00638 
00639     case new_goal:      ROS_INFO("Current state: New tracking head goal");
00640                         if(this->joints_moving)
00641                           this->state=new_goal;
00642                         else{
00643                           this->Float64MultiArray_msg_.data.resize(2);
00644                           this->Float64MultiArray_msg_.data[0]=(this->current_state.position[18]+atan(this->msg_.markers[0].pose.pose.position.x/this->msg_.markers[0].pose.pose.position.z))*180.0/3.14159;
00645                           this->Float64MultiArray_msg_.data[1]=(this->current_state.position[19]+atan(this->msg_.markers[0].pose.pose.position.y/this->msg_.markers[0].pose.pose.position.z))*180.0/3.14159;
00646                           this->tracking_head_goal_.pan_goal=this->Float64MultiArray_msg_.data[0];
00647                           this->tracking_head_goal_.tilt_goal=this->Float64MultiArray_msg_.data[1];
00648                           if(((this->Float64MultiArray_msg_.data[0]<=90.0)||(this->Float64MultiArray_msg_.data[0]>=-90.0))&&((this->Float64MultiArray_msg_.data[1]<=60.0)||(this->Float64MultiArray_msg_.data[1]>=-60.0)))
00649                             this->head_target_publisher_.publish(this->Float64MultiArray_msg_);
00650                           if(this->first_goal){
00651                             this->state=tracking;                            
00652                           }else{                                                  
00653                             if((this->left_pose_.pose.position.x>=0.13)&&(this->right_pose_.pose.position.x>=0.13))
00654                               this->state=go_forward;
00655                             else if((this->left_pose_.pose.position.x<0.13)&&(this->right_pose_.pose.position.x<0.13))
00656                               this->state=go_back;
00657                             else if((this->left_pose_.pose.position.x>=0.13)&&(this->right_pose_.pose.position.x<0.13))
00658                               this->state=go_left;
00659                             else if((this->right_pose_.pose.position.x>=0.13)&&(this->left_pose_.pose.position.x<0.13))
00660                               this->state=go_right;
00661                           }
00662                         }
00663                         break;
00664 
00665     case go_forward:    ROS_INFO("Current state: Go forward");
00666                         this->Twist_msg_.linear.x=0.005;
00667                         this->Twist_msg_.linear.y=0.0;
00668                         this->Twist_msg_.linear.z=0.0;
00669                         this->Twist_msg_.angular.x=0.0;
00670                         this->Twist_msg_.angular.y=0.0;
00671                         this->Twist_msg_.angular.z=0.0;
00672                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00673                         if((this->msg_.markers[0].pose.pose.position.z<=0.10)&&(this->msg_.markers[0].pose.pose.position.z>0.0)){
00674                           tracking_head_client_.cancelGoal();
00675                           this->state=initial_pose;
00676                         }else
00677                           this->state=new_goal;
00678                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00679                           tracking_head_client_.cancelGoal();
00680                           this->state=tracking_angle;
00681                           this->Twist_msg_.linear.x=0.0;
00682                           this->cmd_vel_publisher_.publish(this->Twist_msg_);                          
00683                         }
00684                         break;
00685 
00686     case go_back:       ROS_INFO("Current state: Go back");
00687                         this->Twist_msg_.linear.x=-0.005;
00688                         this->Twist_msg_.linear.y=0.0;
00689                         this->Twist_msg_.linear.z=0.0;
00690                         this->Twist_msg_.angular.x=0.0;
00691                         this->Twist_msg_.angular.y=0.0;
00692                         this->Twist_msg_.angular.z=0.0;
00693                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00694                         if(this->msg_.markers[0].pose.pose.position.z>=0.10){
00695                           tracking_head_client_.cancelGoal();
00696                           this->state=initial_pose;
00697                         }else
00698                           this->state=new_goal;
00699                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00700                           tracking_head_client_.cancelGoal();
00701                           this->state=tracking_angle;
00702                           this->Twist_msg_.linear.x=0.0;
00703                           this->cmd_vel_publisher_.publish(this->Twist_msg_);                          
00704                         }
00705                         break;
00706 
00707     case turn_right:    ROS_INFO("Current state: Turn to the right");
00708                         this->Twist_msg_.linear.x=0.0;
00709                         this->Twist_msg_.linear.y=0.0;
00710                         this->Twist_msg_.linear.z=0.0;
00711                         this->Twist_msg_.angular.x=0.0;
00712                         this->Twist_msg_.angular.y=0.0;
00713                         this->Twist_msg_.angular.z=0.005;
00714                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00715                         if((this->msg_.markers[0].pose.pose.position.z<=0.10)&&(this->msg_.markers[0].pose.pose.position.z>0.0)){
00716                           tracking_head_client_.cancelGoal();
00717                           this->state=initial_pose;
00718                         }else
00719                           this->state=new_goal;
00720                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00721                           tracking_head_client_.cancelGoal();
00722                           this->state=tracking_angle;
00723                           this->Twist_msg_.angular.z=0.0;
00724                           this->cmd_vel_publisher_.publish(this->Twist_msg_);                     
00725                         }
00726                         break;
00727 
00728     case turn_left:     ROS_INFO("Current state: Turn to the left");
00729                         this->Twist_msg_.linear.x=0.0;
00730                         this->Twist_msg_.linear.y=0.0;
00731                         this->Twist_msg_.linear.z=0.0;
00732                         this->Twist_msg_.angular.x=0.0;
00733                         this->Twist_msg_.angular.y=0.0;
00734                         this->Twist_msg_.angular.z=-0.005;
00735                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00736                         if((this->msg_.markers[0].pose.pose.position.z<=0.10)&&(this->msg_.markers[0].pose.pose.position.z>0.0)){
00737                           tracking_head_client_.cancelGoal();
00738                           this->state=initial_pose;
00739                         }else
00740                           this->state=new_goal;
00741                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00742                           tracking_head_client_.cancelGoal();
00743                           this->state=tracking_angle;
00744                           this->Twist_msg_.angular.z=0.0;
00745                           this->cmd_vel_publisher_.publish(this->Twist_msg_);                     
00746                         }
00747                         break;
00748 
00749     case go_right:      ROS_INFO("Current state: Go to the right");
00750                         this->Twist_msg_.linear.x=0.0;
00751                         this->Twist_msg_.linear.y=-0.005;
00752                         this->Twist_msg_.linear.z=0.0;
00753                         this->Twist_msg_.angular.x=0.0;
00754                         this->Twist_msg_.angular.y=0.0;
00755                         this->Twist_msg_.angular.z=0.0;
00756                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00757                         if((this->current_state.position[18]<=5.0)&&(this->current_state.position[18]>=-5.0))
00758                           this->state=turn_left;
00759                         else
00760                           this->state=new_goal;
00761                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00762                           tracking_head_client_.cancelGoal();
00763                           this->state=tracking_angle;
00764                           this->Twist_msg_.linear.y=0.0;
00765                           this->cmd_vel_publisher_.publish(this->Twist_msg_);                    
00766                         }
00767                         break;
00768 
00769     case go_left:       ROS_INFO("Current state: Go to the left");
00770                         this->Twist_msg_.linear.x=0.0;
00771                         this->Twist_msg_.linear.y=0.005;
00772                         this->Twist_msg_.linear.z=0.0;
00773                         this->Twist_msg_.angular.x=0.0;
00774                         this->Twist_msg_.angular.y=0.0;
00775                         this->Twist_msg_.angular.z=0.0;
00776                         this->cmd_vel_publisher_.publish(this->Twist_msg_);
00777                         if((this->current_state.position[18]<=5.0)&&(this->current_state.position[18]>=-5.0))
00778                           this->state=turn_right;
00779                         else
00780                           this->state=new_goal;
00781                         if(this->msg_.markers[0].pose.pose.position.z==0.0){
00782                           tracking_head_client_.cancelGoal();
00783                           this->state=tracking_angle;   
00784                           this->Twist_msg_.linear.y=0.0;
00785                           this->cmd_vel_publisher_.publish(this->Twist_msg_);            
00786                         } 
00787                         break;
00788 
00789   }
00790   }catch(...){
00791     ROS_INFO("exception!!!!!!!!!!!!!!!!!!!!!");
00792   }
00793 
00794 }
00795 
00796 /*  [subscriber callbacks] */
00797 void ConvertPoseAlgNode::adc_channels_callback(const std_msgs::Float64MultiArray::ConstPtr& msg) 
00798 { 
00799   //ROS_INFO("ConvertPoseAlgNode::adc_channels_callback: New Message Received"); 
00800 
00801   //use appropiate mutex to shared variables if necessary 
00802   //this->alg_.lock(); 
00803   this->adc_channels_mutex_.enter(); 
00804 
00805   //std::cout << msg->data << std::endl; 
00806   this->current_voltage=msg->data[2];
00807 
00808   //unlock previously blocked shared variables 
00809   //this->alg_.unlock(); 
00810   this->adc_channels_mutex_.exit(); 
00811 }
00812 void ConvertPoseAlgNode::joint_state_callback(const sensor_msgs::JointState::ConstPtr& msg) 
00813 { 
00814   //ROS_INFO("ConvertPoseAlgNode::joint_state_callback: New Message Received"); 
00815 
00816   //use appropiate mutex to shared variables if necessary 
00817   //this->alg_.lock(); 
00818   this->joint_state_mutex_.enter(); 
00819   //std::cout << msg->data << std::endl; 
00820   this->current_state=(*msg);
00821   
00822   //unlock previously blocked shared variables 
00823   //this->alg_.unlock(); 
00824   this->joint_state_mutex_.exit(); 
00825 }
00826 void ConvertPoseAlgNode::marker_callback(const ar_pose::ARMarkers::ConstPtr& msg) 
00827 { 
00828   bool tf_exists;
00829   int x,y,z;
00830 
00831   //ROS_INFO("ConvertPoseAlgNode::marker_callback: New Message Received");
00832   if((this->ready)&&(!this->ready_for_check)){       
00833   //use appropiate mutex to shared variables if necessary 
00834   //this->alg_.lock(); 
00835   this->marker_mutex_.enter(); 
00836   this->msg_=(*msg);
00837   this->msg_.markers.resize(1);
00838   ROS_INFO("marker dist x, y, z: %f, %f, %f", this->msg_.markers[0].pose.pose.position.x, this->msg_.markers[0].pose.pose.position.y, this->msg_.markers[0].pose.pose.position.z);
00839   //ROS_INFO("id: %d", this->msg_.markers[0].id);  
00840   if(this->msg_.markers[0].pose.pose.position.z>0.0){  
00841     this->conv_msg_.header=this->msg_.markers[0].header;
00842     this->conv_msg_.pose.position=this->msg_.markers[0].pose.pose.position;
00843     //std::cout << this->conv_msg_.pose.position.x << "," << this->conv_msg_.pose.position.y << "," << this->conv_msg_.pose.position.z << std::endl;
00844     this->conv_msg_.pose.orientation=this->msg_.markers[0].pose.pose.orientation;               
00845     try{
00846       this->conv_msg_.header.stamp=ros::Time::now();
00847       // transform the usb_cam frame to the MP_HEAD frame
00848       tf_exists=tf_listener.waitForTransform(this->conv_msg_.header.frame_id,std::string("/MP_HEAD"),this->conv_msg_.header.stamp, ros::Duration(10), ros::Duration(0.01));
00849       tf_listener.transformPose(std::string("/MP_HEAD"),this->conv_msg_,this->marker_);                
00850     }catch(tf::TransformException ex){
00851       ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());    
00852     }catch(...){
00853       ROS_INFO("exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
00854     }    
00855     if((this->msg_.markers[0].id==0)&&(!this->left_done)){
00856       this->left_marker_found=true;
00857       this->n=0;
00858       this->current_pan=this->current_state.position[18]*180.0/3.14159;      
00859       if(this->left_ready_tf){      
00860       try{
00861         // transform the MP_HEAD frame to the MP_BODY frame to move the left arm
00862         this->marker_.header.stamp=ros::Time::now();    
00863         tf_exists=tf_listener.waitForTransform(this->marker_.header.frame_id,std::string("/MP_BODY"),this->marker_.header.stamp, ros::Duration(10), ros::Duration(0.01));
00864         tf_listener.transformPose(std::string("/MP_BODY"),this->marker_,this->left_pose_);
00865 
00866         x=1000*this->left_pose_.pose.position.x;
00867         y=1000*this->left_pose_.pose.position.y;
00868         z=1000*this->left_pose_.pose.position.z;
00869         this->left_pose_.pose.position.x=x/1000.0;
00870         this->left_pose_.pose.position.y=y/1000.0;
00871         this->left_pose_.pose.position.z=z/1000.0;
00872 
00873         ROS_WARN_STREAM(this->left_pose_);
00874                         
00875         this->left_done=true;   
00876         this->left_data_ready=true;
00877         this->left_marker_found=false;  
00878       }catch(tf::TransformException ex){
00879         ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00880       }catch(...){
00881         ROS_INFO("exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
00882       }           
00883       }
00884     }
00885     if((this->msg_.markers[0].id==1)&&(this->left_done)){               
00886       this->right_marker_found=true;
00887       this->n=0;
00888       this->current_pan=this->current_state.position[18]*180.0/3.14159;
00889       if(this->right_ready_tf){      
00890       try{
00891         // transform the MP_HEAD frame to the MP_BODY frame to move the right arm
00892         this->marker_.header.stamp=ros::Time::now();    
00893         tf_exists=tf_listener.waitForTransform(this->marker_.header.frame_id,std::string("/MP_BODY"),this->marker_.header.stamp, ros::Duration(10), ros::Duration(0.01));
00894         tf_listener.transformPose(std::string("/MP_BODY"),this->marker_,this->right_pose_);
00895 
00896         x=1000*this->right_pose_.pose.position.x;
00897         y=1000*this->right_pose_.pose.position.y;
00898         z=1000*this->right_pose_.pose.position.z;
00899         this->right_pose_.pose.position.x=x/1000.0;
00900         this->right_pose_.pose.position.y=y/1000.0;
00901         this->right_pose_.pose.position.z=z/1000.0;
00902         
00903         ROS_WARN_STREAM(this->right_pose_);
00904         
00905         this->left_done=false;  
00906         this->right_data_ready=true;
00907         this->right_marker_found=false;
00908       }catch(tf::TransformException ex){
00909         ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00910       }catch(...){
00911         ROS_INFO("exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
00912       }
00913       }     
00914     }    
00915   }  
00916     //unlock previously blocked shared variables 
00917     //this->alg_.unlock();     
00918     this->marker_mutex_.exit();  
00919   }
00920 }
00921 /*  [service callbacks] */
00922 
00923 /*  [action callbacks] */
00924 void ConvertPoseAlgNode::tracking_headDone(const actionlib::SimpleClientGoalState& state,  const iri_darwin_robot::tracking_headResultConstPtr& result) 
00925 { 
00926   if( state.toString().compare("SUCCEEDED") == 0 ) 
00927     ROS_INFO("ConvertPoseAlgNode::tracking_headDone: Goal Achieved!"); 
00928   else 
00929     ROS_INFO("ConvertPoseAlgNode::tracking_headDone: %s", state.toString().c_str()); 
00930 
00931   //copy & work with requested result   
00932 } 
00933 
00934 void ConvertPoseAlgNode::tracking_headActive() 
00935 { 
00936   //ROS_INFO("ConvertPoseAlgNode::tracking_headActive: Goal just went active!"); 
00937 } 
00938 
00939 void ConvertPoseAlgNode::tracking_headFeedback(const iri_darwin_robot::tracking_headFeedbackConstPtr& feedback) 
00940 { 
00941   //ROS_INFO("ConvertPoseAlgNode::tracking_headFeedback: Got Feedback!"); 
00942 
00943   bool feedback_is_ok = true; 
00944 
00945   //analyze feedback 
00946   //feedback_is_ok=false; 
00947 
00948   //if feedback is not what expected, cancel requested goal 
00949   if( !feedback_is_ok ) 
00950   { 
00951     tracking_head_client_.cancelGoal(); 
00952     //ROS_INFO("ConvertPoseAlgNode::tracking_headFeedback: Cancelling Action!"); 
00953   } 
00954 }
00955 void ConvertPoseAlgNode::move_jointsDone(const actionlib::SimpleClientGoalState& state,  const control_msgs::FollowJointTrajectoryResultConstPtr& result) 
00956 { 
00957   if( state.toString().compare("SUCCEEDED") == 0 ) 
00958     ROS_INFO("ConvertPoseAlgNode::move_jointsDone: Goal Achieved!"); 
00959   else 
00960     ROS_INFO("ConvertPoseAlgNode::move_jointsDone: %s", state.toString().c_str()); 
00961 
00962   //copy & work with requested result
00963   this->joints_moving=false;  
00964 } 
00965 
00966 void ConvertPoseAlgNode::move_jointsActive() 
00967 { 
00968   //ROS_INFO("ConvertPoseAlgNode::move_jointsActive: Goal just went active!"); 
00969 } 
00970 
00971 void ConvertPoseAlgNode::move_jointsFeedback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback) 
00972 { 
00973   //ROS_INFO("ConvertPoseAlgNode::move_jointsFeedback: Got Feedback!"); 
00974 
00975   bool feedback_is_ok = true; 
00976 
00977   //analyze feedback 
00978   //feedback_is_ok = false;
00979   //my_var = feedback->var; 
00980 
00981   //if feedback is not what expected, cancel requested goal 
00982   if( !feedback_is_ok ) 
00983   {
00984     move_joints_client_.cancelGoal(); 
00985     //ROS_INFO("ConvertPoseAlgNode::move_jointsFeedback: Cancelling Action!"); 
00986   } 
00987 }
00988 void ConvertPoseAlgNode::move_right_armDone(const actionlib::SimpleClientGoalState& state,  const arm_navigation_msgs::MoveArmResultConstPtr& result) 
00989 { 
00990   if( state.toString().compare("SUCCEEDED") == 0 )
00991     ROS_INFO("ConvertPoseAlgNode::move_right_armDone: Goal Achieved!"); 
00992   else 
00993     ROS_INFO("ConvertPoseAlgNode::move_right_armDone: %s", state.toString().c_str()); 
00994 
00995   //copy & work with requested result
00996   this->right_arm_moving=false;  
00997 } 
00998 
00999 void ConvertPoseAlgNode::move_right_armActive() 
01000 { 
01001   //ROS_INFO("ConvertPoseAlgNode::move_right_armActive: Goal just went active!"); 
01002 } 
01003 
01004 void ConvertPoseAlgNode::move_right_armFeedback(const arm_navigation_msgs::MoveArmFeedbackConstPtr& feedback) 
01005 { 
01006   //ROS_INFO("ConvertPoseAlgNode::move_right_armFeedback: Got Feedback!"); 
01007 
01008   bool feedback_is_ok = true; 
01009 
01010   //analyze feedback 
01011   //my_var = feedback->var; 
01012 
01013   //if feedback is not what expected, cancel requested goal 
01014   if( !feedback_is_ok ) 
01015   { 
01016     move_right_arm_client_.cancelGoal(); 
01017     //ROS_INFO("ConvertPoseAlgNode::move_right_armFeedback: Cancelling Action!"); 
01018   } 
01019 }
01020 
01021 void ConvertPoseAlgNode::move_left_armDone(const actionlib::SimpleClientGoalState& state,  const arm_navigation_msgs::MoveArmResultConstPtr& result)
01022 {
01023   if( state.toString().compare("SUCCEEDED") == 0 )
01024     ROS_INFO("ConvertPoseAlgNode::move_left_armDone: Goal Achieved!");
01025   else
01026     ROS_INFO("ConvertPoseAlgNode::move_left_armDone: %s", state.toString().c_str());
01027 
01028   //copy & work with requested result
01029   this->left_arm_moving=false;  
01030 }
01031 
01032 void ConvertPoseAlgNode::move_left_armActive()
01033 {
01034   //ROS_INFO("ConvertPoseAlgNode::move_left_armActive: Goal just went active!"); 
01035 }
01036 
01037 void ConvertPoseAlgNode::move_left_armFeedback(const arm_navigation_msgs::MoveArmFeedbackConstPtr& feedback)
01038 {
01039   //ROS_INFO("ConvertPoseAlgNode::move_left_armFeedback: Got Feedback!"); 
01040 
01041   bool feedback_is_ok = true;
01042 
01043   //analyze feedback 
01044   //my_var = feedback->var; 
01045 
01046   //if feedback is not what expected, cancel requested goal 
01047   if( !feedback_is_ok )
01048   {
01049     move_left_arm_client_.cancelGoal();
01050     //ROS_INFO("ConvertPoseAlgNode::move_left_armFeedback: Cancelling Action!"); 
01051   }
01052 }
01053 
01054 /*  [action requests] */
01055 void ConvertPoseAlgNode::tracking_headMakeActionRequest() 
01056 { 
01057   ROS_INFO("ConvertPoseAlgNode::tracking_headMakeActionRequest: Starting New Request!"); 
01058 
01059   //wait for the action server to start 
01060   //will wait for infinite time 
01061   tracking_head_client_.waitForServer();  
01062   ROS_INFO("ConvertPoseAlgNode::tracking_headMakeActionRequest: Server is Available!"); 
01063 
01064   //send a goal to the action 
01065   //tracking_head_goal_.data = my_desired_goal; 
01066   tracking_head_client_.sendGoal(tracking_head_goal_, 
01067               boost::bind(&ConvertPoseAlgNode::tracking_headDone,     this, _1, _2), 
01068               boost::bind(&ConvertPoseAlgNode::tracking_headActive,   this), 
01069               boost::bind(&ConvertPoseAlgNode::tracking_headFeedback, this, _1)); 
01070   ROS_INFO("ConvertPoseAlgNode::tracking_headMakeActionRequest: Goal Sent. Wait for Result!");     
01071 }
01072 void ConvertPoseAlgNode::move_jointsMakeActionRequest() 
01073 { 
01074   ROS_INFO("ConvertPoseAlgNode::move_jointsMakeActionRequest: Starting New Request!"); 
01075 
01076   //wait for the action server to start 
01077   //will wait for infinite time 
01078   move_joints_client_.waitForServer();  
01079   ROS_INFO("ConvertPoseAlgNode::move_jointsMakeActionRequest: Server is Available!"); 
01080 
01081   //send a goal to the action 
01082   //move_joints_goal_.data = my_desired_goal; 
01083   move_joints_client_.sendGoal(move_joints_goal_, 
01084               boost::bind(&ConvertPoseAlgNode::move_jointsDone,     this, _1, _2), 
01085               boost::bind(&ConvertPoseAlgNode::move_jointsActive,   this), 
01086               boost::bind(&ConvertPoseAlgNode::move_jointsFeedback, this, _1)); 
01087   ROS_INFO("ConvertPoseAlgNode::move_jointsMakeActionRequest: Goal Sent. Wait for Result!"); 
01088   this->joints_moving=true;  
01089 }
01090 void ConvertPoseAlgNode::move_right_armMakeActionRequest() 
01091 {
01092   ROS_INFO("ConvertPoseAlgNode::move_right_armMakeActionRequest: Starting New Request!"); 
01093 
01094   //wait for the action server to start 
01095   //will wait for infinite time 
01096   move_right_arm_client_.waitForServer();  
01097   ROS_INFO("ConvertPoseAlgNode::move_right_armMakeActionRequest: Server is Available!"); 
01098 
01099   //send a goal to the action      
01100   //move_right_arm_goal_.data = my_desired_goal; 
01101   move_right_arm_client_.sendGoal(move_right_arm_goal_, 
01102               boost::bind(&ConvertPoseAlgNode::move_right_armDone,     this, _1, _2), 
01103               boost::bind(&ConvertPoseAlgNode::move_right_armActive,   this), 
01104               boost::bind(&ConvertPoseAlgNode::move_right_armFeedback, this, _1)); 
01105   ROS_INFO("ConvertPoseAlgNode::move_right_armMakeActionRequest: Goal Sent. Wait for Result!");   
01106   this->right_arm_moving=true;  
01107 }
01108 void ConvertPoseAlgNode::move_left_armMakeActionRequest()
01109 {
01110   ROS_INFO("ConvertPoseAlgNode::move_left_armMakeActionRequest: Starting New Request!");
01111 
01112   //wait for the action server to start 
01113   //will wait for infinite time 
01114   move_left_arm_client_.waitForServer();
01115   ROS_INFO("ConvertPoseAlgNode::move_left_armMakeActionRequest: Server is Available!");
01116 
01117   //send a goal to the action      
01118   //move_left_arm_goal_.data = my_desired_goal; 
01119   move_left_arm_client_.sendGoal(move_left_arm_goal_,
01120               boost::bind(&ConvertPoseAlgNode::move_left_armDone,     this, _1, _2),
01121               boost::bind(&ConvertPoseAlgNode::move_left_armActive,   this),
01122               boost::bind(&ConvertPoseAlgNode::move_left_armFeedback, this, _1));
01123   ROS_INFO("ConvertPoseAlgNode::move_left_armMakeActionRequest: Goal Sent. Wait for Result!");    
01124   this->left_arm_moving=true;  
01125 }
01126 
01127 void ConvertPoseAlgNode::node_config_update(Config &config, uint32_t level)
01128 {
01129   this->alg_.lock();
01130 
01131   this->alg_.unlock();
01132 }
01133 
01134 void ConvertPoseAlgNode::addNodeDiagnostics(void)
01135 {
01136 }
01137 
01138 /* main function */
01139 int main(int argc,char *argv[])
01140 {
01141   return algorithm_base::main<ConvertPoseAlgNode>(argc, argv, "convert_pose_alg_node");
01142 }


convert_pose
Author(s): darwin
autogenerated on Fri Dec 6 2013 20:54:52