laser_localisation_alg_node.cpp
Go to the documentation of this file.
00001 #include "laser_localisation_alg_node.h"
00002 
00003 LaserLocalisationAlgNode::LaserLocalisationAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<LaserLocalisationAlgorithm>(),
00005   received_ref_(false),
00006   received_sens_(false),
00007   received_pose_ref_(false)
00008 {
00009   ROS_INFO("Laser Localisation: Warming up..");
00010   //init class attributes if necessary
00011   //this->loop_rate_ = 2;//in [Hz]
00012 
00013   // [init publishers]
00014   pose_publisher_ =
00015     public_node_handle_.advertise<geometry_msgs::PoseWithCovarianceStamped>("pose", 100);
00016 
00017    // [init subscribers]
00018   scan_ref_subscriber_ = public_node_handle_.subscribe("scan_ref", 100,
00019                             &LaserLocalisationAlgNode::scan_ref_callback, this);
00020   scan_sens_subscriber_ = public_node_handle_.subscribe("scan_sens", 100,
00021                            &LaserLocalisationAlgNode::scan_sens_callback, this);
00022   pose_ref_subscriber_ = public_node_handle_.subscribe("pose_ref", 100,
00023                             &LaserLocalisationAlgNode::pose_ref_callback, this);
00024 
00025   // [init services]
00026   estimate_server_ = public_node_handle_.advertiseService("estimate",
00027                              &LaserLocalisationAlgNode::estimateCallback, this);
00028   localise_server_ = public_node_handle_.advertiseService("localise",
00029                              &LaserLocalisationAlgNode::localiseCallback, this);
00030 
00031   // [init clients]
00032   get_relative_pose_client_ =
00033     public_node_handle_.serviceClient<iri_laser_icp::GetRelativePose>("/iri_laser_icp/get_relative_pose");
00034 
00035   // [init action servers]
00036 
00037   // [init action clients]
00038   T_mapa_odom_.setOrigin( tf::Vector3(0, 0, 0.0) );
00039   T_mapa_odom_.setRotation( tf::createIdentityQuaternion() );
00040 
00041   //tfb_.sendTransform(tf::StampedTransform(T_mapa_odom_, ros::Time::now(), alg_.config_.map_frame,alg_.config_.odom_frame)); //"/map", "/odom")); //
00042 
00043   ROS_DEBUG("Laser Localisation: T map odom broadcasted");
00044 }
00045 
00046 LaserLocalisationAlgNode::~LaserLocalisationAlgNode(void)
00047 {
00048   // [free dynamic memory]
00049 }
00050 
00051 void LaserLocalisationAlgNode::mainNodeThread(void)
00052 {
00053   // [fill msg structures]
00054   //this->PoseWithCovarianceStamped_msg_.data = my_var;
00055 
00056   if(received_sens_ && received_ref_ & received_pose_ref_ && alg_.config_.continuous)
00057   {
00058     ROS_INFO("mainnodethread");
00059     calc_T_mapa_odom_(scan_ref_,scan_sens_,pose_ref_);
00060     tfb_.sendTransform(tf::StampedTransform(T_mapa_odom_, ros::Time::now(), alg_.config_.map_frame, alg_.config_.odom_frame));
00061     received_sens_ = false;
00062   }else{
00063    tfb_.sendTransform(tf::StampedTransform(T_mapa_odom_, ros::Time::now(), alg_.config_.map_frame, alg_.config_.odom_frame));
00064   }
00065 }
00066 
00067 /*  [subscriber callbacks] */
00068 void LaserLocalisationAlgNode::scan_ref_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00069 {
00070   //ROS_INFO("LaserNavigationAlgNode::scan_callback: New Message Received");
00071   //use appropiate mutex to shared variables if necessary
00072   //this->alg_.lock();
00073   this->scan_ref_mutex_.enter();
00074   scan_ref_ = *msg;
00075   received_ref_ = true;
00076   //std::cout << msg->data << std::endl;
00077   //unlock previously blocked shared variables
00078   //this->alg_.unlock();
00079   this->scan_ref_mutex_.exit();
00080 }
00081 
00082 void LaserLocalisationAlgNode::scan_sens_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00083 {
00084   //ROS_INFO("LaserNavigationAlgNode::scan_callback: New Message Received");
00085   //use appropiate mutex to shared variables if necessary
00086   //this->alg_.lock();
00087   this->scan_sens_mutex_.enter();
00088   scan_sens_ = *msg;
00089   received_sens_ = true;
00090   //std::cout << msg->data << std::endl;
00091   //unlock previously blocked shared variables
00092   //this->alg_.unlock();
00093   this->scan_sens_mutex_.exit();
00094 }
00095 
00096 void LaserLocalisationAlgNode::pose_ref_callback(const geometry_msgs::Pose::ConstPtr& msg)
00097 {
00098   ROS_INFO("LaserNavigationAlgNode::scan_callback: New Message Received");
00099   //use appropiate mutex to shared variables if necessary
00100   //this->alg_.lock();
00101   this->pose_ref_mutex_.enter();
00102   pose_ref_ = *msg;
00103   received_pose_ref_ = true;
00104   //std::cout << msg->data << std::endl;
00105   //unlock previously blocked shared variables
00106   //this->alg_.unlock();
00107   this->pose_ref_mutex_.exit();
00108 }
00109 
00110 /*  [service callbacks] */
00111 bool LaserLocalisationAlgNode::estimateCallback(iri_laser_localisation::DoEstimation::Request &req, iri_laser_localisation::DoEstimation::Response &res)
00112 {
00113   ROS_INFO("LaserLocalisationAlgNode::estimateCallback: New Request Received!");
00114 
00115   //use appropiate mutex to shared variables if necessary
00116   //this->alg_.lock();
00117   //this->estimate_mutex_.enter();
00118 
00119   T_mapa_odom_init_est_.setOrigin( tf::Vector3(req.pose_est.pose.position.x, req.pose_est.pose.position.y, req.pose_est.pose.position.z) );
00120 
00121   tf::Quaternion q;
00122   //ROS_INFO("QUATERNION MSG: %f %f %f %f",req.pose_est.pose.orientation.x, req.pose_est.pose.orientation.y, req.pose_est.pose.orientation.z, req.pose_est.pose.orientation.w);
00123   tf::quaternionMsgToTF (req.pose_est.pose.orientation, q);
00124   //ROS_ERROR("QUATERNION MSG: %f %f %f %f" req.pose_est.pose.orientation.x, req.pose_est.pose.orientation.y, req.pose_est.pose.orientation.z, req.pose_est.pose.orientation.w);
00125   //q.normalized();
00126 
00127   T_mapa_odom_init_est_.setRotation(q.normalized());
00128 
00129   T_mapa_odom_ = T_mapa_odom_init_est_;
00130 
00131   tfb_.sendTransform(tf::StampedTransform(T_mapa_odom_, ros::Time::now(), alg_.config_.map_frame, alg_.config_.odom_frame));
00132 
00133   ROS_INFO_XYR("T_mapa_odom_ ESTIMACIO INICIAL",T_mapa_odom_);
00134 
00135   res.ok = true;
00136 
00137   ROS_INFO("Laser Localisation: T map odom estimation broadcasted");
00138 
00139   //if(this->alg_.isRunning())
00140   //{
00141     //ROS_INFO("LaserLocalisationAlgNode::estimateCallback: Processin New Request!");
00142     //do operations with req and output on res
00143     //res.data2 = req.data1 + my_var;
00144   //}
00145   //else
00146   //{
00147     //ROS_INFO("LaserLocalisationAlgNode::estimateCallback: ERROR: alg is not on run mode yet.");
00148   //}
00149 
00150   //unlock previously blocked shared variables
00151   //this->alg_.unlock();
00152   //this->estimate_mutex_.exit();
00153 
00154   return true;
00155 }
00156 
00157 bool LaserLocalisationAlgNode::localiseCallback(iri_laser_localisation::DoLocalisation::Request &req, iri_laser_localisation::DoLocalisation::Response &res)
00158 {
00159   ROS_DEBUG("Laser Localisation: New Request");
00160 
00161   //use appropiate mutex to shared variables if necessary
00162   //this->alg_.lock();
00163   //this->localise_mutex_.enter();
00164   pose_ref_ = req.pose_ref;
00165   received_pose_ref_ = true;
00166   calc_T_mapa_odom_(req.scan_ref,req.scan_sens,pose_ref_);
00167 
00168   tf::poseTFToMsg(T_mapa_odom_,res.pose.pose.pose);
00169 
00170   //broadcast Tf
00171   ros::Duration tolerance(0.2);
00172   tfb_.sendTransform(tf::StampedTransform(T_mapa_odom_, ros::Time::now()+tolerance, alg_.config_.map_frame, alg_.config_.odom_frame));
00173   ROS_DEBUG("Laser Localisation: Tf Broadcasted");
00174 
00175     //this->pose_publisher_.publish(this->PoseWithCovarianceStamped_msg_);
00176 
00177   //unlock previously blocked shared variables
00178   //this->alg_.unlock();
00179   //this->localise_mutex_.exit();
00180 
00181 
00182   return true;
00183 }
00184 
00185 /*  [action callbacks] */
00186 
00187 /*  [action requests] */
00188 
00189 void LaserLocalisationAlgNode::node_config_update(Config &config, uint32_t level)
00190 {
00191   this->alg_.lock();
00192 
00193   this->alg_.unlock();
00194 }
00195 
00196 void LaserLocalisationAlgNode::addNodeDiagnostics(void)
00197 {
00198 }
00199 
00201 
00202 void LaserLocalisationAlgNode::calc_T_mapa_odom_(sensor_msgs::LaserScan const& scan_ref,
00203                                                 sensor_msgs::LaserScan const& scan_sens,
00204                                                 geometry_msgs::Pose const& pose_ref)
00205 {
00206 
00207   get_relative_pose_srv_.request.scan_ref  = scan_ref;
00208   get_relative_pose_srv_.request.scan_sens = scan_sens;
00209 
00210   //do operations with req and output on res
00211   ROS_DEBUG("Laser Localisation: Sending New Request");
00212   if (get_relative_pose_client_.call(get_relative_pose_srv_))
00213   {
00214 /* Es calcula amb ICP
00216     */
00217     T_path_base_.setOrigin( tf::Vector3(
00218                   get_relative_pose_srv_.response.pose_rel.pose.pose.position.x,
00219                   get_relative_pose_srv_.response.pose_rel.pose.pose.position.y,
00220                   0.0) );
00221     tf::Quaternion path_base_q;
00222     tf::quaternionMsgToTF (get_relative_pose_srv_.response.pose_rel.pose.pose.orientation,
00223                            path_base_q);
00224     T_path_base_.setRotation(path_base_q.normalized());
00225     ROS_INFO_XYR("T_path_base_",T_path_base_);
00226     //ROS_INFO_XYR("T_path_base_ inverse",T_path_base_.inverse());
00227     //T_path_base_=T_path_base_.inverse();
00228 /* Es mira via TF
00230     */
00231     tf::StampedTransform T_base_odom_stamped;
00232     try{
00233       tfl_.lookupTransform(alg_.config_.base_frame, alg_.config_.odom_frame,
00234                            ros::Time(0), T_base_odom_stamped);
00235       T_base_odom_ = T_base_odom_stamped;
00236       ROS_INFO_XYR("T_base_odom_",T_base_odom_);
00237       //ROS_INFO_XYR("T_base_odom_ inverse",T_base_odom_.inverse());
00238       //T_base_odom_=T_base_odom_.inverse();
00239     }
00240     catch (tf::TransformException ex){
00241       ROS_ERROR("%s",ex.what());
00242     }
00243 /* Ve donada per la pose del mapa
00245     */
00246     tf::poseMsgToTF(pose_ref, T_mapa_path_);
00247     ROS_INFO_XYR("T_mapa_path_",T_mapa_path_);
00248     //T_mapa_path_ = T_mapa_path_ * T_mapa_odom_init_est_;
00249     //ROS_INFO_XYR("T_mapa_path_ - init_est_",T_mapa_path_* T_mapa_odom_init_est_);
00250 /* Càlcul final
00252     */
00253     T_mapa_odom_ = T_mapa_path_ * T_path_base_ * T_base_odom_;
00254 
00255     ROS_INFO_XYR("T_mapa_odom_",T_mapa_odom_);
00256 
00257   }
00258   else
00259   {
00260     ROS_ERROR("Laser Localisation: Failed to Call Server on topic get_relative_pose ");
00261   }
00262 }
00263 
00264 void LaserLocalisationAlgNode::ROS_INFO_XYR(const std::string & str,const tf::Transform & tftrans)
00265 {
00266   geometry_msgs::Transform gtrans;
00267   tf::transformTFToMsg(tftrans,gtrans);
00268   ROS_INFO("Laser Localisation: %s",str.c_str());
00269   ROS_INFO("\033[31mx:\033[0m%f \033[32my:\033[0m%f \033[34mth:\033[0m%f ",
00270            gtrans.translation.x,gtrans.translation.y,tf::getYaw(gtrans.rotation));
00271 }
00272 
00273 /* main function */
00274 int main(int argc,char *argv[])
00275 {
00276   return algorithm_base::main<LaserLocalisationAlgNode>(argc, argv, "laser_localisation_alg_node");
00277 }


iri_laser_localisation
Author(s): Marti
autogenerated on Fri Dec 6 2013 22:51:11