工作需要,搜遍网络,但没有现成的,故自行解决,秉承开源之原力,
有不足之处,诸君海涵
- --[[结构体:]]
- local pid_struct = {pv=0.0,sp=0.0,integral=0,
- pgain=1.5, igain=0.1,dgain=0.0,
- deadband=1.0,last_error=0.0,
- max_value=50.0,min_value=30.0,
- stable_time=1, modify_time=current};
- --[[计算PID函数]]
- function pid_calc(pid)
- local err;
- local pterm;
- local dterm;
- local result;
- local ferror;
- err = (pid.sp) - (pid.pv);
-
-
- if (math.abs(err) > pid.deadband) then
- ferror = err;
-
- pterm = pid.pgain * ferror;
-
- if (pterm > 100 or pterm < -100) then
- pid.integral = 0.0;
- else
-
- pid.integral = pid.integral + pid.igain * ferror;
-
- if (pid.integral > 3.0) then
- pid.integral = 3.0;
- end
- if (pid.integral < -3.0) then
- pid.integral = -3.0;
- end
- end
-
- dterm = (err - pid.last_error) * pid.dgain;
- result = pterm + pid.integral + dterm;
- else
- --进入死区
- result = pid.integral;
- end
-
- result = pid.sp + result;
-
- --极值限制
- if(result > pid.max_value) then
- result = pid.max_value;
- end
- if(result < pid.min_value) then
- result = pid.min_value;
- end
-
- return result;
- end
阅读(3300) | 评论(0) | 转发(0) |