ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
PID.cpp
Go to the documentation of this file.
1
14#include "ART/PID.h"
15
16namespace art
17{
18
20 : m_startTime(std::chrono::high_resolution_clock::now())
21 {
22 setConstants(1, 0, 0, 0);
23 }
24
26 {
27 m_error = 0;
28 m_prevError = 0;
29
30 m_derivative = 0;
31 m_integral = 0;
32
33 m_startTime = std::chrono::high_resolution_clock::now();
34 m_startSettledTime = std::chrono::high_resolution_clock::now();
35 }
36
37 void PID::setConstants(double kp, double ki, double kd)
38 {
39 m_kp = kp;
40 m_ki = ki;
41 m_kd = kd;
42 }
43 PID &PID::withConstants(double kp, double ki, double kd)
44 {
45 setConstants(kp, ki, kd);
46 return *this;
47 }
48 void PID::setConstants(double kp, double ki, double kd, double ff)
49 {
50 m_kp = kp;
51 m_ki = ki;
52 m_kd = kd;
53 m_ff = ff;
54 }
55 PID &PID::withConstants(double kp, double ki, double kd, double ff)
56 {
57 setConstants(kp, ki, kd, ff);
58 return *this;
59 }
60
61 double PID::getkp(){ return m_kp; }
62 double PID::getki(){ return m_ki; }
63 double PID::getkd(){ return m_kd; }
64 double PID::getff(){ return m_ff; }
65
66 void PID::setIntegralZone(double integralZone) { m_integralZone = integralZone; }
67 PID &PID::withIntegralZone(double integralZone)
68 {
69 setIntegralZone(integralZone);
70 return *this;
71 }
72
73 void PID::setTimeout(double timeout) { m_timeout = timeout; }
74 PID &PID::withTimeout(double timeout)
75 {
76 setTimeout(timeout);
77 return *this;
78 }
79
80 void PID::setSettleZone(double settleZone) { m_settleZone = settleZone; }
81 PID &PID::withSettleZone(double settleZone)
82 {
83 setSettleZone(settleZone);
84 return *this;
85 }
86
87 void PID::setSettleTimeout(double settleTimeout) { m_settleTimeout = settleTimeout; }
88 PID &PID::withSettleTimeout(double settleTimeout)
89 {
90 setSettleTimeout(settleTimeout);
91 return *this;
92 }
93
94 double PID::calculate(double error)
95 {
96 m_error = error;
98
99 if (std::abs(m_error) < m_integralZone)
100 {
102 }
103 else
104 {
105 m_integral = 0;
106 }
107 if ((m_error > 0 && m_prevError < 0) || (m_error < 0 && m_prevError > 0))
108 {
109 // m_integral *= -0.5;
110 }
111
112 if (std::abs(error) > m_settleZone)
113 {
114 m_startSettledTime = std::chrono::high_resolution_clock::now();
115 }
116
117 double output = (m_kp * m_error) + (m_kd * m_derivative) + (m_ki * m_integral) + m_ff;
118
120 return output;
121 }
122 double PID::calculate(double target, double feedback)
123 {
124 return calculate(target - feedback);
125 }
126
128 {
129 if (timePassed() > m_timeout && m_timeout != 0)
130 {
131 return true;
132 }
134 {
135 return true;
136 }
137 return false;
138 }
139
141 {
142 auto currentTime = std::chrono::high_resolution_clock::now();
143 return std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - m_startTime).count() / 1000.f;
144 }
145
147 {
148 auto currentTime = std::chrono::high_resolution_clock::now();
149 return std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - m_startSettledTime).count() / 1000.f;
150 }
151
152 double PID::getProportional(){ return m_error * m_kp; }
153 double PID::getIntegral(){ return m_integral * m_ki; }
155
156} // namespace Jath
Header containing a general use PID class.
A general use PID class.
Definition PID.h:59
double m_settleZone
The range which the error must be for the PID to be considered settled.
Definition PID.h:606
double getki()
Returns the value of m_ki.
Definition PID.cpp:62
double m_timeout
The duration the PID should run for before it is considered finished.
Definition PID.h:566
PID & withConstants(double kp, double ki, double kd)
Sets the PID constans and returns a refrence to the PID object.
Definition PID.cpp:43
double m_integralZone
The range within which the error is applied to calculate the integral.
Definition PID.h:484
double m_prevError
The error fed into the PID controller on it's previous iteration.
Definition PID.h:456
double m_integral
The integral caluculated by the PID controller.
Definition PID.h:477
double m_error
The error last fed into the PID controller.
Definition PID.h:445
double m_derivative
The derivate calculated by the PID controller.
Definition PID.h:466
double getProportional()
Get the output produced by the Proportional(P) term.
Definition PID.cpp:152
TimePoint m_startSettledTime
A point in time representing when the PID first entered the acceptable error range.
Definition PID.h:580
void setConstants(double kp, double ki, double kd)
Sets the PID constants.
Definition PID.cpp:37
double getkp()
Returns the value of m_kp.
Definition PID.cpp:61
void setIntegralZone(double integralZone)
Sets the range within which the error will be applied to the integral term.
Definition PID.cpp:66
void setSettleTimeout(double settleTimeout)
Set the duration the PID must be settled for before being complete.
Definition PID.cpp:87
double timePassed()
Gets the amoount of time passed since the PID started.
Definition PID.cpp:140
double getff()
Returns the value of m_ff.
Definition PID.cpp:64
PID & withIntegralZone(double integralZone)
Set range within which the error will be applied to the integral term and returns a PID refrence.
Definition PID.cpp:67
double calculate(double error)
Runs the PID by providing an input, then calculates and returns an output.
Definition PID.cpp:94
void setTimeout(double timeout)
Sets the timeout duration.
Definition PID.cpp:73
TimePoint m_startTime
A point in time representing when the PID started.
Definition PID.h:554
bool isCompleted()
Gets the current state of the PID.
Definition PID.cpp:127
double getDerivative()
Get the output produced by the Derivative(D) term.
Definition PID.cpp:154
double getIntegral()
Get the output produced by the Integral(I) term.
Definition PID.cpp:153
double getkd()
Returns the value of m_kd.
Definition PID.cpp:63
double m_kp
A constant applied to the Proportional term.
Definition PID.h:499
void setSettleZone(double settleZone)
Set the range within which the PID is considered settled.
Definition PID.cpp:80
void reset()
Resets the PID object.
Definition PID.cpp:25
double m_ff
A constant applied directly to the output.
Definition PID.h:541
double m_ki
A constant applied to the Integral term.
Definition PID.h:514
PID()
Constructs a new PID object.
Definition PID.cpp:19
double m_settleTimeout
The duration the error must be within before the PID is considered complete.
Definition PID.h:591
double settledTimePassed()
Gets the amoount of time the PID has been settled for.
Definition PID.cpp:146
double m_kd
A constant applied to the Derivative term.
Definition PID.h:528
PID & withTimeout(double timeout)
Sets the timeout duration and returns a refrence to the PID object.
Definition PID.cpp:74
PID & withSettleZone(double settleZone)
Set the range within which the PID is considered settled and returns a refrence to the PID object.
Definition PID.cpp:81
PID & withSettleTimeout(double settleTimeout)
Set the duration the PID must be settled for before being complete.
Definition PID.cpp:88
Definition PID.h:20