Arduino PID Library

by Brett Beauregard,contact: br3ttb@gmail.com

What Is PID?   PID是什么

From  Wikipedia: "A PID controller calculates an 'error' value as the difference between a measured [Input] and a desired setpoint. The controller attempts to minimize the error by adjusting [an Output]."

So, you tell the PID what to measure (the "Input",) Where you want that measurement to be (the "Setpoint",) and the variable to adjust that can make that happen (the "Output".) The PID then adjusts the output trying to make the input equal the setpoint.

For reference, in a car, the Input, Setpoint, and Output would be the speed, desired speed, and gas pedal(踏板) angle respectively.

Functions

PID()

Description 建立一个PID控制器

Creates a PID controller linked to the specified Input, Output, and Setpoint. The PID algorithm is in parallel form.

Syntax

PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction)

PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, Direction)

Parameters:

Input: The variable we're trying to control (double)

Output: The variable that will be adjusted by the pid (double)

Setpoint: The value we want to Input to maintain (double)

Kp, Ki, Kd: Tuning Parameters. these affect how the pid will chage the output. (double>=0)

Direction: Either DIRECT or REVERSE. determines which direction the output will move when faced with a given error. DIRECT is most common.

POn: Either P_ON_E (Default) or P_ON_M. Allows Proportional on Measurement to be specified.

Returns:None

Compute()

Description  在loop()中做PID运算

Contains the pid algorithm. it should be called once every loop(). Most of the time it will just return without doing anything. At a frequency specified by SetSampleTime it will calculate a new Output.

Syntax:

Compute()

Parameters:None

Returns:

True: when the output is computed

False: when nothing has been done

SetMode()

Description  工作模式设定(自动/手动)

Specifies whether the PID should be on (Automatic) or off (Manual.) The PID defaults to the off position when created.

Syntax:

SetMode(mode)

Parameters:

mode: AUTOMATIC or MANUAL

Returns:None

 

SetOutputLimits()

Description:输出限幅

The PID controller is designed to vary its output within a given range. By default this range is 0-255: the arduino PWM range. There's no use sending 300, 400, or 500 to the PWM. Depending on the application though, a different range may be desired.

Syntax:

SetOutputLimits(min, max)

Parameters:

min: Low end of the range. must be < max (double)

max: High end of the range. must be > min (double)

Returns:None

SetTunings()

Description:PID参数实时调整

Tuning parameters (or "Tunings") dictate(决定) the dynamic behavior of the PID. Will it oscillate or not? Will it be fast or slow? An initial set of Tunings is specified when the PID is created. For most users this will be enough. There are times however, tunings need to be changed during run-time. At those times this function can be called.

Syntax:

SetTunings(Kp, Ki, Kd)

SetTunings(Kp, Ki, Kd, POn)

Parameters:

Kp: Determines how aggressively the PID reacts to the current amount of error (Proportional) (double >=0)

Ki: Determines how aggressively the PID reacts to error over time (Integral) (double>=0)

Kd: Determines how aggressively the PID reacts to the change in error (Derivative) (double>=0)

POn: Either P_ON_E (Default) or P_ON_M. Allows Proportional on Measurement to be specified.

Returns:None

SetSampleTime()

Decription:采样时间设定(毫秒)

Determines how often the PID algorithm evaluates. The default is 200mS. For robotics applications this may need to be faster, but for the most part 200mS is plenty fast.

Syntax:

SetSampleTime(SampleTime)

Parameters:

SampleTime:  How often, in milliseconds, the PID will be evaluated. (int>0)

Returns:None

SetControllerDirection()

Description: 控制方向设定(正作用/逆作用)

If my Input is above Setpoint, should the output be increased or decreased? Depending on what the PID is connected to, either could be true. With a car, the output should be decreased to bring the speed down. For a refrigerator, the opposite is true. The output (cooling) needs to be increased to bring my temperature down.

This function specifies which type of process the PID is connected to. This information is also specified when the PID constructed. Since it's unlikely that the process will switch from direct to reverse, it's equally unlikely that anyone will actually use this function.

Syntax:

SetControllerDirection(Direction);

Parameters:

DirectionDIRECT (like a car,输出减弱) or REVERSE (like a refrigerator,输出增强)

Returns:None

Display Functions

Decription:PID 参数询问显示

These functions query(询问) the PID internals to get current values. These are useful for display purposes.

GetKp()

GetKi()

GetKd()

GetMode()

GetDirection()

Syntax:

GetKp()

GetKi()

GetKd()

GetMode()

GetDirection()

Parameters:None

Returns:The corresponding internal value

Examples  PID控制举例

 Basic   模拟量输入控制模拟量输出

 1 /********************************************************
2 * PID Basic Example
3 * Reading analog input 0 to control analog PWM output 3
4 ********************************************************/
5 #include <PID_v1.h>
6 //Define Variables we'll be connecting to double Setpoint, Input, Output;
7 //Specify the links and initial tuning parameters
8 PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
9
10 void setup()
11 {
12 Input = analogRead(0); //initialize the variables we're linked to
13 Setpoint = 100;
14 myPID.SetMode(AUTOMATIC); //turn the PID on
15 }
16
17 void loop()
18 {
19 Input = analogRead(0);
20 myPID.Compute();
21 analogWrite(3,Output);
22 }

