What is the embedded PID algorithm? Analysis of the embedded PID algorithm

Publisher:rho27Latest update time:2024-01-29 Source: elecfansKeywords:Embedded Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1.1 Overview

A proportional-integral-differential controller (PID controller or three-term controller) is a control loop mechanism that uses feedback and is widely used in industrial control systems and various other applications that require continuous modulation control.


A PID controller continuously calculates an error value e(t) as the difference between the desired set point (SP) and the measured process variable (PV) and applies a correction based on the proportional, integral, and derivative terms (denoted P, I, and D, respectively), hence the name.

34dc96ce-7bf5-11ee-939d-92fbcf53809c.png

34e62874-7bf5-11ee-939d-92fbcf53809c.png

r(t) is the desired process value or set point (SP) and y(t) is the measured process value (PV).

1.2 Historical Development

In 1911, the first PID controller was developed by Elmer Sperry.

The first formal control law for what we now call PID or three-term control was developed using theoretical analysis by a Russian-American engineer, Nicolas Minorsky, in 1922. Minorsky was researching and designing automatic ship steering systems for the U.S. Navy, and based his analysis on observations of helmsmen.

He pointed out that the helmsman steers the ship not only based on the current heading error, but also based on past errors and the current rate of change; Minorsky then treated this mathematically. His goal was stability, not general control, which greatly simplified the problem.

In 1933, TIC (Taylor Instrument Company) implemented a fully adjustable front pneumatic controller. A few years later, control engineers eliminated the steady-state error found in proportional controllers by returning the end point to some false value until the error is not zero. This return contains the error, and this is called a proportional-integral controller.

In 1940, the first pneumatic PID controller was developed using derivative action to reduce overshoot problems.

In 1942, Ziegler & Nichols introduced the tuning rules for engineers to find and set the appropriate parameters of a PID controller.

Automatic PID controllers were widely used in industry in the mid-1950s. Most modern PID controls in industry are implemented as DCS, PLC or microcontroller programs.

1.3 Application

• Rocket attitude control

• UAV hover control, etc.

• Camera stabilizer, camera gimbal

•Balance car

• Cruise control and steering control of cars

• Engine speed control

• Temperature controller on 3D printer

•In industrial automation, approximately 95% of closed-loop operations use PID controllers.

1.4 Comparison with ON/OFF type controller

A closed loop system like a PID controller consists of a feedback control system. The system evaluates the feedback variable using a fixed point, which produces an error signal. Based on this, it changes the system output. This process continues until the error reaches zero, otherwise the value of the feedback variable is equal to a fixed point.

This controller provides good results compared to the ON/OFF type controller. In the ON/OFF type controller, only two conditions are needed to manage the system. Most of the HVAC systems, refrigerators use this method.

For example, in a refrigerator, it cools the interior until the desired temperature is reached, then turns the cooler off until a setpoint above the desired temperature is reached. Once the process value is below the fixed point, it turns on.

Similarly, once the value is higher than the fixed value, it will turn off. The output of this controller is unstable and oscillates frequently in the region of the fixed point. However, compared to the ON/OFF type controller, the PID controller is more stable and accurate.

34f4492c-7bf5-11ee-939d-92fbcf53809c.png

1.6 Response Type

Systems driven by a PID controller typically have three types of responses: underdamped, overdamped, and critically damped.

35010a5e-7bf5-11ee-939d-92fbcf53809c.png

3505b04a-7bf5-11ee-939d-92fbcf53809c.gif

• An underdamped response oscillates around a reference value before settling.

• An overdamped response rises slowly and does not exceed the reference value.

•The critically damped response has the fastest rise time and will not exceed the reference value.

formula

2.1 PID system definition and formula

35098cd8-7bf5-11ee-939d-92fbcf53809c.png

r(t) setpoint, reference, is the desired process value or setpoint (SP);

y(t) output, process variable, is the measured process value, output value (PV);

e(t) error, is the deviation;

u(t) control effort, is the control amount;

The notable feature of the PID controller is its ability to perform precise and optimal control using the effects of the three control terms, proportional, integral, and derivative, on the controller output.

A PID controller continuously calculates the error value e(t) as the difference between the desired set point SP=r(t) and the measured process variable PV=y(t): e(t)=r(t)−y(t) and applies corrections based on proportional, integral and derivative terms.

The controller attempts to minimize the time-varying error u(t) by adjusting the manipulated variable (MV).

351e9b14-7bf5-11ee-939d-92fbcf53809c.png

3531bd0c-7bf5-11ee-939d-92fbcf53809c.png

3540ddfa-7bf5-11ee-939d-92fbcf53809c.png

2.2 PID digital formula

Since computer control is a sampling control, it can only calculate the control quantity based on the deviation at the sampling time, and cannot continuously output the control quantity and perform continuous control like analog control. Due to this feature, the integral term and differential term in (Equation 1-1) cannot be used directly and must be discretized.

The discretization method is: take τ as the sampling period and k as the sampling number, then the discrete sampling time kτ corresponds to the continuous time t, use the rectangular method numerical integration to approximate the integral, and use the first-order backward difference to approximate the differential, and the following approximate transformation can be made:

