In our previous lesson, we explored open-loop control systems. Today, we'll dive into closed-loop control systems, focusing on the simplest form: on-off control, also known as bang-bang control.
Closed-loop control systems use feedback to adjust their output. They constantly compare the actual output with the desired output (setpoint) and make corrections based on the difference (error).
Input (Setpoint): The desired value or state of the system
Controller: The decision-making component that determines what action to take
Plant (System being controlled): The physical system or process being controlled
Output: The actual value or state of the system
Sensor: Measures the output and converts it into a signal the controller can understand
Feedback loop: The path that brings output information back to the controller
Setpoint: Desired speed set by the driver
Controller: Car's computer
Plant: Car's engine and drivetrain
Output: Actual speed of the car
Sensor: Speedometer
Feedback loop: Constant speed measurements sent to the computer
On-off control, or bang-bang control, is the simplest form of closed-loop control. The output switches between two states based on whether the measured value is above or below the setpoint.
The system has a setpoint (desired value)
The sensor measures the current value
If the measured value is below the setpoint, the controller turns the system ON
If the measured value is above the setpoint, the controller turns the system OFF
Refrigerator:
Setpoint: Desired cooling temperature (e.g., 4°C)
If temperature > 4°C, compressor ON
If temperature ≤ 4°C, compressor OFF
Water Tank Level Control:
Setpoint: Desired water level (e.g., 80% full)
If level < 80%, pump ON
If level ≥ 80%, pump OFF
Automatic Street Light Control:
Setpoint: Desired light level for activation (e.g., 10 lux)
If ambient light < 10 lux, street light ON
If ambient light ≥ 10 lux, street light OFF
Home Heating System:
Setpoint: Desired room temperature (e.g., 20°C)
If temperature < 20°C, heater ON
If temperature ≥ 20°C, heater OFF
Simple to implement
Cost-effective
Works well for systems with slow response times
Robust and reliable for many applications
Can lead to oscillation around the setpoint
May cause wear on mechanical components due to frequent switching
Not suitable for systems requiring precise control
The Maqueen robot uses Infrared (IR) reflective sensors for line detection. Here's how they work:
Working Principle:
The sensor has an IR emitter and an IR receiver.
The emitter sends out IR light.
The light reflects off the surface below the robot.
The receiver detects the amount of reflected light.
Dark surfaces (like a black line) absorb more light, resulting in less reflection.
Light surfaces (like a white background) reflect more light.
Sensor Values:
The sensors return analog values ranging from 0 to 1023.
Lower values indicate darker surfaces (more light absorbed).
Higher values indicate lighter surfaces (more light reflected).
The Maqueen has 5 of these sensors, but we'll focus on the three centered at the front:
Left sensor (L1)
Center sensor (M)
Right sensor (R1)
Objective: Determine appropriate threshold values to distinguish between the line and the background.
Materials:
Maqueen robot
micro:bit
Line following mat (white background with black line)
Code:
from microbit import *
from maqueenplusv2 import *
init_maqueen()
while True:
center = read_line_sensor(M)
print("Center: {}".format(center))
sleep(500)
Run this program and observe the values when:
The sensor is over the white background
The sensor is over the black line
The sensor is partially on the line
Choose a threshold value that reliably distinguishes between the line and background.
Now that we understand how the sensors work, let's break down how bang-bang control applies to line following:
Setpoint: The robot should stay centered on the line. Our "setpoint" is the center sensor detecting the line while the side sensors do not.
Sensor Input: Readings from left, center, and right sensors, compared against our chosen threshold.
Error: Deviation from the center of the line. This is detected when either the left or right sensor sees the line instead of the center sensor.
Control Action: Adjust wheel speeds to turn towards the line.
We'll use our threshold value to determine if a sensor is "ON" (detecting the line) or "OFF" (detecting the background):
ON state: Sensor value < threshold (detecting dark line)
OFF state: Sensor value ≥ threshold (detecting light background)
If center sensor ON, left and right OFF: Move forward (on the line)
If left sensor ON, others OFF: Turn right (line is to the left)
If right sensor ON, others OFF: Turn left (line is to the right)
This creates a zig-zag motion along the line:
When the robot veers left, the right sensor detects the line, causing a right turn.
When it veers right, the left sensor detects the line, causing a left turn.
These continuous corrections keep the robot following the line.
Now that we understand how the sensors work and how to interpret their data, your challenge is to implement the line following algorithm using bang-bang control. Write a MicroPython program that allows the Maqueen robot to follow a black line on a white background using the three front line sensors (L1, M, R1).
from microbit import *
from maqueenplusv2 import *
init_maqueen()
# Your threshold value from Challenge 1
THRESHOLD = 500 # Adjust this based on your calibration results
while True:
# Your code here
sleep(10) # Small delay to prevent overwhelming the micro:bit
read_line_sensor(sensor) returns a single analog reading from the sensor chosen. Sensor can be L2, L1, M, R1, R2.
drive(speed) will drive straight.
spin_left(speed) spins the robot left.
spin_right(speed) spins the robot right.
motors(left speed, left direction, right speed, right direction) gives you finer control over both motors