Skip to the lesson
FixTheRobot Explore Robotics / Robot Foundations / Module 8 / Lesson 1

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.

  • Lesson 1 of 4
  • Beginner
  • No code
  • About 9 minutes

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.
Pip's programs and the messages between themSeven boxes joined by arrows. A scanner driver sends scan messages to a mapper, ten times a second. An encoder driver sends wheel tick messages to odometry, twenty times a second. Odometry sends pose messages to the mapper and to a controller. The mapper sends a map to the planner, about once a second. The planner sends a path to the controller. The controller sends a command to a motor driver.Scanner driver10 times a secondEncoder driver20 times a secondMapper10 times a secondOdometry20 times a secondPlannerabout once a secondController20 times a secondMotor driversets the wheelsscanwheel ticksposemapposepathcommand
Pip's software. Each box is a program with one job and its own rate. Each arrow is a message topic. Nothing is wired to anything else.

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.

The messages of Pip's software, with their rates and sizes
TopicSent byRead byRateSize of one messageBytes per second
scanscanner drivermapper10 Hz288 bytes2880 bytes
wheel ticksencoder driverodometry20 Hz8 bytes160 bytes
poseodometrymapper, controller20 Hz12 bytes240 bytes
mapmapperplanner1 Hz960 bytes960 bytes
pathplannercontroller1 Hz120 bytes120 bytes
commandcontrollermotor driver20 Hz8 bytes160 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.

  1. The scanner driver reads the device and publishes a scan: 72 distances, 288 bytes, ten times a second.
  2. 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.
  3. 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.
  4. 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.
  5. 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

  1. Robot software is a set of small programs (nodes), each with one job, passing named messages on topics.
  2. Publishers and subscribers do not know each other. Either can be replaced.
  3. Each node has its own rate: steering fast, planning slow.
  4. 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.

Next lesson

Simulation and Real Robots

Say what a simulator is good for, what it leaves out, and why code that works in simulation can fail on a robot.

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.