5050
5151#include " rcl/time.h"
5252
53- #include " rclcpp/create_timer.hpp"
54- #include " rclcpp/rclcpp.hpp"
53+ #include " rclcpp/node_interfaces/node_base_interface.hpp"
54+ #include " rclcpp/node_interfaces/node_clock_interface.hpp"
55+ #include " rclcpp/node_interfaces/node_logging_interface.hpp"
56+ #include " rclcpp/node_interfaces/node_parameters_interface.hpp"
57+ #include " rclcpp/node_interfaces/node_timers_interface.hpp"
58+ #include " rclcpp/node_interfaces/node_topics_interface.hpp"
5559
5660namespace diagnostic_updater
5761{
@@ -381,43 +385,7 @@ class Updater : public DiagnosticTaskVector
381385 std::shared_ptr<rclcpp::node_interfaces::NodeParametersInterface> parameters_interface,
382386 std::shared_ptr<rclcpp::node_interfaces::NodeTimersInterface> timers_interface,
383387 std::shared_ptr<rclcpp::node_interfaces::NodeTopicsInterface> topics_interface,
384- double period = 1.0 )
385- : verbose_(false ),
386- base_interface_(base_interface),
387- timers_interface_(timers_interface),
388- clock_(clock_interface->get_clock ()),
389- period_(rclcpp::Duration::from_seconds(period)),
390- publisher_(
391- rclcpp::create_publisher<diagnostic_msgs::msg::DiagnosticArray>(
392- topics_interface, " /diagnostics" , 1 )),
393- logger_(logging_interface->get_logger ()),
394- node_name_(base_interface->get_name ()),
395- warn_nohwid_done_(false )
396- {
397- constexpr const char * period_param_name = " diagnostic_updater.period" ;
398- rclcpp::ParameterValue period_param;
399- if (parameters_interface->has_parameter (period_param_name)) {
400- period_param = parameters_interface->get_parameter (period_param_name).get_parameter_value ();
401- } else {
402- period_param = parameters_interface->declare_parameter (
403- period_param_name, rclcpp::ParameterValue (period));
404- }
405- period = period_param.get <double >();
406- period_ = rclcpp::Duration::from_seconds (period);
407-
408- reset_timer ();
409-
410- constexpr const char * use_fqn_param_name = " diagnostic_updater.use_fqn" ;
411- rclcpp::ParameterValue use_fqn_param;
412- if (parameters_interface->has_parameter (use_fqn_param_name)) {
413- use_fqn_param = parameters_interface->get_parameter (use_fqn_param_name).get_parameter_value ();
414- } else {
415- use_fqn_param = parameters_interface->declare_parameter (
416- use_fqn_param_name, rclcpp::ParameterValue (false ));
417- }
418- node_name_ = use_fqn_param.get <bool >() ?
419- base_interface->get_fully_qualified_name () : base_interface->get_name ();
420- }
388+ double period = 1.0 );
421389
422390 /* *
423391 * \brief Returns the interval between updates.
@@ -459,105 +427,20 @@ class Updater : public DiagnosticTaskVector
459427 *
460428 * \param msg Status message to output.
461429 */
462- void broadcast (unsigned char lvl, const std::string msg)
463- {
464- std::vector<diagnostic_msgs::msg::DiagnosticStatus> status_vec;
465-
466- const std::vector<DiagnosticTaskInternal> & tasks = getTasks ();
467- for (std::vector<DiagnosticTaskInternal>::const_iterator iter =
468- tasks.begin ();
469- iter != tasks.end (); iter++)
470- {
471- diagnostic_updater::DiagnosticStatusWrapper status;
472-
473- status.name = iter->getName ();
474- status.summary (lvl, msg);
475-
476- status_vec.push_back (status);
477- }
430+ void broadcast (unsigned char lvl, const std::string msg);
478431
479- publish (status_vec);
480- }
481-
482- void setHardwareIDf (const char * format, ...)
483- {
484- va_list va;
485- const int kBufferSize = 1000 ;
486- char buff[kBufferSize ]; // @todo This could be done more elegantly.
487- va_start (va, format);
488- if (vsnprintf (buff, kBufferSize , format, va) >= kBufferSize ) {
489- RCLCPP_DEBUG (logger_, " Really long string in diagnostic_updater::setHardwareIDf." );
490- }
491- hwid_ = std::string (buff);
492- va_end (va);
493- }
432+ void setHardwareIDf (const char * format, ...);
494433
495434 void setHardwareID (const std::string & hwid) {hwid_ = hwid;}
496435
497436private:
498- void reset_timer ()
499- {
500- update_timer_ = rclcpp::create_timer (
501- base_interface_,
502- timers_interface_,
503- clock_,
504- period_,
505- std::bind (&Updater::update, this ));
506- }
437+ void reset_timer ();
507438
508439 /* *
509440 * \brief Causes the diagnostics to update if the inter-update interval
510441 * has been exceeded.
511442 */
512- void update ()
513- {
514- if (rclcpp::ok (base_interface_->get_context ())) {
515- bool warn_nohwid = hwid_.empty ();
516-
517- std::vector<diagnostic_msgs::msg::DiagnosticStatus> status_vec;
518-
519- std::unique_lock<std::mutex> lock (
520- lock_); // Make sure no adds happen while we are processing here.
521- const std::vector<DiagnosticTaskInternal> & tasks = getTasks ();
522- for (std::vector<DiagnosticTaskInternal>::const_iterator iter =
523- tasks.begin ();
524- iter != tasks.end (); iter++)
525- {
526- diagnostic_updater::DiagnosticStatusWrapper status;
527-
528- status.name = iter->getName ();
529- status.level = 2 ;
530- status.message = " No message was set" ;
531- status.hardware_id = hwid_;
532-
533- iter->run (status);
534-
535- status_vec.push_back (status);
536-
537- if (status.level ) {
538- warn_nohwid = false ;
539- }
540-
541- if (verbose_ && status.level ) {
542- RCLCPP_WARN (
543- logger_, " Non-zero diagnostic status. Name: '%s', status %i: '%s'" ,
544- status.name .c_str (), status.level , status.message .c_str ());
545- }
546- }
547-
548- if (warn_nohwid && !warn_nohwid_done_) {
549- std::string error_msg = " diagnostic_updater: No HW_ID was set." ;
550- error_msg += " This is probably a bug. Please report it." ;
551- error_msg += " For devices that do not have a HW_ID, set this value to 'none'." ;
552- error_msg += " This warning only occurs once all diagnostics are OK." ;
553- error_msg += " It is okay to wait until the device is open before calling setHardwareID." ;
554- RCLCPP_WARN (logger_, " %s" , error_msg.c_str ());
555- warn_nohwid_done_ = true ;
556- }
557-
558- publish (status_vec);
559- }
560- }
443+ void update ();
561444
562445 /* *
563446 * Recheck the diagnostic_period on the parameter server. (Cached)
@@ -574,41 +457,18 @@ class Updater : public DiagnosticTaskVector
574457 /* *
575458 * Publishes a single diagnostic status.
576459 */
577- void publish (diagnostic_msgs::msg::DiagnosticStatus & stat)
578- {
579- std::vector<diagnostic_msgs::msg::DiagnosticStatus> status_vec;
580- status_vec.push_back (stat);
581- publish (status_vec);
582- }
460+ void publish (diagnostic_msgs::msg::DiagnosticStatus & stat);
583461
584462 /* *
585463 * Publishes a vector of diagnostic statuses.
586464 */
587- void publish (std::vector<diagnostic_msgs::msg::DiagnosticStatus> & status_vec)
588- {
589- for (std::vector<diagnostic_msgs::msg::DiagnosticStatus>::iterator iter =
590- status_vec.begin ();
591- iter != status_vec.end (); iter++)
592- {
593- iter->name = node_name_ + std::string (" : " ) + iter->name ;
594- }
595- diagnostic_msgs::msg::DiagnosticArray msg;
596- msg.status = status_vec;
597- msg.header .stamp = clock_->now ();
598- publisher_->publish (msg);
599- }
465+ void publish (std::vector<diagnostic_msgs::msg::DiagnosticStatus> & status_vec);
600466
601467 /* *
602468 * Causes a placeholder DiagnosticStatus to be published as soon as a
603469 * diagnostic task is added to the Updater.
604470 */
605- virtual void addedTaskCallback (DiagnosticTaskInternal & task)
606- {
607- DiagnosticStatusWrapper stat;
608- stat.name = task.getName ();
609- stat.summary (0 , " Node starting up" );
610- publish (stat);
611- }
471+ virtual void addedTaskCallback (DiagnosticTaskInternal & task);
612472
613473 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr base_interface_;
614474 rclcpp::node_interfaces::NodeTimersInterface::SharedPtr timers_interface_;
0 commit comments