Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2011. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Commands/PIDSubsystem.h" |
| 9 | #include "PIDController.h" |
| 10 | #include "float.h" |
| 11 | |
| 12 | /** |
| 13 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 14 | * values. |
| 15 | * @param name the name |
| 16 | * @param p the proportional value |
| 17 | * @param i the integral value |
| 18 | * @param d the derivative value |
| 19 | */ |
| 20 | PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d) |
| 21 | : Subsystem(name) { |
| 22 | m_controller = std::make_shared<PIDController>(p, i, d, this, this); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 27 | * values. |
| 28 | * @param name the name |
| 29 | * @param p the proportional value |
| 30 | * @param i the integral value |
| 31 | * @param d the derivative value |
| 32 | * @param f the feedforward value |
| 33 | */ |
| 34 | PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d, |
| 35 | double f) |
| 36 | : Subsystem(name) { |
| 37 | m_controller = std::make_shared<PIDController>(p, i, d, f, this, this); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 42 | * values. It will also space the time |
| 43 | * between PID loop calculations to be equal to the given period. |
| 44 | * @param name the name |
| 45 | * @param p the proportional value |
| 46 | * @param i the integral value |
| 47 | * @param d the derivative value |
| 48 | * @param f the feedfoward value |
| 49 | * @param period the time (in seconds) between calculations |
| 50 | */ |
| 51 | PIDSubsystem::PIDSubsystem(const std::string &name, double p, double i, double d, |
| 52 | double f, double period) |
| 53 | : Subsystem(name) { |
| 54 | m_controller = std::make_shared<PIDController>(p, i, d, f, this, this, period); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 59 | * values. |
| 60 | * It will use the class name as its name. |
| 61 | * @param p the proportional value |
| 62 | * @param i the integral value |
| 63 | * @param d the derivative value |
| 64 | */ |
| 65 | PIDSubsystem::PIDSubsystem(double p, double i, double d) |
| 66 | : Subsystem("PIDSubsystem") { |
| 67 | m_controller = std::make_shared<PIDController>(p, i, d, this, this); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 72 | * values. |
| 73 | * It will use the class name as its name. |
| 74 | * @param p the proportional value |
| 75 | * @param i the integral value |
| 76 | * @param d the derivative value |
| 77 | * @param f the feedforward value |
| 78 | */ |
| 79 | PIDSubsystem::PIDSubsystem(double p, double i, double d, double f) |
| 80 | : Subsystem("PIDSubsystem") { |
| 81 | m_controller = std::make_shared<PIDController>(p, i, d, f, this, this); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Instantiates a {@link PIDSubsystem} that will use the given p, i and d |
| 86 | * values. |
| 87 | * It will use the class name as its name. |
| 88 | * It will also space the time |
| 89 | * between PID loop calculations to be equal to the given period. |
| 90 | * @param p the proportional value |
| 91 | * @param i the integral value |
| 92 | * @param d the derivative value |
| 93 | * @param f the feedforward value |
| 94 | * @param period the time (in seconds) between calculations |
| 95 | */ |
| 96 | PIDSubsystem::PIDSubsystem(double p, double i, double d, double f, |
| 97 | double period) |
| 98 | : Subsystem("PIDSubsystem") { |
| 99 | m_controller = std::make_shared<PIDController>(p, i, d, f, this, this, period); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Enables the internal {@link PIDController} |
| 104 | */ |
| 105 | void PIDSubsystem::Enable() { m_controller->Enable(); } |
| 106 | |
| 107 | /** |
| 108 | * Disables the internal {@link PIDController} |
| 109 | */ |
| 110 | void PIDSubsystem::Disable() { m_controller->Disable(); } |
| 111 | |
| 112 | /** |
| 113 | * Returns the {@link PIDController} used by this {@link PIDSubsystem}. |
| 114 | * Use this if you would like to fine tune the pid loop. |
| 115 | * |
| 116 | * @return the {@link PIDController} used by this {@link PIDSubsystem} |
| 117 | */ |
| 118 | std::shared_ptr<PIDController> PIDSubsystem::GetPIDController() { |
| 119 | return m_controller; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sets the setpoint to the given value. If {@link PIDCommand#SetRange(double, |
| 124 | * double) SetRange(...)} |
| 125 | * was called, |
| 126 | * then the given setpoint |
| 127 | * will be trimmed to fit within the range. |
| 128 | * @param setpoint the new setpoint |
| 129 | */ |
| 130 | void PIDSubsystem::SetSetpoint(double setpoint) { |
| 131 | m_controller->SetSetpoint(setpoint); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Adds the given value to the setpoint. |
| 136 | * If {@link PIDCommand#SetRange(double, double) SetRange(...)} was used, |
| 137 | * then the bounds will still be honored by this method. |
| 138 | * @param deltaSetpoint the change in the setpoint |
| 139 | */ |
| 140 | void PIDSubsystem::SetSetpointRelative(double deltaSetpoint) { |
| 141 | SetSetpoint(GetSetpoint() + deltaSetpoint); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Return the current setpoint |
| 146 | * @return The current setpoint |
| 147 | */ |
| 148 | double PIDSubsystem::GetSetpoint() { return m_controller->GetSetpoint(); } |
| 149 | |
| 150 | /** |
| 151 | * Sets the maximum and minimum values expected from the input. |
| 152 | * |
| 153 | * @param minimumInput the minimum value expected from the input |
| 154 | * @param maximumInput the maximum value expected from the output |
| 155 | */ |
| 156 | void PIDSubsystem::SetInputRange(float minimumInput, float maximumInput) { |
| 157 | m_controller->SetInputRange(minimumInput, maximumInput); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets the maximum and minimum values to write. |
| 162 | * |
| 163 | * @param minimumOutput the minimum value to write to the output |
| 164 | * @param maximumOutput the maximum value to write to the output |
| 165 | */ |
| 166 | void PIDSubsystem::SetOutputRange(float minimumOutput, float maximumOutput) { |
| 167 | m_controller->SetOutputRange(minimumOutput, maximumOutput); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Set the absolute error which is considered tolerable for use with |
| 172 | * OnTarget. |
| 173 | * @param percentage error which is tolerable |
| 174 | */ |
| 175 | void PIDSubsystem::SetAbsoluteTolerance(float absValue) { |
| 176 | m_controller->SetAbsoluteTolerance(absValue); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Set the percentage error which is considered tolerable for use with |
| 181 | * OnTarget. |
| 182 | * @param percentage error which is tolerable |
| 183 | */ |
| 184 | void PIDSubsystem::SetPercentTolerance(float percent) { |
| 185 | m_controller->SetPercentTolerance(percent); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Return true if the error is within the percentage of the total input range, |
| 190 | * determined by SetTolerance. This asssumes that the maximum and minimum input |
| 191 | * were set using SetInput. Use OnTarget() in the IsFinished() method of |
| 192 | * commands |
| 193 | * that use this subsystem. |
| 194 | * |
| 195 | * Currently this just reports on target as the actual value passes through the |
| 196 | * setpoint. |
| 197 | * Ideally it should be based on being within the tolerance for some period of |
| 198 | * time. |
| 199 | * |
| 200 | * @return true if the error is within the percentage tolerance of the input |
| 201 | * range |
| 202 | */ |
| 203 | bool PIDSubsystem::OnTarget() const { return m_controller->OnTarget(); } |
| 204 | |
| 205 | /** |
| 206 | * Returns the current position |
| 207 | * @return the current position |
| 208 | */ |
| 209 | double PIDSubsystem::GetPosition() { return ReturnPIDInput(); } |
| 210 | |
| 211 | /** |
| 212 | * Returns the current rate |
| 213 | * @return the current rate |
| 214 | */ |
| 215 | double PIDSubsystem::GetRate() { return ReturnPIDInput(); } |
| 216 | |
| 217 | void PIDSubsystem::PIDWrite(float output) { UsePIDOutput(output); } |
| 218 | |
| 219 | double PIDSubsystem::PIDGet() { return ReturnPIDInput(); } |
| 220 | |
| 221 | std::string PIDSubsystem::GetSmartDashboardType() const { return "PIDCommand"; } |
| 222 | void PIDSubsystem::InitTable(std::shared_ptr<ITable> table) { |
| 223 | m_controller->InitTable(table); |
| 224 | Subsystem::InitTable(table); |
| 225 | } |