Module 3 — Controls & Feedback
Objective: add sensing and closed-loop control.
Prereqs: Command-based project with subsystems.
Steps
- Configure encoders/gyros; read positions/velocities.
- Implement PID (position/velocity) and feedforward; tune gains.
- Hold setpoints on a mechanism (arm/flywheel).
- Add motion profiling or slew limits to reduce shocks.
- Verify sensor phase/direction before tuning; protect against runaway by clamping outputs.
Deliverables
- Mechanism that holds a setpoint within tolerance.
- Logged tuning session (gains, observed response, final values).
Code example (simple PID)
PIDController pid = new PIDController(0.08, 0, 0);
double output = pid.calculate(arm.getPosition(), targetDegrees);
arm.setVoltage(output);
Resources
- WPILib PID/FF examples: docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/index.html
- CTRE: v5.docs.ctr-electronics.com
- REV: docs.revrobotics.com
Instructions (numbered)
- Configure sensors (encoders/gyros); verify direction and units.
- Implement PID/FF; clamp outputs; add slew limits if needed.
- Tune gains (P first, then D/FF); record responses and final values.
- Test holding and moving to setpoints; measure error and stability.
- Log tuning session and results.
Example
- Arm PID: kP 0.08, kD 0.001, kS/kG/kV from characterization; holds ±2° at setpoint.
Best practices
- Verify sensor phase before tuning; safe defaults/clamps to avoid runaway.
- Tune incrementally; log data for each change.
- Use characterization tools for FF when possible.
Common mistakes
- Wrong units/phase; no clamping leading to runaway.
- Tuning without logging; guessing gains.
- Ignoring load changes (gravity/CG shifts).
Spec notes / data to log
- Sensors: units/phase verified; sample rates.
- Controls: kP/kI/kD/kS/kG/kV values; clamps/slew limits.
- Data: log setpoint vs actual, error, voltage/current during tuning.
Checklist
- Sensors verified (units/phase)
- PID/FF implemented with clamps/slew limits
- Gains tuned and logged
- Setpoint hold/move tested
- Final gains recorded
Recommended tools
- WPILib SysId/characterization, logging (AdvantageKit or built-in), Sim GUI for dry runs.
Sample log (template)
- Date:
- Mechanism:
- Gains tried/results:
- Final gains:
- Notes/issues:
Photos/diagrams
- [Placeholder: plot of setpoint vs actual with tuned gains]
Numeric Example
Example drivetrain velocity PID loop:
- Input: wheel speed (m/s)
- Output: motor command (volts)
- kP: 0.12 V / (m/s)
- kI: 0.00 V / (m/s·s)
- kD: 0.001 V / (m/s²)
- kS: 0.13 V
- kV: 1.41 V / (m/s)
- kA: 0.09 V / (m/s²)
Expected behavior:
- Steady-state error: < 5% of target speed
- Overshoot: < 10% for step changes in setpoint
Data to Log
At minimum, log the following for any closed-loop test:
- Timestamp
- Commanded setpoint (m/s, deg/s, etc.)
- Measured value (same units as setpoint)
- Controller output (volts or percent)
- Battery voltage
- Motor current (A)
- Error (setpoint - measured)
- Any fault codes or limit events
Purpose:
- Validate tuning (check overshoot, settle time, and steady-state error)
- Detect brownouts and current spikes
- Compare different tuning runs over time