The robot as a system · University · about 20 min
The control loop, its period, and the deadman that stops a robot whose program has gone quiet.
[1 mark]Put the body of the control loop in order, as it runs on every pass.
Number the lines 1 to 4 to put them in the right order.
act(command)wait(period)command = decide(measurement)measurement = sense()measurement = sense() command = decide(measurement) act(command) wait(period)
Read the sensors, work out what to do, tell the motors, then wait out the rest of the period. The rest of the course is about what goes inside decide.
[1 mark]A loop ends each pass with wait(0.1) and does nothing else that takes noticeable time. How often does it run, and roughly what is the fastest disturbance its measurements can represent properly?
[1 mark]A loop has a period of 0.04 s. What is its rate in Hz?
[1 mark]A loop calls wait(0.1), and the sensor reads and arithmetic in each pass take a further 0.03 s. This works out the rate it really runs at. What does it print?
work = 0.03
period = 0.1 + work
print("rate:", round(1 / period, 1), "Hz")rate: 7.7 Hz
The real period is 0.1 + 0.03 = 0.13 s, so the rate is 1 / 0.13 = 7.7 Hz. wait() is what is left over after the work, and the work is not free.
[1 mark]A program calls forward(60) once and then wait(2), with no other commands. What does the robot do?
[1 mark]Why does every controller in the course use forward(60) followed by wait(0.1), rather than forward(60, distance=40)?
[1 mark]Which of these are set directly by the loop's period?
Tick every answer that is true.
Without driving, read the depth sensor thirty times at ten times a second, print each reading, and print how long the loop actually took on average, as period: 0.1.
from bugbot import * connect() # clock() is the seconds since the program started start = clock()
The hint students can ask for: clock() gives the seconds since the program started. Read it before the loop and after it, and divide the difference by how many times round you went.
from bugbot import *
connect()
start = clock()
n = 30
for i in range(n):
print(i, distance())
wait(0.1)
print("period:", round((clock() - start) / n, 3))
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.