354e672c-7bf5-11ee-939d-92fbcf53809c.png

2.3 Position PID algorithm

Substituting (Equation 2-1) into (Equation 1-1), we can get the discrete PID expression:

3555cae4-7bf5-11ee-939d-92fbcf53809c.png

Substituting (Equation 2-1) into (Equation 1-2), we can get the discrete PID expression:

3559f6a0-7bf5-11ee-939d-92fbcf53809c.png

The integral coefficient and differential coefficient are replaced as follows:

Note: τ must be a constant value, or its change must be small enough to be ignored, so that P, I, and D are fixed constants and can be adjusted.

35618780-7bf5-11ee-939d-92fbcf53809c.png 357065de-7bf5-11ee-939d-92fbcf53809c.png

2.4 Incremental PID algorithm

357d1d74-7bf5-11ee-939d-92fbcf53809c.png

The incremental PID control algorithm can be derived from (Equation 2-2). From (Equation 2-2), the output value of the controller at the k-1th sampling moment can be obtained as follows:

35901f5a-7bf5-11ee-939d-92fbcf53809c.png

From (Equation 2-3), we can get the output value of the controller at the k-1th sampling time:

359a381e-7bf5-11ee-939d-92fbcf53809c.png

Subtract (Equation 2-7) from (Equation 2-3) and sort them out to get the incremental PID control algorithm formula:

35a18e34-7bf5-11ee-939d-92fbcf53809c.png

It can be seen from (Equation 2-8) that if the computer control system uses a constant sampling period τ, once A, B, and C are determined, the control quantity can be calculated from (Equation 2-8) by simply using the deviation values ​​of the three previous and subsequent measurements.

Compared with the position PID algorithm (Formula 2-3), the incremental PID control algorithm only needs to keep the deviation value of the three moments before the current moment. The cumulative error is smaller and the amount of calculation is much smaller. Therefore, it is widely used in practice.

The position PID control algorithm can also be derived from the incremental control algorithm to obtain the recursive calculation formula:

35a5b5c2-7bf5-11ee-939d-92fbcf53809c.png

(Equation 2-9) is the digital recursive PID control algorithm currently widely used in computer control.

Debugging Tips

35b0c1ce-7bf5-11ee-939d-92fbcf53809c.gif

Code Implementation


Python


import numpy as np

import matplotlib.pyplot as plt



class PositionPID(object):

"""Implementation of Position PID Algorithm"""



def __init__(self, target, cur_val, dt, max, min, p, i, d) -> None:

self.dt = dt # loop time interval

self._max = max # Maximum output limit to avoid overshoot

self._min = min # Minimum output limit

self.k_p = p # proportionality factor

self.k_i = i # integral coefficient

self.k_d = d # differential coefficient



self.target = target # target value

self.cur_val = cur_val # The current PID position value of the algorithm, the first time is the set initial position

self._pre_error = 0 # Error value at time t-1

self._integral = 0 # Error integral value





def calculate(self):

"""

Calculate the PID output value cur_val at time t

"""

error = self.target - self.cur_val # Calculate the current error

# Proportional term

p_out = self.k_p * error

# Integral term

self._integral += (error * self.dt)

i_out = self.k_i * self._integral

# Differential term

derivative = (error - self._pre_error) / self.dt

d_out = self.k_d * derivative



# pid output at time t

output = p_out + i_out + d_out



# Limit output value

if output > self._max:

output = self._max

elif output < self._min:

output = self._min

self._pre_error = error

self.cur_val = output

return self.cur_val



def fit_and_plot(self, count = 200):

"""

Use PID fitting setPoint

"""

counts = np.arange(count)

outputs = []



for i in counts:

outputs.append(self.calculate())

print('Count %3d: output: %f' % (i, outputs[-1]))



print('Done')

# print(outputs)

plt.figure()

plt.axhline(self.target, c='red')

plt.plot(counts, np.array(outputs), 'b.')

plt.ylim(min(outputs) - 0.1 * min(outputs), max(outputs) + 0.1 * max(outputs))

plt.plot(outputs)

plt.show()



pid = PositionPID(10, -5, 0.5, 100, -100, 0.2, 0.1, 0.01)

pid.fit_and_plot(150)


92bc30d4babd87bea5c7ab6673aec5c0_wKgZomVIQTKADgw4AAEiki3ksPM572.png

C/C++


//First define the PID structure to store the data of a PID

typedef struct

{

float kp,ki,kd; //three coefficients

float error,lastError; //Error, last error

float integral,maxIntegral; //Integral, integral limit

float output,maxOutput; //output, output limit

}PID;

//Function used to initialize pid parameters

void PID_Init(PID *pid, float p, float i, float d, float maxI, float maxOut)

[1] [2] [3]
Keywords:Embedded Reference address:What is the embedded PID algorithm? Analysis of the embedded PID algorithm

Previous article:General principles for selecting low voltage circuit breakers
Next article:PROFIBUS-DP communication between SIMATIC S7-1500 PLC and ET200MP

Latest Embedded Articles
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号