How to implement a PID controller on Arduino
We continue with the series of posts dedicated to the theory of controllers, seeing how to implement a PID control in a microprocessor like Arduino.
In previous posts we have seen an introduction to control theory, presented the on/off controller with hysteresis, the powerful PID controller, and seen how to adjust the parameters of a PID controller.
Now it’s time to leave the theory, dust off the keyboard, see how a PID controller is implemented in a microprocessor like Arduino. Fortunately, making a basic PID is quite simple and, as we will see in the second part, we have a very complete library available to make it even easier.
A PID is an essential element when addressing a large number of interesting projects, such as automatically regulating the level of light, maintaining temperature, ensuring that a motor rotates at a constant speed, stabilizing platforms, making a robot move straight, or even robots that remain balanced on two wheels.
我们将继续撰写关于控制器理论的系列文章,看看如何在 Arduino 等微处理器中实现 PID 控制。 在之前的文章中,我们已经介绍了控制理论,介绍了带滞后的开/关控制器和功能强大的 PID 控制器,并了解了如何调整 PID 控制器的参数。 现在是时候抛开理论,看看如何在 Arduino 等微处理器中实现 PID 控制器了。 幸运的是,制作一个基本的 PID 非常简单,而且正如我们将在第二部分看到的那样,我们有一个非常完整的库,可以让制作变得更加容易。 在处理大量有趣的项目时,PID 是一个必不可少的元素,例如自动调节光照度、保持温度、确保电机以恒定的速度旋转、稳定平台、使机器人笔直移动,甚至使机器人在两个轮子上保持平衡。
Manual PID controller
First let’s see how to implement a simple controller ourselves. In fact, the code is not overly complex. Let’s assume our controller takes the input of the controlled variable at A0, and outputs it using a PWM signal on pin 3.
The code for a basic PID could be as follows
首先,让我们看看如何自己实现一个简单的控制器。 事实上,代码并不复杂。 假设我们的控制器在 A0 端接收受控变量的输入,并在 3 引脚上使用 PWM 信号输出。 基本 PID 的代码如下
// Pin assignments
const int PIN_INPUT = A0;
const int PIN_OUTPUT = 3;
// Controller constants
double Kp=