In electronic design competitions, we sometimes hope to measure the speed of the friction wheel of the robot's gimbal launch mechanism. One reason is to prevent the starting current from being too large when the friction wheel is ramp-started, and the other is to prevent the speed of the friction wheel from being affected by the bullet being ejected during the firing process, resulting in speed fluctuations.
In the early stage, we had two solutions, one was to use the AS5048 encoder to measure the speed, and the other was to use the Hall sensor to measure the speed. When verifying the codes of the two solutions, since AS5048 is rarely used in China and there is little information, I had to read its English documents and found that AS5048 is actually an absolute encoder and is not suitable for measuring friction wheels with such a high speed. However, given the power of STM32F4 (high-frequency processing capability), we finally used the average method twice to calculate the speed of the friction wheel. I will talk about the speed measurement method and code of AS5048 when I have the chance.
Next, let's talk about the Hall sensor speed measurement we use. After a deeper understanding, it is found that there are actually two methods for Hall sensor speed measurement, one is the frequency measurement method, and the other is the period measurement method.
The so-called frequency measurement method is to use two timers, such as TIM2 and TIM3, where TIM2 is used to capture the falling edge of the Hall sensor and count the falling edge in the capture interrupt, and TIM3 is used to count a fixed period of time, so that the number of pulses obtained within a certain period of time can be measured. Assuming that the TIM3 timing time is T, the number of magnets installed on the friction wheel is N, and the number of pulses obtained is P, it can be known that the friction wheel speed is (P*60)/(N*T).
The so-called cycle measurement method is to use only one timer, such as TIM2, and use the TIM2 input to capture the falling edge output of the Hall sensor. However, we do not count the falling edge at this time, but directly calculate the time between two input captures, and this time is the time used for turning 1/N circles. In this way, the speed can also be obtained, and only one timer is used. However, when writing the code for the cycle measurement method, I realized an important thing, that is, the frequency of the system clock will directly affect the accuracy of the cycle measurement method.
Here we need to add some important information about the system clock. By reading P108 of the "STM32F4 Development Guide - Library Function Version", we know that "STM32F4 has two PLLs. The first one is the main PLL (PLL) which provides the clock signal from HSE or HSI and has two different output clocks. The first output PLLP is used to generate a high-speed system clock (up to 168MHz), and the second output PLLQ is used to generate the clock of USB OTG FS (48MHz), the clock of the random number generator and the SDIO clock; the second one is a dedicated PLL (PLLI2S) used to generate a precise clock, so as to achieve high-quality audio performance on the I2S interface." When we use the clock used by TIMx for input capture, this clock is actually obtained by dividing the first output of the above main PLL clock. So the question is, what is the frequency of the main clock? What is the division factor of TIMx? Only by determining these two quantities can we correctly initialize our input capture function.
Similarly, according to the "STM32F4 Development Guide - Library Function Version" P108, the calculation formula for the first output high-speed clock PLLP generated by the main PLL can be obtained, PLL=E*N/(M*P), where E is the external crystal frequency, N is the multiplier multiplication factor, and M and P are both divider division factors (for their specific locations, see the STM32 system clock diagram of "STM32F4 Development Guide - Library Function Version" P108). At the code level, E is the external crystal frequency, which can be obtained by referring to the core board schematic diagram, and N, M, and P can all be seen in the system_stm32f4xx.c file. This file also provides the bus division factors of various F4 MCUs, such as APB1 Prescaler, APB2 Prescaler, etc., which can all be found and modified.
The necessity of understanding the above is that since learners generally use the STM32F407 core board provided by Zhengdian Atom, its external crystal frequency is 8MHz, and the other three coefficients are M=8, N=336, and P=2. Substituting into the above calculation formula, the main PLL clock is 168MHz, and the frequency division coefficient of APB1 is 2. However, when we want to transplant the code, we will inevitably encounter the situation where all these values have changed. For example, the STM32F427 board provided by DJI has an external crystal frequency of 12MHz. In order to ensure that the previous code in F407 can still be used, we must make certain code settings so that the calculated main PLL is still 168MHz. Simply set it to set the PLL_M value of line 316 in the system_stm32f4xx.c file to 12, and the PLL_N value of line 335 to 336. Substituting into the main PLL calculation formula, the calculation result is 168MHz. This value is generally the frequency of the system clock. In this way, the code of F407 can be easily transplanted directly to F427. It should be noted that the frequency division coefficients of each bus of F407 and F427 for the main PLL are also different, so special attention should be paid when initializing the clock. For example, after the above settings, the frequency division coefficient of APB1 of F407 is 4, while that of F401 is 2. This is very important to pay attention to in various initializations of clocks. Unless the frequency division coefficient of APB1 is 1, the clock of the general timer is equal to twice the clock of APB1.
There is another point that needs special attention. Generally speaking, the larger the count value of input capture, the better. In the general timer, TIM2 and TIM5 are both 32-bit timers, and these two timers should be used as much as possible for speed measurement. For example, we initialize the TIM5 clock to TIM5_CH2_Cap_Init(0xFFFFFFFF,42-1). Simple calculation shows that the time required to count 0xFFFFFFFF is about 4000 seconds, which is more than enough for measuring most signal frequencies. If a 16-bit timer is used, its maximum count value is 0xFFFF, and the counting time is greatly reduced to about 66ms, which is generally not suitable for input capture.
By understanding the above knowledge points, you can obtain accurate pulse period when using the period measurement method to measure speed, accurately obtain speed and perform feedback control.
In general, the frequency measurement method is only suitable for testing high-frequency signals and can give speed values within a fixed time, while the period measurement method is more applicable to low-frequency and high-frequency signals, but its measurement speed is controlled by the measured pulse. When MCU resources are scarce, it is more appropriate to use the period measurement method.
Previous article:Graphics and texts teach you to understand the STM32 microcontroller clock
Next article:STM32F0 Development Notes 7: Pin Usage Notes
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- [Sipeed LicheeRV 86 Panel Review] 2. Data Links
- TI DSP CAN online program upgrade question
- List of instruments, equipment and main components for the 2019 National Undergraduate Electronic Design Competition
- Has this condition reached the level of shock (moderate)?
- TE wireless connectivity unleashes the unlimited potential of the Internet of Things. Join the challenge and win great prizes!
- Please tell me about the stop mode of RL78
- Briefly talk to beginners about concepts and choices such as HASL and OSP
- New books exchanged
- DLP LightCrafter Display 2000 Evaluation Module with Embedded Linux Host Processor
- When the circuit designed with TPS40057 is powered on, a sound similar to a relay closing can be heard, and the power output is abnormal.