[Take you to control the motor with pid algorithm]——(4) Cascade PID control motor

foreword

1. This series of tutorials is based on the hal library development of the smallest system board of stm32f103c8t6. It will guide you to learn the speed loop, position loop and speed position cascaded pid using the Pid algorithm in the most easy-to-understand way.

2. The idea of ​​publishing this series of Pid series tutorials is that I participated in a competition some time ago and needed to use the function of cascading Pids, but I found that the tutorials on the Internet were scattered and did not focus on explaining these three together. Functional, or the written content is not simple and direct enough, and it will be very difficult for students with poor foundation to learn. In order to take everyone to master the ability to use pid to control the motor with the most intuitive steps and the simplest code, I plan to publish a complete and easy-to-understand series of tutorials, which also serves as a summary of what I have learned some time ago.

3. The Pid algorithm involves a lot of content, and it is worth our in-depth research to discover its beauty. The study of the Pid algorithm is not only a theoretical understanding, but also a practice to realize the function of controlling the speed and position. It is also a science to adjust the parameters after transplanting the pid algorithm! Hahaha, so mastering the pid is not an easy task, but I have realized the cascaded pid, please believe that I will lead everyone to realize it bit by bit from the beginning to the end!

1. The function to be realized in this chapter is to use cascaded pid to control the motor. The final effect is: the motor can rotate to a specific position at a specified speed, which requires accurate speed and position and strong anti-interference ability (after turning to the target angle , it is difficult to turn the motor by hand, and it is difficult to stop the motor movement with a certain resistance when the motor is rotating, and the motor will replenish the speed in time after being disturbed).

2. Everyone will learn: use cascade PID (position loop and speed loop) to control the motor; Note: the position loop and speed loop must be adjusted separately before being combined into a cascade pid. Generally speaking, the synthesized cascade pid It can be used. If it is not accurate, you can also adjust the parameters again. First adjust the speed loop and comment out the position loop. After adjusting the speed loop, make fun of the position loop comment, and then continue to adjust the position loop to achieve the desired effect .

3. Before studying this chapter, it is recommended that you finish the study first: [Take you by hand to control the motor with the pid algorithm]——(1) Use of the encoder motor and 0.96-inch OLED display

【Take you through the pid algorithm to control the motor】——(2) PID speed loop

[Hand in hand to guide you to control the motor with pid algorithm]——(3) PID position loop

After mastering the use of speed loop and position loop, cascade PID control is very simple.

1. Principle of Cascade PID

As the name implies, the speed-position cascaded PID is used in series with the speed loop and the position loop; the inner loop is the speed loop, the outer loop is the position loop, the output of the position pid is used as the input of the speed pid, and the output of the position pid is used as the speed pid target value to control.

The control block diagram is as follows:

 2. Code

The code of this tutorial is based on [Hand in hand to take you to control the motor with the pid algorithm] - (2) PID speed loop

No need to write code anymore, just change some places in the code of that article.

Change to the code for Cascade PID.

/* USER CODE BEGIN PV */
int16_t  speed,encoder_counter;
 
float Position_KP=0.18,Position_KI=0.002,Position_KD=0; //位置PID系数
float Velocity_KP=4.5,Velocity_KI=0.1,Velocity_KD=0; //速度PID系数
int Encoder,Target_Velocity=30;
int Moto,Position_Moto;//电机PWM变量
int limit_a;
int Position,Target_Position=850; //位置和目标位置自己设定
 
 
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
 
/**
 * @function: void GET_NUM(void)
 * @description: 使用STM32编码器模式,读取编码器产生的脉冲值
 * @param {*} 
 * @return {*}
 */
void GET_NUM(void)
{
	encoder_counter=(short) __HAL_TIM_GET_COUNTER(&htim3);
	__HAL_TIM_SET_COUNTER(&htim3,0);//将编码器模式的定时器清零
}
 
/**
 * @function:void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
 * @description: 定时器中断回调函数,0.1S中断一次并计算转速,将电机转速以及编码器产生的脉冲数显示在OLED屏上
 * @param {TIM_HandleTypeDef *htim} 
 * @return {*}
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim==&htim1)
	{
		GET_NUM();//得到所记录的脉冲数 
		Position+=encoder_counter;//脉冲数积累
		Position_Moto = Position_PID(Position,Target_Position);
		limit_a=Xianfu(Position_Moto,myabs(Target_Velocity));
		Moto = Incremental_PI(encoder_counter,limit_a);
		//Moto = Incremental_PI(encoder_counter,Target_Velocity);
		//Moto =Xianfu(Position_Moto,(这里填PWM满幅));
		Set_Pwm(Moto);
		
		 //speed=(float)encoder_counter/2040/0.1;//转速为n,r/s  脉冲数转化为速度
		 //OLED_Showdecimal(0,4,speed,2,2,12,0);//在特定位置显示2位整数+2位小数的电机转速
	}
}
 
/*串口重定向*/
 #ifdef __GNUC__
     #define PUTCHAR_PROTOTYPE int _io_putchar(int ch)
 #else
     #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
 #endif /* __GNUC__*/
 
 /******************************************************************
     *@brief  Retargets the C library printf  function to the USART.
     *@param  None
     *@retval None
 ******************************************************************/
 PUTCHAR_PROTOTYPE
 {
     HAL_UART_Transmit(&huart1, (uint8_t *)&ch,1,0xFFFF);
     return ch;
 }
 
 
