The stm32 single-chip microcomputer realizes the PID control of the motor

     PID control should be regarded as a very old and widely used control algorithm, ranging from the temperature control of the kettle to the control of the flight attitude and flight speed of the drone, etc. In motor control, the PID algorithm is especially commonly used.

1. Positional PID

1. Calculation formula

In motor control, what we output to the motor is a PWM duty cycle value.
Not much to say, directly on the basic formula of positional PID:

picture

The control flow chart is as follows:

picture

The target position in the above figure can generally be programmed to change the target value through buttons or switches, and the measurement position is to collect the encoder data through stm32.

The difference between the target position and the measured position is the deviation of the current system. Send it to the PID controller for calculation and output, and then control the rotation of the motor through the power amplification of the motor drive to reduce the deviation and finally reach the target position.

2. C language implementation

How to write the above theoretical analysis and control schematic diagram in C language, this is an interesting and practical process. The specific code of positional PID realized by C language is as follows:

int Position_PID (int Encoder,int Target){
   
     static float Bias,Pwm,Integral_bias,Last_Bias;  Bias=Target- Encoder; //计算偏差  Integral_bias+=Bias; //求出偏差的积分  //PID基本公式  Pwm=Position_KP*Bias+Position_KI*Integral_bias+Position_KD*(Bias-Last_Bias);  Last_Bias=Bias; //保存上一次偏差  return Pwm; //输出}

The entry parameters are the position measurement value of the encoder and the target value of the position control, and the return value is the motor control PWM (now it is easier to understand if it is easier to see the control block diagram above).

The first line is the definition of the relevant internal variable.

The second line is to find the position deviation, subtract the target value from the measured value.

The third line calculates the integral of the deviation by summing up.

Line 4 finds the motor PWM using a positional PID controller.

The fifth line saves the last deviation for the next call.

The last line is return.

2. Incremental PID

1. Calculation formula

The speed closed-loop control is to measure the speed information of the motor according to the number of pulses obtained per unit time (here the M method is used to measure the speed), and compare it with the target value to obtain the control deviation, and then control the proportion, integral and differential of the deviation to make The process in which the deviation tends to zero.

picture

Only PI control is used in our speed control closed-loop system, so the PID controller can be simplified

is the following formula:

picture

The control block diagram is the same as the positional one.

picture

The target speed in the above figure can generally be programmed to change the target value through buttons or switches. As mentioned in the chapter on the encoder before measuring the speed, it is to collect the encoder data regularly through the single-chip microcomputer and clear it.

The difference between the target speed and the measured speed is the deviation of the current system. Send it to the PID controller for calculation and output, and then control the rotation of the motor through the power amplification of the motor drive to reduce the deviation and finally reach the target speed.

2. C language implementation

How to write the above theoretical analysis and control schematic diagram in C language, this is an interesting and practical process. The specific code of positional PID realized by C language is as follows:

int Incremental_PI (int Encoder,int Target){
   
     static float Bias,Pwm,Last_bias;  Bias=Encoder-Target; //计算偏差  //增量式 PI 控制器  Pwm+=Velocity_KP*(Bias-Last_bias)+Velocity_KI*Bias;  Last_bias=Bias; //保存上一次偏差  return Pwm; //增量输出}

The entry parameters are the speed measurement value of the encoder and the target value of the speed control, and the return value is the motor control PWM.

The first line is the definition of the relevant internal variable.

The second line is to find the speed deviation, subtract the target value from the measured value.

The third line finds the motor PWM using an incremental PI controller.

The fourth line saves the last deviation for the next call.

The last line is return.

3. The role of each parameter of P, I, D

The performance index of the automatic control system mainly has three aspects: stability, rapidity and accuracy.

Stability: After the system is subjected to external effects, if the control system makes the controlled variable grow with time and finally agrees with the given expected value, the system is said to be stable, and we generally call it system convergence.

If the controlled quantity deviates more and more from the given value with the increase of time, the system is said to be unstable, which we generally call the system divergence. Only a stable system can complete the task of automatic control, so system stability is a necessary condition to ensure the normal operation of the control system.

In a stable control system, the initial deviation of the controlled quantity from the given value should gradually decrease with time and tend to zero.

Rapidity: Rapidity refers to the length of time for the dynamic process of the system to proceed. The shorter the process time, the better the rapidity of the system, and the longer the process time, it means that the system responds slowly and it is difficult to realize the fast-changing command signal.

Stability and rapidity reflect the performance of the system in the control process. During the tracking process of the system, the smaller the controlled quantity deviates from the given value, the shorter the deviation time is, indicating that the dynamic accuracy of the system is relatively high.

Accuracy: refers to the deviation of the controlled variable (or feedback quantity) from the given value after the end of the dynamic process of the system. This deviation is the steady-state error. performance in the later stages of the dynamic process.

In practical production engineering, different control systems have different requirements for the controller effect. For example, balance cars and inverted pendulums have high requirements on the speed of the system, and if the response is too slow, the system will lose control.

The door and window automatic opening and closing system in the smart home does not have high requirements for speed, but has high requirements for stability and accuracy, so it is necessary to strictly control the overshoot and static error of the system.

Guess you like

Origin blog.csdn.net/weixin_41114301/article/details/132503096