-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRovePIDController.h
151 lines (121 loc) · 3.65 KB
/
RovePIDController.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef ROVEPIDCONTROLLER_H
#define ROVEPIDCONTROLLER_H
#include <cfloat>
#include <cstdint>
class RovePIDController {
private:
float m_kP = 0, m_kI = 0, m_kD = 0;
float m_iZone = FLT_MAX, m_maxIntegralAccum = FLT_MAX;
float m_maxOutput = FLT_MAX, m_minOutput = -FLT_MAX;
mutable float m_lastError, m_integral;
mutable bool m_firstLoop = true;
mutable uint32_t m_lastTimestamp;
bool m_continuous = false;
float m_maxFeedback, m_minFeedback;
float m_offset;
public:
/**
* @brief Construct a new RovePIDController object.
*
*/
RovePIDController() {}
/**
* @brief Construct a new RovePIDController object.
*
* @param kP
* @param kI
* @param kD
*/
RovePIDController(const float kP, const float kI, const float kD) : m_kP(kP), m_kI(kI), m_kD(kD) {}
/**
* @brief Configure the gains of the PID Controller.
*
* @param kP
* @param kI
* @param kD
*/
void configPID(const float kP, const float kI, const float kD);
/**
* @brief Configure the proportional gain of the PID controller.
*
* @param kP
*/
void configKP(const float kP);
/**
* @brief Configure the integral gain of the PID controller.
*
* @param kI
*/
void configKI(const float kI);
/**
* @brief Configure the derivative gain of the PID controller.
*
* @param kD
*/
void configKD(const float kD);
/**
* @brief Configure the integral zone of the PID controller. If the absolute error is greater than this value
* when calculate() is called, the integral will be reset to 0.
*
* @param iZone [0, FLT_MAX], defaults to FLT_MAX.
*/
void configIZone(const float iZone);
/**
* @brief Configure the maximum integral accumulation of the PID controller. If the integral is greater than this
* value when calculate() is called, it will be set to equal this value.
*
* @param max [0, FLT_MAX], defaults to FLT_MAX.
*/
void configMaxIntegralAccum(const float max);
/**
* @brief Configure the maximum and minimum values returned by calculate().
*
* @param min
* @param max
*/
void configOutputLimits(const float min, const float max);
/**
* @brief Configure the maximum value returned by calculate().
*
* @param max
*/
void configMaxOutput(const float max);
/**
* @brief Configure the minimum value returned by calculate().
*
* @param min
*/
void configMinOutput(const float min);
void configOffset(const float offset);
/**
* @brief Enable wrapping of feedback with the given range. For 360 degree rotation, pass (0, 360).
*
* @param minFeedback Smaller representation of wrapping point.
* @param maxFeedback Larger representation of wrapping point.
*/
void enableContinuousFeedback(const float minFeedback, const float maxFeedback);
/**
* @brief Disable wrapping of feedback; disabled by default.
*/
void disableContinuousFeedback();
/**
* @brief Read the last error in position.
*
* @return Last error.
*/
float error() const;
/**
* @brief Reset the PID controller. When calculate() is next called, the derivative and integral terms will be 0.
*
*/
void reset() const;
/**
* @brief Calculate the output of the PID controller.
*
* @param target
* @param feedback
* @return The output of the PID controller, bounded by the configured maximum and minimum values.
*/
float calculate(float target, float feedback) const;
};
#endif