00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "rawlog_record_node.h"
00030 #include <boost/interprocess/sync/scoped_lock.hpp>
00031
00032 #include <mrpt_bridge/pose.h>
00033 #include <mrpt_bridge/laser_scan.h>
00034 #include <mrpt_bridge/time.h>
00035
00036
00037 int main(int argc, char **argv) {
00038
00039 ros::init(argc, argv, "DataLogger");
00040 ros::NodeHandle n;
00041 RawlogRecordNode my_node(n);
00042 my_node.init();
00043 my_node.loop();
00044 return 0;
00045 }
00046
00047 RawlogRecordNode::~RawlogRecordNode() {
00048 }
00049
00050 RawlogRecordNode::RawlogRecordNode(ros::NodeHandle &n) :
00051 RawlogRecord(new RawlogRecordNode::ParametersNode()), n_(n), loop_count_(0) {
00052
00053 }
00054
00055 RawlogRecordNode::ParametersNode *RawlogRecordNode::param() {
00056 return (RawlogRecordNode::ParametersNode*) param_;
00057 }
00058
00059 void RawlogRecordNode::init() {
00060 updateRawLogName(mrpt::system::getCurrentLocalTime());
00061 ROS_INFO("rawlog file: %s", param_->raw_log_name.c_str());
00062 subLaser0_ = n_.subscribe("scan", 1, &RawlogRecordNode::callbackLaser, this);
00063 subLaser1_ = n_.subscribe("scan1", 1, &RawlogRecordNode::callbackLaser, this);
00064 subLaser2_ = n_.subscribe("scan2", 1, &RawlogRecordNode::callbackLaser, this);
00065 }
00066
00067 void RawlogRecordNode::loop() {
00068 for (ros::Rate rate(param()->rate); ros::ok(); loop_count_++) {
00069 param()->update(loop_count_);
00070 ros::spinOnce();
00071 rate.sleep();
00072 }
00073 }
00074
00075
00076 bool RawlogRecordNode::waitForTransform(mrpt::poses::CPose3D &des, const std::string& target_frame, const std::string& source_frame, const ros::Time& time, const ros::Duration& timeout, const ros::Duration& polling_sleep_duration){
00077 tf::StampedTransform transform;
00078 try
00079 {
00080 listenerTF_.waitForTransform(target_frame, source_frame, time, polling_sleep_duration);
00081 listenerTF_.lookupTransform(target_frame, source_frame, time, transform);
00082 }
00083 catch(tf::TransformException)
00084 {
00085 ROS_INFO("Failed to get transform target_frame (%s) to source_frame (%s)", target_frame.c_str(), source_frame.c_str());
00086 return false;
00087 }
00088 mrpt_bridge::convert(transform, des);
00089 return true;
00090 }
00091
00092 void RawlogRecordNode::callbackLaser (const sensor_msgs::LaserScan &_msg) {
00093
00094 mrpt::slam::CObservation2DRangeScanPtr laser = mrpt::slam::CObservation2DRangeScan::Create();
00095
00096 if(laser_poses_.find(_msg.header.frame_id) == laser_poses_.end()) {
00097 updateLaserPose (_msg.header.frame_id);
00098 } else {
00099
00100
00101 mrpt_bridge::convert(_msg, laser_poses_[_msg.header.frame_id], *laser);
00102
00103
00104 std::string base_frame_id = tf::resolve(param()->tf_prefix, param()->base_frame_id);
00105 std::string odom_frame_id = tf::resolve(param()->tf_prefix, param()->odom_frame_id);
00106 mrpt::poses::CPose3D poseOdom;
00107 if(this->waitForTransform(poseOdom, odom_frame_id, base_frame_id, _msg.header.stamp, ros::Duration(1))){
00108 mrpt::slam::CObservationOdometryPtr odometry = mrpt::slam::CObservationOdometry::Create();
00109 odometry->sensorLabel = odom_frame_id;
00110 odometry->hasEncodersInfo = false;
00111 odometry->hasVelocities = false;
00112 odometry->odometry.x() = poseOdom.x();
00113 odometry->odometry.y() = poseOdom.y();
00114 odometry->odometry.phi() = poseOdom.yaw();
00115
00116 observation(laser, odometry);
00117 } else {
00118 ROS_INFO("Failed to get odom for laser observation!");
00119 }
00120 }
00121 }
00122
00123 void RawlogRecordNode::updateLaserPose (std::string _frame_id) {
00124 std::string base_frame_id = tf::resolve(param()->tf_prefix, param()->base_frame_id);
00125 mrpt::poses::CPose3D pose;
00126 tf::StampedTransform transform;
00127 try {
00128
00129 listenerTF_.lookupTransform(base_frame_id, _frame_id, ros::Time(0), transform);
00130 ROS_INFO("Requesting laser pose for %s!", _frame_id.c_str());
00131 tf::Vector3 translation = transform.getOrigin();
00132 tf::Quaternion quat = transform.getRotation();
00133 pose.x() = translation.x();
00134 pose.y() = translation.y();
00135 pose.z() = translation.z();
00136 double roll, pitch, yaw;
00137 tf::Matrix3x3 Rsrc(quat);
00138 mrpt::math::CMatrixDouble33 Rdes;
00139 for(int c = 0; c < 3; c++)
00140 for(int r = 0; r < 3; r++)
00141 Rdes(r,c) = Rsrc.getRow(r)[c];
00142 pose.setRotationMatrix(Rdes);
00143 laser_poses_[_frame_id] = pose;
00144 }
00145 catch (tf::TransformException ex) {
00146 ROS_ERROR("%s",ex.what());
00147 ros::Duration(1.0).sleep();
00148 }
00149
00150 }
00151