Embedded systems: inside BugBot
What makes a system embedded, the robot's three boards, and the sense-decide-act loop.
Do this lesson in the simulatorMost computers are not laptops or phones. They are hidden inside other things: a washing machine, a car's brakes, a microwave, a traffic light. These are embedded systems, and BugBot is one. This lesson opens the robot up and looks at how its computer senses, decides and acts.
What is an embedded system?
An embedded system is a computer built into a larger device to control it. Compared with a general-purpose computer like a laptop, an embedded system usually:
- does one dedicated job, like controlling a washing machine's cycle;
- is small, cheap and low power, often running from a battery;
- has limited memory and processing power, just enough for its job;
- reads sensors and controls hardware directly, often with strict timing;
- is reliable: it runs for years without being restarted, and has no keyboard or screen, or only simple buttons and lights.
| Embedded system | Inputs | Outputs |
|---|---|---|
| washing machine | dial, door switch, water level, temperature | motor, heater, valves, display |
| car anti-lock brakes | wheel speed sensors | brake pressure valves |
| BugBot | camera, distance sensor, motion sensors, battery voltage | motors, servos, LED, radio |
Inside BugBot
BugBot is three small boards stacked on top of each other:
| Board | What is on it |
|---|---|
| Vision (top) | the main processor, an ESP32-P4, with a second chip for Wi-Fi; the camera; the RGB LED; the power switch |
| Motion (middle) | the battery charger and power supply, four motor drivers, two servo outputs, the connector for the distance sensor |
| Odometry (bottom) | an optical-flow sensor that watches the mat move, like a computer mouse, and an IMU that senses turning |
The ESP32-P4 is a microcontroller: a processor, memory and input and output connections on a single chip, made for embedded systems. The robot moves by vibration: four small motors spin weights that shake it across the mat, which is why it needs its motion sensors to know where it really went.
Sense, decide, act
Every embedded system runs the same loop, forever:
- Sense: read the inputs.
- Decide: work out what to do.
- Act: set the outputs.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for step in range(40):
# sense
gap = distance()
power = battery()
# decide
if power < 20:
colour = (255, 0, 0)
elif gap < 30:
colour = (255, 160, 0)
else:
colour = (0, 255, 0)
# act
led(*colour)
wait(0.05)
print("last reading:", gap, "cm, battery", power, "%")
The firmware on BugBot, written in C, runs a loop like this hundreds of times a second to keep the motors under control, while your Python program runs on top of it. Firmware is software stored in the device's non-volatile memory that controls its hardware.
Real time
Many embedded systems must respond within a fixed time. A car's brakes must react in milliseconds, whatever else the processor is doing. BugBot's firmware stops the motors if it does not hear from your program for half a second, so a crashed program cannot leave the robot driving off the table.
Task: a sense-decide-act loop
There is a wall ahead. Write a sense-decide-act loop that drives the robot forward slowly. Each time round: sense the distance; decide; act. While the gap is 20 cm or more, keep driving with a green LED. When it is under 20 cm, stop, turn the LED red, play a tone, and end the loop. Do not hit the wall.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
Challenges
- Add an amber LED when the gap is between 20 and 40 cm, and slow down there.
- Add a check of
battery()that stops everything below 20%. - List the inputs, processing and outputs of a microwave oven as an embedded system.