├── .gitignore ├── PID.c ├── main.c ├── README.md └── PID.h /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | -------------------------------------------------------------------------------- /PID.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangHongxi2001/PID_Library/HEAD/PID.c -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangHongxi2001/PID_Library/HEAD/main.c -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ****************************************************************************** 2 | * PID_Library 3 | * @author Hongxi Wong 4 | * @version V1.0.5 5 | * @date 2020/5/1 6 | * 7 | * Reference: Brett Beauregard(https://github.com/br3ttb/Arduino-PID-Library) 8 | ****************************************************************************** 9 | * @attention 10 | * For more details, please see https://www.cnblogs.com/HongxiWong/p/12404424.html 11 | ****************************************************************************** 12 | -------------------------------------------------------------------------------- /PID.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file pid.h 4 | * @author Hongxi Wong 5 | * @version V1.0.6 6 | * @date 2019/12/17 7 | * @brief 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | ****************************************************************************** 12 | */ 13 | #ifndef _PID_H 14 | #define _PID_H 15 | 16 | #include "stdint.h" 17 | 18 | #define ABS(x) ((x > 0) ? x : -x) 19 | 20 | typedef enum pid_Improvement_e 21 | { 22 | NONE = 0X00, //0000 0000 23 | Integral_Limit = 0x01, //0000 0001 24 | Derivative_On_Measurement = 0x02, //0000 0010 25 | Trapezoid_Intergral = 0x04, //0000 0100 26 | Proportional_On_Measurement = 0x08, //0000 1000 27 | OutputFilter = 0x10, //0001 0000 28 | ChangingIntegralRate = 0x20, //0010 0000 29 | DerivativeFilter = 0x40, //0100 0000 30 | ErrorHandle = 0x80, //1000 0000 31 | } PID_Improvement_e; 32 | 33 | typedef enum errorType_e 34 | { 35 | PID_ERROR_NONE = 0x00U, 36 | Motor_Blocked = 0x01U 37 | } ErrorType_e; 38 | 39 | typedef struct 40 | { 41 | uint64_t ERRORCount; 42 | ErrorType_e ERRORType; 43 | } PID_ErrorHandler_t; 44 | 45 | typedef struct _PID_TypeDef 46 | { 47 | float Target; 48 | float LastNoneZeroTarget; 49 | float Kp; 50 | float Ki; 51 | float Kd; 52 | 53 | float Measure; 54 | float Last_Measure; 55 | float Err; 56 | float Last_Err; 57 | 58 | float Pout; 59 | float Iout; 60 | float Dout; 61 | float ITerm; 62 | 63 | float Output; 64 | float Last_Output; 65 | float Last_Dout; 66 | 67 | float MaxOut; 68 | float IntegralLimit; 69 | float DeadBand; 70 | float ControlPeriod; 71 | float MaxErr; 72 | float ScalarA; //For Changing Integral 73 | float ScalarB; //ITerm = Err*((A-abs(err)+B)/A) when B<|err|