pose_broadcaster.cpp
Go to the documentation of this file.
00001 
00007 #include "../include/pose_broadcaster.h"
00008 #include <tf/tf.h>
00009 #include <gazebo_msgs/GetModelState.h>
00010 #include <icr_msgs/GetObjectPose.h>
00011 
00012 namespace ICR
00013 {
00014   PoseBroadcaster::PoseBroadcaster() : nh_private_("~"),ref_frame_id_("/fixed"),camera_frame_id_("/camera_rgb_frame")
00015   {
00016     std::string param;
00017     std::string gazebo_prefix;
00018     std::string uc3m_objtrack_prefix;
00019 
00020     nh_private_.searchParam("pose_source", param);
00021     nh_private_.getParam(param, pose_source_);
00022 
00023     if(nh_private_.searchParam("reference_frame_id", param)) //try to read the reference frame from the parameter server
00024       nh_private_.getParam(param, ref_frame_id_);
00025     else
00026       ROS_WARN("No reference frame id found on the parameter server - using %s",ref_frame_id_.c_str());
00027 
00028   if(nh_private_.searchParam("camera_frame_id", param)) //try to read the camera frame from the parameter server
00029       nh_private_.getParam(param, camera_frame_id_);
00030     else
00031       ROS_WARN("No camera frame id found on the parameter server - using %s",camera_frame_id_.c_str());
00032 
00033     nh_private_.searchParam("gazebo_prefix", param);
00034     nh_private_.getParam(param, gazebo_prefix);
00035 
00036     nh_private_.searchParam("uc3m_objtrack_prefix", param);
00037     nh_private_.getParam(param, uc3m_objtrack_prefix);
00038 
00039     gazebo_get_ms_clt_ = nh_.serviceClient<gazebo_msgs::GetModelState>(gazebo_prefix + "/get_model_state");
00040     get_tracked_obj_ = nh_.serviceClient<icr_msgs::GetObjectPose>(uc3m_objtrack_prefix + "/get_object_pose");
00041 
00042     //If Gazebo is the intended pose source, wait for Gazebo services to be available 
00043     if(!strcmp(pose_source_.c_str(),"gazebo"))
00044       gazebo_get_ms_clt_.waitForExistence();  
00045     else if(!strcmp(pose_source_.c_str(),"uc3m_objtrack"))
00046        get_tracked_obj_.waitForExistence();  
00047 
00048   }
00049   //-----------------------------------------------------------------------------------
00050   void PoseBroadcaster::setObject(pcl::PointCloud<pcl::PointNormal> const & obj_cloud, std::string const & obj_name)
00051   {
00052     lock_.lock();
00053     obj_cloud_=obj_cloud;
00054     obj_name_=obj_name;
00055     lock_.unlock();
00056   }
00057   //-----------------------------------------------------------------------------------
00058   void PoseBroadcaster::broadcastPose()
00059   {
00060     // kca, 29 Apr 2012, pose is a member of PoseBroadcaster
00061     //    tf::Transform pose;
00062     lock_.lock();
00063 
00064     if(!strcmp(pose_source_.c_str(),"gazebo")) //Gazebo is defined as pose source
00065       {
00066         if(!getPoseGazebo())
00067           {
00068             lock_.unlock();
00069             return;
00070           }
00071       }
00072     //kca, 29 April 2012, : this is commented since now in uc3m_objtrack mode the pose of the object is obtained only once while loading the new model of an object.
00073     //
00074     else if(!strcmp(pose_source_.c_str(),"uc3m_objtrack")) //The UC3M objecttracker is defined as pose source
00075       {
00076         // DO NOTHING NOW, pose is calculated only once when loading object
00077 
00078         // if(!getPoseUC3MObjtrack(pose))
00079         //   {
00080         //     lock_.unlock();
00081         //     return;
00082         //   }
00083       }
00084     else
00085       {
00086         ROS_ERROR("%s is an invalid pose source. Implemented are 'gazebo' and 'uc3m_objtrack'. Cannot broadcast object pose ... ",pose_source_.c_str());
00087         lock_.unlock();
00088         return;
00089       }
00090     
00091     //Broadcast the pose to tf
00092     tf_brc_.sendTransform(tf::StampedTransform(object_pose_, ros::Time::now(),ref_frame_id_, obj_cloud_.header.frame_id));
00093 
00094     lock_.unlock();
00095   }
00096   //-----------------------------------------------------------------------------------
00097   bool PoseBroadcaster::getPoseGazebo()
00098   {
00099     gazebo_msgs::GetModelState get_ms;
00100     get_ms.request.model_name=obj_name_;
00101 
00102     if(!gazebo_get_ms_clt_.call(get_ms))
00103       {
00104         ROS_ERROR("Could not get state of object %s from Gazebo. Cannot determine pose...",obj_name_.c_str());
00105         return false;
00106       }
00107 
00108     object_pose_.setOrigin(tf::Vector3(get_ms.response.pose.position.x,get_ms.response.pose.position.y,get_ms.response.pose.position.z));
00109     object_pose_.setRotation(tf::Quaternion(get_ms.response.pose.orientation.x,get_ms.response.pose.orientation.y,get_ms.response.pose.orientation.z,get_ms.response.pose.orientation.w));
00110     return true;
00111   }
00112   //-----------------------------------------------------------------------------------
00113   bool PoseBroadcaster::getPoseUC3MObjtrack()
00114   {
00115     //TODO: at the beginning, fit the obj_cloud_ to the blob and compute a static offset pose, then call the pose service from the UC3M objecttracker to get the current pose
00116     icr_msgs::GetObjectPose obj_pose;
00117     if (!get_tracked_obj_.call(obj_pose) ) {
00118       return false;
00119     } 
00120     geometry_msgs::PoseStamped detected_pose;
00121     //   tf::Transform transform; // transform that is broadcasted
00122     detected_pose.pose = obj_pose.response.object.pose;
00123     object_pose_.setOrigin(tf::Vector3(detected_pose.pose.position.x,
00124                                        detected_pose.pose.position.y,
00125                                        detected_pose.pose.position.z));
00126     object_pose_.setRotation(tf::Quaternion(detected_pose.pose.orientation.x,
00127                                             detected_pose.pose.orientation.y,
00128                                             detected_pose.pose.orientation.z,
00129                                             detected_pose.pose.orientation.w ));
00130 
00131 
00132     tf::StampedTransform R_T_C;//camera pose expressed in the reference frame
00133     R_T_C.stamp_=obj_pose.response.object.header.stamp;
00134     try
00135       {
00136         tf_list_.waitForTransform(ref_frame_id_, camera_frame_id_, R_T_C.stamp_, ros::Duration(1.0));
00137         tf_list_.lookupTransform(ref_frame_id_, camera_frame_id_,R_T_C.stamp_,R_T_C);
00138       }
00139     catch (tf::TransformException ex){
00140       ROS_ERROR("%s",ex.what());
00141       return false;
00142     }
00143 
00144     object_pose_=R_T_C*object_pose_; //object pose R_T_O expressed in the reference frame
00145 
00146     return true;
00147   }
00148   //-----------------------------------------------------------------------------------
00149 
00150 
00151 }//end namespace


icr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:36:10