2021 national college student electronic design competition question F intelligent drug delivery car

Keywords: Python OpenCV AI stm32

2021 national college student electronic design competition question F intelligent drug delivery car

Premise: This article focuses on sharing our own experiences and feelings and reflecting on our own shortcomings. The problem completion of our group is not very good. We solved the most important part, camera tracking and camera digital recognition, but finally lost in motion control.

People often hope to gain experience from winners, but there are not so many winners in life. I think only losers have the most say and experience to share. But I don't want to be a loser. Only we know how much we have paid and how much we have adhered to. As long as we go all out, it doesn't matter to fail. Maybe looking back many years later, I can still burst into tears when I think of these blood, so I have no regrets.

1, Topic analysis
2, Division of labor and construction of trolley
3, Camera part
4, Control part
5, Joint commissioning
6, Casual talk

1, Title





We are in the direction of measurement and control. At that time, the problem came out around 7:30 a.m. on the 4th, and then someone sent it to the group.
After looking at the measurement and control direction, in addition to the UAV, there are questions D and F. question D is an Internet-based camera measurement system (question d). We don't know much about this and don't have any confidence, so we didn't dare to choose. Later, I heard that those who choose question D can find the source code on GitHub, so we have a little regret when the rear car can't be adjusted. Ha ha.

F is a classic car topic, but different from previous years, this year's identification and control are the focus. You must be able to identify first before you can talk about the next tracking and motion control.
When it comes to recognition, we need to use the camera, but we didn't pay much attention to the camera before. Generally, the tracking uses infrared tube, gray sensor or linear CCD. However, in this competition, there are cross and black-and-white color blocks. It is also required to return automatically. It's difficult to use ordinary tracking module.

2, Division of labor and construction of trolley

None of the three in our group had played with the camera. We wanted to start learning now. At first, we prepared to use openmv for tracking and number recognition. Later, we found that our version of openmv could not train the neural network and had to use openmv plus (but there was no, we didn't rush to buy it). It was very uncomfortable, so we used openmv tracking and k210 to identify numbers.
One of the students wrote the code of openmv tracking, one of them completed k210 digital recognition, and I was responsible for the project of stm32f4 to create the bottom code and motion control of the car.
Due to the sufficient preparations for the race, the car was built that morning. The balance car was used, a universal wheel was added, and the turning differential control was carried out.

Trolley Hardware:
STM32F411CEU6, TB6612, car model (with Hall encoder reduction motor), LM2596,MPU6050, aircraft model battery, openMV,K210
Trolley software:
The HAL library is used to establish the project, and the freeRTOS operating system is configured in cubeMX,

KEIL engineering procedure code:

Serial port redirection printf

/*Serial port redirection printf*/
int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart6, (uint8_t *)&ch, 1, 1000);
  return ch;
}

Encoder mode value

/*Encoder mode value*/
int Read_Encoder(uint8_t TIMX)           
{
   int Encoder_TIM;    
   switch(TIMX)
	 {
	   case 1:  Encoder_TIM= (short)TIM1 -> CNT;  TIM1 -> CNT=0;break;
		 case 2:  Encoder_TIM= (short)TIM2 -> CNT;  TIM2 -> CNT=0;break;	
		 
		 default:  Encoder_TIM=0;
	 }
		return Encoder_TIM;
}

Determine whether mpu6050 initialization is successful

void Mpu6050_Init()
{
	while(w_mpu_init() != mpu_ok)           
	{
		printf("0x%x (ID_ERROR)\r\n", w_mpu_init());
		HAL_Delay(10);
	}
	/*DMP initialization*/
	dmp_init();     
}

Initialization program

int main(void)
{
	/*Turn on encoder counting*/
	HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
	HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
	/*Turn on the motor PWM output*/
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
	/*Turn on the PWM output of the steering gear*/
	HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_4);
	/*Enable the USART1 Interrupt*/
	HAL_UART_Receive_IT(&huart1,(uint8_t *)&usart6_rxbuff,1);
	/*temporary*/
	Temporary();
	Mpu6050_Init();
	/*Enable timer 4 interrupt*/
	HAL_TIM_Base_Start_IT(&htim4);
	HAL_UART_Transmit(&huart6, (uint8_t *)&cc, 1,0xFFF);
//	TIM3 ->CCR1 = 400;
//		TIM3 ->CCR2 = 400;
  /* USER CODE END 2 */

  /* Call init function for freertos objects (in freertos.c) */
    MX_FREERTOS_Init();
	HAL_UART_Transmit(&huart6, (uint8_t *)&cc, 1,0xFFF);
  /* Start scheduler */
    osKernelStart();
   while (1)
  {
    /* USER CODE END WHILE */
//		printf("-------------\r\n");
//		HAL_Delay(500);
    /* USER CODE BEGIN 3 */
		
		
  }
  /* USER CODE END 3 */
}

