What is a robot

A robot is a collection of services, each with different jobs. One reads a sensor, one estimates where the robot is, one plans, one drives the motors. Each service is a program of its own.

Building a robot's software means making three kinds of decisions: what the services are, what passes between them, and how each one is implemented. None of the three has one right answer. A choice that suits one robot is often wrong for another.

The same jobs in every robot

Robots look nothing alike, but their software has the same few jobs to do. Compare two open source software stacks: PX4, which flies drones, and Autoware, which drives cars.

Job PX4 Autoware
Sense A gyroscope, an accelerometer, a barometer, GPS Lidars, cameras, radar, GPS
Estimate where it is Its tilt and its height Its place in a lane, on a detailed map
Plan Waypoints through open air A trajectory through traffic
Control The speed of four propellers Steering, throttle, and brake
Decide what to do Take off, hold, return home, land Keep the lane, change lanes, stop

The differences come from the robot's body and from where it works.

A drone is unstable, and it flies in open air. It tips over unless its tilt is corrected many times a second, so PX4 estimates tilt before anything else, and reads its motion sensor about 1000 times a second to do it. Open air has little in it to hit, so a plan can be a short list of waypoints.

A car is stable, and it drives in traffic. It stays upright with no help, so Autoware spends its effort on the road instead: it estimates which lane the car is in, and a large part of the stack finds the other cars and people nearby. A plan has to fit between them.

The two stacks also divide the jobs differently. In PX4, deciding is a service of its own, the commander, which switches between flight modes and takes over when something fails. In Autoware, deciding is part of planning, along with choosing the route.

The jobs are the same in every robot. How each job is done, and how the jobs are divided into services, depends on the robot.

What passes between the services

A service takes data in and sends data out. The agreement between two services about that data is an interface: what the data is, what form it has, and how often it comes.

A good interface carries everything the receiver needs and nothing else. Here are two versions of the service that estimates where a robot is.

# Good: the data says when and where it was measured, and in what units.
@dataclass
class Scan:
    stamp: float          # when it was measured, in seconds
    frame: str            # which sensor measured it
    ranges: list[float]   # meters, one per degree

class Localization:
    def on_scan(self, scan: Scan) -> None: ...
    def pose(self) -> Pose | None: ...   # None until it knows

# Leaky: bare numbers in, and its insides open to every other service.
class Localization:
    def on_scan(self, ranges: list[float]) -> None: ...
    particles: list[Particle]

With the first version, the service can be replaced by any other that has the same two lines. With the second, a service that reads particles works only with this one way of estimating, and nobody can change it.

A leak is sometimes worth its cost. A controller that reads the planner's grid of obstacles directly does not have to build its own, which saves time on a small computer. But then changing the planner means changing the controller too.

The services of one robot

The recording below is a small wheeled robot driving itself across a room to a goal. It shows one robot's services while they run.

The robot reaches the goal in 40 seconds, after driving 7.0 m. Nobody steers it, and no single program drives it. It has six services.

Service Its job
Lidar Measures the distances around the robot
Localization Estimates where the robot is on the map
Planner Finds a path on the map from the robot to the goal
Controller Chooses a speed that keeps the robot on the path
Wheels Turn the two wheels to match the speed they are given
Task Takes the goal and has the planner and the controller carry it out

We draw each service as a box and each interface as an arrow. The gray box is the map of the room, which the robot is given before it starts.

Each service runs at its own rate. In the 40 seconds of the run the lidar sent 10\red{10} scans a second, the controller sent 20\brass{20} speeds a second, and the planner made 1\teal{1} path a second. The robot did not follow one path. It followed 40, each made from where it was at that moment.

The arrows make a loop, and the software does not close it. The room does: the wheels move the robot, so the next scan measures different distances. The two dashed arrows are that part of the loop.

The diagram shows two of the three decisions. The boxes are what the services are, and the arrows are what passes between them. The third is not in the picture: how each one is implemented. A different planner, or a different estimator, would leave every box and every arrow where it is.

Practice

Work each problem on paper before opening its solution.

Problem 1

An arm at a belt

A robot arm stands beside a moving belt at a recycling plant. Plastic bottles pass on the belt, and the arm picks them off one at a time and drops them in a bin. A camera above the belt looks down at the bottles. Each joint of the arm has a motor, and a sensor that measures the joint's angle.