AdaptiveTunings  自动调整PID参数

 1 /********************************************************
2 * PID Adaptive Tuning Example
3 * One of the benefits of the PID library is that you can
4 * change the tuning parameters at any time. this can be
5 * helpful if we want the controller to be aggressive(进取的) at some
6 * times, and conservative(保守的) at others. in the example below
7 * we set the controller to use Conservative Tuning Parameters
8 * when we're near setpoint and more aggressive Tuning
9 * Parameters when we're farther away.
10 ********************************************************/
11 #include <PID_v1.h>
12 //Define Variables we'll be connecting to double Setpoint, Input, Output;
13 //Define the aggressive and conservative Tuning Parameters
14 double aggKp=4, aggKi=0.2, aggKd=1;
15 double consKp=1, consKi=0.05, consKd=0.25;
16 //Specify the links and initial tuning parameters
17 PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
18
19 void setup()
20 {
21 Input = analogRead(0); //initialize the variables we're linked to
22 Setpoint = 100;
23 myPID.SetMode(AUTOMATIC); //turn the PID on
24 }
25
26 void loop()
27 {
28 Input = analogRead(0);
29 double gap = abs(Setpoint-Input); //distance away from setpoint
30 if(gap<10)
31 {
32 myPID.SetTunings(consKp, consKi, consKd);
33 //we're close to setpoint, use conservative tuning parameters
34 }
35 Else
36 {
37 myPID.SetTunings(aggKp, aggKi, aggKd);
38 //we're far from setpoint, use aggressive tuning parameters
39 }
40 myPID.Compute();
41 analogWrite(3,Output);
42 }

Arduino PID Library的更多相关文章

  1. Brett Beauregard大神的Arduino PID算法

    大神的全部PID http://brettbeauregard.com/blog/category/pid/ Improving the Beginner’s PID – Introduction I ...

  2. 手把手教你看懂并理解Arduino PID控制库——引子

    介绍 本文主要依托于Brett Beauregard大神针对Arduino平台撰写的PID控制库Arduino PID Library及其对应的帮助博客Improving the Beginner’s ...

  3. 【技术】Arduino PID自整定库

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  4. Arduino LiquidCrystal Library Bug Report #174181

    Arduino LiquidCrystal Character LCD Driver Library BUG Report #174181 by Conmajia Effected Devices H ...

  5. 使用Arduino Wire Library读取温湿度传感器AM2321

    AM2321是采用I2C总线或单总线通讯的国产温湿度传感器.在AM2321手册中,当采用I2C通讯时,手册指定了多处需要主机等待的时间间隔,包括: (1)唤醒传感器时,从机不回复ACK,但主机主要等待 ...

  6. PID控制器(比例-积分-微分控制器)- I

    形象解释PID算法 小明接到这样一个任务: 有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水. 小明接到任务后就一直守在水 ...

  7. Project 03- STM32F4xx PID controller

    Project 03- STM32F4xx PID controller CMSIS files from ARM provides ARM Math functions. There are als ...

  8. Arduino I2C + 温湿度传感器AM2321

    (2015.5.17:本日志的内容有所更新,参见<使用Arduino Wire Library读取温湿度传感器AM2321>.) AM2321是广州奥松电子生产的数字式温湿度传感器.虽是国 ...

  9. 用Arduino+OSC建立一个iPad铁路王国巡视机

    翻译自:http://blog.mydream.com.hk/howto/build-up-a-ipad-plarail-patrol-with-arduino-osc 简单介绍 这个教程告诉你怎样建 ...

随机推荐

  1. SpringCloud Alibaba Nacos 服务注册

    业务服务接入Nacos服务治理中心 启动Nacos访问地址为:http://101.200.201.195:8848/nacos/ 创建bom工程用于管理依赖(下方附加源码地址) 准备工作完成后开始接 ...

  2. 痞子衡嵌入式:导致串行NOR Flash在i.MXRT下无法正常下载/启动的常见因素之SFDP

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是导致串行NOR Flash在i.MXRT下无法正常下载/启动的常见因素之SFDP. i.MXRT系列MCU发布已两年多了,基于i.MXR ...

  3. 如何写好转正答辩PPT

    如何写好一个转正答辩报告 几个月前,我刚经历了转正答辩,这是我职业生涯中转正答辩表现最好的一次.在我之前经历的几家公司中,转正的流程各不相同,我将它们为主动式和被动式.这里的被动式指的是:公司是主动方 ...

  4. Cubmap

    视差 Cubmap https://chengkehan.github.io/LocalCubmap.html http://www.manew.com/thread-93923-1-1.html h ...

  5. 单元测试框架 python

    1.为什么要做单元测试 单元测试更细致.更有针对性 单元测试能测试到很多系统测试无法达到的测试 单元测试是在编码中做的测试,发现问题方便修改,而系统测试的问题修改成本可能变高 单元测试是自动化测试 2 ...

  6. codewars sum of pairs

    Sum of Pairs Given a list of integers and a single sum value, return the first two values (parse fro ...

  7. vue问题整理

    生命周期面试题 1.什么是 vue 生命周期 vue 实例从创建到销毁的过程就是生命周期. 也就是从开始创建.初始化数据.编译模板.挂在 dom -> 渲染.更新 -> 渲染.卸载等一系列 ...

  8. 干货 | 45张图庖丁解牛18种Queue,你知道几种?

    在讲<21张图讲解集合的线程不安全>那一篇,我留了一个彩蛋,就是Queue(队列)还没有讲,这次我们重点来看看Java中的Queue家族,总共涉及到18种Queue.这篇恐怕是市面上最全最 ...

  9. 没事也来配一个logback

    工程下载:https://files.cnblogs.com/files/xiandedanteng/logbackCfg20200115.zip 首先创建一个maven项目,pom.xml如下书写: ...

  10. 关于h5游戏开发,你想了解的一切都在这儿!

    ​2020年,受疫情影响,线下产业红利褪去,线上迎来的新一轮的高峰.众多商家纷纷抓住了转型时机,开启了流量争夺战.H5游戏定制无疑是今年引流的大热门.如何开发一款有趣.有爆点.用户爱买单的好游戏呢? ...