常驻嘉宾
- 积分
- 3035
- 威望
- 1039
- 贡献
- 1528
- 兑换币
- 428
- 注册时间
- 2012-12-6
- 在线时间
- 234 小时
|
本帖最后由 黄美纯 于 2013-7-11 21:25 编辑
我电机的PWM范围是0-100,0最大,100停止。码盘测速脉冲几十到几百左右,5ms采集一次。这个PID 函数有问题吗?
- uint8 PID_Speed(uint8 RealSpeed,uint8 SetSpeed,float Kp,float Ki,float Kd)
- {
- int8 ei;
- static int8 ei_1,ei_2;
- int8 PWM_Speed;
- static uint8 PrePWM_Speed;
- ei = RealSpeed-SetSpeed;
- PWM_Speed = PrePWM_Speed+(Kp*ei-Ki*ei_1+Kd*ei_2);
- if(PWM_Speed<0)
- PWM_Speed = 0;
- else
- {
- if(PWM_Speed>100)
- PWM_Speed = 100;
- }
- PrePWM_Speed = PWM_Speed;
- ei_2 = ei_1;
- ei_1 = ei;
- return PWM_Speed;
- }
复制代码
|
|