EVE 1.0
pid-controller.h
Go to the documentation of this file.
1 #ifndef PID_CONTROLLER_H
2 #define PID_CONTROLLER_H
3 
4 /**********************************************************************/
5 /*
6  * Copyright (c) 2017, Jetro AS
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
23  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  *
31  * This file is part of the EVE platform.
32  */
33 
34 /**
35  * \file
36  * \brief Abstract PID-controller implementation
37  *
38  * \author SE, Jetro AS
39  */ /******************************************************************/
40 
41 /**
42  * \defgroup pid PID controller
43  * \ingroup util
44  * \{
45  */
46 
47 /**********************************************************************
48  * PUBLIC TYPES
49  **********************************************************************/
50 
51 /**********************************************************************/
52 /** \brief PID controller instance data.
53  */
55 {
56  float ProportionalCoeff; //!< Coefficient for proportional part of controller. Equal to K from standard formula.
57  float IntegralCoeff; //!< Coefficient for integral part of controller. Equal to K/Ti from standard formula.
58  float DifferentialCoeff; //!< Coefficient for differential part of controller. Equal to K*Td from standard formula.
59  float IntegralValue; //!< Variable holding current integral value
60  float PreviousErrorDiff; //!< ErrorDiff on the previous iteration for differential part calculation
61  float MinIntegral; //!< Minimal allowed integrated part value
62  float MaxIntegral; //!< Maximal allowed integrated part value
63 };
64 
65 /**********************************************************************
66  * PUBLIC FUNCTONS
67  **********************************************************************/
68 
69 /**********************************************************************/
70 /**
71  * \brief Initialize PID controller data.
72  * Function gets standard coefficients from PID formula:
73  * f(n) = K * ( Error(n) + SUM(Error(0) * dt(0)...Error(n) * dt(n)) / Ti + Td * Error(n) / dt(n))
74  *
75  * \param Pid Pointer to PID controller data
76  * \param K K coefficient from standard PID formula.
77  * \param Ti Ti coefficient from standard PID formula.
78  * \param Td Td coefficient from standard PID formula.
79  * \param MinIntegral Minimal allowed integrated part value.
80  * \param MaxIntegral Maximal allowed integrated part value.
81  */
82 void PidInit(struct pid_controller_t* Pid, float K, float Ti, float Td,
83  float MinIntegral, float MaxIntegral);
84 
85 /**********************************************************************/
86 /**
87  * \brief Reset PID controller.
88  *
89  * @param Pid Pointer to PID controller data
90  */
91 inline void PidReset(struct pid_controller_t* Pid)
92 {
93  Pid->IntegralValue = 0.;
94  Pid->PreviousErrorDiff = 0.;
95 }
96 
97 /**********************************************************************/
98 /**
99  * \brief Calculate PID function
100  *
101  * \param Pid Pointer to PID controller data
102  * \param Error Desired value minus current value
103  * \param ErrorDiff Error for differencial part.
104  * For classic PID it should be set equal to Error.
105  * To avoid peak output on sharp desired value change, tis can be set to (-) current value.
106  * \param TimeDelta Time between this and previous Error calculation/measurement.
107  * \return Calculated PID value
108  */
109 float PidFunc(struct pid_controller_t* Pid,
110  float Error, float ErrorDiff, float TimeDelta);
111 
112 /** @} pid */
113 
114 #endif // PID_CONTROLLER_H
float PreviousErrorDiff
ErrorDiff on the previous iteration for differential part calculation.
float IntegralCoeff
Coefficient for integral part of controller. Equal to K/Ti from standard formula. ...
float ProportionalCoeff
Coefficient for proportional part of controller. Equal to K from standard formula.
PID controller instance data.
float IntegralValue
Variable holding current integral value.
float PidFunc(struct pid_controller_t *Pid, float Error, float ErrorDiff, float TimeDelta)
Calculate PID function.
void PidReset(struct pid_controller_t *Pid)
Reset PID controller.
float MinIntegral
Minimal allowed integrated part value.
float DifferentialCoeff
Coefficient for differential part of controller. Equal to K*Td from standard formula.
float MaxIntegral
Maximal allowed integrated part value.
void PidInit(struct pid_controller_t *Pid, float K, float Ti, float Td, float MinIntegral, float MaxIntegral)
Initialize PID controller data. Function gets standard coefficients from PID formula: f(n) = K * ( Er...