main.cpp
Go to the documentation of this file.
3 
4 #include <ros/ros.h>
5 #include <string>
6 #include <unistd.h>
7 
8 using namespace std::string_literals;
9 using namespace astra_ros;
10 
11 int main(int argc, char *argv[])
12 {
13  ros::init(argc, argv, "astra_ros_node");
14 
15  ros::NodeHandle nh;
16  ros::NodeHandle pnh("~");
17 
18  // Get all parameter keys from the parameter server (I didn't see a way to only get a namespace)
19  std::vector<std::string> param_names;
20  if (!pnh.getParamNames(param_names))
21  {
22  ROS_ERROR("NodeHandle::getParamNames() failed. Aborting.");
23  return EXIT_FAILURE;
24  }
25 
26  // Extract the device names from the parameter key list
27  std::set<std::string> devices;
28 
29  const std::string prefix = pnh.getNamespace() + "/" + DEVICE_NAMESPACE;
30  for (const auto &param : param_names)
31  {
32  if (param.find(prefix) != 0) continue;
33 
34  std::string::size_type next_slash = param.find("/", prefix.size() + 1);
35  if (next_slash == std::string::npos) next_slash = param.size();
36  const std::string device = param.substr(prefix.size() + 1, next_slash - prefix.size() - 1);
37  devices.insert(device);
38  }
39 
40  // Construct the ROS representations of the devices
41  std::vector<RosDevice> ros_devices;
42  for (const auto &device : devices)
43  {
44  ros_devices.emplace_back(device, nh, pnh);
45  }
46 
47  // Finally, start streaming
48  ros::Rate rate(35.0);
49  while (ros::ok())
50  {
51  for (auto &ros_device : ros_devices)
52  {
53  try
54  {
55  ros_device.update();
56  }
57  catch (const Exception &exception)
58  {
59  ROS_ERROR_STREAM("Failed to update device \"" << ros_device.getName() << "\": " << exception.what());
60  continue;
61  }
62  catch(const std::exception &exception)
63  {
64  ROS_FATAL_STREAM("Unknown exception occurred while updating device \"" << ros_device.getName() << "\". Aborting: " << exception.what());
65  return EXIT_FAILURE;
66  }
67  }
68 
69  ros::spinOnce();
70  rate.sleep();
71  }
72 
73  return EXIT_SUCCESS;
74 }
astra_ros::Exception
Wraps a astra_status_t when an Astra SDK call fails.
Definition: Exception.hpp:14
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ROS_FATAL_STREAM
#define ROS_FATAL_STREAM(args)
ros.h
ros::spinOnce
ROSCPP_DECL void spinOnce()
ros::ok
ROSCPP_DECL bool ok()
astra_ros::Exception::what
virtual const char * what() const noexcept
Definition: Exception.cpp:14
RosDevice.hpp
astra_ros::DEVICE_NAMESPACE
const std::string DEVICE_NAMESPACE
ros::Rate::sleep
bool sleep()
ros::NodeHandle::getParamNames
bool getParamNames(std::vector< std::string > &keys) const
Exception.hpp
ROS_ERROR
#define ROS_ERROR(...)
param
T param(const std::string &param_name, const T &default_val)
ros::Rate
ros::NodeHandle::getNamespace
const std::string & getNamespace() const
ros::NodeHandle
main
int main(int argc, char *argv[])
Definition: main.cpp:11
astra_ros
Definition: Device.hpp:14


astra_ros
Author(s): Braden McDorman
autogenerated on Wed Mar 2 2022 00:53:06