The arm's software has six services.

  1. The lesson's table has five jobs: sense, estimate, plan, control, and decide. Write beside each service the job it does. A service can do more than one.
  2. Copy the six boxes, and draw each interface as an arrow. Write on each arrow what it carries.
  3. Find the loop that goes through the camera, and say which part of it is not software.
Solution

Step 1: Match the services to the jobs

Service Job What it does
Camera Sense Takes pictures of the belt
Bottle finder Estimate Finds where each bottle is in a picture
Task Decide Chooses which bottle to pick next
Planner Plan Finds a motion that takes the hand to that bottle
Controller Control Chooses a speed for each joint that follows the motion
Joints Control and sense Turn the motors, and measure each joint's angle

All five jobs are here. The joints service does two of them, because the motors and the angle sensors are in the same part of the arm.

Step 2: Ask what each service needs

We find the arrows by asking what each service needs before it can do its job.

  • The bottle finder needs a picture, so an arrow goes to it from the camera.
  • The task service needs the positions of the bottles, from the bottle finder.
  • The planner needs the bottle to pick, from the task service. It also needs the joint angles, because a motion starts from where the arm is now.
  • The controller needs the motion, from the planner. It also needs the joint angles, because it has to know how far the arm has moved.
  • The joints need a speed for each motor, from the controller.

That is seven interfaces.

Step 3: Find the loop

The loop goes from the camera to the bottle finder, the task service, the planner, the controller, and the joints, and then back to the camera. The last part is not software. The arm takes a bottle off the belt, so the next picture has one bottle fewer. The belt closes the loop, as the room does for the lesson's robot.

The drawing has a second, shorter loop: the controller sends speeds to the joints, and the joints send their angles back. The arm's own motion closes that one.

Step 4: Compare with the lesson's robot

The two robots have the same five jobs and almost the same drawing. The estimate job is where they differ. The lesson's robot drives around a room, so it estimates where it is. The arm is bolted to the floor, and its joint sensors already measure where its hand is, so it estimates where the bottles are instead.

A drawing with other boxes can be as good as this one (the bottle finder and the task service as one box). The check is the one we used: each service gets what it needs. The arrow that is left out most often is the one from the joints back to the controller. Without it, the controller sends speeds and never learns whether the arm moved.

Answer:

Seven interfaces, in a loop that closes through the belt

Problem 2

Two numbers for the wheels

In the lesson's robot, the controller sends the wheels a speed 20 times a second. One person writes the controller and another writes the wheels service. They agree on this interface.

class Wheels:
    def on_speed(self, speed: list[float]) -> None: ...

The controller sends [0.2, 0.0].

  1. Write down two things that [0.2, 0.0] could mean, and say what the robot does in each case.
  2. List what this interface leaves out.
  3. Write a better version.
Solution

Step 1: Read the message two ways

The person who wrote the controller means a forward speed of 0.2 meters a second and no turning, so the robot should drive straight.

The person who wrote the wheels service reads a speed for the left wheel and a speed for the right wheel. The left wheel turns and the right wheel stands still, so the robot turns in a circle around its right wheel.

Each program does what its author meant, and the robot still drives wrong. The fault is in neither program. It is in the interface.

Step 2: List what is missing

The interface leaves out three things.

  • What each number is. Forward and turning, or left wheel and right wheel.
  • The units. A forward speed of 0.2 could be meters a second or miles an hour. A wheel speed could be turns a second, or a fraction of full power.
  • Which direction is positive. A positive turning speed could turn the robot left or right.

Step 3: Write the better version

@dataclass
class Speed:
    forward: float   # meters a second
    turning: float   # radians a second, positive to the left

class Wheels:
    def on_speed(self, speed: Speed) -> None: ...

The lesson's robot sends these two numbers, in these units. Its wheels service works out the speed of each wheel from them.

Step 4: Compare the two versions

Both versions send the same two numbers, 20 times a second, so the robot does the same work with either. The second is four lines longer. What it adds is the agreement itself, written where both people will read it. In the first version the agreement was only in what each person assumed.

The names do not do all of it. A field named forward could still be in miles an hour, so the units are written beside it.

Answer:

What each number is, its units, and which direction is positive

Problem 3

The controller stops

The lesson's robot is 7 seconds into its run when its controller freezes, together with the planner and the task service. Nothing sends a speed any more. The lidar, the localization service, and the wheels keep running. The last speed the controller sent was 0.2 meters a second forward, turning slowly to the left.

