Week 5 · Lesson 5 · 55 minutes

Add state and test the controller

I can trace a controller whose action depends on earlier state.

Planned lesson. Follow your teacher’s teaching order.

Source workbook pages: 34.

Resources

Vocabulary

Vocabulary
state: information kept from an earlier step; hysteresis: different thresholds for switching on and off
55-minute lesson sequence
  1. 0–5 · Retrieve the noise problem.
  2. 5–15 · Model three readings and the state transition.
  3. 15–25 · Guide the first three rows of task 1; students finish the rest.
  4. 25–45 · Students implement, test the sequence and save evidence.
  5. 45–50 · Compare exact boundaries and the None recovery case.
  6. 50–55 · Collect the state-dependency explanation; assign homework.

Try before notes

Why can a single threshold cause repeated switching near 20?

Build the idea

State is information retained from an earlier step. A stateful controller can give different outputs for the same current temperature because its previous state differs. The trace needs an old-state column as well as the reading.

Hysteresis uses separate switching thresholds. Between them, keep the old state. This reduces rapid switching around a single threshold. Exact boundary behaviour must be specified, not guessed.

Read the rule in order, compute the new state, then pass that new state into the next step. Resetting the state on every reading would erase the memory that hysteresis needs.

below lower → ON
above upper → OFF
between and at boundaries → KEEP old state

See the method

New rule: turn on below 19; turn off above 21; between 19 and 21 inclusive, keep the previous state. Start off. For readings 18,20,22: outputs are on,on,off. A missing reading turns this simulation off and records a fault. State must be updated after every reading.

Predict then reveal

Fresh demo rule: turn on below 16; off above 18; otherwise keep state. Start off and trace 15,17,19,17.

Reveal reasoning

15 is below 16: on. 17 is between thresholds: stay on. 19 is above 18: off. 17 is between thresholds: stay off. Outputs are on,on,off,off.

For that rule, what happens at exactly 16 if old state is on? What if old state is off?

Reveal reasoning

16 is not below 16 and not above 18, so it keeps the previous state. Old on gives on; old off gives off. Current reading alone is not enough to determine the output.

Work one step together

Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.

Practice

  1. Trace readings 18,19,20,21,22,20,18 from an initial off state. Record old state, decision and new state. Explain the result at 19 and 21.

Your turn

Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.

Practice

  1. Trace readings 18,19,20,21,22,20,18 from an initial off state. Record old state, decision and new state. Explain the result at 19 and 21.
  2. Write update(temp, was_on) for the rule. For this task accept numeric readings or None. Test the sequence plus None followed by 20. Save your function and test output for next week’s file project.

Check and explain

A stateful rule cannot be traced correctly if the previous output is discarded.

Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.

Show what you know

Why must the program pass the previous output into the next update?

HL challenge when ready

Modify the function to return both state and a fault flag.

Week 5 homework

Show working. Keep code and output. Stop at 25 minutes and mark where you reached.

  1. Draw a closed-loop automatic door system. Label sensor, controller, actuator, controlled process and feedback. Explain one possible fault.
  2. Trace the hysteresis rule from off for 20,18,21,22,19. Explain both exact-boundary cases.
  3. Choose a cloud model for a team that must configure its own OS. Explain what extra work this choice brings.
  4. Retrieve memory: why must a log be written to persistent storage if it should survive power loss?

Source workbook pages: 35.

Weekly homework (Word)

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Trace a fan with separate on and off thresholds. Explain the middle range.

No imports needed.

Hint and starter code

Hysteresis means the decision depends partly on the previous state, preventing rapid switching near one threshold.

# Hysteresis means the decision depends partly on the previous state, preventing rapid switching near one threshold.
fan = False
for temperature in [29, 27, 25]:
    if temperature > 28:
        fan = True
    elif temperature < 26:
        fan = False
    print(temperature, fan)
Open this starter in Python playground →
Prompt 2

Add one boundary case to this expected/actual test table.

No imports needed.

Hint and starter code

Test 28 explicitly; an expected result is written before the run.

# Test 28 explicitly; an expected result is written before the run.
def fan_on(t):
    return t > 28

for value, expected in [(27, False), (29, True)]:
    print(value, expected, fan_on(value), fan_on(value) == expected)
Open this starter in Python playground →