Ask Your Question
0

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
1

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_status.name = "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";

    robot_status.values.push_back(emergency);
    robot_status.values.push_back(exited_normally);

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

    dia_array.status.push_back(robot_status);
    dia_array.status.push_back(eth_status);

    diagnostic_pub.publish(dia_array);

    ros::spinOnce();

    loop_rate.sleep();
  }

  return 0;
}
edit flag offensive delete link more

Comments

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 imageCerin ( 2017-05-22 09:21:18 -0500 )edit

Did you ever end up working this out? @Cerin

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

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: http://docs.ros.org/api/diagnostic_up...

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

2 followers

Stats

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

Seen: 672 times

Last updated: May 22 '17