Code in freeRTOS.C file

The line patrol and function implementation codes are placed in freeRTOS.C

3, Camera part

For the camera part, the senior wrote the line patrol code of openmv on the first day
After processing the data in openmv, you can find the red line, identify the cross, and identify the black-and-white color block,
Returned data:
The left and right lines are returned as analog quantities, which are transmitted to the single chip microcomputer for pid control

K210 digital identification:


4, Control part

Control part, PD control used for line patrol

First, speed closed loop, PI control, to keep the car at a stable speed

Superimpose a layer of direction loop on the speed loop, PD control, line patrol

int track_control(float collect_openmv)
{
	static float last_bias,Bias,kp=0.75,Kd=0.25;//The effect of 0.5 is OK
	float Turn;     	  
	Bias=-(collect_openmv-60);
	Turn=-Bias*kp-Kd*(Bias-last_bias);
	last_bias = Bias;
	//printf("\r\nt:%.1f\r\n",Turn);
	return Turn;
}

The automatic return of the trolley and the determination of cross turning, black-and-white block parking use the flag bit, define the array, and record the direction of going back according to the principle of out of stack and in stack.

5, Joint commissioning

The data of openmv and k210 is transmitted to the single chip microcomputer
Open the serial port interrupt. Note that the communication protocol between the camera and the MCU needs to be written, as follows:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)    //Serial port interrupt, remote control part.
{
	uint16_t tem;
	if(huart->Instance == USART1)	// Determine which serial port triggered the interrupt
	{

		//printf("ok");
		tem = usart6_rxbuff;
		printf("%x\r\n",tem);
		if(RxState==0&&tem==0x79)
		{
			RxState=1;
			RxBuffer1[RxCounter1++]=tem;
		}
		else if(RxState==1)
		{
			RxBuffer1[RxCounter1++]=tem;
			if(RxCounter1>=10||tem == 0x85)       //RxBuffer1 is full, receiving data ends
			{
				RxState=2;
			}
		 }
		 else if(RxState==2)		//Detect whether the end flag is accepted
		 {
				if(RxBuffer1[RxCounter1-1] == 0x85)
				{
						if(RxBuffer1[1] == 0xaa)//distinguish
						{
							Flag = Flag + 1;
		
							JG=RxBuffer1[RxCounter1-2];
							Turn = RxBuffer1[RxCounter1-3];
							if(Flag>3)
							{
								if(JG>0&&FS==0) {FS = JG;}
								printf("\r\n Turn:%d FS:%d  \r\n",Turn,FS);
							}

						}
						if(RxBuffer1[1] == 0xbb)//Line search
						{
							printf("\r\n___sou xian___\r\n");
							printf("\r\n Cx:%d, STOP:%d \r\n",Cx,STOP);
							Cx=RxBuffer1[RxCounter1-2];

							STOP=RxBuffer1[RxCounter1-3];
						}
						


							RxCounter1 = 0;
							RxState = 0;
						
				}
				else   //Receive error
				{
							RxState = 0;
							RxCounter1=0;
							for(i=0;i<10;i++)
							{
									RxBuffer1[i]=0x00;      //Clear the stored data array
							}
							printf("Receive error\r\n");
				}
			}
		 if(state==2)
		 {
			 if(STOP == 0)
			 {
				 state = 0;
			 }
		 }
		 if(state==4)
		 {
			 if(STOP == 0)
			 {
				 state = 0;
			 }
		 }
	}
	HAL_UART_Receive_IT(&huart1, (uint8_t *)&usart6_rxbuff, 1); 
}

6, Casual talk



We finished the number recognition the night before the end. There was only one day left for the logic of the whole shunting program. Seriously, the program was written less. It was really difficult to think of the program logic, especially when we had slept only two hours a day for three consecutive days. Our mind was very chaotic and dull.
We should write more programs and practice our logical thinking in writing programs.

**Conclusion: * * I participated in the national competition for the first time and the video competition for the first time. It should have started at the end of my freshman year. The video competition was postponed to a period of time after the beginning of my sophomore year. I still need to learn a lot. Study hard, improve my skills, and continue to rush for the provincial competition next year!

Project code:

openmv line patrol Code:
https://download.csdn.net/download/cubejava/40298555
k210 digital identification:
https://download.csdn.net/download/cubejava/40320008
stm32HAL library keil project:https://download.csdn.net/download/cubejava/40319397
Fusion code of line patrol and digital identification (K210):https://download.csdn.net/download/cubejava/40320331
**Note: * * these are the program codes that we boil out day and night. It takes a lot of energy, so there is no free open source (hope to understand), and the payment is set. However, relevant ideas are also written in the blog. You can refer to and learn from some experience, and come on in the next year!!!
Now video games are more and more inclined to vision and artificial intelligence. We must study camera, vision and neural network.

Posted by mikewooten on Thu, 11 Nov 2021 09:50:10 -0800