Lab: QoS is checked twice
Needs first: Add a second node
Status: this chapter runs on
nros-board-mps2-an385-freertos: Verified in emulator (daily). From the board support registry; Board Support Levels explains the words.
A ROS 2 publisher and subscription agree on QoS when they meet, at run time,
and a mismatch shows up as messages that never arrive. On a microcontroller
there is no one watching at run time, so nano-ros checks the agreement
earlier, and twice: when the image is built, against the other nodes of the
system, and when the image boots, against what your code asked for. In this
lab you break each check on purpose, read what it says, and then make the
two nodes of chapter 3 agree on a non-default QoS: the talker keeps its last
message for a subscriber that joins late (transient_local durability), and
the listener asks for it.
At the end, nros run prints I heard: again, from a pair that agrees on
transient_local in its code and in its contract.
The lab starts from the workspace of chapter 3: sensor_node publishes on
/chatter and monitor_node subscribes to it. Run every step from the root
of sensor_ws, in a shell where ROS 2 is sourced. It takes about an hour,
most of it reading.
The lab changes the contract and both nodes’ code:
sensor_ws/
└── src/
├── monitor_node/
│ └── src/lib.rs (changed)
├── sensor_node/
│ └── src/lib.rs (changed)
└── sensor_node_bringup/
└── system.contract.yaml (changed)
sensor_ws/
└── src/
├── monitor_node/
│ └── src/Listener.c (changed)
├── sensor_node/
│ └── src/Talker.c (changed)
└── sensor_node_bringup/
└── system.contract.yaml (changed)
sensor_ws/
└── src/
├── monitor_node/
│ └── src/Listener.cpp (changed)
├── sensor_node/
│ └── src/Talker.cpp (changed)
└── sensor_node_bringup/
└── system.contract.yaml (changed)
What transient_local buys, and costs
A volatile publisher sends each message to the subscribers that exist when
it publishes, and forgets it. A subscriber that starts a second later has
missed it for good. A transient_local publisher keeps its last messages and
hands them to a subscriber that joins later, which is what a topic that
changes rarely needs: a map, a configuration, a robot’s mode. A node that
boots late, or restarts, still learns the current value without waiting for
the next change.
On a workstation the kept messages live in a heap the middleware grows as it likes. On a microcontroller every kept message is a buffer fixed when the image is built, so the build must know which publishers keep messages and how many. That is why the choice belongs in the contract as well as in the code: the contract is what the build sizes from, and the code is what runs.
The contract the scaffold wrote
The bringup holds a contract beside system.toml. The scaffold wrote the
talker’s row when it made the workspace, and nros new --to added the
listener’s in chapter 3:
cat src/sensor_node_bringup/system.contract.yaml
The part that matters (comments trimmed):
nodes:
sensor_node:
params: {}
paths:
on_tick:
trigger: { timer: { rate: 1Hz } }
output: [chatter]
pub:
chatter:
qos:
durability: volatile
monitor_node:
params: {}
sub:
chatter:
qos:
durability: volatile
A contract row states what a node creates and with which QoS. The build
sizes the image from it (chapter 8 shows what that
changes), and, which is this lab’s subject, checks it. Both rows say volatile, which is what the scaffolded code
asks for, so the two checks have nothing to say yet. Every node has a
params: line, even an empty one: the build sizes the parameter store from
every node’s declaration or from none, and a contract that states some nodes
and not others is refused.
Check one: the build compares the nodes
Change monitor_node’s row to ask for more than the talker offers: a
transient_local subscription, against a volatile publisher. Replace the
contract with:
src/sensor_node_bringup/system.contract.yamlversion: 1
nodes:
sensor_node:
params: {}
paths:
on_tick:
trigger: { timer: { rate: 1Hz } }
output: [chatter]
pub:
chatter:
qos:
durability: volatile
monitor_node:
params: {}
sub:
chatter:
qos:
durability: transient_local
topics:
/chatter:
type: std_msgs/msg/String
pub: [sensor_node/chatter]
sub: [monitor_node/chatter]
# Read outside this workspace, by `ros2 topic echo` or a ROS 2 node.
external: sub
params: {} says the node declares no parameters. A C or C++ bringup needs
the key on every row or on none, so write it on both.
Build:
nros build
The build stops before it compiles anything. The lines that matter, at the end of its output:
error: [qos-match] error: incompatible QoS on topic '/chatter' field 'durability': pub '/sensor_node/chatter' offers 'volatile', sub '/monitor_node/chatter' requests 'transient_local' (at topics./chatter.qos.durability)
Error: refusing to emit a SystemModel: 2 contract error(s) (run `check` on the same launch file for source excerpts)
This is the rule ROS 2 applies when the two endpoints meet, “requested
versus offered”: a subscription may ask for less than the publisher offers,
never more. A transient_local subscription wants the samples published
before it joined, and a volatile publisher keeps none. ROS 2 would connect
nothing and report an incompatible-QoS event at run time, if anyone listened
for it. Here both rows are in one file, so the build compares them first and
names the topic, the field and both endpoints.
Check two: the boot compares the contract with the code
Make the talker offer what the listener asks for, in the contract only. Change the publisher’s row:
src/sensor_node_bringup/system.contract.yaml, change the marked lines pub:
chatter:
qos:
- durability: volatile
+ durability: transient_local
The two rows agree now, and the build accepts them. Run it:
nros run
The image builds and boots, then stops on its own. The lines that matter,
each trimmed of its [WARN] nros: or [ERROR] nros: prefix and the board’s
clock:
declared QoS: taking durability transient_local on publisher `/chatter`; the call site asked volatile. Change the contract row to keep the call site's.
declared QoS: subscription `/chatter` registers volatile and the contract declares durability transient_local; taking it would widen what the code narrowed. Fix the contract row or the call site.
nros run: the target exited with code 1 after …
When a node creates an endpoint, nano-ros compares the QoS the code passed with the contract’s row for it, and the two lines are the two directions of that rule:
- A publisher’s row may strengthen the call site. The code asked for
volatile; keeping one sample for a late subscriber costs the image a buffer the build already reserved from the row, and every subscriber the publisher served before is still served. So the image takes the row’s value and warns, naming both. - A subscription’s row may not widen what the call site narrowed. The
code subscribed
volatile, which is the narrower request; the row claimstransient_local. Taking the row would hand the callback samples published before it joined, which the code never asked for and may not handle. So the image refuses to create the subscription, and the boot fails withDeclaredQosMismatch.
The contract describes the code; it does not replace it. Where they disagree, the safe direction is taken with a warning, and the unsafe one is refused.
Make the code say it
Change both call sites to transient_local, so the code and the contract
say the same thing.
The talker’s create_publisher_for_topic has a _with_qos form that takes
a QoSProfile:
src/sensor_node/src/lib.rs, change the marked lines use nros::{
Callback, CallbackCtx, Component, DeclarativeNode, ExecutableNode, NodeContext, NodeOptions,
- NodeResult, TimerDuration,
+ NodeResult, QoSProfile, TimerDuration,
};
src/sensor_node/src/lib.rs, change the marked lines let mut node = ctx.create_node(NodeOptions::new("sensor_node"))?;
- let pub_chatter = node.create_publisher_for_topic::<StringMsg>("/chatter")?;
+ let pub_chatter = node.create_publisher_for_topic_with_qos::<StringMsg>(
+ "/chatter",
+ QoSProfile::default().transient_local(),
+ )?;
let _timer =
The listener’s create_subscription_for_callback_name has no QoS form; the
one that does, create_subscription_for_topic_with_qos, names the callback
after the topic, so on_callback matches "/chatter" instead of
"on_chatter":
src/monitor_node/src/lib.rs, change the marked lines use nros::{
Callback, CallbackCtx, Component, DeclarativeNode, ExecutableNode, NodeContext, NodeOptions,
- NodeResult,
+ NodeResult, QoSProfile,
};
src/monitor_node/src/lib.rs, change the marked lines let mut node = ctx.create_node(NodeOptions::new("monitor_node"))?;
- let _sub =
- node.create_subscription_for_callback_name::<StringMsg>("on_chatter", "/chatter")?;
+ let _sub = node.create_subscription_for_topic_with_qos::<StringMsg>(
+ "/chatter",
+ QoSProfile::default().transient_local(),
+ )?;
log_info!(&LOGGER, "Subscriber created for topic: /chatter");
src/monitor_node/src/lib.rs, change the marked lines- if callback.as_str() == "on_chatter"
+ if callback.as_str() == "/chatter"
&& let Ok(msg) = ctx.message::<StringMsg>()
rclc_publisher_init_default and rclc_subscription_init_default have
_with_qos forms that take an rclc_qos_t. Start from the default profile
and change one field, in the talker:
src/sensor_node/src/Talker.c, change the marked lines self->pub = rcl_get_zero_initialized_publisher();
- rcl_ret_t rc = rclc_publisher_init_default(
- &self->pub, node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "/chatter");
+ rclc_qos_t qos = RCLC_QOS_DEFAULT;
+ qos.durability = RCLC_QOS_DURABILITY_TRANSIENT_LOCAL;
+ rcl_ret_t rc = rclc_publisher_init_with_qos(
+ &self->pub, node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "/chatter",
+ &qos);
if (rc != RCL_RET_OK) {
and in the listener:
src/monitor_node/src/Listener.c, change the marked lines self->sub = rcl_get_zero_initialized_subscription();
- rcl_ret_t rc = rclc_subscription_init_default(
- &self->sub, node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "/chatter");
+ rclc_qos_t qos = RCLC_QOS_DEFAULT;
+ qos.durability = RCLC_QOS_DURABILITY_TRANSIENT_LOCAL;
+ rcl_ret_t rc = rclc_subscription_init_with_qos(
+ &self->sub, node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "/chatter",
+ &qos);
if (rc != RCL_RET_OK) {
Both calls take a QoS after the topic name, the way rclcpp does. In the
talker:
src/sensor_node/src/Talker.cpp, change the marked lines ::setvbuf(stdout, nullptr, _IONBF, 0);
- ::rclcpp::Result r = node.create_publisher(pub_, "/chatter");
+ ::rclcpp::Result r = node.create_publisher(
+ pub_, "/chatter", ::rclcpp::QoS::default_profile().transient_local());
if (!r.ok()) return r;
and in the listener:
src/monitor_node/src/Listener.cpp, change the marked lines ::rclcpp::Result r =
::rclcpp::bind_subscription<::std_msgs::msg::String, Listener, &Listener::on_msg>(
- node, "/chatter", this);
+ node, "/chatter", this, ::rclcpp::QoS::default_profile().transient_local());
if (r.ok()) {
Run the pair
nros run
I heard: [Hello World: …]
The image boots, and the listener hears the talker. The console still carries warnings, of a different kind (trimmed):
[WARN] nros: [ 2.122472] qos: publisher '/chatter' asked for TRANSIENT_LOCAL KEEP_LAST(10); this image retains 1 sample(s) per transient-local publisher and replays them on a late joiner's query. Granting 1 and advertising it to the graph. Declare the publisher's…
[WARN] nros: [ 2.127589] qos: subscription '/chatter' asked for KEEP_LAST(10); this image's receive ring holds 4. Granting 4 and advertising it to the graph. Raise NROS_ZENOH_SUBSCRIBER_RING_DEPTH to keep more.
[INFO] monitor_node: [ 3.123307] I heard: [Hello World: 1]
Press Ctrl-C when you have read them.
These are not mismatches between the code and the contract. They are the
image granting less depth than the code asked for: QoSProfile::default(),
RCLC_QOS_DEFAULT and QoS::default_profile() all ask for KEEP_LAST(10),
and this image keeps one sample per transient-local publisher and four per
subscription. The image grants what it has, advertises the granted value to
the ROS 2 graph (so ros2 topic info -v /chatter shows the truth), and names
the knob that would raise it.
Change it
Silence the depth warnings by asking for what the image grants. Add
depth: 1 beside durability in both contract rows, and ask for depth 1 at
both call sites: .keep_last(1) on the Rust QoSProfile, qos.depth = 1;
in C, .keep_last(1) on the C++ QoS. Run again: the qos: lines are gone,
and the image reserves one sample per endpoint instead of the default ring.
Then try the other direction of check two: put the talker’s contract row back
to volatile while its code keeps transient_local. The build refuses it
first (check one, since the listener still asks for transient_local); make
the listener volatile in both places too, and read what the boot says about
a publisher whose row is weaker than its call site.
If it doesn’t work
- Step one builds instead of refusing: compare the file with the one
above. The listener’s row must sit under
nodes:with the namemonitor_node, and/chatterundertopics:must listmonitor_node/chatterin itssub:, so the build knows the two rows meet on one topic. - Step two fails without a
declared QoSline: the build itself failed, before the image booted. Read the firsterrorin its output; a C or C++ bringup refuses a contract where some rows carryparams:and others do not. - The last run prints
declared QoSerrors again: one call site still asks forvolatile. Each line names the endpoint (publisherorsubscription) and its topic. - The listener prints nothing in the last run: in a Rust project,
on_callbackstill matches"on_chatter". The_with_qossubscription names its callback after the topic.
What just happened
You met the two places a nano-ros system’s QoS is checked, and the rule each applies:
| When | Compares | Rule | On a mismatch |
|---|---|---|---|
build (qos-match) | a subscription’s row with its publisher’s row | the subscription may request no more than the publisher offers | the build refuses and names topic, field and both endpoints |
| boot, publisher | the call site with the row | the row may strengthen the call site, never weaken it | stronger: taken, with a warning; weaker: refused |
| boot, subscription | the call site with the row | the row may not widen what the call site narrowed | refused (DeclaredQosMismatch) |
| boot, any endpoint | the asked depth with the image’s pools | the image grants what it has | granted and advertised, with a warning naming the knob |
In ROS 2, all of this happens when two endpoints meet on the network, and a
mismatch is a topic that stays silent. nano-ros states the system in one
contract, so the comparison between nodes moves to the build, and the
comparison between the contract and the code happens once, at boot, before
any message flows. What remains at run time is the same “requested versus
offered” check with peers outside the image, such as ros2 topic echo.
Next
- Tell the build what your system creates: the contract as a whole, and what it does to the image’s memory.
- Talk to ROS 2:
ros2 topic info -vshows the QoS the board granted, from the ROS 2 side.
Design background
The contract and what the build derives from it follow RFC-0100. The lab grows the Tutorial’s one project, as RFC-0124 D3 lays out.