Easy Control Library for Cubic 0.01
Loading...
Searching...
No Matches
PID.h
1#pragma once
2#include <Arduino.h>
3#include <limits.h>
4
5namespace PID
6{
7 constexpr bool EXCEED_MICROS_LIMIT = false;
8
9 constexpr unsigned long MAX_MICROSECONDS = ULONG_MAX;
10 constexpr double MICROSECONDS_TO_SECONDS = 1.0 / 1000000.0;
11
12 class PID
13 {
14 private:
15 double Kp;
16 double Ki;
17 double Kd;
18 double current;
19 double target;
20 double diff;
21 double preDiff;
22 double integral;
23 unsigned long preMicros;
24
25 double dutyCycle = 0;
26 double capableDutyCycle;
27
28 bool direction;
29
30 public:
32 double dt;
33
45 PID(double capableDutyCycle, double Kp, double Ki, double Kd, double current, double target, bool direction);
46
54 void setGains(double Kp, double Ki, double Kd);
55
61 void setKp(double Kp);
62
68 void setKi(double Ki);
69
75 void setKd(double Kd);
76
82 void setTarget(double target);
83
89 double getTarget() const;
90
96 double getCurrent() const;
97
104 double getDutyCycle() const;
105
111 void reset();
112
118 void reset(double target);
119
127 void reset(double Kp, double Ki, double Kd);
128
137 void reset(double Kp, double Ki, double Kd, double target);
138
146 double compute_PID(double current, bool logging = false);
147
153 double getDt() const
154 {
155 return dt;
156 }
157 };
158
159 inline void PID::setGains(const double Kp, const double Ki, const double Kd)
160 {
161 this->Kp = Kp;
162 this->Ki = Ki;
163 this->Kd = Kd;
164 }
165 inline void PID::setKp(const double Kp)
166 {
167 this->Kp = Kp;
168 }
169 inline void PID::setKi(const double Ki)
170 {
171 this->Ki = Ki;
172 }
173 inline void PID::setKd(const double Kd)
174 {
175 this->Kd = Kd;
176 }
177 inline void PID::setTarget(const double target)
178 {
179 this->target = target;
180 }
181 inline double PID::getTarget() const
182 {
183 return this->target;
184 }
185 inline double PID::getCurrent() const
186 {
187 return this->current;
188 }
189 inline double PID::getDutyCycle() const
190 {
191 return this->dutyCycle;
192 }
193 inline void PID::reset()
194 {
195 preMicros = micros();
196 preDiff = 0;
197 integral = 0;
198 }
199 inline void PID::reset(const double target)
200 {
201 this->target = target;
202 this->reset();
203 }
204
205 inline void PID::reset(const double Kp, const double Ki, const double Kd)
206 {
207 this->Kp = Kp;
208 this->Ki = Ki;
209 this->Kd = Kd;
210 this->reset();
211 }
212
213 inline void PID::reset(const double Kp, const double Ki, const double Kd, const double target)
214 {
215 this->Kp = Kp;
216 this->Ki = Ki;
217 this->Kd = Kd;
218 this->target = target;
219 this->reset();
220 }
221
222}
Definition PID.h:13
double getDt() const
Get the Dt object.
Definition PID.h:153
void setKi(double Ki)
Set the Ki object.
Definition PID.h:169
void setKp(double Kp)
Set the Kp object.
Definition PID.h:165
double dt
dt[s]
Definition PID.h:32
void setTarget(double target)
目標を変更する。
Definition PID.h:177
void setKd(double Kd)
Set the Kd object.
Definition PID.h:173
double getTarget() const
目標を取得する。
Definition PID.h:181
void reset()
制御器のリセット
Definition PID.h:193
double getCurrent() const
現在値を取得する。
Definition PID.h:185
void setGains(double Kp, double Ki, double Kd)
ゲインを変更する。
Definition PID.h:159
double getDutyCycle() const
Duty比の取得
Definition PID.h:189