TactivergeLite

Lite / Building Machines That Respond / Week 3

Variables that track a changing machine

A robot adds speed × time to its distance fifty times a second. How does one variable keep track of all that?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Machines are full of quantities that change: parts made, distance travelled, energy used. In code, each is a variable updated over and over. Get the update right and the robot knows where it is; get it wrong and the error grows every step.

Predict

left_c = 31.5, right_c = 29.0, then left_c = right_c, then right_c = left_c. What are they now?

  1. 29.0 and 31.5 (swapped)
  2. 29.0 and 29.0
  3. 31.5 and 31.5
Guess first, then reveal

Both are 29.0. The first assignment overwrote 31.5 before it was copied anywhere, so it is lost. A swap needs a spare variable, or Python's left_c, right_c = right_c, left_c.

Discover

Run 'Odometer update' and 'Part counter' line by line: which variables change at each step? Then compare 'Swap two sensors (buggy)' with the fixed version and find the exact line where 31.5 is lost.

What the interactive shows (text version)

In the program tracer, 'Odometer update' shows distance_m changing 0 → 3.0 → 6.0 → 7.0 as lines 4, 5 and 7 run. 'Swap two sensors (buggy)' shows left_c becoming 29.0 on line 3 and right_c staying 29.0 on line 4, so 31.5 is lost; the fixed version stores 31.5 in spare_c first.

You can also open the interactive on the full site when you have a faster connection.

Understand

Names that explain themselves

distance_m, speed_m_per_s, parts_made say what they are and in which unit. Python names use letters, digits and underscores, cannot start with a digit and are case-sensitive (Speed and speed are different). Clear names are the cheapest documentation there is.

Updating from the old value

parts_made = parts_made + 1 reads the old value, adds 1 and stores the result: a counter. distance_m = distance_m + speed_m_per_s * dt_s is a running total. The short form parts_made += 1 does the same. Both need a starting value first, such as parts_made = 0.

Swapping without losing data

left_c, right_c = 31.5, 29.0
left_c, right_c = right_c, left_c
print(left_c, right_c)   # 29.0 31.5

Python evaluates the whole right side first, then assigns, so nothing is overwritten too early. The step-by-step version needs a spare: spare_c = left_c, then left_c = right_c, then right_c = spare_c.

Explain it another way

Intuitive

A counter is like a tally on a whiteboard: rub out the old number and write one more. You have to read it before you rub it out.

Technical

x = x + 1 evaluates x + 1 using the current binding of x, then rebinds x. A tuple assignment a, b = b, a builds the tuple (b, a) before unpacking, so both original values are captured.

Mathematical

Dead reckoning: d_n = d_(n−1) + v·Δt. After n steps at constant speed, d_n = d_0 + n·v·Δt. Each small error in v or Δt is added every step, so the position error grows with time.

Applied

Wheel odometry on a mobile robot is exactly this running total, updated many times a second from wheel encoders. Because errors accumulate, robots correct it with other sensors such as cameras or GPS.

Worked example 1

distance_m = 0; speed 1.5 m/s for two steps of 2 s, then 0.5 m/s for one step of 2 s. Final distance?

0 + 1.5 × 2 = 3.0 → 3.0 + 1.5 × 2 = 6.0 → 6.0 + 0.5 × 2 = 7.0 m. This is the 'Odometer update' program; the tracer prints 7.0 m.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

m

Practice 2. Which lines correctly swap a and b?
Practice 3. Which is a valid Python variable name?

Practice 5. A program runs total_wh = total_wh + 5 but never set total_wh before. What happens?

Apply it

Write a program for a conveyor: start parts_made and rejects at 0, then record eight parts (three of them rejected) with updates. Print the number of good parts and the reject rate as a percentage. Trace it by hand, then run it.

Reflect

Why does a robot's dead-reckoning position drift further from the truth the longer it runs?

Write your answer on paper or in your own notes. Lite never stores what you write.

← How the interpreter reads your programint, float, str and list →