pid代码

时间:2022-07-13 10:15:15 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
下面提供一组最简单的pid伪代码和C代码

previous_error = 0 integral = 0 start:

error = setpoint_speed - actual_speed integral = integral + (error*dt)

derivative = (error - previous_error)/dt

output = (Kp*error) + (Ki*integral) + (Kd*derivative) previous_error = error wait(dt) goto start C

typedef struct {

double dState; // Last position input double iState; // Integrator state double iMax, iMin;

// Maximum and minimum allowable integrator state


double iGain, // integral gain pGain, // proportional gain dGain; // derivative gain } SPid;

double UpdatePID(SPid * pid, double error, double position) {

double pTerm, dTerm, iTerm;

pTerm = pid->pGain * error; // calculate the proportional term

// calculate the integral state with appropriate limiting pid->iState += error;

if (pid->iState > pid->iMax) pid->iState = pid->iMax; else if (pid->iState <

pid->iMin) pid->iState = pid->iMin;

iTerm = pid->iGain * iState; // calculate the integral term dTerm = pid->dGain * (position - pid->dState); pid->dState = position;

return pTerm + iTerm - dTerm; }


本文来源:https://www.wddqw.com/doc/bd6aa90a581b6bd97f19ea81.html