The answersDownload the PDF
Worksheet

U12.2 Fitting a model to data

Learning, and the capstone · University · about 35 min

BugBotLab
NameClassDate

What this lesson is about

Least squares in plain Python, and a robot that drives by the model it fitted to itself.

Questions 7 marks in all

  1. [1 mark]What does this least squares fit of speed against command print?

    data = [(20, 3.1), (40, 7.4), (60, 11.2), (80, 15.3), (100, 19.0)]
    n = len(data)
    sx = sum(c for c, v in data)
    sv = sum(v for c, v in data)
    sxx = sum(c * c for c, v in data)
    sxv = sum(c * v for c, v in data)
    slope = (n * sxv - sx * sv) / (n * sxx - sx * sx)
    intercept = (sv - slope * sx) / n
    print(round(slope, 4), round(intercept, 3))
  2. [1 mark]A fit gives v = 0.2 c - 0.8. How many seconds should the robot drive at command 60 to cover 80 cm open loop? Give 2 decimal places.

  3. [1 mark]Speeds are read 0.2 s after each command change. Why is that worse than random noise?

    1. AEvery reading is taken on the ramp and is low by the same fraction, so the slope comes out quietly wrong
    2. BThe readings become too noisy to average
    3. CThe intercept becomes exactly zero
    4. DThe fit fails to converge
  4. [1 mark]What does taking (forward - backward) / 2 at each command achieve?

    1. AIt cancels any fixed offset in the sensor and reduces the noise, and the robot ends where it began
    2. BIt doubles the speed range of the sweep
    3. CIt removes the drive's dead band
    4. DIt corrects the flow sensor's scale factor
  5. [1 mark]A straight line is fitted to commands from 5 to 100, including the region below 15 where the drive does not move. What is the sensible response?

    1. ARestrict the fit to where the model applies and state that domain, or use a model with a dead band in it
    2. BKeep all the points, because more data always gives a better fit
    3. CForce the intercept to zero
    4. DFit a degree 5 polynomial to follow the bend
  6. [1 mark]A proportional heading correction asks for 8 percent turn during the sweep, and the robot slowly turns anyway. Why?

    1. A8 percent is below the 15 percent dead band, so the correction does nothing; it needs a minimum magnitude above it
    2. BThe gyro is not being read
    3. CThe correction has the wrong sign
    4. DHeading has no effect on flow readings
  7. [1 mark]Driving for D / v open loop lands close to D despite the 0.25 s lag. Why?

    1. AThe robot loses about v tau of travel while speeding up and gains about the same coasting after the stop
    2. BThe lag only affects turning
    3. CThe flow sensor corrects the distance during the run
    4. DThe fit's intercept absorbs the lag

The task: fit the drive

Sweep the command, fit speed against command by least squares, print slope: and intercept:, then use the fit to drive 80 cm open loop and stop there.

from bugbot import *
connect()

DT = 0.1
COMMANDS = [20, 30, 40, 50, 60, 70, 80, 90, 100]
TARGET = 80.0

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/u12-2-fitting-a-model/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Fit the lateral axis as well. Is the slope the same as forward, and should it be?
  2. Fit with the command 20 point included and excluded. How much does the intercept move?
  3. Work out, from the residuals, how far off your 80 cm drive should be expected to land, and compare with where it landed.