【Control Theory】——Control System Classification & PID Algorithm Introduction & PID Parameter Setting & PID Host Computer Communication Protocol

Table of contents

foreword

1. PID algorithm

1. Control system classification & parameters & signals

2. Introduction to PID algorithm

2. PID parameter tuning

3. PID upper computer communication protocol

1. Data frame & protocol debugging

2. Protocol code implementation

expand:

Summarize


foreword

Disclaimer: The study notes are from the 421 construction team of station b and the punctual atomic motor tutorial, and are only for learning and communication! !


1. PID algorithm

PID is an acronym for Proportional, Integral, and Differential. It is a closed-loop control algorithm that combines proportional, integral, and differential. The essence is to perform calculations according to the proportional, integral, and differential functional relationships based on the input deviation value, and the calculation results are used to control the output .

                       

The PID algorithm is suitable for linear systems (satisfying superposition and homogeneity) - linear systems within the second order.

       

      

 Unsuitable systems: higher order systems, nonlinear systems . If used, higher-order systems need to be reducible to second-order systems, and nonlinear systems need to be linearized at the equilibrium points of the nonlinear system using Lyapunov's first method. More research can continue!

1. Control system classification & parameters & signals

Classification:

①Open -loop control system (no feedback): general open-loop system , front-feedback control system (the actuator compensates for the interference when acting on the object, as shown in the figure below)

         

②Closed- loop control system (with feedback): single closed-loop (feedback at the output end), double closed-loop (not only feedback at the output end, but also feedback at the secondary object in the middle, as shown in the double loop as shown in the figure below)

         

Compound control system (full name feedforward and feedback compound control system)

parameter:

Error (e) = expected output (input) - actual output

Controller output (C) : signal to the actuator

Actuator output: changes in the object being acted on (such as a valve)

System output Y: actual output (such as water flow change)

Signal:

Continuous and discrete signals: the sample-and-hold acquires the voltage signal at that moment and keeps it for one sampling period, which is the time corresponding to the red horizontal line

2. Introduction to PID algorithm

Three links:

Proportional link: It reflects the deviation signal of the control system in proportion, that is, the output C is proportional to the input deviation e, which can be used to reduce the deviation of the system.

Conclusion: The larger the K_p is, the faster the system responds and reaches the target value; if the K_p is too large, the system will produce large overshoot and oscillation, which will lead to the deterioration of the stability of the system; the proportional link cannot eliminate the static error.

Static error: the deviation between the target value and the measured value when the system control process tends to be stable. Cause: The output is offset by external influences. Static errors can be eliminated by introducing an integral link.

Integral link: Integrate the input deviation e, as long as there is a deviation, the integral link will continue to work, mainly used to eliminate static errors.

Conclusion: The larger the K_i is, the shorter the time to eliminate the static error is, and the faster the target value is reached; if the K_i is too large, the system will have greater overshoot and oscillation, which will lead to the deterioration of the stability of the system; for a system with a large inertia, the integration link Poor dynamic response, prone to overshoot and oscillation.

Derivative link: respond to the variation trend of the deviation, make corresponding control in advance according to the variation of the deviation, reduce the overshoot and overcome the oscillation.

Conclusion: The larger K_d or the trend of change, the stronger the effect of the differential link, and the stronger the suppression of overshoot and oscillation; if K_d is too large, it will cause system instability and easily introduce high-frequency noise.

official:

In fact, the formula is divided into two types: position type and incremental type (the output of the punctual atomic controller is represented by uk, which corresponds to the above formula C)

position type

Features: uk directly corresponds to the output (position) of the object. If the calculation is abnormal, it will have a great impact on the system; full calculation (the previous quantities must be fully involved in the calculation of the output), and the deviation e must be accumulated, which requires a large amount of calculation ; Good results can be obtained in objects without integral components, such as electro-hydraulic servo valves, temperature control equipment, etc.

②Incremental _

Features: Incremental PID calculates the increment relative to the previous output, that is, u_k= u_k−1+ ∆u_k; the increment is only related to the deviation of the last three times, and the abnormality of the calculation has little impact on the system; the amount of calculation Less, real-time performance is relatively good.

Example understanding:

