place_alg_node.cpp
Go to the documentation of this file.
00001 #include "place_alg_node.h"
00002 
00003 PlaceAlgNode::PlaceAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<PlaceAlgorithm>(),
00005   place_aserver_(public_node_handle_, "place")
00006 {
00007   //init class attributes if necessary
00008   //this->loop_rate_ = 2;//in [Hz]
00009   place_pose_frame_id_ = "/wam_link0";
00010   place_action_success_ = false;
00011   place_action_status_ = 0;
00012 
00013   // [init publishers]
00014   
00015   // [init subscribers]
00016   
00017   // [init services]
00018   
00019   // [init clients]
00020   joints_move_client_ = this->public_node_handle_.serviceClient<iri_wam_common_msgs::joints_move>("joints_move");
00021   get_tcp_ik_client_ = this->public_node_handle_.serviceClient<iri_wam_common_msgs::wamInverseKinematics>("get_tcp_ik");
00022   bhand_cmd_client_ = this->public_node_handle_.serviceClient<iri_wam_common_msgs::bhand_cmd>("bhand_cmd");
00023   
00024   // [init action servers]
00025   place_aserver_.registerStartCallback(boost::bind(&PlaceAlgNode::placeStartCallback, this, _1)); 
00026   place_aserver_.registerStopCallback(boost::bind(&PlaceAlgNode::placeStopCallback, this)); 
00027   place_aserver_.registerIsFinishedCallback(boost::bind(&PlaceAlgNode::placeIsFinishedCallback, this)); 
00028   place_aserver_.registerHasSucceedCallback(boost::bind(&PlaceAlgNode::placeHasSucceedCallback, this)); 
00029   place_aserver_.registerGetResultCallback(boost::bind(&PlaceAlgNode::placeGetResultCallback, this, _1)); 
00030   place_aserver_.registerGetFeedbackCallback(boost::bind(&PlaceAlgNode::placeGetFeedbackCallback, this, _1)); 
00031   place_aserver_.start();
00032   
00033   // [init action clients]
00034 }
00035 
00036 PlaceAlgNode::~PlaceAlgNode(void)
00037 {
00038   // [free dynamic memory]
00039 }
00040 
00041 void PlaceAlgNode::mainNodeThread(void)
00042 {
00043   // [fill msg structures]
00044   
00045   // [fill srv structure and make request to the server]
00046   //joints_move_srv_.request.data = my_var; 
00047   //ROS_INFO("PlaceAlgNode:: Sending New Request!"); 
00048   //if (joints_move_client_.call(joints_move_srv)) 
00049   //{ 
00050     //ROS_INFO("PlaceAlgNode:: Response: %s", joints_move_srv_.response.result); 
00051   //} 
00052   //else 
00053   //{ 
00054     //ROS_INFO("PlaceAlgNode:: Failed to Call Server on topic joints_move "); 
00055   //}
00056   //get_tcp_ik_srv_.request.data = my_var; 
00057   //ROS_INFO("PlaceAlgNode:: Sending New Request!"); 
00058   //if (get_tcp_ik_client_.call(get_tcp_ik_srv)) 
00059   //{ 
00060     //ROS_INFO("PlaceAlgNode:: Response: %s", get_tcp_ik_srv_.response.result); 
00061   //} 
00062   //else 
00063   //{ 
00064     //ROS_INFO("PlaceAlgNode:: Failed to Call Server on topic get_tcp_ik "); 
00065   //}
00066   //bhand_cmd_srv_.request.data = my_var; 
00067   //ROS_INFO("PlaceAlgNode:: Sending New Request!"); 
00068   //if (bhand_cmd_client_.call(bhand_cmd_srv)) 
00069   //{ 
00070     //ROS_INFO("PlaceAlgNode:: Response: %s", bhand_cmd_srv_.response.result); 
00071   //} 
00072   //else 
00073   //{ 
00074     //ROS_INFO("PlaceAlgNode:: Failed to Call Server on topic bhand_cmd "); 
00075   //}
00076   //pose_move_srv_.request.data = my_var; 
00077   //ROS_INFO("PlaceAlgNode:: Sending New Request!"); 
00078   //if (pose_move_client_.call(pose_move_srv)) 
00079   //{ 
00080     //ROS_INFO("PlaceAlgNode:: Response: %s", pose_move_srv_.response.result); 
00081   //} 
00082   //else 
00083   //{ 
00084     //ROS_INFO("PlaceAlgNode:: Failed to Call Server on topic pose_move "); 
00085   //}
00086   
00087   // [fill action structure and make request to the action server]
00088 
00089   // [publish messages]
00090 }
00091 
00092 /*  [subscriber callbacks] */
00093 
00094 /*  [service callbacks] */
00095 
00096 /*  [action callbacks] */
00097 void PlaceAlgNode::placeStartCallback(const object_manipulation_msgs::PlaceGoalConstPtr& goal)
00098 { 
00099     //check goal 
00100     ROS_INFO("Starting place action");
00101     //TODO check collision avoidance
00102     if(goal->place_locations[0].header.frame_id != place_pose_frame_id_){
00103       ROS_ERROR("frame_id mismatch");
00104       std::cout << goal->place_locations[0].header.frame_id << std::endl;
00105       std::cout << place_pose_frame_id_ << std::endl;
00106       alg_.lock();
00107       place_action_status_ = FINISH;
00108       alg_.unlock();
00109       return;
00110     }
00111 
00112     alg_.lock();
00113     place_action_status_ = POSE_CHECKED;
00114     alg_.unlock();
00115     //execute goal 
00116     get_tcp_ik_srv_.request.pose = goal->place_locations[0];
00117     if (get_tcp_ik_client_.call(get_tcp_ik_srv_)) { 
00118         ROS_INFO("PlaceAlgNode:: Response %f %f %f %f %f %f %f", get_tcp_ik_srv_.response.joints.position[0], get_tcp_ik_srv_.response.joints.position[1], get_tcp_ik_srv_.response.joints.position[2], get_tcp_ik_srv_.response.joints.position[3], get_tcp_ik_srv_.response.joints.position[4], get_tcp_ik_srv_.response.joints.position[5], get_tcp_ik_srv_.response.joints.position[6]); 
00119     } else { 
00120         ROS_ERROR("PlaceAlgNode:: Failed to Call Server on topic %s", get_tcp_ik_client_.getService().c_str()); 
00121         alg_.lock();
00122         place_action_status_ = FINISH;
00123         alg_.unlock();
00124         return;
00125     }
00126 
00127     joints_move_srv_.request.joints = get_tcp_ik_srv_.response.joints.position;
00128     ROS_INFO("Moving");
00129     if (joints_move_client_.call(joints_move_srv_)) {
00130       ROS_DEBUG("joints_move success %d", joints_move_srv_.response.success);
00131     } else {
00132       ROS_ERROR("Failed to call service %s", joints_move_client_.getService().c_str());
00133       alg_.lock();
00134       place_action_status_ = FINISH;
00135       alg_.unlock();
00136       return;
00137     }
00138 
00139     alg_.lock();
00140     place_action_status_ = POSITION_REACHED;
00141     alg_.unlock();
00142 
00143     ROS_INFO("Opening hand");
00144     bhand_cmd_srv_.request.bhandcmd = "GTO";
00145     if (bhand_cmd_client_.call(bhand_cmd_srv_))
00146     {
00147       ROS_DEBUG("bhand move success %d", bhand_cmd_srv_.response.success);
00148     } else {
00149       ROS_ERROR("Failed to call service  %s", bhand_cmd_client_.getService().c_str());
00150       alg_.lock();
00151       place_action_status_ = FINISH;
00152       alg_.unlock();
00153       return;
00154     }
00155 
00156     alg_.lock();
00157     place_action_status_ = HAND_OPENED; //no effect
00158     place_action_status_ = FINISH;
00159     place_action_success_ = true;
00160     alg_.unlock();
00161     ROS_INFO("Place action completed");
00162 
00163 } 
00164 
00165 void PlaceAlgNode::placeStopCallback(void) 
00166 { 
00167   alg_.lock(); 
00168     //stop action 
00169   alg_.unlock(); 
00170 } 
00171 
00172 bool PlaceAlgNode::placeIsFinishedCallback(void) 
00173 { 
00174   bool ret = false; 
00175 
00176   alg_.lock(); 
00177   //if action has finish for any reason 
00178   //ret = true; 
00179   if(place_action_status_ == FINISH){
00180       ret = true;
00181   }
00182   alg_.unlock(); 
00183 
00184   return ret; 
00185 } 
00186 
00187 bool PlaceAlgNode::placeHasSucceedCallback(void) 
00188 { 
00189   bool ret = false; 
00190 
00191   //TODO what happens if the action is called twice? How the correct interleaving of the action access to action_success flag is guaranteed?
00192   alg_.lock(); 
00193     //if goal was accomplished 
00194     //ret = true 
00195   last_place_action_success_ = place_action_success_;
00196   if(place_action_success_ == true){
00197       ret = true;
00198       //reset flag //is this concurrency safe?
00199       place_action_success_ = false;
00200   }
00201   alg_.unlock(); 
00202 
00203   return ret; 
00204 } 
00205 
00206 void PlaceAlgNode::placeGetResultCallback(object_manipulation_msgs::PlaceResultPtr& result) 
00207 { 
00208   alg_.lock(); 
00209     //update result data to be sent to client 
00210     if(last_place_action_success_){
00211       result->manipulation_result.value = object_manipulation_msgs::ManipulationResult::SUCCESS; 
00212     }else{
00213       result->manipulation_result.value = object_manipulation_msgs::ManipulationResult::FAILED; 
00214     }
00215   alg_.unlock(); 
00216 } 
00217 
00218 void PlaceAlgNode::placeGetFeedbackCallback(object_manipulation_msgs::PlaceFeedbackPtr& feedback) 
00219 { 
00220   alg_.lock(); 
00221     //keep track of feedback 
00222     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00223   alg_.unlock(); 
00224 }
00225 
00226 /*  [action requests] */
00227 
00228 void PlaceAlgNode::node_config_update(Config &config, uint32_t level)
00229 {
00230 
00231   ROS_INFO("Reconfigure called");
00232 
00233   this->alg_.lock();
00234   place_pose_frame_id_ = config.pose_frame_id; 
00235   this->alg_.unlock();
00236 
00237 }
00238 
00239 void PlaceAlgNode::addNodeDiagnostics(void)
00240 {
00241 }
00242 
00243 /* main function */
00244 int main(int argc,char *argv[])
00245 {
00246   return algorithm_base::main<PlaceAlgNode>(argc, argv, "place_alg_node");
00247 }


iri_place
Author(s): pmonso
autogenerated on Fri Dec 6 2013 22:02:44