The wheels service can be written in two ways.

  • Version A keeps the wheels at the last speed it was given.
  • Version B does the same, but it stops the wheels when no speed has come for 0.5 seconds.
  1. Sketch the room, and mark where the robot ends up with each version.
  2. For version B, work out how far the robot drives after the last speed.
  3. The controller sends 20 speeds a second. Work out how many speeds in a row are missing when version B stops the wheels.
Solution

Step 1: Version A follows the last speed

Version A cannot tell a controller that has frozen from a controller that wants the same speed again. It keeps the wheels at 0.2 meters a second, turning slowly to the left. Nothing changes that speed, so the robot drives on in a wide curve until a wall is in its way.

Step 2: Version B stops after half a second

Version B drives at the last speed for 0.5 seconds and then stops the wheels. In that time the robot covers

0.2 m/s×0.5 s=0.10 m.0.2\ \text{m/s} \times 0.5\ \text{s} = 0.10\ \text{m}.

Step 3: Count the missing speeds

The controller sends 20 speeds a second, so in 0.5 seconds the wheels service expects

20×0.5=1020 \times 0.5 = 10

speeds. Version B stops the wheels when 10 speeds in a row are missing.

Step 4: Watch both versions

Both runs were recorded on the course's robot, and they are drawn in one room.

With version A the robot drives 2.6 m more. It reaches the wall at the top of the picture 13 seconds after the last speed, and it keeps pushing against the wall. With version B the robot stops 11 cm after the last speed. This is 1 cm more than we worked out, because the wheels take a moment to slow down.

Step 5: Compare the two versions

The fault is the same in both runs, and it is not in the wheels service. The wheels service decides what the fault does to the robot. Version B uses a part of the interface that version A ignores: how often the data comes. The lesson's definition of an interface includes it for this reason.

The limit of 0.5 seconds is a choice, and it costs something either way. A limit of 0.05 seconds would stop the robot each time one speed came late. A limit of 5 seconds would let the robot drive 1 m with nothing steering it. The course's robot uses version B with a limit of 0.5 seconds.

Answer:

A drives 2.6 m more, into a wall. B stops 11 cm on

Problem 4

Thirty centimeters from the goal

In the lesson's run, the pose from the localization service stayed within 10 cm of where the robot was.

On another day the robot drives a smooth path and stops, and the task service reports that it has arrived. The pose says that the robot is on the goal. A tape measure says that the robot is 30 cm from the goal. The goal was entered correctly.

Use the diagram of the robot's services in the lesson.

  1. List what could be at fault: each service, and anything a service is given.
  2. Say which services are not at fault, and why, even though the robot stopped in the wrong place.
  3. For each one on the list, give a check that shows whether it is at fault.
Solution

Step 1: Say which piece of data is wrong

The pose says that the robot is on the goal, and the robot is 30 cm away. So the pose is wrong by 30 cm. Everything else we know is consistent with that: the robot drove to the place where the pose reads as the goal.

Step 2: Follow the arrows backward

The pose comes from the localization service. That service makes it from three things: the scan from the lidar, the wheel turns from the wheels, and the map. A wrong pose can come from a fault in the localization service or from any of the three things it is given. So there are four suspects.

Step 3: Clear the services that used the pose

The planner, the controller, and the task service are not at fault. The planner made a path from the pose to the goal. The controller kept the pose on that path. The task service reported an arrival when the pose reached the goal. Each one did its job with the pose it was given.

The wheels are on both sides. They drove at the speeds they were given, so that part of their job is cleared. The wheel turns they report are still a suspect.

Step 4: Check each suspect

Each check compares one piece of data with something measured by hand.

Suspect Check
Map Measure the room with a tape, and compare it with the map
Lidar Put the robot 1 m from a wall, and read that distance in the scan
Wheels Drive the robot 1 m along a tape, and read the distance from the wheel turns
Localization If the other three pass, the fault is inside this service

Step 5: Compare where the fault showed with where it was

The fault showed in where the robot stopped, which is the work of the controller and the wheels. The fault was in the pose, which is earlier in the loop. Data passes from service to service, so a wrong value shows up later than the place it was made. This is why we started from the wrong data and followed the arrows backward.

Answer:

The localization service, or one of the three things it is given (the scan, the wheel turns, or the map)