Learn and use the PID library to easily master the PID

Anyone who has worked with a PID controller is familiar with its core formula, which is often represented as follows:

Using this formula, it's straightforward to write an Arduino program that calculates the PID output. Here’s an example of how the PID library can be used to simplify the process:

Use the PID library to easily get the PID

Note: You can copy the source code directly from the original article.

However, when the PID is called irregularly, two main issues can occur:

1. The PID may sometimes be called regularly and sometimes not, which disrupts the continuous and stable behavior of the controller.

2. Calculating the derivative and integral terms becomes more complex because they depend on time intervals.

To solve these problems, ensure the PID is called at fixed time intervals. In each loop, call the compute function based on a preset sampling time. This allows the PID to calculate or return the value immediately. Once we have a fixed interval, the integral and derivative calculations become simpler.

The revised program now looks like this:

Use the PID library to easily get the PID

The gray part represents the newly added lines.

In this updated version, the author converts the time into seconds on line 29, making operations easier. Additionally, two new functions are introduced to convert PID parameters into time-based values.

When running the PID program, you might encounter an issue known as differential overshoot.

Use the PID library to easily get the PID

As shown in the diagram, the error is calculated as Setpoint minus Input. Any change in the Setpoint causes an immediate jump in the error value, which leads to an infinite spike in the differential term.

To address this, we can modify the calculation by using the input instead of the error in the differential term.

Use the PID library to easily get the PID

With this adjustment, the program becomes:

Use the PID library to easily get the PID

We replaced +dError with -dInput to avoid sudden changes in the differential term.

When adjusting PID parameters during runtime, the output can suddenly change. For instance, if you halve the Ki value, the output also halves. Why does this happen? Let’s look at the integral formula:

This shows that changing Ki affects the accumulated error sum, leading to unexpected results. What we really want is for the system to respond smoothly without abrupt changes.

A solution is to re-adjust the accumulated error (errSum) when Ki changes. If Ki doubles, errSum should be halved. Although this method isn’t perfect, it helps prevent large jumps in the output.

By storing the product of error and Ki, we ensure that when Ki changes, the previous values remain intact, preventing sudden fluctuations.

Our updated program now looks like this:

Use the PID library to easily get the PID

This way, parameter changes have minimal impact on the output.

Use the PID library to easily get the PID

This post was last edited by the fold on April 13, 2015, at 7:17 PM.

Sometimes, the PID output exceeds the allowed range. For example, if the maximum output is 255 but the PID tries to output 300, the result is incorrect. To fix this, we add constraints to the PID output. Here’s how we do it:

Use the PID library to easily get the PID

Use the PID library to easily get the PID

If you want to manually set the output, such as setting it to 0, you might write something like this:

void loop()

{

compute();

output = 0;

}

However, this approach confuses the PID. It thinks it’s not working, so it increases its effort, causing instability. To avoid this, we need a switch to disable the PID when using manual outputs.

Here’s our updated program:

Use the PID library to easily get the PID

Use the PID library to easily get the PID

When you turn the PID back on after disabling it, the output jumps to the last value it sent. To avoid this, we use an initialization function. Here’s what the program looks like:

Use the PID library to easily get the PID

Use the PID library to easily get the PID

When switching modes, the initialize() function resets the last input and ITerm values, ensuring a smooth transition and avoiding output jumps.

Ceramic Insulator

Ceramic Insulator,Ceramic Electrical Insulators,Ceramic Standoff Insulators,Ceramic Isolators

Yixing Guangming Special Ceramics Co.,Ltd , https://www.yxgmtc.com