A small DC motor drives a lab centrifuge. Its speed w (radians per second) responds to the voltage u like a first-order system: each volt accelerates it by 40 rad/s per second, and friction slows it with a time constant of 0.5 s. With steps of dt = 0.01 s:
w = w + 0.01 * (40 * u - w / 0.5)
A PI controller sets the voltage. The motor starts at rest, w = 0, with the integral I = 0. Each step does exactly this, in this order:
e = w_set - w (the error, from the speed at the start of the step)
I = I + e * 0.01 (integrate first)
u = Kp * e + Ki * I (then the command; no voltage limit in this problem)
w = w + 0.01 * (40 * u - w / 0.5)
Write pi_motor(w_set, Kp, Ki, steps) that runs steps steps and returns the list of speeds: the starting 0 and then the speed after every step (steps + 1 numbers).
With Kp = 0.2 and Ki = 0 (plain P control) the motor settles at 80 rad/s when asked for 100. With Ki = 2 it reaches 100, after shooting past to about 121. Press Run with plot(pi_motor(100, 0.2, 2, 300)) and the same with Ki = 0 to compare.
Examples
Input: w_set = 100, Kp = 0.2, Ki = 2, steps = 3
Output: [0.0, 8.8, 17.4496, 25.8946432]
Explanation: step 1: e = 100, I = 1, u = 20 + 2 = 22 V, w = 0 + 0.01 * (880 - 0) = 8.8.
Step 2: e = 91.2, I = 1.912, u = 18.24 + 3.824 = 22.064 V, w = 8.8 + 0.01 * (882.56 - 17.6) = 17.4496.
Input: w_set = 50, Kp = 0.2, Ki = 0, steps = 2
Output: [0.0, 4.0, 7.6]
Constraints
- answers are compared with a tolerance of
1e-6; do not round
Goals
- Simulate a closed loop with a PI controller in a stated order
- Carry two states through a loop: the plant's speed and the controller's integral
- See integral action reach a setpoint that P control alone misses