打印
[应用方案]

编写PID控制算法

[复制链接]
180|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
突然下起雨|  楼主 | 2025-3-27 14:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
实现PID控制
编写PID控制算法

c
复制
编辑
typedef struct {
    float Kp;
    float Ki;
    float Kd;
    float prev_error;
    float integral;
} PIDController;

PIDController pid = {1.5, 0.05, 0.5, 0, 0}; // 设定PID参数

int16_t PID_Compute(int16_t target, int16_t actual) {
    float error = target - actual;
    pid.integral += error;
    float derivative = error - pid.prev_error;
    pid.prev_error = error;

    int16_t output = (int16_t)(pid.Kp * error + pid.Ki * pid.integral + pid.Kd * derivative);

    // 限制PWM范围
    if (output > PWM_DUTY_MAX) output = PWM_DUTY_MAX;
    if (output < 0) output = 0;

    return output;
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

384

帖子

1

粉丝