2024-12-28 00:38:42 +08:00
|
|
|
#ifndef _PID_H_
|
|
|
|
#define _PID_H_
|
2024-12-27 10:34:06 +08:00
|
|
|
|
2024-12-28 00:38:42 +08:00
|
|
|
#include <Arduino.h>
|
2024-12-27 10:34:06 +08:00
|
|
|
#include "consts.h"
|
|
|
|
|
|
|
|
struct PIDConfig
|
|
|
|
{
|
|
|
|
float Kp;
|
|
|
|
float Ki;
|
|
|
|
float Kd;
|
2024-12-28 00:38:42 +08:00
|
|
|
float outputMax; // 输出限幅
|
|
|
|
float outputMin; // 输出限幅
|
|
|
|
float integralMax; // 积分限幅
|
2024-12-27 10:34:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PID
|
|
|
|
{
|
|
|
|
float error;
|
2024-12-28 00:38:42 +08:00
|
|
|
float last_error;
|
2024-12-27 10:34:06 +08:00
|
|
|
float integral;
|
|
|
|
float derivative;
|
2024-12-28 00:38:42 +08:00
|
|
|
unsigned long lastTime; // 上次更新时间
|
2024-12-27 10:34:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class PIDController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PID pid;
|
2024-12-28 00:38:42 +08:00
|
|
|
|
|
|
|
static PIDConfig pidConfig;
|
|
|
|
static void reset(); // 重置PID状态
|
|
|
|
static void setConfig(float Kp, float Ki, float Kd,
|
|
|
|
float outputMax = 255, float outputMin = -255,
|
|
|
|
float integralMax = 1000);
|
2024-12-27 10:34:06 +08:00
|
|
|
static float update(float target, float current);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|