Robot Foundations · Module 8: The Robot as a System, and Your Next Step
Robot Software: Small Programs That Talk
Robot software is not one big program. It is a team of small ones that each do one job and pass messages.
Everything in this course has been one idea at a time: measure, localize, map, plan, steer. On a real robot all of them run at once, at different speeds, and nobody wants one failure to stop the others. So the software is split into small programs, each of which reads some messages, does one job and sends out new ones. This lesson shows how Pip's software is put together, and why the split is more than tidiness.
What you will be able to do
- Explain why robot software is split into small programs that exchange messages.
- Read a diagram of programs and the messages between them.
- Say what publishing and subscribing mean, and what a rate is for a message.
- Describe what happens to the others when one program fails.
The idea
Think of the jobs of the course: read the scanner, count the wheel ticks, keep the pose, build the map, plan the route, steer, drive the motors. They run at different speeds (lesson The Robot Loop: Sense, Think, Act): the steering at 20 Hz and the planning about once a second. They are written and tested by different people. They need to be swapped and improved one at a time. A single program that does everything makes all of that hard.
The usual answer is to give each job to its own small program, called a node, and have the nodes talk through messages.
- A message is a small packet of data with a name: pose carries x, y and θ, and command carries v and ω.
- A node publishes a message when it has one to share: the odometry publishes a new pose twenty times a second.
- A node subscribes to the messages it needs: the controller subscribes to pose and path.
- The named channel a message travels on is a topic. A publisher does not know who is listening, and a subscriber does not know who sent it. That is the point: either side can be replaced without touching the other.
Small programs, one job each, joined by named messages. Replace one without touching the rest.
What you gain
- Each part runs at its own rate. The planner thinks once a second while the controller steers twenty times a second. A single big loop would have to run everything at one speed.
- Parts can be swapped. A better mapper replaces the old one. As long as it publishes the same map, the planner never knows.
- Parts can be tested alone. Feed the controller recorded pose and path messages and check the command it makes, with no robot in the room.
- Everything can be recorded and replayed. Log the messages from a bad run and play them back, as often as needed, to find the fault.
- A failure stays local. If the mapper crashes, the odometry and the controller carry on. (Which brings in a danger, taken up in the lesson When Things Go Wrong: Failing Safely: a controller that goes on using the last path it received, long after it was current.)
The most widely used toolkit for this is ROS 2 (the Robot Operating System, version 2), a free, open-source set of tools in which these ideas are called nodes, topics and messages. The names in this lesson are the same ones you will meet there. The Navigation track on this site shows how a real navigation stack is put together from nodes like these.
How much talk is there?
Messages cost something: each has a size and each topic has a rate. Here is Pip's traffic, taking each number as 4 bytes.
| Topic | Sent by | Read by | Rate | Size of one message | Bytes per second |
|---|---|---|---|---|---|
| scan | scanner driver | mapper | 10 Hz | 288 bytes | 2880 bytes |
| wheel ticks | encoder driver | odometry | 20 Hz | 8 bytes | 160 bytes |
| pose | odometry | mapper, controller | 20 Hz | 12 bytes | 240 bytes |
| map | mapper | planner | 1 Hz | 960 bytes | 960 bytes |
| path | planner | controller | 1 Hz | 120 bytes | 120 bytes |
| command | controller | motor driver | 20 Hz | 8 bytes | 160 bytes |
The whole robot talks about 4.5 kilobytes a second, less than a thousandth of the capacity of an ordinary home network. A single camera is another matter: a 640 by 480 colour image is 921600 bytes, and thirty a second is 27.6 megabytes a second, about 6117 times all of Pip's other traffic together. That is why vision needs a faster computer than steering does, and why real systems think hard about which node runs where.
A diagram like the one above is the first thing to draw when you meet any robot. Name the programs, name the messages, and write the rate on each. Most debugging starts with finding which arrow stopped carrying what it should.
Words to know
- Node
- A small program with one job in a robot's software. On Pip: the mapper, the planner and the controller are each a node.
- Message
- A named packet of data passed from one node to others. On Pip: pose carries x, y and θ.
- Topic
- The named channel a message travels on. On Pip: the scan topic carries the scanner readings.
- Publish and subscribe
- To publish is to send a message to a topic. To subscribe is to ask to receive the messages on a topic. On Pip: the odometry publishes pose and the controller subscribes to it.
- Rate
- How many messages a node sends each second, in hertz. On Pip: 20 Hz for command, 1 Hz for path.
- ROS 2
- The Robot Operating System: a free toolkit for building a robot's software from nodes, topics and messages. On Pip: how a robot like it would normally be built.
A worked example: following one scan through the robot
Follow what happens after the scanner sees something new.
- The scanner driver reads the device and publishes a scan: 72 distances, 288 bytes, ten times a second.
- The mapper receives it, takes the latest pose from the odometry, and writes the beams into the occupancy grid (lesson Occupancy Grids: Free, Occupied, Unknown). It publishes the updated map once a second.
- The planner receives the map once a second. If the path still runs through free cells, it republishes the same path. If a new obstacle blocks it, it plans again (lesson When the World Changes: Obstacles and Replanning) and publishes a new path.
- The controller runs 20 times a second. On each pass it takes the latest pose and the latest path, aims at a point ahead on the path (lesson Following a Path), works out v and ω, and publishes a command.
- The motor driver turns the command into wheel speeds (lesson How a Two-Wheeled Robot Drives).
So the news that "a pallet is in the way" reaches the wheels through four nodes. The slowest links are the scanner's ten scans a second and the planner's once a second. At 0.5 m/s the robot travels 0.5 m in the time the planner takes to run once. That is a reason to keep the controller's own safety checks separate and fast, which is the subject of the lesson When Things Go Wrong: Failing Safely.
Try it yourself
Draw the same kind of diagram for a robot vacuum cleaner. Write down its programs, name a message between each pair, and write a plausible rate on each. Then answer: (a) which program would run slowest, and which fastest? (b) if the program that plans the cleaning route crashed, what should the robot still be able to do?
One possible answer
Programs: bump and cliff sensor driver, wheel encoder driver, odometry, mapper, route planner, controller, motor driver, brush driver. Messages: bump, wheel ticks, pose, map, route, command. (a) The route planner is slowest, perhaps once every few seconds, and the cliff sensor and the controller fastest, tens of times a second, because a missed cliff is a fall. (b) It should still be able to avoid stairs and obstacles, and stop. The safety nodes must not depend on the planner.
Common mistakes
"One program is simpler, so it is better."
It is simpler to start and far harder to grow, test and fix. Splitting by job costs a little at the beginning and pays back every time a part changes.
"The controller always has fresh data."
It has the latest message it received, which may be old if the sender is slow or has stopped. Always know how old a message is, and decide what to do when it is too old.
"A node must know which node sends its messages."
It does not. It knows the topic. That is what allows a simulator to publish the same scan messages as a real scanner, and the rest of the robot not to notice.
Remember this
- Robot software is a set of small programs (nodes), each with one job, passing named messages on topics.
- Publishers and subscribers do not know each other. Either can be replaced.
- Each node has its own rate: steering fast, planning slow.
- A failure stays in one node, but others may go on using stale messages.
Check yourself
Three questions. Write an answer in your own words, then reveal the explanation. Nothing you type leaves your browser.
Question 1
Name two advantages of splitting a robot's software into nodes instead of writing one program.
Explanation
Any two of: parts run at their own rates, parts can be swapped without touching the rest, parts can be tested alone with recorded messages, runs can be recorded and replayed, and a failure of one part does not stop the others.
Question 2
The controller publishes a command 20 times a second and each command message is 8 bytes. How much data per second is that?
Explanation
20 × 8 = 160 bytes per second: tiny. Commands are small and frequent, in contrast with camera images, which are large.
Question 3
A simulator wants to replace the real scanner. What must it do for the rest of the robot to work unchanged?
Explanation
Publish a scan topic with the same message layout and about the same rate as the real scanner driver. Nothing else needs to change, because the subscribers listen to the topic, not to a particular sender.
What’s next
Before software touches a real robot, it is almost always run in a simulation. The next lesson says what that buys and what it leaves out.
Where this goes on the site
Nothing below is needed to continue this course. These pages take the topic of this module further, and most assume more mathematics or Python than this course does.
- LaterThe Navigation Stack at a Glance
The programs of a real mobile robot and the messages between them.
- LaterBuild a CBF Safety Filter
A safety layer that edits unsafe commands.
- LaterLinear Algebra for Robotics
The mathematics the specialised tracks use.