仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
代码太多放不下,请到附件下载后浏览,谢谢!!!
单片机源程序如下:
- #include "pid.h"
- #include "huoer.h"
- extern times_A,times;
- float PID_M_Kp =0.1382, PID_M_Ki = 0.001, PID_M_Kd =0.01; //定义 Kp、Ki、Kd 三个参数
- float pid_Sv;
- u16 PID_m_add=0,pid_pwm;
- float PID_current_error; //当前差值
- float PID_last_error; //上次差值
- float PID_prev_error; //上上次差值
- float integrality_date=0; //积分数据
- void Motor(u16 std_speed)
- {
- float P,D;//,I;//,I,D; //定义局部变量
- std_speed+=120;
- PID_prev_error=PID_last_error; //更新每次的差值
- PID_last_error=PID_current_error; //更新每次的差值
- PID_current_error=std_speed-times; //更新每次的差值
-
- integrality_date=integrality_date+PID_current_error;
- P=PID_M_Kp*PID_current_error; //比例 P 输出公式
- // I=PID_M_Ki*integrality_date; //积分 I 输出公式
- D=PID_M_Kd*(PID_current_error-PID_last_error); //微分 D 输出公式
-
- PID_m_add=P+D;//+I;//+I+D+PID_m_add; //电机的 PID 增量值输
-
- output_pwm(PID_m_add);
- }
- void output_pwm(u16 output)
- {
- if(output>200)output=200; //限制电机的最大速度
- if(output<1)output=1;
- pid_pwm = output;
- }
- /*/////////////////
- 60 360
- 420 0
-
- /////////////////*/
复制代码
所有资料51hei提供下载:
1602霍尔编码电机.zip
(85.26 KB, 下载次数: 213)
|