Atlas Robotics
Lesson 11/11 · about 45 min

Watch on YouTube ↗Watch on Facebook ↗

Exercise: temperature_monitor

Rewrite yourself from the beginning to remember the knowledge, don't copy ros2_basics and then change the name. The goal is understanding, not getting every line right.

What you need to do

  • temp_sensor: publishes temperature every second, type std_msgs/Float64, has a parameter for the baseline value
  • temp_alert: subscribe to that topic, log WARN when exceeding threshold_celsius (default 28.0), otherwise log INFO
  • Launch file runs both, allowing thresholds to be changed via command line parameters

Visualizing data streams

Before writing, draw out on paper: which node publishes, what is the topic name, which node subscribes, and which parameters are located where. Compare with the model below to check.

Suggestion

  • Read threshold_celsius once or every time in the callback? To answer this question, you need to understand the lesson Parameter.
  • If you want to quickly test without writing temp_sensor, use sensor_publisher temporarily and change base_temperature with ros2 param set to pass the threshold.

Review questions (click to see the answer)

What is the difference between Node, topic, service, action? When to use which one?

Node is a program. Topic: data flows continuously, one-way, without waiting (sensor, /cmd_vel). Service: one time question and answer, short task (save map). Action: action that lasts, progresses and can be canceled (goes to a point).

Publisher and subscriber have the same topic name, same type but still do not receive data. Common cause?

QoS incompatibility — for example publisher BEST_EFFORT (common with sensor data) but subscriber requires RELIABLE. Check with ros2 topic info -v. (Different ROS_DOMAIN_ID values also makes both sides unable to see each other.)

Why does each new terminal have to be source install/setup.bash?

Let the terminal know where the packages in your workspace are located (environment variables such as package paths, Python). Without source, ros2 run/ros2 launch says package not found.

Changing the parameter with ros2 param set while the node is running will definitely work?

Not necessarily — it only works if the node reads back the parameters when used. Values that are only read once at startup (eg timer cycle in sensor_publisher) must be passed at launch.

What problem does Launch file solve?

Start multiple nodes at the same time, with parameters, remap, namespace — instead of opening dozens of terminals and typing each command by hand.

What does spin() do, and why can't sleep be long in the callback?

spin() runs the executor to call the callbacks. Default Executor 1 thread: callback that sleeps or runs for a long time will block all other callbacks (timer delay, messages are pushed/abandoned).

Self-check

Done when

  • ros2 run can run each individual node
  • ros2 launch runs both at the same time, change to threshold_celsius:= and the behavior changes accordingly
  • If the temperature exceeds the threshold, the log shows WARN, not INFO
  • Explain why the continuous temperature data uses a topic, not service

After completing this lesson, Module 1 ends. Module 2 will teach how to represent the robot's position and direction in space (TF2).

Next module

Module 2: TF2 & coordinate frames

Start →
Exercise: temperature_monitor · Module 1: ROS 2 Core Concepts — Atlas Robotics