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