|
| 1 | +# Copyright (c) FIRST and other WPILib contributors. |
| 2 | +# Open Source Software; you can modify and/or share it under the terms of |
| 3 | +# the WPILib BSD license file in the root directory of this project. |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from wpimath.controller import PIDController |
| 7 | + |
| 8 | +from .subsystem import Subsystem |
| 9 | + |
| 10 | + |
| 11 | +class PIDSubsystem(Subsystem): |
| 12 | + """ |
| 13 | + A subsystem that uses a {@link PIDController} to control an output. The |
| 14 | + controller is run synchronously from the subsystem's periodic() method. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, controller: PIDController, initial_position: float = 0.0): |
| 18 | + """ |
| 19 | + Creates a new PIDSubsystem. |
| 20 | +
|
| 21 | + :param controller: The PIDController to use. |
| 22 | + :param initial_position: The initial setpoint of the subsystem. |
| 23 | + """ |
| 24 | + super().__init__() |
| 25 | + |
| 26 | + self._controller = controller |
| 27 | + self.setSetpoint(initial_position) |
| 28 | + self.addChild("PID Controller", self._controller) |
| 29 | + self._enabled = False |
| 30 | + |
| 31 | + def periodic(self): |
| 32 | + """ |
| 33 | + Executes the PID control logic during each periodic update. |
| 34 | +
|
| 35 | + This method is called synchronously from the subsystem's periodic() method. |
| 36 | + """ |
| 37 | + if self._enabled: |
| 38 | + self.useOutput( |
| 39 | + self._controller.calculate(self.getMeasurement()), self.getSetpoint() |
| 40 | + ) |
| 41 | + |
| 42 | + def getController(self) -> PIDController: |
| 43 | + """ |
| 44 | + Returns the PIDController used by the subsystem. |
| 45 | +
|
| 46 | + :return: The PIDController. |
| 47 | + """ |
| 48 | + return self._controller |
| 49 | + |
| 50 | + def setSetpoint(self, setpoint: float): |
| 51 | + """ |
| 52 | + Sets the setpoint for the subsystem. |
| 53 | +
|
| 54 | + :param setpoint: The setpoint for the subsystem. |
| 55 | + """ |
| 56 | + self._controller.setSetpoint(setpoint) |
| 57 | + |
| 58 | + def getSetpoint(self) -> float: |
| 59 | + """ |
| 60 | + Returns the current setpoint of the subsystem. |
| 61 | +
|
| 62 | + :return: The current setpoint. |
| 63 | + """ |
| 64 | + return self._controller.getSetpoint() |
| 65 | + |
| 66 | + def useOutput(self, output: float, setpoint: float): |
| 67 | + """ |
| 68 | + Uses the output from the PIDController. |
| 69 | +
|
| 70 | + :param output: The output of the PIDController. |
| 71 | + :param setpoint: The setpoint of the PIDController (for feedforward). |
| 72 | + """ |
| 73 | + raise NotImplementedError("Subclasses must implement this method") |
| 74 | + |
| 75 | + def getMeasurement(self) -> float: |
| 76 | + """ |
| 77 | + Returns the measurement of the process variable used by the PIDController. |
| 78 | +
|
| 79 | + :return: The measurement of the process variable. |
| 80 | + """ |
| 81 | + raise NotImplementedError("Subclasses must implement this method") |
| 82 | + |
| 83 | + def enable(self): |
| 84 | + """Enables the PID control. Resets the controller.""" |
| 85 | + self._enabled = True |
| 86 | + self._controller.reset() |
| 87 | + |
| 88 | + def disable(self): |
| 89 | + """Disables the PID control. Sets output to zero.""" |
| 90 | + self._enabled = False |
| 91 | + self.useOutput(0, 0) |
| 92 | + |
| 93 | + def isEnabled(self) -> bool: |
| 94 | + """ |
| 95 | + Returns whether the controller is enabled. |
| 96 | +
|
| 97 | + :return: Whether the controller is enabled. |
| 98 | + """ |
| 99 | + return self._enabled |
0 commit comments