forked from r-downing/AutoPID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoPID.h
64 lines (53 loc) · 1.99 KB
/
AutoPID.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef AUTOPID_H
#define AUTOPID_H
#include <Arduino.h>
class AutoPID {
public:
// Constructor - takes pointer inputs for control variales, so they are updated automatically
AutoPID(double *input, double *setpoint, double *output, double outputMin, double outputMax,
double Kp, double Ki, double Kd);
// Allows manual adjustment of gains
void setGains(double Kp, double Ki, double Kd);
// Sets bang-bang control ranges, separate upper and lower offsets, zero for off
void setBangBang(double bangOn, double bangOff);
// Sets bang-bang control range +-single offset
void setBangBang(double bangRange);
// Allows manual readjustment of output range
void setOutputRange(double outputMin, double outputMax);
// Allows manual adjustment of time step (default 1000ms)
void setTimeStep(unsigned long timeStep);
// Returns true when at set point (+-threshold)
bool atSetPoint(double threshold);
// Runs PID calculations when needed. Should be called repeatedly in loop.
// Automatically reads input and sets output via pointers
void run();
// Stops PID functionality, output sets to
void stop();
void reset();
bool isStopped();
double getIntegral();
void setIntegral(double integral);
private:
double _Kp, _Ki, _Kd;
double _integral, _previousError;
double _bangOn, _bangOff;
double *_input, *_setpoint, *_output;
double _outputMin, _outputMax;
unsigned long _timeStep, _lastStep;
bool _stopped;
};//class AutoPID
class AutoPIDRelay : public AutoPID {
public:
AutoPIDRelay(double *input, double *setpoint, bool *relayState, double pulseWidth, double Kp, double Ki, double Kd)
: AutoPID(input, setpoint, &_pulseValue, 0, 1.0, Kp, Ki, Kd) {
_relayState = relayState;
_pulseWidth = pulseWidth;
};
void run();
double getPulseValue();
private:
bool * _relayState;
unsigned long _pulseWidth, _lastPulseTime;
double _pulseValue;
};//class AutoPIDRelay
#endif