ROS 2 Lyrical Luth · Lesson 77 of 86
Multi-robot coordination
Shared resources, reservations, priorities and deadlock avoidance.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain multi-robot coordination 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
Robots that share corridors, doors, elevators or charging stations need rules so they do not block each other. The simplest is a reservation: a robot asks a coordinator for a resource (a corridor segment), waits until it is granted, and releases it when it leaves. Requests are granted first-come-first-served or by priority.
Deadlock can occur when robots each hold one resource and wait for the other's. Avoid it by reserving whole paths at once, using a fixed ordering of resources, or adding timeouts that release and retry. Publish the reservation table on a topic so operators can see who waits for whom.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the multi-robot coordination example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 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
class Reservations {
// A table of who holds each shared resource.
public:
// Public interface.
bool request(const std::string& resource, const std::string& robot) {
// Try to take a resource.
auto [it, inserted] = owner_.try_emplace(resource, robot);
// Insert only if nobody owns it yet.
return inserted || it->second == robot;
// Granted if newly taken or already held by this robot.
}
// End of request.
void release(const std::string& resource) { owner_.erase(resource); }
// Give the resource back.
private:
// Hidden state.
std::map<std::string, std::string> owner_;
// Resource name to owning robot.
};
// End of the class.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Mobile robot
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.
Demo for Multi-robot coordination. Expected result: A granted B denied B granted
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
#include <map>
#include <string>
class Reservations {
public:
bool request(const std::string& resource, const std::string& robot) {
// TODO: insert the owner only if the resource is free with try_emplace; grant when inserted or already owned by this robot
}
void release(const std::string& resource) { owner_.erase(resource); }
private:
std::map<std::string, std::string> owner_;
};
int main() {
Reservations r;
std::cout << "A " << (r.request("corridor", "A") ? "granted" : "denied") << '\n';
std::cout << "B " << (r.request("corridor", "B") ? "granted" : "denied") << '\n';
r.release("corridor");
std::cout << "B " << (r.request("corridor", "B") ? "granted" : "denied") << '\n';
return 0;
}Expected output
A granted
B denied
B grantedStuck? Show the full solution (try it yourself first)
#include <iostream>
#include <map>
#include <string>
class Reservations {
public:
bool request(const std::string& resource, const std::string& robot) {
auto [it, inserted] = owner_.try_emplace(resource, robot);
return inserted || it->second == robot;
}
void release(const std::string& resource) { owner_.erase(resource); }
private:
std::map<std::string, std::string> owner_;
};
int main() {
Reservations r;
std::cout << "A " << (r.request("corridor", "A") ? "granted" : "denied") << '\n';
std::cout << "B " << (r.request("corridor", "B") ? "granted" : "denied") << '\n';
r.release("corridor");
std::cout << "B " << (r.request("corridor", "B") ? "granted" : "denied") << '\n';
return 0;
}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
Simulate two robots that must both cross a one-lane corridor from opposite ends. Implement reservations, show that without them they meet, then create a deadlock with two resources and fix it by ordering them.
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
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.