/* USER CODE END 0 */
/* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
		printf("%d,%d\n",Position,Target_Position);    //输出编码器的值(实际值)和目标值到vofa软件
  }

Display the image of the actual position and the target position, and observe whether the graph line rises to a certain value with a certain slope and does not change; principle: the derivative of the position is the speed, so the slope of the position image indicates the size of the speed;

The same slope means that the speed is not changed, that is, the effect of the speed loop is OK;

If the pulse value does not rise after reaching a certain value, it means that the position loop effect is OK;

At the same time, these two phenomena indicate that the cascade PID effect is OK.

3. Adjust parameters

3.1 Speed ​​inner loop setting

(1) We need to adjust the speed inner loop first to ensure the stability, speed and accuracy of the step response.

(2) The effect to be adjusted by the speed loop: from speed 0 to receiving a control target, the speed quickly reaches 20; from speed 20 to receiving a control target, the speed quickly reaches 0.

(3) The deviation of ±1 is the minimum level of the encoder, and the PID controller acts on the deviation, and it will take effect only after there is a deviation, so it belongs to the normal control range.

3.2 Integrated location outer ring

Because the output of the position loop should be used as the speed target value of the speed loop, it should be noted that when setting the parameters of the position loop, the output of the position loop should be close to the magnitude of the speed loop. This way of understanding: the magnitude of the speed loop refers to the target value of the speed loop It refers to the pulse value captured in each interrupt cycle. For example, the pulse value of the motor in every 10ms is relatively small, so the speed target value is also relatively small, so the parameters of the position loop may also be relatively small.

In short: the parameters of the position loop should be set according to the requirements of the speed loop, and the position loop must be limited to achieve the desired effect.

The effect of the cascade pid is as follows: the abscissa is time, and there are two ordinates, which are the position target value and the actual position value. The actual speed value can be reflected by the slope of the actual position curve. A constant slope means that the actual speed value is constant.

4. Problems encountered

Theoretically, after the speed loop and position loop are adjusted separately, the effect of cascade pid is better when integrated. If the parameters of the inner and outer loops are adjusted separately, it is normal. After the integration, the motor rotates randomly (not according to the ideal situation) turn), nine out of ten you have been grinding for a long time now, your head is already dizzy, it is definitely because your code is wrong, pay attention to the parameters input in the position loop and speed loop control functions do not make mistakes, I It was still adjusting at 3 o'clock in the morning, and it was not adjusted properly. The next day, I found that the input parameters of my function were wrong.

I filled in the wrong two parameters of the speed loop, which caused the motor to rotate randomly, which made me baffled.

Therefore, when you transplant the code, you must carefully analyze the code logic from top to bottom to make it clear and avoid low-level mistakes. 

5 Conclusion

Speaking of the end, I thought of the preface and said that I recently made a project that used cascaded pids. It is the last section of this series of tutorials, and it is time to tell everyone what the project I am doing. I made an intelligent elevator, and used the n20 motor to simulate the up and down movement of the elevator. The motor rotates forward and reverse at a constant speed to represent the elevator ascending or descending to different floors, and it can continue to work repeatedly, and the motor is running normally. , the error will not be too large each time. Through this project, I have a preliminary understanding of the speed loop, position loop, and cascaded Pid.

Later, I used the speed loop, position loop, cascade control and other operations again in many places. Every time I practice, I feel that I have a new understanding and experience of the pid control algorithm, so I feel that I want to complete a knowledge at a time. It is impossible to master it, or to master it more comprehensively; after initially learning a knowledge, you must practice more in other projects to achieve a more comprehensive and reliable understanding of the knowledge, instead of just staying in the meeting Use, only half-knowledge of knowledge. You know, often climbing up, the real masters fight, it's all about basic knowledge and ability! Let's encourage each other!

So far, this series of tutorials has been preliminarily completed. I will improve the articles from time to time in the future with new experience and understanding, but I will ensure that there are no mistakes in the articles published every time. Please rest assured to learn!

Code words are not easy, I hope friends who like it don't forget to like + bookmark + follow , your affirmation is the driving force for my creation.

欢迎大家积极交流,本文未经允许谢绝转载!!!

Guess you like

Origin blog.csdn.net/weixin_62261692/article/details/129843668