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