ros_bag.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2014, 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 ***************************************************************************************************/
27 
28 
29 #include <iostream>
30 #include <string>
31 
32 #include <ros/ros.h>
33 #include <rosbag/bag.h>
34 
35 #include <opencv2/highgui/highgui.hpp>
36 #include <opencv2/opencv.hpp>
37 
38 #include <sensor_msgs/Image.h>
39 #include <sensor_msgs/CompressedImage.h>
40 #include <sensor_msgs/CameraInfo.h>
41 #include <sensor_msgs/LaserScan.h>
42 
43 #include <std_msgs/String.h>
44 
45 #include <boost/date_time/posix_time/posix_time.hpp>
46 #include <boost/lexical_cast.hpp>
47 // #include <boost/thread.hpp>
48 
49 using namespace std;
50 
51 class MyRosBag
52 {
53  rosbag::Bag bag;
54  std::string image_topic;
55  std::string info_topic;
56  std::string laser_topic;
57 
58  ros::NodeHandle nh;
59  ros::Subscriber image_sub;
60  ros::Subscriber info_sub;
61  ros::Subscriber laser_sub;
62 
63  bool record;
64  public:
65  MyRosBag(std::string path_, std::string image_topic_, std::string info_topic_, std::string laser_topic_)
66  :bag(),
67  image_topic(image_topic_),
68  info_topic(info_topic_),
69  laser_topic(laser_topic_)
70  {
71  global = ros::Time::now();
72  start = ros::Time::now();
73  ref = ros::Time::now();
74 
75  boost::posix_time::ptime t(boost::posix_time::second_clock::local_time()); // Gets the current date and time
76  boost::gregorian::date d = t.date(); // Gets the date part
77  std::string date = to_iso_extended_string(d); // Converts the date to a string
78 
79  boost::posix_time::time_duration time = t.time_of_day(); // Gets the time part
80 
81 
82 // std::string hours = boost::lexical_cast<std::string>(time.hours()); // Gets the current hours
83 // std::string minutes = boost::lexical_cast<std::string>(time.minutes()); // Gets the current minutes
84 // std::string seconds = boost::lexical_cast<std::string>(time.seconds()); // Gets the current seconds
85 
86  boost::format hours("%02f"); // Format the output string
87  hours % time.hours(); // Assign alpha to the output string
88 
89  boost::format minutes("%02f"); // Format the output string
90  minutes % time.minutes();
91 
92  boost::format seconds("%02f"); // Format the output string
93  seconds % time.seconds();
94 
95  std::string path = path_ + "_" + date + "-" + hours.str() + "-" + minutes.str() + "-" + seconds.str() + ".bag"; // Sets the path where to save the bag file
96  bag.open(path, rosbag::bagmode::Write); // Creates the bag file in write mode
97  record = false;
98  }
99 
101  {
102  std::cout<<__FUNCTION__<<std::endl;
103  bag.close(); // Closes the previously created bag file
104  std::cout<<__FUNCTION__<<std::endl;
105  }
106 
107  void SetupSubs()
108  {
109 // topic_sub = nh.subscribe(topic,1000,&MyRosBag::WriteBagCallback, this);
110  image_sub = nh.subscribe(image_topic,1,&MyRosBag::compressedImageHandler, this);
111  info_sub = nh.subscribe(info_topic,1,&MyRosBag::CameraInfoHandler, this);
112  laser_sub = nh.subscribe(laser_topic,1,&MyRosBag::LaserScanHandler, this);
113  }
114 
115 
116  void toogle()
117  {
118  if(record)
119  {
120 // std::cout<<"Paused"<<std::endl;
121  ref = ref + (ros::Time::now() - start);
122  record = false;
123 
124  }else
125  {
126 // std::cout<<"Start"<<std::endl;
127  start = ros::Time::now();
128  record = true;
129  }
130 
131  }
132 
133  private:
134 
135  void writeImage(const sensor_msgs::ImagePtr& msg)
136  {
137  ros::Time warped_time = ref + (ros::Time::now() - start);
138 // msg->header.stamp = warped_time;
139 
140 // cout<<"Ref time: "<<ref-global<<endl;
141 // cout<<"Start time: "<<start-global<<endl;
142 // cout<<"Writing time: "<<warped_time-global<<endl<<endl;
143 
144  bag.write(image_topic, warped_time, msg);
145  }
146 
147  void writeImage(const sensor_msgs::CompressedImagePtr& msg)
148  {
149  ros::Time warped_time = ref + (ros::Time::now() - start);
150 // msg->header.stamp = warped_time;
151 
152 // cout<<"Ref time: "<<ref-global<<endl;
153 // cout<<"Start time: "<<start-global<<endl;
154 // cout<<"Writing time: "<<warped_time-global<<endl<<endl;
155 
156  bag.write(image_topic, warped_time, msg);
157  }
158 
159 
160  void compressedImageHandler(const sensor_msgs::CompressedImagePtr msg)
161  {
162  if (record)
163  {
164  writeImage(msg);
165 // threads.create_thread(boost::bind(&MyRosBag::writeImage,this,msg));
166 
167 
168  }else
169  {
170 // cout<<"not writing"<<endl;
171  }
172  }
173 
174  void CameraInfoHandler(const sensor_msgs::CameraInfo& cam_info_msg)
175  {
176  if ( record )
177  {
178  ros::Time warped_time = ref + (ros::Time::now() - start);
179  // cam_info_msg.header.stamp = warped_time;
180 
181 // cout<<"Ref time: "<<ref-global<<endl;
182 // cout<<"Start time: "<<start-global<<endl;
183 // cout<<"Writing time: "<<warped_time-global<<endl<<endl;
184 
185  bag.write(info_topic, warped_time, cam_info_msg);
186  }
187 // info_topic.shutdown();
188  }
189 
190  void LaserScanHandler(const sensor_msgs::LaserScan& laser_msg)
191  {
192  if ( record )
193  {
194  ros::Time warped_time = ref + (ros::Time::now() - start);
195  // cam_info_msg.header.stamp = warped_time;
196 
197 // cout<<"Ref time: "<<ref-global<<endl;
198 // cout<<"Start time: "<<start-global<<endl;
199 // cout<<"Writing time: "<<warped_time-global<<endl<<endl;
200 
201  bag.write(laser_topic, warped_time, laser_msg);
202  }
203  }
204 
205 // boost::thread_group threads;
206 
207  ros::Time global;
208  ros::Time ref;
209  ros::Time start;
210 };
211 
212 
213 int main(int argc, char** argv)
214 {
215  ros::init(argc, argv, "bag");
216 
217  ros::NodeHandle nh("~");
218 
219  std::string path;
220  nh.param<std::string>("bag_path",path,"");
221 
222  std::string image_topic;
223  nh.param<std::string>("image_topic",image_topic," ");
224 
225  std::string info_topic;
226  nh.param<std::string>("info_topic",info_topic," ");
227 
228  std::string laser_topic;
229  nh.param<std::string>("laser_topic",laser_topic," ");
230 
231  MyRosBag my_bag(path, image_topic, info_topic, laser_topic);
232  my_bag.SetupSubs();
233 
234 // cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
235  cv::Mat image(320, 240, CV_8UC3, cv::Scalar(0,0,0));
236  cv::Mat image_clone = image.clone();
237  cv::putText(image_clone, std::string("Paused"), cv::Point (image.cols/2 - 50 , image.rows/2 - 10), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,255,0), 2, 8);
238 
239  bool record = false;
240  ros::Rate r(500);
241  ros::Time begin;
242  while(ros::ok()){
243  char key;
244 // std::cout << key << std::endl;
245  if ((ros::Time::now() - begin).toSec() >= 2 & record)
246  {
247  my_bag.toogle();
248  image_clone = image.clone();
249  cv::putText(image_clone, std::string("Paused"), cv::Point (image.cols/2 - 50 , image.rows/2 - 10), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,255,0), 2, 8);
250  record = false;
251  }
252  else
253  key = cv::waitKey(2);
254  if (key == 'p' )
255  {
256  my_bag.toogle();
257  image_clone = image.clone();
258  if (record)
259  {
260  cv::putText(image_clone, std::string("Paused"), cv::Point (image.cols/2 - 50 , image.rows/2 - 10), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,255,0), 2, 8);
261  record = false;
262  }else
263  {
264  cv::putText(image_clone, std::string("Recording"), cv::Point (image.cols/2 - 60 , image.rows/2 - 10), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,255,0), 2, 8);
265  record = true;
266  begin = ros::Time::now();
267  }
268 
269  }else if(key=='q')
270  break;
271  cv::imshow("Display window", image_clone);
272  ros::spinOnce();
273  r.sleep();
274  }
275 
276 
277  return 0;
278 }
ros::NodeHandle nh
Definition: ros_bag.cpp:58
std::string image_topic
Definition: ros_bag.cpp:54
ros::Time ref
Definition: ros_bag.cpp:208
ros::Time global
Definition: ros_bag.cpp:207
void LaserScanHandler(const sensor_msgs::LaserScan &laser_msg)
Definition: ros_bag.cpp:190
void writeImage(const sensor_msgs::ImagePtr &msg)
Definition: ros_bag.cpp:135
ros::Subscriber laser_sub
Definition: ros_bag.cpp:61
~MyRosBag()
Definition: ros_bag.cpp:100
int main(int argc, char **argv)
Definition: ros_bag.cpp:213
rosbag::Bag bag
Definition: ros_bag.cpp:53
void CameraInfoHandler(const sensor_msgs::CameraInfo &cam_info_msg)
Definition: ros_bag.cpp:174
bool record
Definition: ros_bag.cpp:63
void compressedImageHandler(const sensor_msgs::CompressedImagePtr msg)
Definition: ros_bag.cpp:160
ros::Time start
Definition: ros_bag.cpp:209
void SetupSubs()
Definition: ros_bag.cpp:107
void toogle()
Definition: ros_bag.cpp:116
MyRosBag(std::string path_, std::string image_topic_, std::string info_topic_, std::string laser_topic_)
Definition: ros_bag.cpp:65
std::string info_topic
Definition: ros_bag.cpp:55
void writeImage(const sensor_msgs::CompressedImagePtr &msg)
Definition: ros_bag.cpp:147
ros::Subscriber image_sub
Definition: ros_bag.cpp:59
ros::Subscriber info_sub
Definition: ros_bag.cpp:60
std::string laser_topic
Definition: ros_bag.cpp:56


datamatrix_detection
Author(s): Luís Pedras Carrão
autogenerated on Mon Mar 2 2015 01:31:36