Ask Your Question

Publishing diagnostics from C++?

asked 2017-05-21 20:33:00 -0500

Cerin gravatar image

Are there any examples of how to publish diagnostics messages from C++? I can only find examples using Python.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-05-22 02:11:19 -0500

Its like publishing anything else.

Setup a Publisher, fill in the Message and publish it.

Here is a completely senseless example but gives you perhaps an idea:

#include "ros/ros.h"
#include <diagnostic_msgs/DiagnosticArray.h>
#include <diagnostic_msgs/DiagnosticStatus.h>
#include <diagnostic_msgs/KeyValue.h>

int main(int argc, char **argv)
  ros::init(argc, argv, "diagnostics");
  ros::NodeHandle nh;

  ros::Publisher diagnostic_pub = nh.advertise<diagnostic_msgs::DiagnosticArray>("/diagnostics", 1);

  ros::Rate loop_rate(10);
  while (ros::ok())
    diagnostic_msgs::DiagnosticArray dia_array;
    diagnostic_msgs::DiagnosticStatus robot_status; = "Robot";
    robot_status.level = diagnostic_msgs::DiagnosticStatus::OK;
    robot_status.message = "Everything seem to be ok.";
    diagnostic_msgs::KeyValue emergency;
    emergency.key = "Emgergencystop hit";
    emergency.value = "false";
    diagnostic_msgs::KeyValue exited_normally;
    emergency.key = "Exited normally";
    emergency.value = "true";


    diagnostic_msgs::DiagnosticStatus  eth_status; = "EtherCAT Master";
    eth_status.level = diagnostic_msgs::DiagnosticStatus::OK;
    eth_status.message = "Running";





  return 0;
edit flag offensive delete link more


I'm compiling to an Arduino using rosserial_arduino, so push_back() isn't available. How do I dynamically allocate lists of type diagnostic_msgs::DiagnosticArray::_status_type*?

Cerin gravatar image Cerin  ( 2017-05-22 09:21:18 -0500 )edit

Did you ever end up working this out? @Cerin

TheMilkman gravatar image TheMilkman  ( 2018-08-14 00:46:10 -0500 )edit

answered 2017-05-22 12:50:13 -0500

ahendrix gravatar image

If you running a full ROS (and not rosserial on an Arduino) you can use the diagnostic_updater package to automatically fill in the diagnostics message and publish it.

The diagnostics_updater package also includes diagnosed publishers that you can use to make sure that you're publishing at your desired rate.

The diagnostics_updater package ships with some fairly good examples:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-05-21 20:33:00 -0500

Seen: 1,773 times

Last updated: May 22 '17