Skip to content
Tactiverge

Communication inside a robot

How the parts of a robot exchange information: buses, messages, publish/subscribe and request/response, and why every message should carry a timestamp.

Lesson 5 of 6 · 30 min · Self-paced · Free

Parts that need to talk

The previous lessons split a robot into subsystems, processors and software layers. All of those pieces need to exchange information: the camera needs to tell the planner what it sees, the planner needs to tell the controller where to go, and the controller needs to report back how things are going. How that communication is organised affects how fast the robot reacts, how easy it is to change, and how it behaves when something goes wrong.

Physical links: buses

At the hardware level, information travels over wires using agreed rules called protocols. A bus is a link that several devices share. The embedded systems pathway covers these in depth; here is the map:

  • UART serial: a simple link between two devices, common between a microcontroller and a computer.
  • I2C: a slow, two-wire bus where one controller talks to many small chips, such as sensors, each with its own address.
  • SPI: a faster bus for short distances, often used for chips that produce lots of data.
  • CAN bus: a robust bus designed for vehicles, good at carrying short messages between many motor controllers and sensors in an electrically noisy environment, with built-in priorities and error checking.
  • USB and Ethernet: high-bandwidth links for cameras and between computers.

Choosing between them is a matter of distance, speed, number of devices, electrical noise and how reliably messages must arrive.

Messages

On top of the wires, software exchanges messages: structured pieces of data with a defined meaning. A joint state message might contain the joint names, their angles in radians, their speeds in radians per second, and the time the measurement was taken. Defining messages carefully — fields, units, coordinate frames — is the data interface agreement from the first lesson in this module, written precisely enough for a computer.

Two patterns: request/response and publish/subscribe

There are two common ways for software parts to exchange messages.

Request/response. One part asks a question and waits for an answer: "What is the battery level?" → "11.8 V". This is natural for occasional queries and one-off commands, but the asker must know who to ask, and it may sit waiting if the answer is slow.

Publish/subscribe (often shortened to pub/sub). A part that produces data publishes messages on a named channel, often called a topic, such as /camera/image or /joint_states. Any part interested in that data subscribes to the topic and receives every message published there. The publisher does not need to know who is listening, or how many listeners there are.

Pub/sub suits robots well, because so much robot data is a stream: sensor readings arriving at a steady rate, used by several consumers at once. The camera publishes images; the object detector, a logger and an operator display all subscribe. Adding a new consumer requires no change to the camera code. The widely used ROS framework (Robot Operating System) is built largely around this pattern.

Pub/sub has costs too. Because publishers do not know their subscribers, nobody automatically notices if a subscriber stops receiving messages, or if a publisher stops sending. Robust systems therefore watch for missing or stale data and react, for example by stopping safely if motor commands stop arriving.

Why timestamps matter

Every measurement describes the world at a particular moment, and messages take time to travel. A camera image might be captured, compressed, sent, received and processed before anyone uses it, by which time the robot has moved on. If the software assumes data describes the world now, it will make errors that grow with speed and delay.

The cure is to stamp each message with the time its measurement was taken, not the time it arrived. With timestamps, software can line up data from different sensors correctly, discard data that is too old, and measure the system's actual latency.

Worked example: matching an image to the arm's position

An arm moves its gripper steadily along x at 0.2 m/s and logs its position every 10 ms. A camera captures an image at t = 1234 ms, but processing means the planner only receives it at t = 1300 ms. To work out where the gripper was in the picture, the planner must use the arm's position at the capture time, interpolating between log entries. The data below is made up for illustration.

# Made-up data for illustration. The arm logs its gripper x position
# every 10 ms. A camera image was captured at t = 1234 ms but only
# reached the planner at t = 1300 ms.
arm_log = [(1220 + 10 * i, 412.0 + 2.0 * i) for i in range(9)]  # (ms, mm)

def x_at(t):
    """Linearly interpolate the arm's logged position at time t (ms)."""
    for (t0, x0), (t1, x1) in zip(arm_log, arm_log[1:]):
        if t0 <= t <= t1:
            return x0 + (x1 - x0) * (t - t0) / (t1 - t0)
    raise ValueError("time outside the log")

captured, arrived = 1234, 1300
print(f"gripper x when the image arrived:  {x_at(arrived):.1f} mm")
print(f"gripper x when the image was taken: {x_at(captured):.1f} mm")
print(f"error if we ignore the timestamp:   {x_at(arrived) - x_at(captured):.1f} mm")

Output:

gripper x when the image arrived:  428.0 mm
gripper x when the image was taken: 414.8 mm
error if we ignore the timestamp:   13.2 mm

A 66 ms delay at 0.2 m/s gives 0.2 × 0.066 = 0.0132 m, matching the program. Ignoring the timestamp would place an object seen next to the gripper 13 mm away from where it really was — easily enough to miss a grasp.

Clocks must agree

Timestamps only help if all parts of the robot agree on the time. Every processor has its own clock, and clocks drift apart slowly. Robots with several computers therefore synchronise their clocks, often with a network time protocol, or have one device timestamp everything on arrival and correct for known delays. The embedded systems pathway returns to this problem in detail.

Knowledge check

Check your understanding

  1. 1. In publish/subscribe, what does a publisher need to know about its subscribers?
  2. 2. Which pattern is the most natural fit for asking a battery monitor for its current reading once?
  3. 3. An object moves at 0.5 m/s. An image of it reaches the planner 80 ms after capture. If the timestamp is ignored, how large is the position error?
  4. 4. Which time should a sensor message's timestamp record?
  5. 5. Why do robots with several computers need to synchronise their clocks?

Go further