2022-10-20 17:13:02 +08:00
|
|
|
/**
|
2022-11-01 22:32:15 +08:00
|
|
|
******************************************************************************
|
|
|
|
* @file controller.h
|
|
|
|
* @author Wang Hongxi
|
|
|
|
* @version V1.1.3
|
|
|
|
* @date 2021/7/3
|
|
|
|
* @brief
|
|
|
|
******************************************************************************
|
|
|
|
* @attention
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
2022-10-20 17:13:02 +08:00
|
|
|
#ifndef _CONTROLLER_H
|
|
|
|
#define _CONTROLLER_H
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "stdint.h"
|
2023-02-16 15:46:04 +08:00
|
|
|
#include "memory.h"
|
2022-10-20 17:13:02 +08:00
|
|
|
#include "stdlib.h"
|
|
|
|
#include "bsp_dwt.h"
|
|
|
|
#include "arm_math.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#ifndef abs
|
|
|
|
#define abs(x) ((x > 0) ? x : -x)
|
|
|
|
#endif
|
|
|
|
|
2023-02-16 15:46:04 +08:00
|
|
|
// PID 优化环节使能标志位,通过位与可以判断启用的优化环节;也可以改成位域的形式
|
2022-11-11 21:00:02 +08:00
|
|
|
typedef enum
|
2022-10-20 17:13:02 +08:00
|
|
|
{
|
2023-02-16 15:46:04 +08:00
|
|
|
PID_IMPROVE_NONE = 0b00000000, // 0000 0000
|
|
|
|
PID_Integral_Limit = 0b00000001, // 0000 0001
|
|
|
|
PID_Derivative_On_Measurement = 0b00000010, // 0000 0010
|
|
|
|
PID_Trapezoid_Intergral = 0b00000100, // 0000 0100
|
|
|
|
PID_Proportional_On_Measurement = 0b00001000, // 0000 1000
|
|
|
|
PID_OutputFilter = 0b00010000, // 0001 0000
|
|
|
|
PID_ChangingIntegrationRate = 0b00100000, // 0010 0000
|
|
|
|
PID_DerivativeFilter = 0b01000000, // 0100 0000
|
|
|
|
PID_ErrorHandle = 0b10000000, // 1000 0000
|
2022-10-20 17:13:02 +08:00
|
|
|
} PID_Improvement_e;
|
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
/* PID 报错类型枚举*/
|
2022-10-20 17:13:02 +08:00
|
|
|
typedef enum errorType_e
|
|
|
|
{
|
|
|
|
PID_ERROR_NONE = 0x00U,
|
2023-02-20 18:34:23 +08:00
|
|
|
PID_MOTOR_BLOCKED_ERROR = 0x01U
|
2022-10-20 17:13:02 +08:00
|
|
|
} ErrorType_e;
|
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
typedef struct
|
2022-10-20 17:13:02 +08:00
|
|
|
{
|
|
|
|
uint64_t ERRORCount;
|
|
|
|
ErrorType_e ERRORType;
|
|
|
|
} PID_ErrorHandler_t;
|
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
/* PID结构体 */
|
|
|
|
typedef struct
|
2022-10-20 17:13:02 +08:00
|
|
|
{
|
2022-11-01 22:32:15 +08:00
|
|
|
//---------------------------------- init config block
|
2022-10-31 20:20:16 +08:00
|
|
|
// config parameter
|
2022-10-20 17:13:02 +08:00
|
|
|
float Kp;
|
|
|
|
float Ki;
|
|
|
|
float Kd;
|
2022-10-31 20:20:16 +08:00
|
|
|
float MaxOut;
|
|
|
|
float DeadBand;
|
2023-02-16 15:46:04 +08:00
|
|
|
|
2023-02-20 18:34:23 +08:00
|
|
|
// improve parameter
|
2023-02-16 15:46:04 +08:00
|
|
|
PID_Improvement_e Improve;
|
2023-03-23 18:57:54 +08:00
|
|
|
float IntegralLimit; // 积分限幅
|
|
|
|
float CoefA; // 变速积分 For Changing Integral
|
|
|
|
float CoefB; // 变速积分 ITerm = Err*((A-abs(err)+B)/A) when B<|err|<A+B
|
|
|
|
float Output_LPF_RC; // 输出滤波器 RC = 1/omegac
|
2023-02-20 18:34:23 +08:00
|
|
|
float Derivative_LPF_RC; // 微分滤波器系数
|
2022-10-31 20:20:16 +08:00
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
//-----------------------------------
|
2022-10-31 20:20:16 +08:00
|
|
|
// for calculating
|
2022-10-20 17:13:02 +08:00
|
|
|
float Measure;
|
|
|
|
float Last_Measure;
|
|
|
|
float Err;
|
|
|
|
float Last_Err;
|
|
|
|
float Last_ITerm;
|
|
|
|
|
|
|
|
float Pout;
|
|
|
|
float Iout;
|
|
|
|
float Dout;
|
|
|
|
float ITerm;
|
|
|
|
|
|
|
|
float Output;
|
|
|
|
float Last_Output;
|
|
|
|
float Last_Dout;
|
|
|
|
|
2022-10-31 20:20:16 +08:00
|
|
|
float Ref;
|
2022-10-20 17:13:02 +08:00
|
|
|
|
|
|
|
uint32_t DWT_CNT;
|
|
|
|
float dt;
|
|
|
|
|
|
|
|
PID_ErrorHandler_t ERRORHandler;
|
2022-12-05 21:01:26 +08:00
|
|
|
} PIDInstance;
|
2022-10-20 17:13:02 +08:00
|
|
|
|
2022-10-31 20:20:16 +08:00
|
|
|
/* 用于PID初始化的结构体*/
|
2023-02-16 15:46:04 +08:00
|
|
|
typedef struct // config parameter
|
2022-10-20 17:13:02 +08:00
|
|
|
{
|
2023-02-16 15:46:04 +08:00
|
|
|
// basic parameter
|
2022-10-31 20:20:16 +08:00
|
|
|
float Kp;
|
|
|
|
float Ki;
|
|
|
|
float Kd;
|
2023-02-16 15:46:04 +08:00
|
|
|
float MaxOut; // 输出限幅
|
|
|
|
float DeadBand; // 死区
|
2022-10-20 17:13:02 +08:00
|
|
|
|
2023-02-16 15:46:04 +08:00
|
|
|
// improve parameter
|
|
|
|
PID_Improvement_e Improve;
|
2022-11-11 21:00:02 +08:00
|
|
|
float IntegralLimit; // 积分限幅
|
2023-02-20 18:34:23 +08:00
|
|
|
float CoefA; // AB为变速积分参数,变速积分实际上就引入了积分分离
|
2022-11-01 22:32:15 +08:00
|
|
|
float CoefB; // ITerm = Err*((A-abs(err)+B)/A) when B<|err|<A+B
|
2022-10-31 20:20:16 +08:00
|
|
|
float Output_LPF_RC; // RC = 1/omegac
|
|
|
|
float Derivative_LPF_RC;
|
2023-01-02 23:20:35 +08:00
|
|
|
} PID_Init_Config_s;
|
2022-10-20 17:13:02 +08:00
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
/**
|
|
|
|
* @brief 初始化PID实例
|
2023-02-16 15:46:04 +08:00
|
|
|
* @todo 待修改为统一的PIDRegister风格
|
2022-11-01 22:32:15 +08:00
|
|
|
* @param pid PID实例指针
|
|
|
|
* @param config PID初始化配置
|
|
|
|
*/
|
2023-02-16 15:46:04 +08:00
|
|
|
void PIDInit(PIDInstance *pid, PID_Init_Config_s *config);
|
2022-10-20 17:13:02 +08:00
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
/**
|
|
|
|
* @brief 计算PID输出
|
|
|
|
*
|
|
|
|
* @param pid PID实例指针
|
|
|
|
* @param measure 反馈值
|
|
|
|
* @param ref 设定值
|
|
|
|
* @return float PID计算输出
|
|
|
|
*/
|
2023-02-16 15:46:04 +08:00
|
|
|
float PIDCalculate(PIDInstance *pid, float measure, float ref);
|
2022-10-20 17:13:02 +08:00
|
|
|
|
2022-11-01 22:32:15 +08:00
|
|
|
#endif
|