tibi_dabo_kinect_arm_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_kinect_arm_alg_node.h"
00002 
00003 TibiDaboKinectArmAlgNode::TibiDaboKinectArmAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboKinectArmAlgorithm>(),
00005   FollowJointTrajectory_client_("FollowJointTrajectory", true)
00006 {
00007   //init class attributes if necessary
00008   //strcpy(desired_frames[0],"/skeleton/right_shoulder_1");
00009   //strcpy(desired_frames[1],"/skeleton/right_hand_1");
00010   this->public_node_handle_.getParam("arm_id", arm_id);
00011   x=250;
00012   y=-300;
00013   z=50;
00014   
00015   //Choose arm from user (note that kinect detects like a mirror, so right hand is left, and left is right)
00016   if(arm_id=="Left"){
00017     strcpy(hand_tf,"/skeleton/right_hand_1"); //Real left
00018   }else if(arm_id=="Right"){
00019     strcpy(hand_tf,"/skeleton/left_hand_1"); //Real right
00020   }
00021   
00022   this->loop_rate_ = 10;//in [Hz]
00023 
00024   // [init publishers]
00025   
00026   // [init subscribers]
00027   this->tf_subscriber_ = this->public_node_handle_.subscribe("tf", 20, &TibiDaboKinectArmAlgNode::tf_callback, this);
00028   
00029   // [init services]
00030   
00031   // [init clients]
00032   GetPositionIK_client_ = this->public_node_handle_.serviceClient<kinematics_msgs::GetPositionIK>("GetPositionIK");
00033   
00034   // [init action servers]
00035   
00036   // [init action clients]
00037   
00038   this->joint_motion_done=true;
00039   
00040   // initialize the message for all angles
00041   this->FollowJointTrajectory_goal_.trajectory.joint_names.resize(4);
00042   if(arm_id=="Left"){
00043   this->FollowJointTrajectory_goal_.trajectory.joint_names[0]="left_shoulder_yaw";
00044   this->FollowJointTrajectory_goal_.trajectory.joint_names[1]="left_shoulder_pitch";
00045   this->FollowJointTrajectory_goal_.trajectory.joint_names[2]="left_shoulder_roll";
00046   this->FollowJointTrajectory_goal_.trajectory.joint_names[3]="left_elbow";   
00047   }else if(arm_id=="Right"){
00048   this->FollowJointTrajectory_goal_.trajectory.joint_names[0]="right_shoulder_yaw";
00049   this->FollowJointTrajectory_goal_.trajectory.joint_names[1]="right_shoulder_pitch";
00050   this->FollowJointTrajectory_goal_.trajectory.joint_names[2]="right_shoulder_roll";
00051   this->FollowJointTrajectory_goal_.trajectory.joint_names[3]="right_elbow"; 
00052   }
00053 
00054   this->FollowJointTrajectory_goal_.trajectory.points.resize(1);
00055   this->FollowJointTrajectory_goal_.trajectory.points[0].positions.resize(4);
00056   this->FollowJointTrajectory_goal_.trajectory.points[0].velocities.resize(4);
00057   this->FollowJointTrajectory_goal_.trajectory.points[0].accelerations.resize(4);
00058   this->FollowJointTrajectory_goal_.trajectory.points[0].time_from_start=ros::Duration(0);
00059   
00060 }
00061 
00062 TibiDaboKinectArmAlgNode::~TibiDaboKinectArmAlgNode(void)
00063 {
00064   // [free dynamic memory]
00065 }
00066 
00067 void TibiDaboKinectArmAlgNode::mainNodeThread(void)
00068 {  
00069   static bool first_time=true;
00070   
00071   // [fill msg structures]  
00072   // [fill srv structure and make request to the server]
00073   
00074   if(this->joint_motion_done || true)
00075   {
00076     
00077     if(listener.frameExists("/skeleton/neck_1") && listener.frameExists(hand_tf) && !debug){
00078       
00079       if(first_time){
00080         sleep(1.0);
00081         first_time=false;
00082         //Velocity[0] seed message is used as boolean to show if there is a new user (1=new, 0=not new)
00083         GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity.resize(1);
00084         GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity[0]=1;
00085       }
00086       
00087       
00088       listener.lookupTransform("/skeleton/neck_1", hand_tf, ros::Time(0), echo_transform);
00089       v = echo_transform.getOrigin();
00090       //ROS_WARN_STREAM("Hand from neck: X: " << v.getX() << " Y: " << v.getY() << " Z: " << v.getZ());
00091     
00092       //Kinect to neck1 (urdf) axes
00093       x=-v.getZ()*1000;
00094       y=-v.getX()*1000;
00095       z=v.getY()*1000;
00096     
00097       //Pose
00098       GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.x=x;
00099       GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.y=y;
00100       GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.z=z;
00101     
00102       //theta3 seed (rads)
00103       theta3=0;
00104       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(1);
00105       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(1);
00106       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="shoulder_roll";
00107       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=theta3;
00108   
00109       //ROS_INFO("KinematicsClientAlgNode:: Sending New Request!");
00110       ROS_WARN_STREAM("KinematicsClientAlgNode:: Pose sent: " << x << " " << y << " " << z);
00111       if (GetPositionIK_client_.call(GetPositionIK_srv_)){ 
00112         
00113         theta1=GetPositionIK_srv_.response.solution.joint_state.position[0];
00114         theta2=GetPositionIK_srv_.response.solution.joint_state.position[1];
00115         theta3=GetPositionIK_srv_.response.solution.joint_state.position[2];
00116         theta4=GetPositionIK_srv_.response.solution.joint_state.position[3];
00117       
00118         ROS_WARN_STREAM("KinematicsClientAlgNode:: Angles received: " << theta1*180/3.14159 << " " << theta2*180/3.14159 << " " << theta3*180/3.14159 << " " << theta4*180/3.14159);
00119           
00120         //fill all 4 angles
00121         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[0]=theta1;
00122         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[0]=25*3.14159/180.0;
00123         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[1]=theta2;
00124         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[1]=25.0*3.14159/180.0;
00125         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[2]=theta3;
00126         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[2]=25.0*3.14159/180.0;
00127         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[3]=theta4;
00128         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[3]=25.0*3.14159/180.0;
00129 
00130         this->joint_motion_done=true;
00131         FollowJointTrajectoryMakeActionRequest();
00132         
00133       } 
00134       else{
00135         ROS_ERROR("KinematicsClientAlgNode:: Failed to Call Server on topic GetPositionIK or IK could not be calculated");
00136       }
00137       //Velocity[1] seed message is used as boolean to show if there is a new user (1=new, 0=not new)
00138       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity.resize(1);
00139       GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity[0]=0;
00140     }else if(debug){
00141         
00142         x=x_debug;
00143         y=y_debug;
00144         z=z_debug;
00145         
00146         if(!angle_mode_debug){
00147           
00148           
00149         //Debug mode (skips escalate arm in tibi_dabo_arm_kinematics)
00150             GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity.resize(1);
00151             GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.velocity[0]=2;
00152           
00153            //Pose
00154           GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.x=x;
00155           GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.y=y;
00156           GetPositionIK_srv_.request.ik_request.pose_stamped.pose.position.z=z;
00157     
00158           //theta3 seed (rads)
00159           theta3=0;
00160           GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.name.resize(1);
00161           GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.position.resize(1);
00162           GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.name[0]="shoulder_roll";
00163           GetPositionIK_srv_.request.ik_request.ik_seed_state.joint_state.position[0]=theta3;
00164   
00165           ROS_WARN_STREAM("KinematicsClientAlgNode:: Pose sent: " << x << " " << y << " " << z);
00166           
00167           if (GetPositionIK_client_.call(GetPositionIK_srv_)){
00168             theta1=GetPositionIK_srv_.response.solution.joint_state.position[0];
00169             theta2=GetPositionIK_srv_.response.solution.joint_state.position[1];
00170             theta3=GetPositionIK_srv_.response.solution.joint_state.position[2];
00171             theta4=GetPositionIK_srv_.response.solution.joint_state.position[3];
00172             
00173             ROS_WARN_STREAM("KinematicsClientAlgNode:: Angles received: " << theta1*180/3.14159 << " " << theta2*180/3.14159 << " " << theta3*180/3.14159 << " " << theta4*180/3.14159);
00174           }
00175         }else{
00176           theta1=theta1_debug*3.1416/180;
00177           theta2=theta2_debug*3.1416/180;
00178           theta3=theta3_debug*3.1416/180;
00179           theta4=theta4_debug*3.1416/180;
00180         }
00181       
00182         //fill all 4 angles (not doing IK)
00183         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[0]=theta1;
00184         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[0]=25*3.14159/180.0;
00185         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[1]=theta2;
00186         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[1]=25.0*3.14159/180.0;
00187         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[2]=theta3;
00188         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[2]=25.0*3.14159/180.0;
00189         this->FollowJointTrajectory_goal_.trajectory.points[0].positions[3]=theta4;
00190         this->FollowJointTrajectory_goal_.trajectory.points[0].velocities[3]=25.0*3.14159/180.0;
00191         
00192         this->joint_motion_done=true;
00193         FollowJointTrajectoryMakeActionRequest();
00194     
00195     }else{
00196       //If user has left and comes back
00197       first_time=true;  
00198     }
00199   }
00200    
00201   // [fill action structure and make request to the action server]
00202 
00203   // [publish messages]
00204 }
00205 
00206 /*  [subscriber callbacks] */
00207 void TibiDaboKinectArmAlgNode::tf_callback(const tf::tfMessage::ConstPtr& msg) 
00208 { 
00209 
00210   //ROS_INFO("TibiDaboKinectArmAlgNode::tf_callback: New Message Received");   
00211   
00212   
00213   //use appropiate mutex to shared variables if necessary 
00214   //this->alg_.lock(); 
00215   //this->tf_mutex_.enter(); 
00216 
00217   //std::cout << msg->data << std::endl; 
00218 
00219   //unlock previously blocked shared variables 
00220   //this->alg_.unlock(); 
00221   //this->tf_mutex_.exit(); 
00222 }
00223 
00224 /*  [service callbacks] */
00225 
00226 /*  [action callbacks] */
00227 void TibiDaboKinectArmAlgNode::FollowJointTrajectoryDone(const actionlib::SimpleClientGoalState& state,  const control_msgs::FollowJointTrajectoryResultConstPtr& result) 
00228 { 
00229   if( state.toString().compare("SUCCEEDED") == 0 ) 
00230     ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryDone: Goal Achieved!"); 
00231   else 
00232     ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryDone: %s", state.toString().c_str());
00233   this->joint_motion_done=true;
00234 
00235   //copy & work with requested result 
00236 } 
00237 
00238 void TibiDaboKinectArmAlgNode::FollowJointTrajectoryActive() 
00239 { 
00240   //ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryActive: Goal just went active!"); 
00241 } 
00242 
00243 void TibiDaboKinectArmAlgNode::FollowJointTrajectoryFeedback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback) 
00244 { 
00245   //ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryFeedback: Got Feedback!"); 
00246 
00247   bool feedback_is_ok = true; 
00248 
00249   //analyze feedback 
00250   //my_var = feedback->var;
00251 
00252   //if feedback is not what expected, cancel requested goal 
00253   if( !feedback_is_ok ) 
00254   { 
00255     FollowJointTrajectory_client_.cancelGoal(); 
00256     //ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryFeedback: Cancelling Action!"); 
00257   } 
00258 }
00259 
00260 /*  [action requests] */
00261 void TibiDaboKinectArmAlgNode::FollowJointTrajectoryMakeActionRequest() 
00262 { 
00263   ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryMakeActionRequest: Starting New Request!"); 
00264 
00265   //wait for the action server to start 
00266   //will wait for infinite time 
00267   FollowJointTrajectory_client_.waitForServer();  
00268   ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryMakeActionRequest: Server is Available!"); 
00269 
00270   //send a goal to the action 
00271   //FollowJointTrajectory_goal_.data = my_desired_goal; 
00272   FollowJointTrajectory_client_.sendGoal(FollowJointTrajectory_goal_, 
00273               boost::bind(&TibiDaboKinectArmAlgNode::FollowJointTrajectoryDone,     this, _1, _2), 
00274               boost::bind(&TibiDaboKinectArmAlgNode::FollowJointTrajectoryActive,   this), 
00275               boost::bind(&TibiDaboKinectArmAlgNode::FollowJointTrajectoryFeedback, this, _1)); 
00276   ROS_INFO("TibiDaboKinectArmAlgNode::FollowJointTrajectoryMakeActionRequest: Goal Sent. Wait for Result!"); 
00277   this->joint_motion_done=false;
00278 }
00279 
00280 void TibiDaboKinectArmAlgNode::node_config_update(Config &config, uint32_t level)
00281 {
00282   this->alg_.lock();
00283   
00284   arm_id=config.arm_id;
00285   debug=config.debug;
00286   angle_mode_debug=config.angle_mode_debug;
00287   x_debug=config.x_debug;
00288   y_debug=config.y_debug;
00289   z_debug=config.z_debug;
00290   theta1_debug=config.theta1_debug;
00291   theta2_debug=config.theta2_debug;
00292   theta3_debug=config.theta3_debug;
00293   theta4_debug=config.theta4_debug;
00294 
00295   this->alg_.unlock();
00296 }
00297 
00298 void TibiDaboKinectArmAlgNode::addNodeDiagnostics(void)
00299 {
00300 }
00301 
00302 /* main function */
00303 int main(int argc,char *argv[])
00304 {
00305   return algorithm_base::main<TibiDaboKinectArmAlgNode>(argc, argv, "tibi_dabo_kinect_arm_alg_node");
00306 }


tibi_dabo_kinect_arm
Author(s): dblasco
autogenerated on Fri Dec 6 2013 20:34:24