Skip to content

Commit a03a1fd

Browse files
authored
Add auto standby mode (#29)
* Add auto standby mode Turn on/off motor based on topic subsribers * Set auto_standby off by default
1 parent 69541cd commit a03a1fd

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

include/rplidar_node.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class RPLIDAR_ROS_PUBLIC rplidar_node : public rclcpp::Node
9595
bool checkRPLIDARHealth() const;
9696
bool set_scan_mode();
9797
void publish_loop();
98+
void start();
99+
void stop();
98100

99101
/* parameters */
100102
std::string channel_type_;
@@ -109,6 +111,7 @@ class RPLIDAR_ROS_PUBLIC rplidar_node : public rclcpp::Node
109111
bool flip_x_axis_;
110112
int m_angle_compensate_multiple;
111113
std::string scan_mode_;
114+
bool auto_standby_;
112115
/* Publisher */
113116
LaserScanPub m_publisher;
114117
/* Services */
@@ -124,6 +127,8 @@ class RPLIDAR_ROS_PUBLIC rplidar_node : public rclcpp::Node
124127
double angle_min = deg_2_rad(0);
125128
double angle_max = deg_2_rad(359);
126129
const float min_distance = 0.15f;
130+
/* State */
131+
bool m_running = false;
127132
};
128133

129134
} // namespace rplidar_ros

src/rplidar_node.cpp

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ rplidar_node::rplidar_node(const rclcpp::NodeOptions & options)
5353
flip_x_axis_ = this->declare_parameter("flip_x_axis", false);
5454
scan_mode_ = this->declare_parameter("scan_mode", std::string());
5555
topic_name_ = this->declare_parameter("topic_name", std::string("scan"));
56+
auto_standby_ = this->declare_parameter("auto_standby", false);
5657

5758
RCLCPP_INFO(
5859
this->get_logger(),
@@ -103,15 +104,9 @@ rplidar_node::rplidar_node(const rclcpp::NodeOptions & options)
103104
return;
104105
}
105106

106-
/* start motor */
107-
m_drv->startMotor();
108-
109-
if (!set_scan_mode()) {
110-
/* set the scan mode */
111-
m_drv->stop();
112-
m_drv->stopMotor();
113-
RPlidarDriver::DisposeDriver(m_drv);
114-
exit(1);
107+
/* start motor and scanning */
108+
if (!auto_standby_) {
109+
this->start();
115110
}
116111

117112
/* done setting up RPLIDAR stuff, now set up ROS 2 stuff */
@@ -247,24 +242,28 @@ bool rplidar_node::checkRPLIDARHealth() const
247242

248243
void rplidar_node::stop_motor(const EmptyRequest req, EmptyResponse res)
249244
{
250-
if (nullptr == m_drv) {
245+
if (auto_standby_) {
246+
RCLCPP_INFO(
247+
this->get_logger(),
248+
"Ingnoring stop_motor request because rplidar_node is in 'auto standby' mode");
251249
return;
252250
}
253-
254251
RCLCPP_DEBUG(this->get_logger(), "Call to '%s'", __FUNCTION__);
255-
m_drv->stop();
256-
m_drv->stopMotor();
252+
253+
this->stop();
257254
}
258255

259256
void rplidar_node::start_motor(const EmptyRequest req, EmptyResponse res)
260257
{
261-
if (nullptr == m_drv) {
258+
if (auto_standby_) {
259+
RCLCPP_INFO(
260+
this->get_logger(),
261+
"Ingnoring start_motor request because rplidar_node is in 'auto standby' mode");
262262
return;
263263
}
264-
265264
RCLCPP_DEBUG(this->get_logger(), "Call to '%s'", __FUNCTION__);
266-
m_drv->startMotor();
267-
m_drv->startScan(0, 1);
265+
266+
this->start();
268267
}
269268

270269
bool rplidar_node::set_scan_mode()
@@ -332,6 +331,17 @@ void rplidar_node::publish_loop()
332331
size_t count = 360 * 8;
333332
auto nodes = std::make_unique<rplidar_response_measurement_node_hq_t[]>(count);
334333

334+
if (auto_standby_) {
335+
if (m_publisher->get_subscription_count() > 0 && !m_running) {
336+
this->start();
337+
} else if (m_publisher->get_subscription_count() == 0) {
338+
if (m_running) {
339+
this->stop();
340+
}
341+
return;
342+
}
343+
}
344+
335345
start_scan_time = this->now();
336346
op_result = m_drv->grabScanDataHq(nodes.get(), count);
337347
end_scan_time = this->now();
@@ -394,6 +404,35 @@ void rplidar_node::publish_loop()
394404
}
395405
}
396406

407+
void rplidar_node::start()
408+
{
409+
if (nullptr == m_drv) {
410+
return;
411+
}
412+
413+
RCLCPP_INFO(this->get_logger(), "Start");
414+
m_drv->startMotor();
415+
if (!set_scan_mode()) {
416+
this->stop();
417+
RCLCPP_ERROR(this->get_logger(), "Failed to set scan mode");
418+
RPlidarDriver::DisposeDriver(m_drv);
419+
exit(1);
420+
}
421+
m_running = true;
422+
}
423+
424+
void rplidar_node::stop()
425+
{
426+
if (nullptr == m_drv) {
427+
return;
428+
}
429+
430+
RCLCPP_INFO(this->get_logger(), "Stop");
431+
m_drv->stop();
432+
m_drv->stopMotor();
433+
m_running = false;
434+
}
435+
397436
} // namespace rplidar_ros
398437

399438
#include "rclcpp_components/register_node_macro.hpp"

0 commit comments

Comments
 (0)