PID算法(基于arduino开发板)

本文为作者学习笔记
欢迎交流讨论,喜欢的话点个赞吧

欢迎去看我的主页: NicholasYe’s Hompage.

PID


PID方程

PID标准方程:
在这里插入图片描述在这里插入图片描述


  • P:
    计算目标值与当前值的差值

Term P is proportional to the current value of the SP − PV error {\displaystyle e(t)}e(t). For example, if the error is large and positive, the control output will be proportionately large and positive, taking into account the gain factor “K”. Using proportional control alone will result in an error between the setpoint and the actual process value because it requires an error to generate the proportional response. If there is no error, there is no corrective response.


  • I:
    积分过去的差值

Term I accounts for past values of the SP − PV error and integrates them over time to produce the I term. For example, if there is a residual SP − PV error after the application of proportional control, the integral term seeks to eliminate the residual error by adding a control effect due to the historic cumulative value of the error. When the error is eliminated, the integral term will cease to grow. This will result in the proportional effect diminishing as the error decreases, but this is compensated for by the growing integral effect.


  • D:
    微分未来的差值

Term D is a best estimate of the future trend of the SP − PV error, based on its current rate of change. It is sometimes called “anticipatory control”, as it is effectively seeking to reduce the effect of the SP − PV error by exerting a control influence generated by the rate of error change. The more rapid the change, the greater the controlling or damping effect.

PID三者讲解推荐看:B站


PID算法

注:本文的算法均取自于arduino的PID库中部分代码,可能有部分函数为arduino独有。但PID算法的核心经过编程后可用于任意平台。

标准的PID控制器:

/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastErr;
double kp, ki, kd;
void Compute()
{
   /*How long since we last calculated*/
   unsigned long now = millis();
   //millis(): Returns the number of milliseconds passed since the Arduino board began running the current program.
   double timeChange = (double)(now - lastTime);
  
   /*Compute all the working error variables*/
   double error = Setpoint - Input;//误差值
   errSum += (error * timeChange);//误差的积分
   double dErr = (error - lastErr) / timeChange;//误差的微分
  
   /*Compute PID Output*/
   Output = kp * error + ki * errSum + kd * dErr;
  
   /*Remember some variables for next time*/
   lastErr = error;
   lastTime = now;
}
  
void SetTunings(double Kp, double Ki, double Kd)
{
   kp = Kp;
   ki = Ki;
   kd = Kd;
}

此外,可在函数库里找到PID计算函数

bool PID::Compute()
{
   if(!inAuto) return false;
   unsigned long now = millis();
   unsigned long timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
      double input = *myInput;
      double error = *mySetpoint - input;
      double dInput = (input - lastInput);
      outputSum+= (ki * error);

      /*Add Proportional on Measurement, if P_ON_M is specified*/
      if(!pOnE) outputSum-= kp * dInput;

      if(outputSum > outMax) outputSum= outMax;
      else if(outputSum < outMin) outputSum= outMin;

      /*Add Proportional on Error, if P_ON_E is specified*/
	   double output;
      if(pOnE) output = kp * error;
      else output = 0;

      /*Compute Rest of PID Output*/
      output += outputSum - kd * dInput;

	    if(output > outMax) output = outMax;
      else if(output < outMin) output = outMin;
	    *myOutput = output;

      /*Remember some variables for next time*/
      lastInput = input;
      lastTime = now;
	    return true;
   }
   else return false;
}

关于此代码的链接:


请在转载文章过程中明确标注文章出处!尊重原创,尊重知识产权,谢谢!

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Arduino上的PID算法是一种经典的控制算法,被广泛应用于各种场合,如智能小车、扫地机器人和工业机械臂等。PID算法的计算公式可以使用增量法来表示,其中包括比例项(KP)、积分项(KI)和微分项(KD)。具体的计算公式为:OUT = KP*\[E(t)-E(t-1)\] + KI*E(t) + KD*\[E(t)-2*E(t-1)+E(t-2)\],其中E(t)表示当前的误差,E(t-1)表示上一次的误差,E(t-2)表示上上次的误差。在Arduino上实现PID算法的代码可以使用PID来简化操作,该可以通过Library Manager进行安装。使用PID可以更方便地进行PID控制,具体的使用方法可以参考的文档和示例。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [小白学习日记-----arduino如何使用PID控制算法](https://blog.csdn.net/qq_43193591/article/details/104287293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Arduino智能小车(三):PID算法简介](https://blog.csdn.net/weixin_44845947/article/details/113084477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值