PID Controller:
Pseudo-Code Within the Controller Class:
- error = sensorValue – offset(target)
- integral = integral * dampen + error
- derivative = error – lastError
- pTerm = Kp*error
- iTerm = Ki*integral
- dTerm = Kd*derivative
- correction = pTerm + iTerm + dTerm
Pseudo-Code in the Control Task:
- correction = Range.clip(correction, original min, original max)
- correction = Range.clip(correction, original min, original max, scaled min, scaled max)
- power = Range.clip(tp ± correction, -1, 1) – ALWAYS clip to (-1, 1) when setting motor power
PID Controller Class
- Instantiate a controller for each control task (line following, turning, go forward etc.)
- Instantiate with parameters(Controller name, Kp, Ki, Kd, dampen)
- Can control debugging by setting debug mode on or off with .setDebug() method
- writes the error, lastError, integral, derivative, pTerm, iTerm, dTerm, correction with the timestamp
- writes to the file /FIRST/{controller-name}.txt, one row per iteration
- ONLY use when debugging (costly/consuming when doing file IO(input output) for every iteration)
- clear the file BEFORE each run (prevents it from appending to the same file on different runs)
- Has writeToDebugFile() method to write all messages to file at the end of the control task
- writes to the file /FIRST/debugFor{controller-name}_{time-stamp}.txt, one row per iteration
Tuning the PID Controller – A 2-step Process!
- Step 1: Finding the right scale (P controller only)
- Instantiate controller with Ki, Kd and dampen all set to zero
- Adjust Range.scale(correction, original min, original max, scaled min, scaled max) until you find the scaled min/max which efficiently completes the task
- Step 2: Finding the balance between Kp, Ki, Kd
- Ki starting value (1/10th of Kp)
- Kd starting value (10 times Kp)
- Dampen starting value (0.66)
Tuning the Line Follower:
Kp = 1.07, Ki = 0, Kd = 0, dampen = 0 (tp = .2, scale = [-.5, .5])
Observations:
- Follows the line with significant oscillation
- Relatively fast
Kp = 1, Ki = 0.1, Kd = 0, dampen = 0.15 (tp = .2, scale = [-.7, .7])
Observations
- Follows the line with significant oscillation
- Overshoots after oscillating once
- Staggered movement
Kp= 1, Ki = 0.1, Kd = 10, dampen = 0.15 (tp = .2, scale = [-.5, .5])
Observations
- Follows the line with even more significant oscillation
- Relatively fast
Kp = 1, Ki = .098, Kd = 10, dampen = 0.15(tp = .2, scale = [-.5, .5])
- Follows line with little oscillation
- Relatively fast
4/16/16 Software