rgbd
rgbd_to_ros.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/server_ros.h"
13 
14 #include <string>
15 
16 int main(int argc, char **argv)
17 {
19  ros::removeROSArgs(argc, argv, myargv);
20  bool publish_rgb = false, publish_depth = false, publish_pc = false;
21  {
22  bool valid_arg_provided = false;
23  for (uint i = 1; i < myargv.size(); ++i)
24  {
25  const std::string& opt = myargv[i];
26  if (opt == "-h" || opt == "--help")
27  {
28  std::cout << "Usage: rgbd_to_ros [OPTIONS]" << std::endl
29  << " If no valid options are provided, rgb and depth images and camera info will be published" << std::endl
30  << "Options:" << std::endl
31  << " -h, --help: show this message" << std::endl
32  << " -a, --all: publish rgb, depth and pointcloud" << std::endl
33  << " --rgb, --color: publish rgb image and camera info" << std::endl
34  << " --depth: publish depth image and camera info" << std::endl
35  << " --rgbd: publish rgb and depth images and camera info" << std::endl
36  << " --pc, --pointcloud: publish pointcloud" << std::endl;
37  return 0;
38  }
39  else if (opt == "-a" || opt == "--all")
40  {
41  publish_rgb = true;
42  publish_depth = true;
43  publish_pc = true;
44  valid_arg_provided = true;
45  }
46  else if (opt == "--rgb" || opt == "--color")
47  {
48  publish_rgb = true;
49  valid_arg_provided = true;
50  }
51  else if (opt == "--depth")
52  {
53  publish_depth = true;
54  valid_arg_provided = true;
55  }
56  else if (opt == "--rgbd")
57  {
58  publish_rgb = true;
59  publish_depth = true;
60  valid_arg_provided = true;
61  }
62  else if (opt == "--pc" || opt == "--pointcloud")
63  {
64  publish_pc = true;
65  valid_arg_provided = true;
66  }
67  else if (!opt.compare(0, 2, "--"))
68  {
69  std::cout << "[rgbd_to_ros] Unknown option: '" << opt << "'." << std::endl;
70  return 1;
71  }
72  else
73  {
74  std::cout << "[rgbd_to_ros] Ignoring option: '" << opt << "'." << std::endl;
75  }
76  }
77  if (!valid_arg_provided)
78  {
79  publish_rgb = true;
80  publish_depth = true;
81  }
82  }
83 
84  ros::init(argc, argv, "rgbd_to_ros");
85  ros::start(); // Required to use ros::names::resolve, without creating a nodehandle
86 
87  ROS_DEBUG_STREAM("publish_rgb: " << publish_rgb);
88  ROS_DEBUG_STREAM("publish_depth: " << publish_depth);
89  ROS_DEBUG_STREAM("publish_pc: " << publish_pc);
90 
91  // Listener
92  rgbd::Client client;
93  client.initialize(ros::names::resolve("rgbd"));
94 
95  // Publishers
96  rgbd::ServerROS server;
97  server.initialize("", publish_rgb, publish_depth, publish_pc);
98 
99  ros::NodeHandle nh_private("~");
100  float rate = 30;
101  nh_private.getParam("rate", rate);
102 
103  rgbd::Image image;
104 
105  ros::WallTime last_master_check = ros::WallTime::now();
106 
107  ros::Rate r(rate);
108  while (ros::ok())
109  {
110  if (ros::WallTime::now() >= last_master_check + ros::WallDuration(1))
111  {
112  last_master_check = ros::WallTime::now();
113  if (!ros::master::check())
114  {
115  ROS_FATAL("Lost connection to master");
116  return 1;
117  }
118  }
119  if (client.nextImage(image))
120  {
121  server.send(image);
122  }
123 
124  r.sleep();
125  }
126 
127  return 0;
128 }
std::string
std::vector
std::vector::size
T size(T... args)
std::cout
rgbd::Image
Definition: image.h:43
rgbd::ServerROS
Server which publishes ROS rgb image, depth image and pointcloud messages.
Definition: server_ros.h:16
rgbd::Client::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize the client.
Definition: client.cpp:34
std::string::compare
T compare(T... args)
rgbd::ServerROS::send
void send(const Image &image)
Publish a new image to the selected ROS topics.
Definition: server_ros.cpp:75
server_ros.h
image.h
main
int main(int argc, char **argv)
Definition: rgbd_to_ros.cpp:16
rgbd::ServerROS::initialize
void initialize(std::string ns="", const bool publish_rgb=false, const bool publish_depth=false, const bool publish_pc=false)
initialize server
Definition: server_ros.cpp:31
std::endl
T endl(T... args)
rgbd::Client
Client which uses the interfaces of ClientRGBD and ClientSHM.
Definition: client.h:29
client.h
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
string