laserWriter.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
31 // #include <laser_scan_to_text/laserWriter.h>
32 
33 #include <ros/ros.h>
34 #include <string>
35 #include <iostream>
36 #include <fstream>
37 #include <vector>
38 #include <sensor_msgs/LaserScan.h>
39 
41  public:
42  int version;
43  bool firstScan;
44  std::string outFilePath;
45 
46  LaserWriter(const ros::NodeHandle& nh_,std::string outFile):
47  nh(nh_),
48  firstScan(true),
49  outFilePath(outFile),
50  version(1)
51  {}
52 
54  {}
55 
57  {
58  laserSubscriber = nh.subscribe<sensor_msgs::LaserScan>("laserScan", 1000, &LaserWriter::laserScanHandler, this);
59  }
60 
61  void laserScanHandler(const sensor_msgs::LaserScan::ConstPtr& scan)
62  {
63  std::ofstream outFile;
64 
65  if(firstScan){
66  outFile.open(outFilePath, std::ios_base::out);
67  outFile << "#Laser Scan in Text Format, version: " << version<< std::endl;
68  outFile << "#Created by Jorge Almeida from ros/bag"<< std::endl;
69  outFile << "angle_min: " << scan->angle_min<<" #rad "<<std::endl;
70  outFile << "angle_max: " << scan->angle_max<<" #rad "<<std::endl;
71  outFile << "angle_increment: " << scan->angle_increment<<" #rad "<<std::endl;
72  outFile << "time_increment: " << scan->time_increment<<" #sec"<<std::endl;
73  outFile << "scan_time: " << scan->scan_time<<" #sec"<<std::endl;
74  outFile << "range_min: " << scan->range_min<<" #m"<< std::endl;
75  outFile << "range_max: " << scan->range_max<<" #m"<< std::endl;
76  outFile <<"#Format of each scan: TIMESTAMP RANGE_1 RANGE_2 ... [RANGE_N]"<<std::endl;
77  firstScan = false;
78  }else{
79  outFile.open(outFilePath, std::ios_base::app);
80  }
81 
82 
83  outFile << scan->header.stamp << " ";
84  for(float range : scan->ranges){
85  outFile << range << " ";
86  }
87 
88  outFile << std::endl;
89  }
90 
91  void loop()
92  {
93  ros::spin();
94  }
95 
97  ros::NodeHandle nh;
99  ros::Subscriber laserSubscriber;
100 };
101 
102 int main(int argc, char** argv)
103 {
104  std::cout<<"Starting laserWriter"<<std::endl;
105  ros::init(argc, argv, "laserWriter", ros::init_options::AnonymousName);
106  ros::NodeHandle nh;
107 
108  LaserWriter laserWriter(nh,"/home/jorge14/Desktop/test1.txt");
109 
110  //Setup message advertise and subscription
111  laserWriter.setupMessaging();
112 
113  std::cout<<"In main loop"<<std::endl;
114  //Main program loop
115  laserWriter.loop();
116 
117  return true;
118 };
ros::NodeHandle nh
Ros node handler.
Definition: laserWriter.cpp:97
void setupMessaging()
Definition: laserWriter.cpp:56
std::string outFilePath
Definition: laserWriter.cpp:44
void laserScanHandler(const sensor_msgs::LaserScan::ConstPtr &scan)
Definition: laserWriter.cpp:61
ros::Subscriber laserSubscriber
Ros command subscriber.
Definition: laserWriter.cpp:99
LaserWriter(const ros::NodeHandle &nh_, std::string outFile)
Definition: laserWriter.cpp:46
int main(int argc, char **argv)


laser_scan_to_text
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:07