打印
[LKS32 软件]

c语言实现PID算法

[复制链接]
153|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
macpherson|  楼主 | 2024-7-21 22:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


#include <stdio.h>
struct _pid
{
       float  error;        //差值
       float error_last;    //上一次差值
       float integral;      //差值的和
}

float Kp =0.01;    //比例系数,  提升速度
float Ki = 1;      //积分系数,稳定输出
float Kd =0.01;    //微分系数,减小振荡

float cur_speed = 0;        //当前的速度
float level = 0;            //用于输出pwm
float set_speed = 50;       //设定的速度

struct _pid pid;
void pid_init()
{
     pid.error = 0;
     pid.error_last = 0;
     pid.integral = 0;
}

void pid_control()
{
     pid.error=set_speed-cur_speed;
     pid.integral +=pid.error;

     level =Kp*pid.error+Ki*pid.integral+Kd*(pid.error-pid.error_last);
     pid.error=pid.error_lasst;

}
int main()
{
     pid_init();
     for(i=0;i<10;i++)
     {
           pid_control();
           printf("当前速度%f  等级 %f\n",cur_speed,level);

     }
     return 0 ;

}


使用特权

评论回复
沙发
田舍郎| | 2024-7-21 23:37 | 只看该作者
很好,参数要根据不同应用来调参

使用特权

评论回复
板凳
shenxiaolin| | 2024-7-22 14:32 | 只看该作者
感谢分享!很实用的资料

使用特权

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

本版积分规则

36

主题

1504

帖子

1

粉丝