The answersDownload the PDF
Worksheet

A10.9 Project: a tiny robot operating system

Operating systems, software and translators · A level · OCR H446 1.2.1, AQA 7517 4.6.1.4, Eduqas A500QS 2.6 · about 50 min

BugBotLab
NameClassDate

What this lesson is about

Share the robot between three tasks with round robin, drive it through device drivers, and stop the patrol with an interrupt.

Questions 5 marks in all

  1. [1 mark]In the tiny robot OS, what does the check after every step model?

    1. AThe check for interrupts at the end of each fetch-decode-execute cycle
    2. BPaging
    3. CLexical analysis
    4. DThe BIOS power-on self-test
  2. [1 mark]Why does the project run every step through a driver table instead of calling forward, led and tone directly?

    1. AThe scheduler does not need to know how any device works, and a new device only needs a new driver
    2. BDriver tables make the robot drive faster
    3. CPython cannot call robot commands from inside a loop
    4. DIt stops the interrupt from firing
  3. [1 mark]What does this program print?

    queue = ['patrol', 'lights', 'music']
    left = {'patrol': 3, 'lights': 1, 'music': 2}
    order = []
    while queue:
        name = queue.pop(0)
        left[name] = left[name] - 1
        order.append(name[0])
        if left[name] > 0:
            queue.append(name)
    print(''.join(order))
  4. [1 mark]BugBot's motor loop must correct the robot's heading within a few milliseconds every time. Which two types of operating system describe FreeRTOS on the robot?

    1. AEmbedded and real-time
    2. BDistributed and multi-user
    3. CMulti-user and real-time
    4. DDistributed and embedded
  5. [1 mark]In the project, the patrol task is stopped by the interrupt with one step still to run. Which scheduling idea does this show?

    1. APre-emption: a running task loses the processor before it has finished its slice
    2. BFirst come first served
    3. CShortest job first
    4. DDisk thrashing

The task: a tiny robot OS

Build the operating system from the brief. The inputs are: - tasks, a list of (name, steps) tuples in their starting queue order, where steps is a list of (command, value) pairs; command is "forward" (value: cm, a positive integer), "led" (value: a colour name) or "tone" (value: hertz, 100 to 10000); - TIME_SLICE, the most steps a task runs before the next task starts (2); - SAFE_CM, the interrupt threshold: the obstacle interrupt fires when distance() is less than this (25). Write a drivers dictionary with a driver for each command: "forward" drives forward at speed 50, "led" sets the LED colour, and "tone" plays the note for 0.2 seconds. Run every step through drivers. Print exactly these lines, built from your variables: <name>: <command> <value> after each step, interrupt: obstacle and patrol stopped from the handler (with the 220 Hz, 0.2 second tone between them), and <name> finished when a task runs its last step. The wall is 51 cm ahead of the robot.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

tasks = [
    ("patrol", [("forward", 10), ("forward", 10), ("forward", 10), ("forward", 10)]),
    ("lights", [("led", "blue"), ("led", "yellow"), ("led", "green")]),
    ("music", [("tone", 523), ("tone", 659), ("tone", 784)]),
]
TIME_SLICE = 2
SAFE_CM = 25

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

QR code
Do it on the robot
www.bugbotlab.com/learn/a10-9-project-a-tiny-robot-os/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Change TIME_SLICE to 1 and then to 4. Predict the order of the lines each time, then check. Does the slice change how close the patrol gets to the wall? Explain why.
  2. Give each task a priority and replace round robin with a priority scheduler. Can a low-priority task starve?
  3. Add a second interrupt, low battery, with a higher priority than the obstacle. What should happen if both are raised after the same step?