The difference between the drone example and the car is that the car only has a proportional control of P. Although the change is slow at the end, as long as there is a little speed (not equal to 0), it will approach the desired position, but the drone is different because it has to overcome itself. Gravity, (only when the proportion p is controlled) when the speed is small to a certain value (suspension speed), the drone will always hover at this position and can no longer approach the desired position, and can only approach and cannot reach the exact position. That is, the proportional control of a single P item cannot eliminate the steady-state error, and the integral control I is needed at this time . If the error e remains unchanged (hover), the integral item can be accumulated, increasing the control amount, and the index of the integral item will not be due to The value of the current error e is equal to 0 and changes, it just stops accumulating. The differential control D is mainly used to offset over-regulation (normally, the error e is constantly decreasing, when over-regulation will lead to overshoot, and the error will become larger), then the differential term will become a negative value to offset the over-regulation .

PID structure definition & function implementation:

typedef struct
{
    __IO float  SetPoint; 			/* 目标值 */ 
    __IO float  ActualValue;     	/* 期望值 */
    __IO float  SumError;            	/* 偏差累计 */
    __IO float  Proportion;          	/* 比例常数 P */
    __IO float  Integral;            	/* 积分常数 I */
    __IO float  Derivative;          	/* 微分常数 D */
    __IO float  Error;               		/* Error[1] */
    __IO float  LastError;           	/* Error[-1] */
    __IO float  PrevError;           	/* Error[-2] */
} PID_TypeDef;

PID initialization function: initialize PID related parameters (clear), set PID coefficient

PID closed-loop control function: realize position and incremental PID control algorithm

TIM6 update interrupt function: Call the PID closed-loop control function, and return the output (expected value) after calculation. That is to control the PWM output

PID upper computer debugging function: realize the communication between the upper computer and the development board, and perform PID parameter adjustment.

2. PID parameter tuning

Sampling period: The sampling period refers to the sampling time interval of the actual value in PID control. The shorter it is, the more continuous the effect will be, but the higher the occupation of hardware resources will be. How to choose? In theory, it can be based on Shannon sampling theorem ; in practical applications, the sampling period can be selected according to the ability of actual value mutation .

There are two types of PID parameter tuning methods. Theoretical calculation setting (mathematical model), engineering setting method (trial and error method, critical ratio method, general adjustment method). But no matter which method, the final application should be adjusted according to experience and actual situation:

system oscillation

If the oscillation is too large, the proportional coefficient can be reduced to reduce the oscillation; after the oscillation has been reduced a lot, but overshoot begins to exist, then add the differential link adjustment to eliminate the initial oscillation overshoot

 static error

If there is a static error, the integral link (or increase the integral coefficient) can be introduced to reach the target value. If you think it takes too long to eliminate the static error, you can continue to increase the integral coefficient.

overshoot

If there is a large overshoot, it can be adjusted by reducing the integral coefficient and increasing the differential coefficient, as shown in the right figure.

3. PID upper computer communication protocol

1. Data frame & protocol debugging

The PID debugging assistant sends and receives data according to the following format (of course, this is punctual and atomic):

The data category refers to the data attribute, speed, temperature, etc.; the data field refers to the specific value; the checksum adopts CRC16-MODBUS checksum, and the checksum range includes the frame header, data category and data field.

Use the serial debugging assistant to simulate the development board, and communicate with the PID debugging assistant. Need to prepare USB to TTL module (hardware), PID debugging assistant and serial port debugging assistant (software), PID debugging assistant communication protocol (file).

This is not universal, the agreement is a kind of regulation, but here is the regulation of the punctual atom!

......

...waiting to practice it myself!

2. Protocol code implementation

The following are the implementation ideas, and continue to update when you wait for your own practice...

The data category and some data fields of the protocol have fixed data frames, and it is more convenient to use the enumerated type to manage:

The underlying code implementation idea:

①Serial port 1 initialization function: initialize serial port 1, open the middle-end receiving, and use it for host computer communication

②Memory initialization function: initialize a section of memory at a specified address (clear)

③CRC16 check function: check the frame header, data type, and data field

④ Host computer data analysis function: analyze the data sent by the host computer and store it in the specified structure

⑤ Data upload function: upload the specified data to the host computer

Application layer code implementation idea:

①Initialize the debugging function: use the memory to initialize the underlying function, and initialize the required content

②PID data upload function: upload PID parameters, set PID target value data synchronization

③A series of data upload functions: call the underlying data upload function, upload current value, voltage value, etc.

④PID parameter receiving function: accept the PID parameter value set by the host computer

⑤Command receiving function: accept commands issued by the host computer, such as motor running, braking, etc.

⑥ Speed ​​range setting function: limit the range of motor target speed and the maximum sudden change value of speed

More relevant knowledge is in the motor series, DC brushed motor!

expand:

The expansion comes from the 421 construction team. I don’t understand the part very well. Record it first!

