rawlog_record_node.cpp
Go to the documentation of this file.
00001 /***********************************************************************************
00002  * Revised BSD License                                                             *
00003  * Copyright (c) 2014, Markus Bader <markus.bader@tuwien.ac.at>                    *
00004  * All rights reserved.                                                            *
00005  *                                                                                 *
00006  * Redistribution and use in source and binary forms, with or without              *
00007  * modification, are permitted provided that the following conditions are met:     *
00008  *     * Redistributions of source code must retain the above copyright            *
00009  *       notice, this list of conditions and the following disclaimer.             *
00010  *     * Redistributions in binary form must reproduce the above copyright         *
00011  *       notice, this list of conditions and the following disclaimer in the       *
00012  *       documentation and/or other materials provided with the distribution.      *
00013  *     * Neither the name of the Vienna University of Technology nor the           *
00014  *       names of its contributors may be used to endorse or promote products      *
00015  *       derived from this software without specific prior written permission.     *
00016  *                                                                                 *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND *
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
00019  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          *
00020  * DISCLAIMED. IN NO EVENT SHALL Markus Bader BE LIABLE FOR ANY                    *
00021  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES      *
00022  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    *
00023  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND     *
00024  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT      *
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS   *
00026  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                    *                       *
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     //ROS_INFO("callbackLaser");
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         //mrpt::poses::CPose3D pose = laser_poses_[_msg.header.frame_id];
00100         //ROS_INFO("LASER POSE %4.3f, %4.3f, %4.3f, %4.3f, %4.3f, %4.3f",  pose.x(), pose.y(), pose.z(), pose.roll(), pose.pitch(), pose.yaw());
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 


mrpt_rawlog
Author(s):
autogenerated on Mon Aug 11 2014 11:23:25