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
00011
00012
00013
00014 pose_publisher_ =
00015 public_node_handle_.advertise<geometry_msgs::PoseWithCovarianceStamped>("pose", 100);
00016
00017
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
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
00032 get_relative_pose_client_ =
00033 public_node_handle_.serviceClient<iri_laser_icp::GetRelativePose>("/iri_laser_icp/get_relative_pose");
00034
00035
00036
00037
00038 T_mapa_odom_.setOrigin( tf::Vector3(0, 0, 0.0) );
00039 T_mapa_odom_.setRotation( tf::createIdentityQuaternion() );
00040
00041
00042
00043 ROS_DEBUG("Laser Localisation: T map odom broadcasted");
00044 }
00045
00046 LaserLocalisationAlgNode::~LaserLocalisationAlgNode(void)
00047 {
00048
00049 }
00050
00051 void LaserLocalisationAlgNode::mainNodeThread(void)
00052 {
00053
00054
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
00068 void LaserLocalisationAlgNode::scan_ref_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00069 {
00070
00071
00072
00073 this->scan_ref_mutex_.enter();
00074 scan_ref_ = *msg;
00075 received_ref_ = true;
00076
00077
00078
00079 this->scan_ref_mutex_.exit();
00080 }
00081
00082 void LaserLocalisationAlgNode::scan_sens_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00083 {
00084
00085
00086
00087 this->scan_sens_mutex_.enter();
00088 scan_sens_ = *msg;
00089 received_sens_ = true;
00090
00091
00092
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
00100
00101 this->pose_ref_mutex_.enter();
00102 pose_ref_ = *msg;
00103 received_pose_ref_ = true;
00104
00105
00106
00107 this->pose_ref_mutex_.exit();
00108 }
00109
00110
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
00116
00117
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
00123 tf::quaternionMsgToTF (req.pose_est.pose.orientation, q);
00124
00125
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
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
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
00162
00163
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
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
00176
00177
00178
00179
00180
00181
00182 return true;
00183 }
00184
00185
00186
00187
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
00211 ROS_DEBUG("Laser Localisation: Sending New Request");
00212 if (get_relative_pose_client_.call(get_relative_pose_srv_))
00213 {
00214
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
00227
00228
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
00238
00239 }
00240 catch (tf::TransformException ex){
00241 ROS_ERROR("%s",ex.what());
00242 }
00243
00245
00246 tf::poseMsgToTF(pose_ref, T_mapa_path_);
00247 ROS_INFO_XYR("T_mapa_path_",T_mapa_path_);
00248
00249
00250
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
00274 int main(int argc,char *argv[])
00275 {
00276 return algorithm_base::main<LaserLocalisationAlgNode>(argc, argv, "laser_localisation_alg_node");
00277 }