Skyward boardcore
Loading...
Searching...
No Matches
PIController.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Vincenzo Santomarco
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <limits>
26
27namespace Boardcore
28{
29
34{
35public:
36 PIController(float Kp, float Ki, float Ts = 1,
37 float uMin = -std::numeric_limits<float>::infinity(),
38 float uMax = std::numeric_limits<float>::infinity())
39 : Kp(Kp), Ki(Ki), Ts(Ts), uMin(uMin), uMax(uMax)
40 {
41 }
42
46 float update(float error)
47 {
48 if (!saturation)
49 i = i + Ki * Ts * error;
50
51 float u = Kp * error + i;
52
53 lastOutput = antiWindUp(u);
54 return lastOutput;
55 }
56
57 float antiWindUp(float u) { return antiWindUp(u, uMin, uMax); }
58
62 float antiWindUp(float u, float uMin, float uMax)
63 {
64 if (u < uMin)
65 {
66 u = uMin;
67 saturation = true;
68 }
69 else if (u > uMax)
70 {
71 u = uMax;
72 saturation = true;
73 }
74 else
75 {
76 saturation = false;
77 }
78
79 return u;
80 }
81
82 float getI() { return i; }
83
84 float getLastOutput() { return lastOutput; }
85
86 bool isSaturated() { return saturation; }
87
88 float Kp; // Proportional factor.
89 float Ki; // Integral factor.
90 float Ts; // Sampling period.
91 float uMin, uMax; // Anti-windup limits.
92
93private:
94 float i = 0; // Integral contribution.
95 float lastOutput; // Last computed controller output.
96 bool saturation = false;
97};
98
99} // namespace Boardcore
Proportional and integral controller with saturation.
float antiWindUp(float u)
PIController(float Kp, float Ki, float Ts=1, float uMin=-std::numeric_limits< float >::infinity(), float uMax=std::numeric_limits< float >::infinity())
float antiWindUp(float u, float uMin, float uMax)
Anti-windup mechanism.
float update(float error)
Update the PI internal state.
This file includes all the types the logdecoder script will decode.