Unit 07 · lesson

Program a Behavior You Can Explain

A robotics program is not finished when the robot moves. It is finished enough to test when you can explain why each important output changed.

This lesson uses pseudocode on purpose. The behavior model should survive changes in language and hardware.

Example: approach and stop

Goal: approach a wall and stop at a safe distance.

state = APPROACH

repeat:
    distance = read_range()

    if stop_input:
        state = STOPPED

    if state == APPROACH:
        if distance <= stop_distance:
            state = STOPPED
        else:
            left_motor = approach_speed
            right_motor = approach_speed

    if state == STOPPED:
        left_motor = 0
        right_motor = 0

The code is simple. The test cases are where it becomes engineering.

Test the boundary, not just the normal case

CaseInput conditionExpected result
normaldistance decreases toward thresholdrobot stops at threshold
already closestarts inside thresholdno forward motion
stop inputhuman stop becomes activeimmediate STOPPED state
bad rangesensor reports impossible valuedefined safe response
noisy thresholdvalue crosses back and forthno unsafe chatter

The last two cases usually reveal more than another normal run.

Decide what "bad data" means

If a distance sensor returns zero, is the wall touching the sensor? Is zero an error code? Is the sensor disconnected? The answer depends on the device.

Your software needs a defined interpretation.

"Use the sensor" is not a complete design.

Evidence package

Write pseudocode for one behavior and include:

  1. named states;
  2. inputs;
  3. outputs;
  4. normal transitions;
  5. one failure or invalid-data path;
  6. at least five test cases.

If you have a simulator or safe coding environment, implement the behavior there. If not, run the test cases by hand and record the state transitions.

The deliverable is not a screenshot of code. It is the argument that the behavior does what you claim under the cases you tested.

Turn behavior into a testable contract

Suppose the requirement is:

Drive until an obstacle is within 0.30 m, then stop.

That sentence contains several decisions that code must make explicit.

input: distance in meters
normal drive condition: distance > 0.30
stop condition: distance <= 0.30
invalid-data behavior: stop
output while stopped: zero drive command

Now test boundary values before deploying anything:

Sensor valueExpected command
1.20 mdrive
0.31 mdrive
0.30 mstop
0.29 mstop
invalid / stalestop

This table is valuable even if the program is only pseudocode or a browser simulation.

Explain one execution trace

A defensible program is not just code that produced the right final result once. You should be able to trace one run:

distance = 0.42 → DRIVE
distance = 0.34 → DRIVE
distance = 0.30 → STOP
distance = 0.27 → STOP

If you cannot explain why the transition occurred at a specific sample, the program may still be working, but your understanding is not yet reliable.