CodeTheRobot

ROS 2 Lyrical Luth · Lesson 24 of 86

Diagnostics and monitoring

diagnostic_msgs, diagnostic_updater, rqt tools and ros2 topic monitoring.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain diagnostics and monitoring in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Diagnostics report the health of components in a standard way: each DiagnosticStatus has a level (OK, WARN, ERROR, STALE), a name, a message and key/value pairs. Nodes publish them on /diagnostics, and tools such as rqt_robot_monitor show them as a tree.

The diagnostic_updater helper lets a node register checks (battery voltage, motor temperature, message rate) that run periodically. Also use ros2 topic hz/delay, ros2 doctor and rqt_graph to see whether the system as a whole is healthy.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the diagnostics and monitoring example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

diagnostic_updater::Updater updater{this};
// Create the helper that publishes /diagnostics at 1 Hz.
updater.setHardwareID("arm_driver");
// Name the piece of hardware being checked.
updater.add("Motor temperature", [this](diagnostic_updater::DiagnosticStatusWrapper& s) {
// Register a check.
  if (temp_ > 80.0) s.summary(diagnostic_msgs::msg::DiagnosticStatus::WARN, "hot");
// Report WARN above 80 degrees.
  else s.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "normal");
// Otherwise report OK.
  s.add("temperature_c", temp_);
// Attach the value so it appears in the tool.
});
// End of the check.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

/topicpublishersubscribersubscribermessages delivered: 0MESSAGES BETWEEN NODES

Demo for Diagnostics and monitoring. Expected result: rqt_robot_monitor shows Motor temperature: WARN when temp is above 80

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

// inside a diagnostic_updater check
void check_motor(diagnostic_updater::DiagnosticStatusWrapper& s) {
  // TODO: report WARN with message "hot" when temp_ > 80, otherwise OK with "normal"
  s.add("temperature_c", temp_);
}

Expected output

rqt_robot_monitor shows Motor temperature: WARN when temp is above 80
Stuck? Show the full solution (try it yourself first)
// inside a diagnostic_updater check
void check_motor(diagnostic_updater::DiagnosticStatusWrapper& s) {
  if (temp_ > 80.0) s.summary(diagnostic_msgs::msg::DiagnosticStatus::WARN, "hot");
  else s.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "normal");
  s.add("temperature_c", temp_);
}

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Add a diagnostic check for motor temperature and message age to a node, publish on /diagnostics, and view the result with rqt_robot_monitor. Raise the temperature past 80 and confirm the level changes.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.