Smart Robots
Senior 6 · ICT
Smart Robots
Lesson 2 of 5
25 min
A robot carrying medicines along a hospital corridor receives a flood of numbers every second: thousands of distance readings from a laser scanner, millions of pixel values from a camera, wheel-turn counts from its motors. None of those numbers says "a nurse is stepping out of a doorway on the left". Turning raw sensor data into a usable picture of the world is perception; choosing what to do with that picture is decision-making. Both are where artificial intelligence enters robotics.
Definition
Perception: building a model of the robot's surroundings and its own position from sensor data. Example: recognising that a group of laser points is a person, 1.5 m ahead.
Sensor fusion: combining data from several sensors to get a more reliable estimate than any one sensor gives. Example: GPS plus wheel counts plus an inertial sensor for position.
Finite-state machine (FSM): a model of behaviour with a fixed set of states and rules (transitions) for moving between them when events happen.
Path planning: finding a route from a start to a goal that avoids obstacles, often the shortest or safest.
| Sensor | Measures | Strength | Weakness |
|---|---|---|---|
| Camera | Colour images | Rich detail; can recognise objects, signs and people | Needs light; heavy computation; privacy concerns |
| Lidar (laser scanner) | Distances to many points around the robot | Accurate 2-D or 3-D shape of the surroundings | Costly; confused by glass and heavy rain |
| Ultrasonic | Distance to the nearest object | Cheap; works in the dark | Wide beam, false echoes, short range |
| Inertial measurement unit (IMU) | Acceleration and rotation | Fast; needs nothing outside the robot | Errors grow over time (drift) |
| Wheel encoders | How far each wheel has turned | Simple distance travelled | Wheel slip causes errors |
| GPS | Position outdoors | Works anywhere under open sky | A few metres of error; fails indoors |
Every sensor is wrong sometimes. Perception starts by cleaning the data, then combines sensors, then interprets them. Modern robots interpret camera images with neural networks trained on many labelled pictures: the network outputs, for example, "person, 92% confidence" with a box around the person. A wrong detection can be a false positive (seeing a person who is not there) or a false negative (missing one who is), and for a robot the second is usually far more dangerous.
Worked example 1: cleaning a noisy sensor
An ultrasonic sensor on a slow robot reports distances to a wall: 52, 50, 49, 120, 51, 48, 50 cm. The 120 is a false echo. Compare a moving average of 3 readings with a moving median of 3.
readings = [52, 50, 49, 120, 51, 48, 50] # cm; 120 is a false echo
def moving_average(xs, k=3):
return [round(sum(xs[i - k + 1:i + 1]) / k, 1) for i in range(k - 1, len(xs))]
def moving_median(xs, k=3):
return [sorted(xs[i - k + 1:i + 1])[k // 2] for i in range(k - 1, len(xs))]
print("average:", moving_average(readings))
print("median: ", moving_median(readings))
average: [50.3, 73.0, 73.3, 73.0, 49.7]
median: [50, 50, 51, 51, 50]| Method | How it works | Example |
|---|---|---|
| Reactive rules | IF a condition THEN an action, checked every cycle | IF obstacle closer than 20 cm THEN stop and turn |
| Finite-state machine | Different rules in different states; events move the robot between states | The delivery robot below |
| Fuzzy logic | Rules with vague terms give smooth outputs (unit 3) | Slow down more as an obstacle gets nearer |
| Planning and search | Search a map for the best sequence of moves | Breadth-first search for the shortest route |
| Learning | Improve decisions from data or experience | Reinforcement learning (lesson 4) |
In the state NAVIGATE the robot follows its planned route; in AVOID it uses short reactive rules to get round an obstacle; in DELIVER it waits for a nurse. The same event, an obstacle, matters in NAVIGATE but is ignored while the robot is standing still in DELIVER. Breaking behaviour into states keeps each set of rules small and testable.
Worked example 2: trace the state machine
Events, in order: order received; obstacle; path clear; arrived; battery low. Starting in IDLE, list the states.
IDLE → NAVIGATE (order received) → AVOID (obstacle) → NAVIGATE (path clear) → DELIVER (arrived) → RETURN (battery low). The robot goes home with the package still on board: the designers must decide whether that is acceptable, or whether DELIVER should first wait for a nurse or alert staff. Tracing events through a state machine is how such gaps are found before the robot is built.
Given a map, a robot can search for the shortest route. Breadth-first search (BFS) explores the map outwards from the start, one step further each round, like a ripple. The first time the ripple reaches the goal, it has found a shortest route, and the program walks back from the goal to the start to read it. This Python program plans a route through a corridor map; it was run with python3.
from collections import deque
# A delivery robot's map of a school corridor: # = wall, . = free, S = start, G = goal
MAP = ["##########",
"#S...#...#",
"#.##.#.#.#",
"#.#..#.#.#",
"#.#.##.#.#",
"#...#..#G#",
"##.....#.#",
"##########"]
def find(ch):
for r, row in enumerate(MAP):
if ch in row:
return r, row.index(ch)
def shortest_path(start, goal):
"""Breadth-first search: explore cells in order of distance from the start."""
queue = deque([start])
came_from = {start: None}
while queue:
cell = queue.popleft()
if cell == goal:
break
r, c = cell
for nr, nc in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
if MAP[nr][nc] != "#" and (nr, nc) not in came_from:
came_from[(nr, nc)] = cell
queue.append((nr, nc))
if goal not in came_from:
return None # no route exists
path, cell = [], goal
while cell is not None: # walk back from the goal
path.append(cell)
cell = came_from[cell]
return path[::-1]
start, goal = find("S"), find("G")
path = shortest_path(start, goal)
print("Steps:", len(path) - 1)
grid = [list(row) for row in MAP]
for r, c in path[1:-1]:
grid[r][c] = "*"
print("\n".join("".join(row) for row in grid))
Steps: 21
##########
#S...#***#
#*##.#*#*#
#*#..#*#*#
#*#.##*#*#
#**.#**#G#
##****.#.#
##########Real robots use faster relatives of BFS, such as the A* ("A-star") algorithm, which searches towards the goal first, on maps built by their own sensors.
Agility Robotics: Explained, How Humanoid Robots Perceive the World. A robot maker's own explanation, so watch critically. Listen for which sensors are combined and how the robot turns sensor data into a model of its surroundings, and compare with the sensor table above.
Warning
Confidence is not certainty. A vision system that reports "no person, 95% confidence" is still wrong about 1 time in 20 in situations like the ones it was tested on, and may be wrong far more often in new conditions: rain, darkness, unusual clothing, or people it saw few of in training. Safety-critical robots therefore combine several sensors and stop when unsure.
Tip
Draw the states before the code. For any robot behaviour, list the states, then for each state the events that can happen. Every state and event pair needs an answer, even if the answer is "ignore it".
bfs.py. Change the map: add a wall that blocks the route found, and run it again. How many moves is the new route? Then block every route and check that the program reports no route.readings in the filter program to include two false echoes in a row. Does the median of 3 still remove them? What about a median of 5?Check: your planner handles a blocked map without crashing, and your state machine has an answer for every event in every state.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.