Robot programming – sense, decide, act with sensors, IF rules and motors

Computer ScienceAlgorithms & Problem SolvingAges 15–16Virtual lab

Use with my class

A model robot follows a black line or avoids walls on a track. Students write four IF rules that turn two sensor readings into left and right motor outputs, set the sensor threshold and the two motor speeds, then run the robot and watch the sense–decide–act loop one step at a time. Each run records the rules, the threshold, the speeds, the time, whether the track was completed and the collisions.

Lesson: Robot control: sensors, IF rules and motor outputs in the sense–decide–act loop

What it shows

A robot repeats one loop many times a second: it senses (reads its sensors), decides (picks the first IF rule whose condition is true) and acts (sets its motors). This model robot has two line sensors that measure how much light the floor reflects, or two distance sensors that measure how far away the nearest wall is. A threshold turns each reading into yes or no, and four rules cover every combination. The robot keeps moving while the loop runs, so a fast robot can pass a bend before it reacts. All sizes, readings and speeds are model values.

How to use

Choose a Track and press Run. The close-up shows both sensor readings against the threshold, and the three loop lines show which rule fired. Set Playback speed to ×0.1 or press Step to follow sense, decide and act one at a time. Change the Line threshold, the Fast speed and Slow speed, or a rule's motor outputs, then run again and compare the rows of the table.

Parameters you can change

  • Track Oval line, Sharp corners and a darker patch, Walled track with obstacles
  • Line threshold (a reading below it counts as line) 0–100
  • Wall threshold (closer than it counts as a wall) 5–80 cm
  • Fast speed 5–60 cm/s
  • Slow speed (also used backwards) 0–40 cm/s

Questions to explore

  1. Why does the robot leave the sharp track on the darker patch when the line threshold is 50?
  2. What happens to the path on bends when you raise the fast speed, and why?
  3. Which rule brings the robot back to the line after it has lost it?