Skip to content

Commit d8b7494

Browse files
Reenable diagnostics (#181)
Co-authored-by: Бондарь Виталий Александрович <[email protected]>
1 parent 3d08898 commit d8b7494

File tree

3 files changed

+76
-75
lines changed

3 files changed

+76
-75
lines changed

joy_linux/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
1111
endif()
1212

1313
find_package(ament_cmake REQUIRED)
14+
find_package(diagnostic_msgs REQUIRED)
15+
find_package(diagnostic_updater REQUIRED)
1416
find_package(rclcpp REQUIRED)
1517
find_package(sensor_msgs REQUIRED)
1618

@@ -25,6 +27,8 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
2527
include_directories(msg/cpp ${catkin_INCLUDE_DIRS})
2628
add_executable(joy_linux_node src/joy_linux_node.cpp)
2729
ament_target_dependencies(joy_linux_node
30+
"diagnostic_msgs"
31+
"diagnostic_updater"
2832
"rclcpp"
2933
"sensor_msgs")
3034
install(TARGETS joy_linux_node DESTINATION lib/${PROJECT_NAME})

joy_linux/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
<buildtool_depend>ament_cmake</buildtool_depend>
2828

29+
<depend>diagnostic_msgs</depend>
30+
<depend>diagnostic_updater</depend>
2931
<depend>rclcpp</depend>
3032
<depend>sensor_msgs</depend>
3133

joy_linux/src/joy_linux_node.cpp

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <sys/stat.h>
3939
#include <unistd.h>
4040

41-
// #include <diagnostic_updater/diagnostic_updater.h>
41+
#include <diagnostic_updater/diagnostic_updater.hpp>
4242
#include <rclcpp/rclcpp.hpp>
4343
#include <sensor_msgs/msg/joy.hpp>
4444
#include <sensor_msgs/msg/joy_feedback_array.hpp>
@@ -65,45 +65,41 @@ class Joystick
6565
int event_count_;
6666
int pub_count_;
6767
rclcpp::Publisher<sensor_msgs::msg::Joy>::SharedPtr pub_;
68+
rclcpp::Node::SharedPtr node_;
6869
double lastDiagTime_;
6970

7071
int ff_fd_;
7172
struct ff_effect joy_effect_;
7273
bool update_feedback_;
7374

74-
// TODO(mikaelarguedas) commenting out diagnostic logic for now
75-
76-
// diagnostic_updater::Updater diagnostic_;
77-
78-
// ///\brief Publishes diagnostics and status
79-
// void diagnostics(diagnostic_updater::DiagnosticStatusWrapper& stat)
80-
// {
81-
// double now = ros::Time::now().toSec();
82-
// double interval = now - lastDiagTime_;
83-
// if (open_)
84-
// {
85-
// stat.summary(0, "OK");
86-
// }
87-
// else
88-
// {
89-
// stat.summary(2, "Joystick not open.");
90-
// }
91-
92-
// stat.add("topic", pub_.getTopic());
93-
// stat.add("device", joy_dev_);
94-
// stat.add("device name", joy_dev_name_);
95-
// stat.add("dead zone", deadzone_);
96-
// stat.add("autorepeat rate (Hz)", autorepeat_rate_);
97-
// stat.add("coalesce interval (s)", coalesce_interval_);
98-
// stat.add("recent joystick event rate (Hz)", event_count_ / interval);
99-
// stat.add("recent publication rate (Hz)", pub_count_ / interval);
100-
// stat.add("subscribers", pub_.getNumSubscribers());
101-
// stat.add("default trig val", default_trig_val_);
102-
// stat.add("sticky buttons", sticky_buttons_);
103-
// event_count_ = 0;
104-
// pub_count_ = 0;
105-
// lastDiagTime_ = now;
106-
// }
75+
std::shared_ptr<diagnostic_updater::Updater> diagnostic_;
76+
77+
// /\brief Publishes diagnostics and status
78+
void diagnostics(diagnostic_updater::DiagnosticStatusWrapper & stat)
79+
{
80+
double now = node_->now().seconds();
81+
double interval = now - lastDiagTime_;
82+
if (open_) {
83+
stat.summary(0, "OK");
84+
} else {
85+
stat.summary(2, "Joystick not open.");
86+
}
87+
88+
stat.add("topic", pub_->get_topic_name());
89+
stat.add("device", joy_dev_);
90+
stat.add("device name", joy_dev_name_);
91+
stat.add("dead zone", deadzone_);
92+
stat.add("autorepeat rate (Hz)", autorepeat_rate_);
93+
stat.add("coalesce interval (s)", coalesce_interval_);
94+
stat.add("recent joystick event rate (Hz)", event_count_ / interval);
95+
stat.add("recent publication rate (Hz)", pub_count_ / interval);
96+
stat.add("subscribers", pub_->get_subscription_count());
97+
stat.add("default trig val", default_trig_val_);
98+
stat.add("sticky buttons", sticky_buttons_);
99+
event_count_ = 0;
100+
pub_count_ = 0;
101+
lastDiagTime_ = now;
102+
}
107103

108104
/*! \brief Returns the device path of the first joystick that matches joy_name.
109105
* If no match is found, an empty string is returned.
@@ -192,57 +188,59 @@ class Joystick
192188
}
193189
}
194190

195-
/// \brief Opens joystick port, reads from port and publishes while node is active
191+
/// \brief Opens joystick port, reads from port and publishes while node_ is active
196192
int main(int argc, char ** argv)
197193
{
198-
// diagnostic_.add("Joystick Driver Status", this, &Joystick::diagnostics);
199-
// diagnostic_.setHardwareID("none");
200194
(void)argc;
201195
(void)argv;
202196

203-
auto node = std::make_shared<rclcpp::Node>("joy_node");
197+
node_ = std::make_shared<rclcpp::Node>("joy_node");
198+
199+
diagnostic_ = std::make_shared<diagnostic_updater::Updater>(node_);
200+
diagnostic_->add("Joystick Driver Status", this, &Joystick::diagnostics);
201+
diagnostic_->setHardwareID("none");
204202

205203
// Parameters
206-
pub_ = node->create_publisher<sensor_msgs::msg::Joy>("joy", 10);
204+
pub_ = node_->create_publisher<sensor_msgs::msg::Joy>("joy", 10);
207205
rclcpp::Subscription<sensor_msgs::msg::JoyFeedbackArray>::SharedPtr sub_ =
208-
node->create_subscription<sensor_msgs::msg::JoyFeedbackArray>(
206+
node_->create_subscription<sensor_msgs::msg::JoyFeedbackArray>(
209207
"joy/set_feedback",
210208
rclcpp::QoS(10),
211209
std::bind(&Joystick::set_feedback, this, std::placeholders::_1));
212210

213-
joy_dev_ = node->declare_parameter("dev", std::string("/dev/input/js0"));
214-
joy_dev_name_ = node->declare_parameter("dev_name", std::string(""));
215-
joy_dev_ff_ = node->declare_parameter("dev_ff", "/dev/input/event0");
216-
deadzone_ = node->declare_parameter("deadzone", 0.05);
217-
autorepeat_rate_ = node->declare_parameter("autorepeat_rate", 20.0);
218-
coalesce_interval_ = node->declare_parameter("coalesce_interval", 0.001);
219-
default_trig_val_ = node->declare_parameter("default_trig_val", false);
220-
sticky_buttons_ = node->declare_parameter("sticky_buttons", false);
211+
joy_dev_ = node_->declare_parameter("dev", std::string("/dev/input/js0"));
212+
joy_dev_name_ = node_->declare_parameter("dev_name", std::string(""));
213+
joy_dev_ff_ = node_->declare_parameter("dev_ff", "/dev/input/event0");
214+
deadzone_ = node_->declare_parameter("deadzone", 0.05);
215+
autorepeat_rate_ = node_->declare_parameter("autorepeat_rate", 20.0);
216+
coalesce_interval_ = node_->declare_parameter("coalesce_interval", 0.001);
217+
default_trig_val_ = node_->declare_parameter("default_trig_val", false);
218+
sticky_buttons_ = node_->declare_parameter("sticky_buttons", false);
221219

222220
// Checks on parameters
223221
if (!joy_dev_name_.empty()) {
224-
std::string joy_dev_path = get_dev_by_joy_name(joy_dev_name_, node->get_logger());
222+
std::string joy_dev_path = get_dev_by_joy_name(joy_dev_name_, node_->get_logger());
225223
if (joy_dev_path.empty()) {
226224
RCLCPP_ERROR(
227-
node->get_logger(), "Couldn't find a joystick with name %s. "
225+
node_->get_logger(), "Couldn't find a joystick with name %s. "
228226
"Falling back to default device.",
229227
joy_dev_name_.c_str());
230228
} else {
231-
RCLCPP_INFO(node->get_logger(), "Using %s as joystick device.", joy_dev_path.c_str());
229+
RCLCPP_INFO(node_->get_logger(), "Using %s as joystick device.", joy_dev_path.c_str());
232230
joy_dev_ = joy_dev_path;
233231
}
234232
}
235233

236234
if (autorepeat_rate_ > 1 / coalesce_interval_) {
237235
RCLCPP_WARN(
238-
node->get_logger(), "joy_linux_node: autorepeat_rate (%f Hz) > "
236+
node_->get_logger(), "joy_linux_node: autorepeat_rate (%f Hz) > "
239237
"1/coalesce_interval (%f Hz) does not make sense. Timing behavior is not well defined.",
240238
autorepeat_rate_, 1 / coalesce_interval_);
241239
}
242240

243241
if (deadzone_ >= 1) {
244242
RCLCPP_WARN(
245-
node->get_logger(), "joy_linux_node: deadzone greater than 1 was requested. "
243+
node_->get_logger(), "joy_linux_node: deadzone greater than 1 was requested. "
246244
"The semantics of deadzone have changed. It is now related to the range [-1:1] instead "
247245
"of [-32767:32767]. For now I am dividing your deadzone by 32767, but this behavior is "
248246
"deprecated so you need to update your launch file.");
@@ -251,27 +249,27 @@ class Joystick
251249

252250
if (deadzone_ > 0.9) {
253251
RCLCPP_WARN(
254-
node->get_logger(), "joy_node: deadzone (%f) greater than 0.9, setting it to 0.9",
252+
node_->get_logger(), "joy_node: deadzone (%f) greater than 0.9, setting it to 0.9",
255253
deadzone_);
256254
deadzone_ = 0.9;
257255
}
258256

259257
if (deadzone_ < 0) {
260258
RCLCPP_WARN(
261-
node->get_logger(), "joy_node: deadzone_ (%f) less than 0, setting to 0.", deadzone_);
259+
node_->get_logger(), "joy_node: deadzone_ (%f) less than 0, setting to 0.", deadzone_);
262260
deadzone_ = 0;
263261
}
264262

265263
if (autorepeat_rate_ < 0) {
266264
RCLCPP_WARN(
267-
node->get_logger(), "joy_node: autorepeat_rate (%f) less than 0, setting to 0.",
265+
node_->get_logger(), "joy_node: autorepeat_rate (%f) less than 0, setting to 0.",
268266
autorepeat_rate_);
269267
autorepeat_rate_ = 0;
270268
}
271269

272270
if (coalesce_interval_ < 0) {
273271
RCLCPP_WARN(
274-
node->get_logger(), "joy_node: coalesce_interval (%f) less than 0, setting to 0.",
272+
node_->get_logger(), "joy_node: coalesce_interval (%f) less than 0, setting to 0.",
275273
coalesce_interval_);
276274
coalesce_interval_ = 0;
277275
}
@@ -288,12 +286,12 @@ class Joystick
288286

289287
event_count_ = 0;
290288
pub_count_ = 0;
291-
lastDiagTime_ = node->now().seconds();
289+
lastDiagTime_ = node_->now().seconds();
292290

293291
// Big while loop opens, publishes
294292
while (rclcpp::ok()) {
295293
open_ = false;
296-
// diagnostic_.force_update();
294+
diagnostic_->force_update();
297295
bool first_fault = true;
298296
while (true) {
299297
// In the first iteration of this loop, first_fault is true so we just
@@ -310,7 +308,7 @@ class Joystick
310308
} else {
311309
timeout = std::chrono::milliseconds(1000);
312310
}
313-
rclcpp::spin_until_future_complete(node, dummy_future, timeout);
311+
rclcpp::spin_until_future_complete(node_, dummy_future, timeout);
314312
if (!rclcpp::ok()) {
315313
goto cleanup;
316314
}
@@ -330,11 +328,10 @@ class Joystick
330328
}
331329
if (first_fault) {
332330
RCLCPP_ERROR(
333-
node->get_logger(), "Couldn't open joystick %s. Will retry every second.",
331+
node_->get_logger(), "Couldn't open joystick %s. Will retry every second.",
334332
joy_dev_.c_str());
335333
first_fault = false;
336334
}
337-
// diagnostic_.update();
338335
}
339336

340337
if (!joy_dev_ff_.empty()) {
@@ -350,7 +347,7 @@ class Joystick
350347

351348
if (write(ff_fd_, &ie, sizeof(ie)) == -1) {
352349
RCLCPP_ERROR(
353-
node->get_logger(), "Couldn't open joystick force feedback: %s", strerror(errno));
350+
node_->get_logger(), "Couldn't open joystick force feedback: %s", strerror(errno));
354351
}
355352

356353
joy_effect_.id = -1;
@@ -367,9 +364,9 @@ class Joystick
367364
}
368365

369366
RCLCPP_INFO(
370-
node->get_logger(), "Opened joystick: %s. deadzone_: %f.", joy_dev_.c_str(), deadzone_);
367+
node_->get_logger(), "Opened joystick: %s. deadzone_: %f.", joy_dev_.c_str(), deadzone_);
371368
open_ = true;
372-
// diagnostic_.force_update();
369+
diagnostic_->force_update();
373370

374371
bool tv_set = false;
375372
bool publication_pending = false;
@@ -382,7 +379,7 @@ class Joystick
382379
joy_msg->header.frame_id = "joy";
383380

384381
while (rclcpp::ok()) {
385-
rclcpp::spin_some(node);
382+
rclcpp::spin_some(node_);
386383

387384
bool publish_now = false;
388385
bool publish_soon = false;
@@ -419,7 +416,7 @@ class Joystick
419416
break; // Joystick is probably closed. Definitely occurs.
420417
}
421418

422-
joy_msg->header.stamp = node->now();
419+
joy_msg->header.stamp = node_->now();
423420
event_count_++;
424421
switch (event.type) {
425422
case JS_EVENT_BUTTON:
@@ -487,21 +484,21 @@ class Joystick
487484
}
488485
default:
489486
RCLCPP_WARN(
490-
node->get_logger(), "joy_linux_node: Unknown event type. "
487+
node_->get_logger(), "joy_linux_node: Unknown event type. "
491488
"Please file a ticket. time=%u, value=%d, type=%Xh, number=%d",
492489
event.time, event.value, event.type, event.number);
493490
break;
494491
}
495492
} else if (tv_set) { // Assume that the timer has expired.
496-
joy_msg->header.stamp = node->now();
493+
joy_msg->header.stamp = node_->now();
497494
publish_now = true;
498495
}
499496

500497
if (publish_now) {
501498
// Assume that all the JS_EVENT_INIT messages have arrived already.
502499
// This should be the case as the kernel sends them along as soon as
503500
// the device opens.
504-
joy_msg->header.stamp = node->now();
501+
joy_msg->header.stamp = node_->now();
505502
pub_->publish(*joy_msg);
506503

507504
publish_now = false;
@@ -531,21 +528,19 @@ class Joystick
531528
tv.tv_sec = 1;
532529
tv.tv_usec = 0;
533530
}
534-
535-
// diagnostic_.update();
536531
} // End of joystick open loop.
537532

538533
close(ff_fd_);
539534
close(joy_fd);
540-
rclcpp::spin_some(node);
535+
rclcpp::spin_some(node_);
541536
if (rclcpp::ok()) {
542537
RCLCPP_ERROR(
543-
node->get_logger(), "Connection to joystick device lost unexpectedly. Will reopen.");
538+
node_->get_logger(), "Connection to joystick device lost unexpectedly. Will reopen.");
544539
}
545540
}
546541

547542
cleanup:
548-
RCLCPP_INFO(node->get_logger(), "joy_node shut down.");
543+
RCLCPP_INFO(node_->get_logger(), "joy_node shut down.");
549544

550545
return 0;
551546
}

0 commit comments

Comments
 (0)