QuickPID 1
QuickPID is an updated implementation of the Arduino PID library with additional features for PID control. By default, this implementation closely follows the method of processing the p,i,d terms as in the PID_v1 library except for using a more advanced anti-windup mode. Integral anti-windup can be based on conditionally using PI terms to provide some integral correction, prevent deep saturation and reduce overshoot. Anti-windup can also be based on clamping only, or it can be turned completely off. Also, the proportional term can be based on error, measurement, or both. The derivative term can be based on error or measurement. PID controller modes include timer, which allows external timer or ISR timing control.
QuickPID 是 Arduino PID 库的更新实现,具有 PID 控制的额外功能。 默认情况下,除了使用更先进的防倒转模式外,该实现与 PID_v1 库中处理 p、i、d 项的方法非常接近。 积分防倒转可以基于有条件地使用 PI 项来提供一些积分修正、防止深度饱和和减少过冲。 防倒转也可以仅基于箝位,或完全关闭。 此外,比例项可以基于误差、测量或两者。 导数项可以基于误差或测量。 PID 控制器模式包括定时器,允许外部定时器或 ISR 定时控制。
Note: You can use this library in esp-idf tool to program esp32 by cloning this repo into your components folder, then clean the build and rebuild.
Features
Development began with a fork of the Arduino PID Library. Modifications and new features have been added as described in the releases.
开发工作从 Arduino PID 库的分叉开始。 修改和新功能已按版本说明添加。
New feature Summary
- New functions added:
SetProportionalMode
,SetDerivativeMode
andSetAntiWindupMode
新增函数 SetProportionalMode、SetDerivativeMode 和 SetAntiWindupMode timer
mode for calling PID compute by an external timer function or ISR.。定时器模式,用于通过外部定时器函数或 ISR 调用 PID 计算。- Proportional on error
pOnError
, measurementpOnMeas
or bothpOnErrorMeas
options 根据误差 pOnError、测量值 pOnMeas 或同时根据 pOnErrorMeas 选项按比例计算 - Derivative on error
dOnError
and measurementdOnMeas
options。关于误差 dOnError 和测量 dOnMeas 选项的导数 - New PID Query Functions
GetPterm
,GetIterm
,GetDterm
,GetPmode
,GetDmode
andGetAwMode
。新 PID 查询函数 GetPterm、GetIterm、GetDterm、GetPmode、GetDmode 和 GetAwMode - New integral anti-windup options
iAwCondition
,iAwClamp
andiAwOff
。新的集成式防倒转选项 iAwCondition、iAwClamp 和 iAwOff
Functions
QuickPID_Constructor
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint, float Kp, float Ki, float Kd,
pMode pMode = pMode::pOnError, dMode dMode = dMode::dOnMeas,
iAwMode iAwMode = iAwMode::iAwCondition, Action action = Action::direct)
Input
,Output
, andSetpoint
are pointers to the variables holding these values.输入"、"输出 "和 "设定点 "是指向保存这些值的变量的指针。Kp
,Ki
, andKd
are the PID proportional, integral, and derivative gains.pMode
is the proportional mode parameter with options forpOnError
proportional on error (default),pOnMeas
proportional on measurement andpOnErrorMeas
which is 0.5pOnError
+ 0.5pOnMeas
. pMode 是比例模式参数,有以下选项:pOnError(与误差成比例)(默认值)、pOnMeas(与测量成比例)和 pOnErrorMeas(0.5* pOnError + 0.5* pOnMeas)。dMode
is the derivative mode parameter with options fordOnError
derivative on error,dOnMeas
derivative on measurement (default).。dMode 是导数模式参数,可选择 dOnError 误差导数、dOnMeas 测量导数(默认)。awMode
is the integral anti-windup parameter with an option foriAwCondition
(default) that is based on PI terms to provide some integral correction, prevent deep saturation and reduce overshoot. TheiAwClamp
option clamps the summation of the pmTerm and iTerm. TheiAwOff
option turns off all anti-windup.。awMode 是积分防逆风参数,带有 iAwCondition(默认)选项,基于 PI 项提供一定的积分校正,防止深度饱和并减少过冲。 iAwClamp 选项可箝制 pmTerm 和 iTerm 的求和。 iAwOff 选项可关闭所有反卷积功能。Action
is the controller action parameter which hasdirect
(default) andreverse
options. These options set how the controller responds to a change in input.direct
action is used if the input moves in the same direction as the controller output (i.e. heating process).reverse
action is used if the input moves in the opposite direction as the controller output (i.e. cooling process).。Action 是控制器动作参数,有直接(默认)和反向两个选项。 如果输入与控制器输出的移动方向相同(如加热过程),则使用直接动作。如果输入与控制器输出的移动方向相反(如冷却过程),则使用反向动作。
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
float Kp, float Ki, float Kd, Action action)
This allows you to use Proportional on Error without explicitly saying so.这样就可以在不明确说明的情况下使用误差比例。
QuickPID::QuickPID(float *Input, float *Output, float *Setpoint)
This simplified version allows you to define the remaining parameters later via specific setter functions. By default, Kp, Ki, and Kd will be initialized to zero and should be later set via SetTunings
to relevant values.
这个简化版本允许您稍后通过特定的设置函数来定义其余参数。 默认情况下,Kp、Ki 和 Kd 将被初始化为零,随后应通过 SetTunings 设置为相关值。
Compute
bool QuickPID::Compute();
This function contains the PID algorithm and it should be called once every loop(). Most of the time it will just return false without doing anything. However, at a frequency specified by SetSampleTime
it will calculate a new Output and return true.
该函数包含 PID 算法,每个 loop() 都应调用一次。 大多数情况下,它只会返回 false,而不会做任何事情。 不过,在 SetSampleTime 指定的频率下,它会计算新的输出并返回 true。
Initialize
void QuickPID::Initialize();
Does all the things that need to happen to ensure a bump-less transfer from manual to automatic mode.
完成所有必要的工作,确保从手动模式到自动模式的无碰撞转换。
Reset
void QuickPID::Reset();
Clears pTerm
, iTerm
, dTerm
and outputSum
values.
PID Query Functions
These functions query the internal state of the PID.
float GetKp(); // proportional gain
float GetKi(); // integral gain
float GetKd(); // derivative gain
float GetPterm(); // proportional component of output
float GetIterm(); // integral component of output
float GetDterm(); // derivative component of output
uint8_t GetMode(); // manual (0), automatic (1) or timer (2)
uint8_t GetDirection(); // direct (0), reverse (1)
uint8_t GetPmode(); // pOnError (0), pOnMeas (1), pOnErrorMeas (2)
uint8_t GetDmode(); // dOnError (0), dOnMeas (1)
uint8_t GetAwMode(); // iAwCondition (0, iAwClamp (1), iAwOff (2)
PID Set Functions
These functions set the internal state of the PID.
void SetMode(Control mode); // Set PID mode to manual, automatic or timer
void SetOutputLimits(float Min, float Max); // Set and clamps the output to (0-255 by default)
void SetTunings(float Kp, float Ki, float Kd, // set pid tunings and all computational modes
pMode pMode, dMode dMode, iAwMode iAwMode);
void SetTunings(float Kp, float Ki, float Kd); // only set pid tunings, other pid modes are unchanged
void SetControllerDirection(Action Action); // Set controller action to direct (default) or reverse
void SetSampleTimeUs(uint32_t NewSampleTimeUs); // Set PID compute sample time, default = 100000 µs
void SetProportionalMode(pMode pMode); // Set pTerm based on error (default), measurement, or both
void SetDerivativeMode(dMode dMode); // Set the dTerm, based error or measurement (default).
void SetAntiWindupMode(iAwMode iAwMode); // Set iTerm anti-windup to iAwCondition, iAwClamp or iAwOff
void SetOutputSum(float sum); // sets the output summation value
Autotuner
Get sTune
A very fast autotuner capable of on-the-fly tunings and more.
sTune
GitHub - Dlloydev/sTune: Open loop PID autotuner using a novel s-curve inflection point test method.
This is an open loop PID autotuner using a novel s-curve inflection point test method. Tuning parameters are typically determined in about ½Tau on a first-order system with time delay. Full 5Tau testing and multiple serial output options are provided. See WiKi for test results and more.
这是一个开环 PID 自动调节器,采用了新颖的 s 曲线拐点测试方法。 调谐参数通常可在带时间延迟的一阶系统上以大约 ½Tau 的速度确定。 提供完整的 5Tau 测试和多个串行输出选项。 有关测试结果和更多信息,请参见 WiKi。
Inflection Point Tuning Method
This open-loop tuning method is used when the controller action is set to directIP
or reverseIP
. This method works best on processes that respond with an S-shaped reaction curve to a stepped output. Being an open-loop test, there is no setpoint and PID correction involved. The process gain, dead time, time constant and more is determined by doing a shortened step test that ends just after the inflection point has been reached. From here, the apparent maximum PV (input) is mathematically determined and the controller’s tuning parameters are calculated. Test duration is typically only ½Tau.
当控制器动作设置为 directIP
or reverseIP
时,可使用这种开环调整方法。 这种方法最适用于对阶跃输出呈 S 型反应曲线的过程。 由于是开环测试,因此不涉及设定点和 PID 修正。 过程增益、死区时间、时间常数等都是通过缩短阶跃测试来确定的,阶跃测试在达到拐点后结束。 在此基础上,用数学方法确定视在最大 PV(输入),并计算出控制器的调整参数。 测试持续时间通常只有 ½ Tau。
Inflection Point Discovery
Accurate determination of the inflection point was given high priority for this test method. To accomplish this, a circular buffer for the input readings is created that’s sized to 6% of the samples value. The buffer is used as a moving tangent line where the “head” of the tangent is based on the average of all readings in the buffer and the “tail” is based on the oldest instantaneous value in the buffer. The tangent line moves along the reaction curve one sample at a time. The slope of the tangent line is checked at every sample. When the sign of the change in slope changes (i.e. slope goes from increasing to decreasing or from decreasing to increasing), this is the point of inflection (where the tangent turns red here). After 1⁄16 samples has occurred with the new slope direction, then it’s known that the point of inflection has been reached. Final calculations are made and the test ends.
准确确定拐点是本测试方法的重中之重。 为此,我们为输入读数创建了一个圆形缓冲区,其大小为样本值的 6%。 缓冲区用作移动切线,切线的 "头部 "基于缓冲区内所有读数的平均值,"尾部 "基于缓冲区内最旧的瞬时值。 切线每次沿反应曲线移动一个样本。 每次取样都要检查切线的斜率。 当斜率变化的符号发生变化时(即斜率从增大变为减小或从减小变为增大),这就是拐点(此处切线变为红色)。 在新的斜率方向出现 1⁄16 个样本后,就可以知道已经到达拐点。 最后进行计算,测试结束。
S-shaped Step Response
Configuration
-
First, the PID controller is placed in
manual
mode. -
The tuner action is set to
directIP
orreverseIP
, then configure sTune: -
tuner.Configure(inputSpan, outputSpan, outputStart, outputStep, testTimeSec, settleTimeSec, samples);
-
Its expected that the user is already familiar with the controller and can make a rough estimation of what the system’s time constant would be. Use this estimate for the
testTimeSec
constant.
预计用户对控制器已经非常熟悉,可以大致估算出系统的时间常数。 使用此估计值作为 testTimeSec 常量。 -
The
samples
constant is used to define the maximum number of samples used to perform the test. To get an accurate representation of the curve, the suggested range is 200-500.
样本常数用于定义进行测试时使用的最大样本数。 为准确显示曲线,建议范围为 200-500。 -
settleTimeSec
is used to provide additional settling time prior to starting the test.
settleTimeSec 用于在开始测试前提供额外的沉淀时间。 -
inputSpan
andoutputSpan
represent the maximum operating range of input and output. Examples:
inputSpan 和 outputSpan 表示输入和输出的最大工作范围。 例如- If your input works with temp readings of 20C min and 220C max, then
inputSpan = 200;
If the output uses 8-bit PWM and your using its full range, thenoutputSpan = 255;
If the output is digital relay controlled by millis() with window period of 2000ms, thenoutputSpan = 2000;
如果输入的温度读数最小为 20 摄氏度,最大为 220 摄氏度,则 inputSpan = 200;如果输出使用 8 位 PWM,并且使用其全部范围,则 outputSpan = 255;如果输出是由 millis() 控制的数字继电器,窗口周期为 2000 毫秒,则 outputSpan = 2000;
- If your input works with temp readings of 20C min and 220C max, then
-
outputStart
is the initial control output value which is used for thesettleTimeSec
duration and sample 0.
outputStart 是初始控制输出值,用于 settleTimeSec 持续时间和采样 0。 -
outputStep
is the stepped output value used for sample 1 to test completion.
outputStep 是样本 1 测试完成时使用的步进输出值。 -
after test completion, the setup can be updated for the next test and
tuner.Configure()
can be called again.
测试完成后,可以为下一次测试更新设置,并再次调用 tuner.Configure()。
Test
-
The
outputStep
value is applied at sample 1 and inflection point discovery begins.
输出步长值应用于采样 1,并开始发现拐点。 -
Dead time is determined when the averaged input has increased (or decreased) beyond one resolution value from the starting instantaneous input value.
当平均输入值的增加(或减少)超过起始瞬时输入值的一个分辨率值时,就会确定死区时间。 -
When the point of inflection is reached, the test ends. The apparent
pvMax
is calculated using:
达到拐点时,测试结束。 视在功率最大值的计算方法如下 -
pvMax = pvIp + slopeIp * kexp; // where kexp = 4.3004 = (1 / exp(-1)) / (1 - exp(-1))
-
The process gain
Ku
and time constantTu
are determined and the selected tuning rule’s constants are used to determineKp, Ki, Kd, Ti and Td
. Also,controllability
and other details are provided (see comments insTune.cpp
).
确定过程增益 Ku 和时间常数 Tu,并使用选定的调谐规则常数确定 Kp、Ki、Kd、Ti 和 Td。 此外,还提供了可控性和其他详细信息(参见 sTune.cpp 中的注释)。 -
In the user’s sketch, the PID controller is set to automatic, the tuning parameters are applied the PID controller is run.
在用户草图中,PID 控制器被设置为自动,调谐参数被应用,PID 控制器开始运行。
Full 5T Test
A full test to pvMax is used when the controller action is set to direct5T
or reverse5T
. Use this method if the IP
testing isn’t a good fit to the process or if you’d like to get test data for the complete input response. Here, it is assumed the test will complete at about 3.5τ, from which point the apparent pvMax
is estimated and the tuning parameters are calculated.
当控制器动作设置为 direct5T 或 reverse5T 时,将使用到 pvMax 的完整测试。 如果 IPtesting 与流程不匹配,或者您希望获得完整输入响应的测试数据,请使用此方法。 这里假定测试将在约 3.5τ 处完成,并由此估算视在 pvMax 和计算调谐参数。
Functions
sTune Constructor
sTune(float *input, float *output, TuningRule tuningRule, Action action, SerialMode serialMode);
input
andoutput
are pointers to the variables holding these values.输入和输出是指向持有这些值的变量的指针。tuningRule
provides selection of 10 various tuning rules as described in the table below.
tuningRule 提供 10 种不同调整规则的选择,如下表所示。action
provides choices for controller action (direct or reverse) and whether to perform a fast inflection point test (IP) or a full 5 time constant test (5T). Choices aredirectIP
,direct5T
,reverseIP
andreverse5T
.
操作提供了控制器操作(直接或反向)以及执行快速拐点测试 (IP) 还是完整的 5 时间常数测试 (5T) 的选择。 选项包括 directIP, direct5T, reverseIP and reverse5T.。serialMode
provides 6 choices for serial output.
Serial Mode | Description |
---|---|
serialOFF | No serial output will occur. 不会有串行输出。 |
printALL | Prints test data while settling and during the test run. A summary of results is printed when testing completes. 在沉淀和测试运行期间打印测试数据。 测试完成后打印结果摘要。 |
printSUMMARY | A summary of results is printed when testing completes. 测试完成后会打印结果摘要。 |
printDEBUG | Same as printALL but includes printing diagnostic data during test run. 与 printALL 相同,但包括在测试运行期间打印诊断数据。 |
printPIDTUNER | ➩ Prints test data in csv format compatible with pidtuner.com. ➩ Requires the controller action being set to direct5T or reverse5T ➩ Just copy the serial printer data and import (paste) into PID Tuner for further analysis, model identification, fine PID tuning and experimentation. ➩ Note that Kp , Ti and Td is also provided for PID Tuner. 以与 pidtuner.com 兼容的 csv 格式打印测试数据。 ➩ 需要将控制器动作设置为直接 5T 或反向 5T ➩ 只需复制串行打印机数据并导入(粘贴)到 PID Tuner 中,即可进行进一步分析、模型识别、微调 PID 和实验。 请注意,PID Tuner 还提供 Kp、Ti 和 Td。 |
printPLOTTER | Plots pvAvg data for use with Serial Plotter. |
Instantiate sTune
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printALL);
/* ZN_PID directIP serialOFF
DampedOsc_PID direct5T printALL
NoOvershoot_PID reverseIP printSUMMARY
CohenCoon_PID reverse5T printDEBUG
Mixed_PID
ZN_PI
DampedOsc_PI
NoOvershoot_PI
CohenCoon_PI
Mixed_PI
*/
Configure
This function applies the sTune test settings.
void Configure(const float inputSpan, const float outputSpan, float outputStart, float outputStep,
uint32_t testTimeSec, uint32_t settleTimeSec, const uint16_t samples);
Set Functions
void SetEmergencyStop(float e_Stop);
void SetControllerAction(Action Action);
void SetSerialMode(SerialMode SerialMode);
void SetTuningMethod(TuningMethod TuningMethod);
Query Functions
float GetKp(); // proportional gain
float GetKi(); // integral gain
float GetKd(); // derivative gain
float GetTi(); // integral time
float GetTd(); // derivative time
float GetProcessGain(); // process gain
float GetDeadTime(); // process dead time (seconds)
float GetTau(); // process time constant (seconds)
uint8_t GetControllerAction();
uint8_t GetSerialMode();
uint8_t GetTuningMethod();
void GetAutoTunings(float * kp, float * ki, float * kd);
Controllability of the process
When the test ends, sTune determines how difficult the process is to control.
测试结束后,sTune 会确定流程的控制难度。
float controllability = _Tu / _td + epsilon;