Use STM32F1 timer and Hall sensor to measure the spindle speed of drone/unmanned vehicle

Use STM32F1 timer and Hall sensor to measure the spindle speed of drone/unmanned vehicle

In the international standard, the international standard unit is RPM (Revolutions Per Minute, rev/min).

measurement algorithm

The author successively adopted two methods to measure the spindle speed. The first one counts the number of rising or falling edges within a specified time (1 second). On the basis of installing three Hall sensors, each time When the fourth rising edge/falling edge occurs, it means that the rotation of a circle is completed. At this time, the number of circles is increased by one (or the total number of rising edges is divided by three to obtain the number of rotations). Multiplying the number of rotations in one second by 60 is the standard speed RPM, referred to as "counting method"; the
  second method is not to collect the number of jump edges, but to keep Time is recorded. Whenever it is judged that a revolution is completed, the time is recorded. Divide 60 by the time of a revolution (converted into seconds) to get the real-time RPM. If the speed is too fast, you can accumulate the number of revolutions and RPM , when displaying in the loop of the main function, the average value can be taken and then cleared, which is referred to as "timing method".
insert diagram here

Fig.1 Schematic diagram of Hall measurement method

Compared with the two methods, the former:
  when the speed is slow, for example, the RPM is lower than 60, that is, the rotation time is less than 1 circle within 1 second. At this time, the calculated rotational speed is always 0;
the timing time of this method is the number of revolutions per second, and the international standard is in minutes. When converting, it needs to be multiplied by 60, so the final result must be a multiple of 60. The accuracy can be improved by lengthening the statistical time. However, due to the upper limit of the STM32 internal timer and the requirement of real-time display frequency, the expression is destined to be inaccurate;
combined with the actual experimental data, this method has a large error.
The latter:
  By counting the time of one revolution, or recording and accumulating the time of multiple revolutions, there is no need to be bound by a fixed time.

specific code

The code library uses the HAL library that can be developed conveniently with STMCUBEMX. The advantage of this library is that it is easy to use and the development cycle is short; the disadvantage is that the encapsulation is too high, and it is not suitable to troubleshoot problems when the function is used improperly.

The codes involved in monitoring the spindle speed algorithm are mainly the definition of the variables used, the accumulation of a timing cycle in the callback function of timer 1, and the capture of the rising edge of the Hall input pin by the timer:

Definitions of variables used:

unsigned char  TIM1CH1_CAPTURE_STA=0;	//输入捕获状态,用于累加定时器的周期个数。如果需要更长时间间隔,可以将STA数据类型变大				
unsigned short	TIM1CH1_CAPTURE_VAL;	//当前定时器的输入捕获值
unsigned int count = 0;	//统计总圈数,用于打印时计算平均转速,打印后清零。
float RPM = 0;

Timer callback function, the number of cycles plus one

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    
    
	//printf("HAL_TIM_PeriodElapsedCallback\n");
  if(htim->Instance == TIM1)
	{
    
    
		TIM1CH1_CAPTURE_STA++;
	}
}

Rising edge callback function

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    
    
	
	static unsigned char j = 0;	//静态变量,只会初始化一次,用于统计三分之一圈的次数
	unsigned int time = 0;		//用于统计这一圈运动所用时间,单位微秒

	//printf("上升沿\n");
	j++;
	if(j == 1)						//这个算法其实忽略掉了每次j从0到1的时间,即舍弃了三分之一的圆周运动。这样做的原因是避免j == 4时的操作过多从而造成的定时器清零和下一个三分之一旋转开始的不同步,影响测量精度。根据与现有转速传感器的对比结果来看效果较好。
	{
    
    
		__HAL_TIM_SET_COUNTER(&htim1,0);
		TIM1CH1_CAPTURE_STA = 0;
		TIM1CH1_CAPTURE_VAL = 0;
	}
	
	if(j == 4)	//第四次补货到了上升沿
	{
    
    
		count++;	//用于main函数打印平均转速时用到的总圈数,该值+1.
		TIM1CH1_CAPTURE_VAL = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_1);
		time= TIM1CH1_CAPTURE_VAL + TIM1CH1_CAPTURE_STA*65536;
		//	printf("RPM 原始值 = %f\n",60/((float)time/1000000));
		RPM += 60 / ((float)time/1000000);	//在main打印之前累加RPM
		//	time = 0; 回调函数结束后自动释放该变量,无需清零。
		j=0;	//
	}
}

Main function, print speed information

int main(void)
{
    
    
  /* MCU Configuration--------------------------------------------------------*/

  /* USER CODE BEGIN Init */
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();
  
  /* USER CODE BEGIN 2 */
	HAL_TIM_Base_Start_IT(&htim1);
	HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_1);//TIM1 CH1 
  /* USER CODE END 2 */
  /* 其他初始化代码--------------------------------------------------------*/
  
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		printf("RPM:%f,count == %d\r\n",RPM/count,count);//´打印两次进入while(1)循环之间的平均RPM。
		count = 0;
		RPM = 0;
  }
}

The code is for ideas only. If there are any inappropriateness and suggestions, please point them out.

Experimental part

Experimental process: The optical speed sensor of UNI-T combined with the white cloth strip is used as the standard data for the experiment, and the RPM and count can be displayed in real time. The actual picture is shown in the following 2.
COUNT
insert image description here

Figure 2 Unilever Optical Speed ​​Sensor

  The circuit board consists of a core board and a monitoring board near the main shaft. The core board uses STMF103 as the main control, and the input of the Hall sensor on the monitoring board is output to the pin of STM32 through a voltage comparator to complete the monitoring. The two are connected by a socket.
Figure 3 Monitoring circuit board

Driven by the ESC, the speed data is recorded under different PWMs. The motor starts to rotate when the PWM is 1400, the upper limit is 2200, and the interval is 100.
The experimental results are as follows:

PWM Reference speed (RPM) Hall speed (chrono method) Hall speed (counting method)
1400 816 815 1100
1500 950 945 1120
1600 1090 1086 1200
1700 1225 1223 1260
1800 1350 1353 1400
1900 1500 1490 1500
2000 1630 1625 1500
2100 1765 1762 1620
2200 1853 1855 1860

According to the average error calculation formula

insert image description here

The average speed error obtained by the timing method is 3.8RPM. In the case of a certain error in the reference data, the accuracy is higher.

Guess you like

Origin blog.csdn.net/Sky777wdnmd/article/details/123552067