rgbd
save.cpp
Go to the documentation of this file.
1 #include <ros/console.h>
2 #include <ros/duration.h>
3 #include <ros/init.h>
4 #include <ros/master.h>
5 #include <ros/names.h>
6 #include <ros/node_handle.h>
7 #include <ros/rate.h>
8 #include <ros/time.h>
9 
10 #include "rgbd/client.h"
11 #include "rgbd/image.h"
12 #include "rgbd/serialization.h"
13 
14 #include <ctime>
15 #include <fstream>
16 #include <iostream>
17 
18 int main(int argc, char **argv)
19 {
20  ros::init(argc, argv, "rgbd_saver");
21 
22  ros::NodeHandle nh_private("~");
23 
24  float rate = 30;
25  nh_private.getParam("rate", rate);
26 
27  rgbd::Client client;
28  client.initialize(ros::names::resolve("rgbd"));
29 
30  rgbd::Image image;
31 
32  ros::WallTime last_master_check = ros::WallTime::now();
33 
34  ros::Rate r(rate);
35  char key_pressed;
36  while (ros::ok())
37  {
38  if (ros::WallTime::now() >= last_master_check + ros::WallDuration(1))
39  {
40  last_master_check = ros::WallTime::now();
41  if (!ros::master::check())
42  {
43  ROS_FATAL("Lost connection to master");
44  return 1;
45  }
46  }
47  ROS_INFO("Press s to save and q to exit.");
48 
49  #pragma GCC diagnostic push
50  #pragma GCC diagnostic ignored "-Wunused-result"
51  system ("/bin/stty raw");
52  key_pressed = getchar();
53  system ("/bin/stty cooked");
54  #pragma GCC diagnostic pop
55 
56  if (key_pressed == 's')
57  {
58  if (client.nextImage(image))
59  {
61  ss << "image_";
62  double sec;
63  const double fractional = std::modf(image.getTimestamp(), &sec);
64  const std::time_t time = sec;
65  ss << std::put_time(std::localtime(&time), "%Y-%m-%d_%H.%M.%S");
66  ss << "." << std::setw(6) << std::setfill('0') << static_cast<int32_t>(fractional*1e6);
67  ss << ".rgbd";
68  const std::string file_name = ss.str();
69 
70  std::ofstream f_out;
71  f_out.open(file_name.c_str(), std::ifstream::binary);
72  try
73  {
75  rgbd::serialize(image, a_out);
76  ROS_INFO_STREAM("Written image to '" << file_name << "'");
77  }
78  catch (const std::exception& e) // caught by reference to base
79  {
80  ROS_ERROR_STREAM("Error while writing to '" << file_name << "':\n" << e.what());
81  }
82  f_out.close();
83  }
84  }
85  else if (key_pressed == 'q')
86  {
87  ROS_INFO("Exiting");
88  return 0;
89  }
90  r.sleep();
91  }
92 
93  ROS_INFO("No image stored.");
94 
95  return 0;
96 }
ctime
fstream
std::string
std::exception
rgbd::Image::getTimestamp
double getTimestamp() const
Get the timestamp.
Definition: image.h:90
std::stringstream
std::setfill
T setfill(T... args)
tue::serialization::OutputArchive
iostream
rgbd::serialize
bool serialize(const Image &image, tue::serialization::OutputArchive &a, RGBStorageType rgb_type=RGB_STORAGE_JPG, DepthStorageType depth_type=DEPTH_STORAGE_PNG)
Definition: serialization.cpp:23
rgbd::Image
Definition: image.h:43
std::ofstream
std::string::c_str
T c_str(T... args)
rgbd::Client::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize the client.
Definition: client.cpp:34
std::ofstream::close
T close(T... args)
std::ofstream::open
T open(T... args)
image.h
std::time_t
main
int main(int argc, char **argv)
Definition: save.cpp:18
std::modf
T modf(T... args)
std::put_time
T put_time(T... args)
serialization.h
rgbd::Client
Client which uses the interfaces of ClientRGBD and ClientSHM.
Definition: client.h:29
std::setw
T setw(T... args)
std::localtime
T localtime(T... args)
client.h
std::exception::what
T what(T... args)
rgbd::Client::nextImage
bool nextImage(Image &image)
Get a new Image. If no new image has been received since the last call, no image will be written and ...
Definition: client.cpp:76