sick_scan_ros2_example.cpp
Go to the documentation of this file.
1 
61 #include <rclcpp/rclcpp.hpp>
62 #include <sensor_msgs/msg/point_cloud2.hpp>
63 
64 #include <sick_scan_xd/msg/radar_scan.hpp> // generated in sick_scan_xd by msg-generator
65 #include <sick_scan_xd/msg/encoder.hpp> // generated in sick_scan_xd by msg-generator
66 #include <sick_scan_xd/msg/li_doutputstate_msg.hpp> // generated in sick_scan_xd by msg-generator
67 #include <sick_scan_xd/msg/lf_erec_msg.hpp> // generated in sick_scan_xd by msg-generator
68 #include <sick_scan_xd/srv/cola_msg_srv.hpp> // generated in sick_scan_xd by rosidl-generator
69 
70 #define RCLCPP_LOGGER rclcpp::get_logger("sick_scan_ros2_example")
71 #define ROS_INFO_STREAM(...) RCLCPP_INFO_STREAM(RCLCPP_LOGGER,__VA_ARGS__)
72 #define ROS_ERROR_STREAM(...) RCLCPP_ERROR_STREAM(RCLCPP_LOGGER,__VA_ARGS__)
73 
74 /*
75 * @brief class SickScanMessageReceiver subscribes to pointcloud, lidoutputstate and lferec messages. Tiny example how to subscribe to sick_scan messages.
76 */
78 {
79 public:
81  SickScanMessageReceiver(rclcpp::Node::SharedPtr node = 0, const std::string& cloud_topic = "cloud", const std::string& lferec_topic = "lferec", const std::string& lidoutputstate_topic = "lidoutputstate")
82  {
83  if (node)
84  {
85  m_pointcloud_subscriber = node->create_subscription<sensor_msgs::msg::PointCloud2>(cloud_topic, 10, std::bind(&SickScanMessageReceiver::messageCbPointCloudROS2, this, std::placeholders::_1));
86  m_lferec_subscriber = node->create_subscription<sick_scan_xd::msg::LFErecMsg>(lferec_topic, 10, std::bind(&SickScanMessageReceiver::messageCbLFErecROS2, this, std::placeholders::_1));
87  m_lidoutputstate_subscriber = node->create_subscription<sick_scan_xd::msg::LIDoutputstateMsg>(lidoutputstate_topic, 10, std::bind(&SickScanMessageReceiver::messageCbLIDoutputstateROS2, this, std::placeholders::_1));
88  }
89  }
90 protected:
91 
93  void messageCbPointCloudROS2(const std::shared_ptr<sensor_msgs::msg::PointCloud2> msg)
94  {
95  ROS_INFO_STREAM("sick_scan_ros2_example: pointcloud message received, size " << msg-> width << " x " << msg->height);
96  }
97 
99  void messageCbLFErecROS2(const std::shared_ptr<sick_scan_xd::msg::LFErecMsg> msg)
100  {
101  ROS_INFO_STREAM("sick_scan_ros2_example: lferec message received, " << msg->fields_number << " fields");
102  }
103 
105  void messageCbLIDoutputstateROS2(const std::shared_ptr<sick_scan_xd::msg::LIDoutputstateMsg> msg)
106  {
107  ROS_INFO_STREAM("sick_scan_ros2_example: lidoutputstate message received, " << msg->output_state.size() << " output states, " << msg->output_count.size() << " output counter");
108  }
109 
111  rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr m_pointcloud_subscriber;
112  rclcpp::Subscription<sick_scan_xd::msg::LFErecMsg>::SharedPtr m_lferec_subscriber;
113  rclcpp::Subscription<sick_scan_xd::msg::LIDoutputstateMsg>::SharedPtr m_lidoutputstate_subscriber;
114 };
115 
117 bool sendSickScanServiceRequest(rclcpp::Node::SharedPtr node, rclcpp::Client<sick_scan_xd::srv::ColaMsgSrv>::SharedPtr sick_scan_srv_colamsg_client, const std::string& cola_msg)
118 {
119  std::shared_ptr<sick_scan_xd::srv::ColaMsgSrv::Request> sick_scan_srv_request = std::make_shared<sick_scan_xd::srv::ColaMsgSrv::Request>();
120  sick_scan_srv_request->request = cola_msg;
121  ROS_INFO_STREAM("sick_scan_ros2_example: sick_scan service request: \"" << sick_scan_srv_request->request << "\"");
122  std::shared_future<std::shared_ptr<sick_scan_xd::srv::ColaMsgSrv::Response>> sick_scan_srv_result = sick_scan_srv_colamsg_client->async_send_request(sick_scan_srv_request);
123  if (rclcpp::spin_until_future_complete(node, sick_scan_srv_result) == rclcpp::FutureReturnCode::SUCCESS)
124  {
125  ROS_INFO_STREAM("sick_scan_ros2_example: sick_scan service response: \"" << sick_scan_srv_result.get()->response << "\"");
126  return true;
127  }
128  else
129  {
130  ROS_ERROR_STREAM("## ERROR sick_scan_ros2_example: sick_scan service request failed");
131  return false;
132  }
133 }
134 
135 /*
136 * @brief Tiny ROS-2 example to show how to use sick_scan_xd messages and services in a ROS-2 application. Example for TiM7xx topics.
137 */
138 int main(int argc, char** argv)
139 {
140  // ROS node initialization
141  rclcpp::init(argc, argv);
142  rclcpp::NodeOptions node_options;
143  node_options.allow_undeclared_parameters(true);
144  rclcpp::Node::SharedPtr node = rclcpp::Node::make_shared("sick_scan_ros2_example", "", node_options);
145  std::string sick_scan_msg_topic = "/sick_tim_7xx"; // Example for tim7xx: messages publiched on topic "/cloud", "/sick_tim_7xx/lferec" and "/sick_tim_7xx/lidoutputstate"
146  for (int i = 0; i < argc; i++)
147  {
148  if (strncmp(argv[i], "topics:=", 8) == 0)
149  {
150  sick_scan_msg_topic = std::string("/") + std::string(argv[i] + 8);
151  }
152  }
153  ROS_INFO_STREAM("sick_scan_ros2_example initialized");
154 
155  // Receive pointcloud, lidoutputstate and lferec messages
156  SickScanMessageReceiver sickscan_message_receiver(node, "/cloud", sick_scan_msg_topic + "/lferec", sick_scan_msg_topic + "/lidoutputstate");
157 
158  // Create a sick_scan service client
159  rclcpp::Client<sick_scan_xd::srv::ColaMsgSrv>::SharedPtr sick_scan_srv_colamsg_client = node->create_client<sick_scan_xd::srv::ColaMsgSrv>("ColaMsg");
160  while (rclcpp::ok() && !sick_scan_srv_colamsg_client->wait_for_service(std::chrono::seconds(1)))
161  {
162  ROS_INFO_STREAM("sick_scan_ros2_example: Waiting for sick_scan service...");
163  }
164  if (rclcpp::ok())
165  {
166  if (sick_scan_srv_colamsg_client->wait_for_service(std::chrono::milliseconds(1)))
167  ROS_INFO_STREAM("sick_scan_ros2_example: sick_scan service available");
168  else
169  ROS_ERROR_STREAM("## ERROR sick_scan_ros2_example: sick_scan service not available");
170  }
171 
172  // Run event loop
173  // rclcpp::spin(node);
174  rclcpp::Time sick_scan_srv_request_timestamp = rclcpp::Clock().now();
175  while (rclcpp::ok())
176  {
177  rclcpp::spin_some(node);
178  rclcpp::sleep_for(std::chrono::milliseconds(1));
179  if (rclcpp::Clock().now() > sick_scan_srv_request_timestamp + std::chrono::seconds(1))
180  {
181  // Send a sick_scan service request example each second
182  sendSickScanServiceRequest(node, sick_scan_srv_colamsg_client, "sRN SCdevicestate");
183  sick_scan_srv_request_timestamp = rclcpp::Clock().now();
184  }
185  }
186 
187  // Cleanup and exit
188  ROS_INFO_STREAM("sick_scan_ros2_example finished");
189  return 0;
190 }
msg
msg
sensor_msgs::PointCloud2
::sensor_msgs::PointCloud2_< std::allocator< void > > PointCloud2
Definition: PointCloud2.h:90
SickScanMessageReceiver::m_lferec_subscriber
rclcpp::Subscription< sick_scan_xd::msg::LFErecMsg >::SharedPtr m_lferec_subscriber
Definition: sick_scan_ros2_example.cpp:112
sick_scan_xd::LFErecMsg
::sick_scan_xd::LFErecMsg_< std::allocator< void > > LFErecMsg
Definition: LFErecMsg.h:61
ROS::now
ROS::Time now(void)
Definition: ros_wrapper.cpp:116
roswrap::param::init
void init(const M_string &remappings)
Definition: param_modi.cpp:809
SickScanMessageReceiver::m_pointcloud_subscriber
rclcpp::Subscription< sensor_msgs::msg::PointCloud2 >::SharedPtr m_pointcloud_subscriber
Definition: sick_scan_ros2_example.cpp:111
ROS_INFO_STREAM
#define ROS_INFO_STREAM(...)
Definition: sick_scan_ros2_example.cpp:71
sendSickScanServiceRequest
bool sendSickScanServiceRequest(rclcpp::Node::SharedPtr node, rclcpp::Client< sick_scan_xd::srv::ColaMsgSrv >::SharedPtr sick_scan_srv_colamsg_client, const std::string &cola_msg)
Definition: sick_scan_ros2_example.cpp:117
polar_to_cartesian_pointcloud_ros1.node
node
Definition: polar_to_cartesian_pointcloud_ros1.py:81
SickScanMessageReceiver
Definition: sick_scan_ros2_example.cpp:77
SickScanMessageReceiver::messageCbLIDoutputstateROS2
void messageCbLIDoutputstateROS2(const std::shared_ptr< sick_scan_xd::msg::LIDoutputstateMsg > msg)
Definition: sick_scan_ros2_example.cpp:105
SickScanMessageReceiver::m_lidoutputstate_subscriber
rclcpp::Subscription< sick_scan_xd::msg::LIDoutputstateMsg >::SharedPtr m_lidoutputstate_subscriber
Definition: sick_scan_ros2_example.cpp:113
SickScanMessageReceiver::SickScanMessageReceiver
SickScanMessageReceiver(rclcpp::Node::SharedPtr node=0, const std::string &cloud_topic="cloud", const std::string &lferec_topic="lferec", const std::string &lidoutputstate_topic="lidoutputstate")
Definition: sick_scan_ros2_example.cpp:81
SickScanMessageReceiver::messageCbPointCloudROS2
void messageCbPointCloudROS2(const std::shared_ptr< sensor_msgs::msg::PointCloud2 > msg)
Definition: sick_scan_ros2_example.cpp:93
sick_scan_xd::LIDoutputstateMsg
::sick_scan_xd::LIDoutputstateMsg_< std::allocator< void > > LIDoutputstateMsg
Definition: LIDoutputstateMsg.h:110
std_msgs::Time
::std_msgs::Time_< std::allocator< void > > Time
Definition: Time.h:48
main
int main(int argc, char **argv)
Definition: sick_scan_ros2_example.cpp:138
ROS::seconds
double seconds(ROS::Duration duration)
Definition: ros_wrapper.cpp:180
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(...)
Definition: sick_scan_ros2_example.cpp:72
SickScanMessageReceiver::messageCbLFErecROS2
void messageCbLFErecROS2(const std::shared_ptr< sick_scan_xd::msg::LFErecMsg > msg)
Definition: sick_scan_ros2_example.cpp:99
roswrap::ok
ROSCPP_DECL bool ok()
Check whether it's time to exit.
Definition: rossimu.cpp:273


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:11