The worksheetDownload the PDF
Answers

U4.7 Project: hold the gap

Noise and filtering · University · about 40 min

BugBotLab

What this lesson is about

Sit 25 cm from a wall, steadily, on a depth sensor that is 3 cm noisy.

Questions 6 marks in all

  1. [1 mark]The gap controller runs for three ticks with no robot. What does it print?

    TARGET, ALPHA = 25.0, 0.3
    gap = 40.0
    for raw in (37, 34, 31):
        gap = ALPHA * raw + (1 - ALPHA) * gap
        error = gap - TARGET
        if abs(error) < 1.0:
            cmd = 0
        else:
            cmd = max(-45, min(45, 2.2 * error))
        print(round(gap, 2), round(cmd, 1))
    Answer:
    39.1 31.0
    37.57 27.7
    35.6 23.3

    Tick 1: gap = 0.3 x 37 + 0.7 x 40 = 39.1, error 14.1, command 2.2 x 14.1 = 31.0. The filter lags the raw readings, so each command is a little larger than the raw gap would ask for.

  2. [1 mark]With TARGET = 25, a dead band of 1 cm and gain 2.2, what drive command is sent when the filtered gap is 25.6 cm?

    Answer: 0. The error is 0.6 cm, inside the 1 cm dead band, so the robot is stopped rather than asked for 1.32 percent.
  3. [1 mark]The plot of the gap shows a slow sine wave around 25 cm. What does the lesson say to change?

    1. ATurn down the gain or the filter delay
    2. BRaise the gain
    3. CRemove the dead band
    4. DStart the filter at zero
    Answer: A. A slow oscillation is too much gain or too much delay in the loop. Turn one of them down.
  4. [1 mark]The robot settles at 27 cm and stays there. What is this, and where is the proper fix?

    1. AA steady state error from a dead band; the integral term in U5.4
    2. BSensor bias; more filtering
    3. CFilter delay; a smaller alpha
    4. DNoise; a median filter
    Answer: A. Proportional control with a dead band stops short when the error is too small to act on. An integral term removes that offset.
  5. [1 mark]The chart holds 25 cm, but a ruler shows the robot is nearer the wall. What does the lesson say is happening?

    1. AThe estimate is biased, and a biased estimate never shows on the chart
    2. BThe dead band is too wide
    3. CThe gain is too low
    4. DThe loop period is too long
    Answer: A. The chart plots the estimate and the controller holds the estimate, so an estimate that reads long draws the same chart while the robot sits nearer. Measure the real gap once, with a ruler.
  6. [1 mark]Why is a 1 cm dead band not really a cost on this robot?

    1. AThe sensor has 3 cm of noise, so settling within 1 cm is already better than a single reading can resolve
    2. BThe dead band removes the filter delay
    3. CThe dead band makes the steady state error zero
    4. DThe motors cannot move less than 1 cm
    Answer: A. The dead band trades exact settling for stillness. With 3 cm of sensor noise, 1 cm is below what the measurement could confirm anyway.

The task: hold the gap

Settle 25 cm from the wall and stay there. The checker watches the last part of the run as well as the end, so hunting fails even if the final position is right.

from bugbot import *
connect()

TARGET = 25.0

The hint students can ask for: The wall's near face is 150 cm up the mat, so 25 cm from it is y = 125, which is 65 cm from where the robot starts. Filter the depth reading, control on the filtered value, and make sure the robot settles rather than hunting: the checker watches the last part of the run, not just the end.

A solution

from bugbot import *
connect()

TARGET = 25.0
ALPHA = 0.3
gap = distance()
for tick in range(450):
    gap = ALPHA * distance() + (1 - ALPHA) * gap
    plot("gap", gap)
    error = gap - TARGET
    speed = max(-45.0, min(45.0, 2.2 * error))
    if abs(error) < 1.0:
        stop()
    else:
        drive(speed, 0, 0)
    wait(0.1)
stop()
print("settled at", round(gap, 1), "cm from the wall")

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.