2025-10-30 20:20:14 +08:00
|
|
|
#include "pid.h"
|
2025-11-06 02:07:15 +08:00
|
|
|
#define _constrain(amt, low, high) \
|
|
|
|
|
((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
|
|
|
|
|
#include "Can.h"
|
2026-02-02 02:12:14 +08:00
|
|
|
#include "config.h"
|
2026-01-24 01:50:39 +08:00
|
|
|
#include <cmath>
|
2025-10-30 20:20:14 +08:00
|
|
|
|
2026-02-02 02:12:14 +08:00
|
|
|
extern CAN_transmiter CAN0_transmiter;
|
2025-10-30 20:20:14 +08:00
|
|
|
PIDController::PIDController(float P, float I, float D, float ramp, float limit)
|
2025-11-06 02:07:15 +08:00
|
|
|
: P(P), I(I), D(D), output_ramp(ramp) // PID控制器加速度限幅
|
|
|
|
|
,
|
|
|
|
|
limit(limit) // PID控制器输出限幅
|
|
|
|
|
,
|
2025-11-10 02:08:03 +08:00
|
|
|
error_prev(0.0f), output_prev(0.0f), integral_prev(0.0f) {}
|
2025-10-30 20:20:14 +08:00
|
|
|
|
|
|
|
|
// PID 控制器函数
|
2025-11-06 02:07:15 +08:00
|
|
|
float PIDController::operator()(float error) {
|
|
|
|
|
// P环
|
|
|
|
|
float proportional = P * error;
|
2026-01-20 01:28:52 +08:00
|
|
|
|
2025-11-06 02:07:15 +08:00
|
|
|
// Tustin 散点积分(I环)
|
2025-11-08 21:04:35 +08:00
|
|
|
// 添加积分限幅,防止积分饱和
|
2026-01-20 01:28:52 +08:00
|
|
|
float integral =
|
|
|
|
|
integral_prev + I * TIME_SLICE * 0.5f * (error + error_prev);
|
2025-11-06 02:07:15 +08:00
|
|
|
integral = _constrain(integral, -limit, limit);
|
2026-01-20 01:28:52 +08:00
|
|
|
|
2025-11-06 02:07:15 +08:00
|
|
|
// D环(微分环节)
|
2025-11-08 02:15:14 +08:00
|
|
|
float derivative = D * (error - error_prev) / TIME_SLICE;
|
2026-01-20 01:28:52 +08:00
|
|
|
|
2025-11-06 02:07:15 +08:00
|
|
|
// 将P,I,D三环的计算值加起来
|
|
|
|
|
float output = proportional + integral + derivative;
|
|
|
|
|
output = _constrain(output, -limit, limit);
|
2025-10-30 20:20:14 +08:00
|
|
|
|
2025-11-08 21:04:35 +08:00
|
|
|
// 输出斜坡限制
|
2026-01-20 01:28:52 +08:00
|
|
|
// 简化的斜坡限制(单行逻辑)
|
|
|
|
|
if (output_ramp > 0) {
|
|
|
|
|
float max_delta = output_ramp * TIME_SLICE;
|
|
|
|
|
output = output_prev +
|
|
|
|
|
fmaxf(-max_delta, fminf(max_delta, output - output_prev));
|
|
|
|
|
}
|
2025-11-06 02:07:15 +08:00
|
|
|
// 保存值(为了下一次循环)
|
|
|
|
|
integral_prev = integral;
|
|
|
|
|
output_prev = output;
|
|
|
|
|
error_prev = error;
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 设置PID参数
|
|
|
|
|
*
|
|
|
|
|
* @param P: P参数
|
|
|
|
|
* @param I: I参数
|
|
|
|
|
* @param D: D参数
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::set_PID(float P, float I, float D) {
|
2025-11-08 21:04:35 +08:00
|
|
|
this->P = P;
|
|
|
|
|
this->I = I;
|
|
|
|
|
this->D = D;
|
2026-01-20 01:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 设置P参数
|
|
|
|
|
*
|
|
|
|
|
* @param P: P参数
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-03-09 01:48:12 +08:00
|
|
|
void PIDController::set_P(float pp) {
|
|
|
|
|
this->P = pp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 设置输出限幅
|
|
|
|
|
*
|
|
|
|
|
* @param limit: 输出限幅
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::set_limit(float limit) { this->limit = limit; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 重置PID控制器状态
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::reset() {
|
|
|
|
|
error_prev = 0.0f;
|
|
|
|
|
output_prev = 0.0f;
|
|
|
|
|
integral_prev = 0.0f;
|
2026-05-17 23:40:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 仅清除PID积分项,保留输出斜坡和误差历史
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
void PIDController::reset_integral() { integral_prev = 0.0f; }
|