①Integral limit

Imagine a scene where the drone is held down by hand when it takes off-in fact, the integral effect is caused by the error. The larger the error, the stronger the integral effect. And it has the characteristic of always accumulating, the accumulation of points will be very large, it may burn out the motor or it will rush out at a very high speed when you let go, so try to let the drone take off freely or limit the maximum value of points accumulated (in fact, it is Set a maximum limit on Iterm). Iterm is cleared when the set value is exceeded.

Integral separation

The UAV hovers at the surface of the target position for 100m, and the error e=0——at this time, the P term (Pterm) is 0, and the UAV must be kept hovering, so the I term (Iterm) is not 0, and the error is no longer Cumulatively, so the Ierm value remains constant. At this time, if a new target value is suddenly given to the drone, such as 1000m to the surface, the error e suddenly soars to 900m, causing Ierm to change suddenly within a certain period of time, and the same is true for Pterm, which may cause system oscillation or even overshoot. Integral separation is required at this time. For example, the maximum error e for the integral term cannot exceed 500m , and once it exceeds, the integral term Iterm is set to 0.

(3) Differential precedence

In the example of ② above, when the error changes suddenly, the impact will be on the P and I items first, and will not affect the D item.

 The sending and receiving of the above values ​​follow the CAN protocol! See this article xxxxx for details .

1. RM motor M3508 speed single closed loop (refer to the system classification diagram)

/*位置pid算法*/
void pid_calc(_pid* pid){
	pid->e = pid->target - pid->current;//误差e
	pid->p_out = (int32_t)(pid->Kp * pid->e);//比例项P
	
	//积分隔离
	if (fabs(pid->e) < I_Band){
		pid->i_out += (int32_t)(pid->Ki * pid->e);//积分项I
		limit(&(pid->i_out),pid->IntegralLimit);//积分限幅
	}
	else{
		pid->i_out = 0;
	}
	
	pid->d_out = (int32_t)(pid->Kd * (pid->e - pid->last_e));//微分项D
	pid->total_out = pid->p_out + pid->i_out + pid->d_out;//总控制项
	
	//pid输出限幅
	limit(&(pid->total_out),pid->MaxOutput);
	
	pid->last_e = pid->e;//上一次误差
}

/*速度单环*/
chassis_motor_pid[i].SpeedPID.target = 500;
chassis_motor_pid[i].SpeedPID.current = motor_chassis[i].speed_rpm;
pid_clac(&chassis_motor_pid[i].SpeedPID);//调用PID算法做运算

2. RM motor 6020 angle double closed loop (refer to the system classification diagram)

/*角度双环*/
AnglePID.target = 3.14*30/180;//期望角度30°转换为弧度制
update_angle(&manipulator_motor_pid[i].angle , motor_manipulator[i].angle);//更新电机实时角度,函数定义见下
manipulator_motor_pid[i].AnglePID.current = manipulator_motor_pid[i]._angle.angle;//当前角度
pid_calc(&manipulator_motor_pid[i].AnglePID);//调用PID算法做运算

manipulator_motor_pid[i].SpeedPID.target = manipulator_motor_pid[i].AnglePID.total_out//速度期望(内环),即外环控制器的输出
manipulator_motor_pid[i].SpeedPID.current = motor_manipulator[i].speed_rpm;//当前速度(内环)
pid_calc(&manipulator_motor_pid[i].SpeedPID);//调用PID算法做运算,该函数与单闭环一致


void update_angle(motor_angle* _angle , uint16_t angle_fb){
	_angle->encoder = angle_fb;
	if(_angle->encoder_is_init){
		if(_angle->encoder - _angle->last_encoder > 4096)
			_angle->round_cnt --;
		else if(_angle->encoder - _angle->last_encoder < -4096)
			_angle->round_cnt ++;
	}
	else{
		_angle->encoder_offset = _angle->encoder;
		_angle->encoder_is_init = 1;
	}
	_angle->angle_offset = _angle->angle_offset / 8192.0f * 360.0f;
	_angle->last_encoder = _angle->encoder;
	_angle->total_encoder = _angle->round_cnt * 8192 + _angle->encoder - _angle->encoder_offset;
	_angle->angle = _angle->total_encoder / 8192.0f * 360.0f;
}


Summarize

To be continued...

Previous highlights:
STM32 timer input capture (IC)
STM32 timer output comparison (PWM wave)
STM32 timing interrupt
STM32 external interrupt
STM32GPIO intensive talk
...

Guess you like

Origin blog.csdn.net/weixin_51658186/article/details/129914177