A hardware-in-the-loop pulse frequency adaptation measurement analog device and method
By using a hardware-in-the-loop pulse frequency adaptive measurement device, combined with an STM32F407 chip and a MATLAB/Simulink system, the problem of traditional pulse frequency measurement methods being unable to balance high and low frequency adaptability, accuracy, and real-time performance is solved. This achieves high-precision, wide-range, and highly interactive pulse frequency measurement, which is suitable for university teaching and industrial testing.
Patent Information
- Application Number
- CN202610115909.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-28
- Publication Date
- 2026-04-14
- Estimated Expiration
- 2046-01-28
AI Technical Summary
Existing pulse frequency measurement methods struggle to balance high and low frequency adaptability, accuracy, and real-time performance. They lack hardware-in-the-loop interaction and visualization capabilities, have weak anti-interference capabilities, and their parameters are fixed and have poor scalability, failing to meet the high precision and stability requirements of teaching experiments.
Using an m-line AB-phase quadrature Hall encoder and an STM32F407 chip with dual independent timers, combined with a hardware-in-the-loop simulation verification system, adaptive measurement of pulse frequency is achieved through pulse count holding, linear conversion of direction signal, adaptive adjustment of counting period, pulse jitter suppression, and noise suppression type reset time correction algorithm.
It achieves high-precision measurement across the entire frequency band from 5Hz to 5kHz, supports online parameter adjustment and visualization, enhances anti-interference capabilities, adapts to different levels of simulation experiment requirements, and reduces implementation costs.
Smart Images

Figure CN121595951B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of experimental simulation equipment technology, specifically relating to a simulation device and implementation method for pulse frequency measurement and motor speed detection. Background Technology
[0002] Pulse frequency measurement is a core technology in fields such as motor control and industrial testing, and it is also an important part of practical simulation in related majors in universities. Currently, the mainstream pulse frequency measurement methods include the M-method (fixed period counting method), the T-method (fixed pulse number timing method), and the M / T hybrid speed measurement method. Their technical limitations have become a pain point in simulation and engineering applications.
[0003] The M-method measures speed by counting pulses within a fixed time window. While its accuracy is acceptable at high speeds due to a sufficient number of pulses, insufficient pulses at low speeds lead to significantly increased errors, with relative errors exceeding ±2.5%. The T-method calculates rotational speed by measuring pulse intervals. It offers high accuracy at low speeds, but at high speeds, it requires waiting for a fixed number of pulses to accumulate, resulting in poor real-time performance and an inability to respond promptly to dynamic changes in rotational speed. The M / T hybrid method attempts to balance high and low frequency adaptability, but requires manually setting a switching threshold, leading to complex switching logic prone to fluctuations. Furthermore, its measurement accuracy is only ±0.6%, failing to meet the high precision and stability requirements of simulation experiments.
[0004] Existing analog pulse measurement schemes have significant shortcomings: First, they lack hardware-in-the-loop interactive capabilities. Traditional schemes are mostly implemented in a "black box" manner, preventing students from intuitively observing the intermediate processes of encoder counting, timer coordination, and algorithm calculation. Furthermore, core parameters (such as measurement period and anti-interference threshold) cannot be adjusted in real time, hindering understanding of how parameter changes affect measurement accuracy and limiting teaching effectiveness. Second, they have weak anti-interference capabilities. Electromagnetic interference in industrial environments and laboratories easily generates erroneous pulses, and traditional schemes lack targeted suppression mechanisms, leading to large fluctuations in measurement data and failing to meet the requirements of "stable data and verifiable principles" in teaching experiments. Third, their simulation scalability is insufficient. Existing schemes often rely on a single algorithm, failing to support advanced experiments such as algorithm comparison and parameter optimization, and are ill-suited to different levels of simulation needs. Fourth, their measurement range is narrow. Most analog devices can only adapt to specific frequency ranges, unable to achieve full coverage measurement of high and low frequency signals. Therefore, there is an urgent need for an experimental simulation scheme that balances high-precision measurement, strong interactivity (adjustable parameters and visualized processes), high scalability, and low implementation cost to address the adaptation deficiencies of existing technologies in teaching scenarios.
[0005] Furthermore, while the rotating machinery speed measurement method disclosed in patent CN106645784A improves accuracy through master-slave timer synchronous counting, it lacks simulation visualization and hardware-in-the-loop interaction, and has no noise suppression or dynamic parameter adjustment mechanisms, resulting in poor simulation adaptability. Although FPGA-based frequency counter solutions offer higher accuracy, their high hardware cost and programming barrier make them unsuitable for basic simulation scenarios. Therefore, there is an urgent need for an experimental simulation solution that balances high-precision measurement, strong interactivity, high scalability, and low implementation cost to address the many shortcomings of existing technologies.
[0006] This invention aims to overcome the shortcomings of existing pulse frequency measurement simulation devices, and specifically solves the following technical problems:
[0007] 1. Traditional measurement methods (M-method, T-method, M / T method) suffer from the contradiction of high-frequency and low-frequency compatibility, narrow measurement range, and difficulty in balancing accuracy and real-time performance;
[0008] 2. Existing simulation schemes lack hardware-in-the-loop interaction and visualization capabilities, the algorithm process is not transparent, and students cannot intuitively understand the core principles;
[0009] 3. It has weak anti-interference ability and cannot effectively suppress interference factors such as pulse jitter and electromagnetic noise, resulting in poor stability of measurement data;
[0010] 4. The parameters are fixed and have poor scalability, making it unable to support advanced simulation experiments such as algorithm comparison and secondary development;
[0011] 5. The simulation is not adaptable enough, making it difficult to balance the ease of operation of basic simulations with the exploratory needs of advanced experiments. Summary of the Invention
[0012] This invention aims to solve the problems of the prior art. It proposes a hardware-in-the-loop pulse frequency adaptive measurement simulation device and method. The technical solution of this invention is as follows:
[0013] A hardware-in-the-loop pulse frequency adaptive measurement simulation device, comprising:
[0014] The sensor module uses an m-line AB-phase quadrature Hall encoder to acquire pulse signals generated by the motor rotation. It supports dual-channel counting of rising and falling edges of channels A and B, and simultaneously acquires pulse quantity and rotation direction information.
[0015] A microcontroller with dual independent timers (in this embodiment, an STM32F407 chip with a main frequency of 168MHz) is configured. Timer TIM2 is configured in encoder counting mode (receiving AB phase encoder pulses to achieve pulse count statistics and forward / reverse direction identification), and timer TIM6 is configured in dynamic reset mode (TIM6 is configured in upward counting mode, and its automatic reload value ARR6 is dynamically adjusted to achieve adaptive changes in the reset time interval, triggering the capture of the count value before TIM2 timer reset). A pulse count holding algorithm, a direction signal linear conversion algorithm, a counting period adaptive adjustment algorithm, a pulse jitter suppression algorithm, and a noise suppression type reset time correction algorithm are configured and run. The pulse count holding algorithm is used to: latch the count value before TIM2 reset through a dual mechanism of state diagram holding and reset signal delay to avoid count loss; the direction signal linear conversion algorithm is used to: convert the direction signal output by TIM2... Transform into The system synchronously represents the magnitude and direction of rotation; the adaptive adjustment algorithm for the counting period is used to dynamically adjust the ARR6 value of TIM6 based on the deviation between the number of pulses per unit time and the ideal interval, balancing measurement accuracy and real-time performance; the pulse jitter suppression algorithm is used to eliminate erroneous pulses caused by electromagnetic interference by statistically setting a threshold through pulse intervals, ensuring counting accuracy; the noise suppression type reset time correction algorithm is used to smooth the reset time using a moving average algorithm, suppressing measurement noise caused by speed fluctuations.
[0016] The hardware-in-the-loop simulation verification system (built using MATLAB / Simulink in this embodiment) includes a digital twin model, a state diagram programming module, a real-time monitoring interface, and an online parameter adjustment module. The digital twin model is used to construct a digital twin of the measurement system, enabling hardware-in-the-loop simulation and algorithm pre-verification. The state diagram programming module is used to achieve the holding and latching of TIM2 count values through state diagram logic programming, supporting dual-mechanism count extraction. The real-time monitoring interface is used to receive real-time speed and pulse count data transmitted from the STM32 microcontroller and visualize dynamic curves using components such as an oscilloscope. The online parameter adjustment module provides interactive components such as sliders, supporting... , , , Core parameters can be modified online to adapt to different simulation experiment scenarios;
[0017] The serial port DMA communication module is used for real-time data interaction between the microcontroller (in this embodiment, the STM32F407 chip is selected) and the PC. The transmission frame includes a frame header, data length, rotation speed, pulse number and checksum, filters packet loss and bit errors, and ensures the reliability of hardware-in-the-loop data interaction.
[0018] Furthermore, the auto-reload register value of the first timer TIM2 Simultaneously, the count overflow interrupt of the first timer TIM2 is enabled. When an abnormal overflow occurs, the hardware-in-the-loop simulation verification system will alarm in real time and record the fault data.
[0019] A method for using the hardware-in-the-loop pulse frequency adaptive measurement simulation device includes the following steps:
[0020] Steps to establish the pulse-speed mapping relationship: Define the total number of encoder pulses corresponding to one revolution of the motor output shaft. Through formula Calculation, where For encoder lines, For counting multiples, This is the motor reduction ratio, enabling a direct mapping between the number of pulses and the rotational speed;
[0021] Dynamic reset time correction steps: Introduce a noise suppression algorithm and perform moving average smoothing;
[0022] Counting cycle adaptive shifting steps: based on a threshold of pulse count per unit time Dynamically adjust the ARR6 value and automatically clamp it when it exceeds the threshold;
[0023] A dual mechanism is used to extract the count value before TIM2 reset: the dual mechanism includes a state diagram programming retention mechanism and a reset signal delay mechanism;
[0024] Pulse jitter suppression steps: Set a pulse interval threshold and remove erroneous pulses with intervals exceeding the threshold;
[0025] Speed calculation and accuracy optimization steps: Introduce a dynamic accuracy weighting factor to obtain the optimized speed;
[0026] Direction recognition and data verification: through formulas Achieve linear conversion of directional signals. Corresponding to forward rotation, The corresponding inversion; serial communication uses a checksum formula to filter transmission errors.
[0027] Furthermore, the dynamic reset time correction step specifically includes: the basic reset time interval. Determined by the TIM6 timer parameters, the formula is as follows: , For pre-division coefficients, For automatic reload values, The mounting frequency of the second timer TIM6 is set to meet the accuracy requirements of pulse counting and period adjustment; a noise suppression algorithm is introduced, which smooths the original reset time using a moving average method, through the formula... Perform a moving average smoothing process. Indicates the first Corrected reset time after time step smoothing. Indicates the first Corrected reset time after time step smoothing. Indicates the first The time step is the original reset time calculated based on the TIM6 timer parameters. To smooth the weighting factor and suppress measurement noise caused by speed fluctuations.
[0028] Furthermore, the adaptive shifting step of the counting cycle specifically includes:
[0029] Based on the threshold of pulses per unit time , , The counting cycle automatic shifting strategy adopts a dynamically adjusted coefficient, and the second timer TIM6... Automatic reload value of time step Satisfying the formula:
[0030] ;
[0031] in For the second timer TIM6 The current auto-reload value at the time step. To adaptively adjust the gain, , For the first Time step pulse count value; when the new value is calculated Exceeding Automatic clamping to or .
[0032] Furthermore, the dual-mechanism extraction of the count value before TIM2 reset specifically includes:
[0033] ① State diagram programming maintains the mechanism, defining time points Counting and holding output When a reset signal is received, that is hour, , This is the real-time count value of TIM2; no reset signal is required. hour, , For a moment The count maintains the output value, initial value ② Reset signal delay mechanism, the reset signal is delayed. Satisfying the formula: ,in This is the original reset signal. This is the minimum clock step size of the microcontroller. This serves as the time marker for the current moment. Indicates compared to the current time Advance by a minimum clock step At that moment.
[0034] Furthermore, the introduced pulse jitter suppression algorithm specifically includes:
[0035] Based on pulse interval statistics, erroneous pulses are filtered out, and a pulse interval threshold is defined. Satisfying the formula:
[0036] ;
[0037] in This is the jitter tolerance factor. For the first The coefficient of variation of the time step pulse interval, , The standard deviation of the pulse interval. The average pulse interval; the interval between two consecutive pulses detected. When this occurs, it is identified as a false pulse and discarded. When the pulse is detected, it is considered a valid pulse and included in the count.
[0038] Furthermore, the speed calculation and accuracy optimization steps involve introducing a dynamic accuracy weighting factor to obtain the optimized speed, specifically as follows:
[0039] Optimized speed Satisfying the formula:
[0040] ;
[0041] in , To maintain the count value for TIM2, This indicates the total number of encoder pulses corresponding to one revolution of the motor output shaft. This represents the rotational speed direction weighting factor, which compensates for the accuracy loss due to the pulse number deviating from the ideal mean by a dynamic weighting factor, thus reducing the absolute value of the measurement error across the entire frequency range. The measurement error is calculated as: |Measured speed value - Actual speed value| / Actual speed value .
[0042] Furthermore, in the pulse jitter suppression algorithm, the coefficient of variation of the pulse interval... The window length is calculated using the sliding window method. ,Right now ,in This is the sequence number of the current time step. Indicates the current time step The sequence number of the previous 9th time step, Indicates the first The time interval of the current moment and the preceding 9 moments, spanning 10 consecutive time steps, is based solely on statistics of the most recent 10 valid pulse intervals to ensure a rapid response to instantaneous jitter. For the first Standard deviation of pulse interval at time point and the previous 9 time points. The average pulse interval for the corresponding interval, and the window length. Ensure a rapid response to momentary jitter.
[0043] Furthermore, the serial port DMA communication between the microcontroller and the hardware-in-the-loop simulation verification system adopts a data verification mechanism, and the data frame format is: [frame header 0xAA] + [data length] + [rotation speed]. ] + [Pulse Count] ] + [checksum], checksum The calculation formula is:
[0044]
[0045] in To optimize the speed binary bytes and, pulse count value The binary bytes are used to filter packet loss and bit errors during serial port transmission.
[0046] The advantages and beneficial effects of this invention are as follows:
[0047] 1. Breaking through the traditional mindset of "fixed parameters / manual switching" conventional technology (M / T hybrid method) requires manual setting of high and low frequency switching thresholds, which is prone to fluctuations; this invention, through the adaptive logic of "pulse number feedback - dynamic modification of ARR6" (i.e., the counting cycle adaptive shifting step), can adapt to the entire frequency band of 5Hz~5kHz without manual intervention, solving the inherent contradiction of "large error at low speed and poor real-time performance at high speed" in traditional algorithms;
[0048] 2. The conventional technique for extracting count values uses only a single interrupt or state latch to store the count value, which is prone to data loss due to signal transmission delay. This invention innovatively adopts a dual mechanism of "state diagram retention + ns-level delay", which not only ensures the stability of the count latch but also avoids the accuracy loss caused by the delay. This detailed optimization of the hardware characteristics of the STM32 timer requires a deep understanding of the timer interrupt and signal synchronization principle, which is not a conventional basic application approach.
[0049] 3. The hardware-in-the-loop simulation verification system (built using Simulink in this embodiment) supports functions such as real-time visualization of rotational speed, online parameter modification, and disturbance simulation, allowing students to intuitively observe the algorithm's operation process; it also supports secondary development, enabling the expansion of advanced experiments such as algorithm comparison and parameter optimization, adapting to all levels of needs from basic simulation to scientific research training.
[0050] 4. Conventional anti-interference methods rely solely on hardware filtering (such as optocouplers) or single software filtering, with accuracy optimization often involving fixed-coefficient corrections; this invention incorporates "time smoothing" ( ), pulse statistical filtering ( Dynamic weight compensation The three algorithms are integrated to specifically address three types of problems: speed fluctuation noise, electromagnetic interference false pulses, and accuracy loss due to pulse number deviation.
[0051] 5. Deep Adaptation to Industrial-Grade Precision and Simulation Scenarios: Conventional high-precision solutions (FPGA-based) are costly and have high programming barriers, making them unsuitable for simulation; conventional simulation solutions are mostly "black boxes," with opaque algorithms and unadjustable parameters; this invention utilizes a low-cost architecture of a microcontroller + hardware-in-the-loop simulation verification system (in this embodiment, it is built using STM32 + Simulink) to deeply adapt the core algorithm parameters ( , , , Modular and visualized, it supports hierarchical requirements of "basic simulation - advanced experiment - innovative extension", while realizing the absolute value of measurement error. Industrial-grade accuracy (measurement error is calculated as: |Measured rotation speed - Actual rotation speed| / Actual rotation speed) ). Attached Figure Description
[0052] Figure 1 This is a system architecture diagram of a preferred embodiment of the present invention, showing the connection relationship and data flow of the sensor module, the microcontroller with dual independent timers, and the hardware-in-the-loop simulation verification system;
[0053] Figure 2 This is a timing diagram of the TIM2 and TIM6 timers working together, illustrating the time synchronization relationship between the generation of the reset signal and the capture of the count value;
[0054] Figure 3 This is a basic version of the pulse frequency adaptive measurement block diagram without filtering and accuracy optimization.
[0055] Figure 4 This is a system structure diagram of the present invention. Detailed Implementation
[0056] The technical solutions of the embodiments of the present invention will be clearly and thoroughly described below with reference to the accompanying drawings. The described embodiments are merely some embodiments of the present invention.
[0057] This invention provides a hardware-in-the-loop pulse frequency adaptive measurement experiment simulation method based on STM32 and Simulink. Through sensor data acquisition, dual timer collaborative control, multi-algorithm fusion optimization, and hardware-in-the-loop verification, it realizes high-precision, wide-range, and highly interactive pulse frequency measurement and simulation experiments.
[0058] (I) System Composition
[0059] 1. The sensor module uses an incremental AB-phase quadrature encoder (line count is...). , (A positive integer), used to collect pulse signals generated by motor rotation, supports dual-channel counting of rising and falling edges of channels A and B, and synchronously acquires pulse quantity and rotation direction information;
[0060] 2. A microcontroller with dual independent timers (in this embodiment, an STM32F407 chip with a main frequency of 168MHz is used): The TIM2 timer is configured in encoder counting mode and the TIM6 timer is configured in dynamic reset mode. The microcontroller runs pulse count hold algorithm, direction signal linear conversion algorithm, counting period adaptive adjustment algorithm, pulse jitter suppression algorithm and noise suppression type reset time correction algorithm to achieve accurate pulse signal acquisition, interference filtering and dynamic optimization of measurement period.
[0061] 3. Hardware-in-the-loop simulation verification system (built using MATLAB / Simulink in this embodiment): It includes a digital twin model, a state diagram programming module, a real-time monitoring interface and a parameter online adjustment module. It communicates with the STM32 microcontroller in real time via serial port DMA to realize the visualization of measurement data, real-time modification of control parameters and closed-loop optimization of algorithms. It supports the whole process of simulation experiment from "interference simulation-algorithm verification-effect feedback".
[0062] (II) Core Algorithm and Logic
[0063] 1. Establishing the pulse-speed mapping relationship: Define the total number of encoder pulses corresponding to one revolution of the motor output shaft. Through formula Calculation, where For encoder lines, For counting multiples, This is the motor reduction ratio, enabling a direct mapping between the number of pulses and the rotational speed;
[0064] 2. Dynamic reset time correction: Basic reset time interval Determined by the TIM6 timer parameters, the formula is as follows: ( For pre-division coefficients, For automatic reload values, (The frequency of TIM6 mounting); a noise suppression algorithm is introduced, using the formula... Perform moving average smoothing ( (For smoothing weighting factors), suppressing measurement noise caused by speed fluctuations;
[0065] 3. Adaptive Counting Cycle Shifting: Based on a threshold for the number of pulses per unit time. ( , ), through formula
[0066] ;
[0067] Dynamic adjustment value( (for adaptive gain adjustment), exceeding Automatic clamping within the range ensures that the pulse count is always within a high-precision range;
[0068] 4. Pulse jitter suppression:
[0069] Through formula ;
[0070] Set pulse interval threshold ( This is the jitter tolerance factor. (This is the pulse interval variation coefficient), which eliminates erroneous pulses with intervals exceeding the threshold, thereby improving counting accuracy;
[0071] 5. Rotational speed calculation and accuracy optimization: The basic rotational speed is calculated using the formula... calculate( To maintain the count value for TIM2, (For direction weighting factors), a dynamic accuracy weighting factor is introduced to optimize the rotational speed.
[0072] ;
[0073] ( , ), to compensate for the accuracy loss caused by the number of pulses deviating from the ideal average;
[0074] 6. Direction Recognition and Data Verification: Using formulas... Achieve linear conversion of directional signals ( Corresponding to forward rotation, (Corresponding to the reverse); serial communication uses a checksum formula.
[0075]
[0076] Filter transmission errors.
[0077] I. Hardware Selection and Configuration
[0078] 1. Core Controller: The STM32F407VET6 microcontroller is selected, with a main frequency of 168MHz. It has abundant timer resources and DMA channels to meet the requirements of high-speed counting and real-time communication.
[0079] 2. Sensor Module: Employs a 600-line AB-phase quadrature Hall encoder ( Supports dual-channel counting of rising and falling edges for channels A and B. ), motor reduction ratio ( The calculated value is Z = 600 × 4 × 10 = 24000; the incremental AB phase quadrature encoder can be either Hall type or photoelectric type, and this embodiment uses a 600-line Hall encoder ( ).
[0080] 3. Peripheral Configuration: ST-LINK debugger for program download and debugging; USB to serial port module for communication between STM32 and PC; 12V DC power supply for system power, 5V voltage regulator module for encoder and microcontroller peripherals;
[0081] 4. Auxiliary circuit: The encoder signal is shaped by an optocoupler and then input to the TIM2_CH1 (PA0) and TIM2_CH2 (PA1) pins of the STM32 to avoid electromagnetic interference; the serial communication pins (USART1_TX / PA9, USART1_RX / PA10) are configured with DMA channels to improve data transmission efficiency.
[0082] II. Software Configuration and Implementation
[0083] (I) STM32CubeMX Configuration
[0084] 1. Clock configuration: Enable HSE external high-speed crystal oscillator (8MHz), clock tree configured to 168MHz, TIM2 connected to APB1 bus (42MHz), TIM6 connected to APB1 bus (42MHz).
[0085] 2. Timer Configuration: Set TIM2 to encoder mode, enable synchronous counting for channels A and B, and automatically reload values. To avoid count overflow; TIM6 is set to upward counting mode, with a prescaler coefficient. (Counting frequency 1kHz), initial auto-reload value ;
[0086] 3. Serial port and DMA configuration: Set USART1 to asynchronous mode (baud rate 115200bps), enable DMA channels for TX / RX (DMA1_Stream7, DMA1_Stream2), and configure them to loop mode;
[0087] 4. Interrupt Configuration: Enable TIM6 overflow interrupt (used to trigger reset signal) and TIM2 count overflow interrupt (used for fault alarm), and set the interrupt priority to medium.
[0088] (II) Simulink Model Building
[0089] 1. Hardware connection configuration: Select the appropriate microcontroller board (in this embodiment, the “STM32F4xx Based” board is selected), associate the .ioc file generated by CubeMX, set the External mode communication interface to Serial, and select the COM port of the corresponding USART;
[0090] 2. Core Module Design: Includes a data receiving module (parses the speed and pulse count data sent by STM32), a real-time monitoring module (displays the dynamic speed curve on an oscilloscope), and a parameter adjustment module (slider settings). , , , The system includes a parameter (e.g., interference simulation module, generating 0~100Hz pulse jitter), and an algorithm verification module (comparing the measurement results of the algorithm of this invention with those of traditional algorithms).
[0091] 3. Code generation and deployment: C code is generated using the "rapid prototyping" mode and downloaded to the hardware via the corresponding microcontroller support package (in this embodiment, it is downloaded via the STM32 support package) to achieve hardware-in-the-loop real-time communication.
[0092] III. Experimental Verification and Simulation Application
[0093] (a) Performance verification experiment
[0094] Comparison table of accuracy of various measurement methods at different frequency points
[0095]
[0096] Experimental results show that the absolute value of the measurement error of the method of the present invention is within the full frequency range. (The measurement error is calculated as: |Measured speed value - Actual speed value| / Actual speed value) The data stability is significantly better than that of traditional methods, and there is no obvious contradiction between high and low frequency adaptation.
[0097] (II) Simulated Scenario Application
[0098] 1. Basic Simulation: Students observe the dynamic changes in pulse count, reset time, and rotation speed in real time using Simulink, and understand the basic principles of encoder counting and timer coordination.
[0099] 2. Advanced Experiment: Adjustment , , , The effects of parameters on measurement accuracy were observed; the error curves of the algorithm of this invention and the traditional algorithm were compared to analyze the mechanism of the anti-interference algorithm.
[0100] 3. Innovation and Expansion: Develop new optimization algorithms (such as neural network accuracy compensation) based on this system, and verify the algorithm's effectiveness through hardware-in-the-loop verification; simulate industrial noise environments and design targeted interference suppression schemes.
[0101] IV. Key Parameter Description
[0102] - Pulse count threshold Default settings , It can be adjusted according to simulation needs (basic simulation set to...). Advanced experiments are set as follows: );
[0103] - Smoothing weighting factor :in Optimized rotational speed for the current time step (Unit: r / min) The average rotational speed over the most recent 10 time steps
[0104] ( (Unit: r / min) (The absolute value of the difference between the current speed and the average speed, in r / min); when When the speed fluctuation is small, Set to 0.8; when When (speed fluctuations are large), Set to 0.6 to suppress speed fluctuation noise by dynamically adjusting the smoothing weight;
[0105] - Adaptive gain adjustment The range of values is ,in Based on the simulation of the optimal value, The optimal value is selected for dynamic tracking experiments; 0.1 is used for basic simulation (for stable adjustment), and 0.15 is used for dynamic tracking experiments (for faster response).
[0106] - Jitter tolerance factor The default value is 0.3, which can be adjusted according to the interference intensity (0.2 for strong interference environment and 0.4 for weak interference environment).
[0107] This invention achieves a unified approach to high precision, wide adaptability, and interactive simulation in pulse frequency measurement through deep integration of STM32 and Simulink. It overcomes many shortcomings of traditional solutions and provides a set of experimental simulation platforms that combine engineering practicality and simulation exploration for related majors in universities, thus having broad application value.
[0108] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, a computer can be, for example, a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any combination of these devices.
[0109] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0110] The above embodiments should be understood as illustrative only and not as limiting the scope of protection of the present invention. After reading the description of the present invention, those skilled in the art can make various alterations or modifications to the present invention, and these equivalent changes and modifications also fall within the scope defined by the claims of the present invention.
Claims
1. A hardware-in-the-loop pulse frequency adaptive measurement simulation device, characterized in that, include: The sensor module uses an incremental AB-phase quadrature encoder with a line count of [number missing]. , It is a positive integer used to collect pulse signals generated by the motor rotation. It supports dual-channel counting of rising and falling edges of channels A and B, and synchronously acquires pulse quantity and rotation direction information. A microcontroller equipped with dual independent timers is configured with the first timer, TIM2, in encoder counting mode to receive AB phase encoder pulses and achieve pulse count statistics and forward / reverse direction identification. The second timer, TIM6, is configured in dynamic reset mode and is set to upward counting mode. By dynamically adjusting its auto-reload value ARR6, the reset time interval is adaptively changed, triggering the capture of the count value of the first timer, TIM2, before reset. The following algorithms are configured and run: ① Pulse count holding algorithm, which latches the count value of the first timer, TIM2, before reset using a dual mechanism of state diagram holding and reset signal delay; ② Direction signal linear conversion algorithm, which converts the encoder direction signal into a ±1 weighting factor to synchronously represent the speed and direction; ③ Counting period adaptive adjustment algorithm, which dynamically corrects the auto-reload value ARR6 of the second timer, TIM6, based on the deviation between the number of pulses per unit time and the ideal interval; ④ Pulse jitter suppression algorithm, which eliminates erroneous pulses by using a pulse interval statistical threshold. ⑤ Noise suppression type reset time correction algorithm, which uses moving average to smooth the reset time to suppress speed fluctuation noise; The hardware-in-the-loop simulation and verification system includes a digital twin model, a state diagram programming module, a real-time monitoring interface, and an online parameter adjustment module. The digital twin model is used to construct a digital twin of the measurement system, enabling hardware-in-the-loop simulation and algorithm pre-verification. The state diagram programming module uses state diagram logic programming to maintain and latch the count value of the first timer TIM2, supporting dual-mechanism count extraction. The real-time monitoring interface receives real-time speed and pulse count data transmitted from the microcontroller, and visualizes dynamic curves using an oscilloscope component. The online parameter adjustment module provides a slider interaction component, supporting pulse count thresholds [p,q] and smoothing weight factors. The gain β parameter can be adaptively adjusted online to suit different pulse frequency measurement conditions. The serial port DMA communication module is used for real-time data interaction between the microcontroller and the PC. The transmission frame format includes a frame header, data length, rotation speed data, pulse count data, and checksum, and is used to filter packet loss and bit errors during serial port transmission. The pulse jitter suppression algorithm specifically includes: Based on pulse interval statistics, erroneous pulses are filtered out, and a pulse interval threshold is defined. Satisfying the formula: ; in The jitter tolerance coefficient is given in the formula. The current pulse frequency, This represents the pulse count value at time step k. This represents the corrected reset time after smoothing at time step k. For the first The coefficient of variation of the time step pulse interval, , The standard deviation of the pulse interval. The average pulse interval; the interval between two consecutive pulses detected. When this occurs, it is identified as a false pulse and discarded. When the pulse is in time, it is considered a valid pulse and included in the count; Speed calculation and accuracy optimization steps: Introduce a dynamic accuracy weighting factor to obtain the optimized speed, specifically: Optimized speed Satisfying the formula: ; in , To maintain the count value of TIM2, Z represents the total number of encoder pulses corresponding to one revolution of the motor output shaft, and Y represents the speed direction weighting factor. The accuracy loss due to the deviation of the pulse count from the ideal average is compensated by the dynamic weighting factor, so that the measurement error in the entire frequency range is ≤0.3%. The measurement error is calculated as: |Measured speed value - Actual speed value| / Actual speed value × 100%.
2. The hardware-in-the-loop pulse frequency adaptive measurement simulation device according to claim 1, characterized in that, The auto-reload register value ARR2 of the first timer TIM2 is ≥2. 30 -1, and at the same time configure the count overflow interrupt enable of the first timer TIM2. When an abnormal overflow occurs, the hardware-in-the-loop simulation verification system will alarm in real time and record the fault data.
3. A method using the hardware-in-the-loop pulse frequency adaptive measurement simulation device as described in claim 1, characterized in that, Includes the following steps: Steps to establish the pulse-speed mapping relationship: Define the total number of encoder pulses corresponding to one revolution of the motor output shaft. Through formula Calculation, where For encoder lines, For counting multiples, This is the motor reduction ratio, enabling a direct mapping between the number of pulses and the rotational speed; Dynamic reset time correction steps: Introduce a noise suppression algorithm and perform moving average smoothing; Counting cycle adaptive shifting steps: based on a threshold of pulse count per unit time Dynamic adjustment The value will be automatically clamped when it exceeds the threshold. A dual mechanism is used to extract the count value before TIM2 reset: the dual mechanism includes a state diagram programming retention mechanism and a reset signal delay mechanism; Pulse jitter suppression steps: Set a pulse interval threshold and remove erroneous pulses with intervals exceeding the threshold; Speed calculation and accuracy optimization steps: Introduce a dynamic accuracy weighting factor to obtain the optimized speed; Direction recognition and data verification: through formulas Achieve linear conversion of directional signals. Corresponding to forward rotation, Corresponding reversal; Serial communication uses a checksum formula to filter transmission errors.
4. The method according to claim 3, characterized in that, The dynamic reset time correction step is as follows: the basic reset time interval T is determined by the TIM6 timer parameters, and the formula is... , For pre-division coefficients, For automatic reload, f6 is the mounting frequency of the second timer TIM6, satisfying the accuracy requirements of pulse counting and period adjustment; a noise suppression algorithm is introduced, which smooths the original reset time using a moving average method, through the formula... Perform a moving average smoothing process. This represents the corrected reset time after smoothing at time step k-1. This represents the corrected reset time after smoothing at time step k. This represents the original reset time calculated based on the TIM6 timer parameters at time step k. To smooth the weighting factor and suppress measurement noise caused by speed fluctuations.
5. The method according to claim 4, characterized in that, The adaptive shifting step of the counting cycle specifically includes: Based on the threshold of pulses per unit time For 100≤p≤200, 400≤q≤500, an automatic shifting strategy with dynamically adjusted coefficients is adopted. The automatic reload value of the second timer TIM6 at time step k+1 is... Satisfying the formula: ; Where ARR6(k) is the current auto-reload value of the second timer TIM6 at time step k. To adaptively adjust the gain, , This represents the pulse count value at time step k. When the calculated new ARR6 value exceeds Automatic clamping to or .
6. The method according to claim 5, characterized in that, The method of extracting the count value before TIM2 reset using a dual mechanism specifically includes: ① State diagram programming maintains the mechanism, defining time points Counting and holding output When a reset signal is received, that is hour, , This is the real-time count value of TIM2; no reset signal is required. hour, , To maintain the output value for the count at time t-1, the initial value is... ② Reset signal delay mechanism, the reset signal is delayed. Satisfying the formula: ,in This is the original reset signal. This is the minimum clock step size of the microcontroller. This serves as the time marker for the current moment. Indicates compared to the current time Advance by a minimum clock step At that moment.
7. The method according to claim 3, characterized in that, In the pulse jitter suppression step, the coefficient of variation of the pulse interval The sliding window method is used to calculate that the window length N=10, i.e. Where k is the sequence number of the current time step, k-9 represents the sequence number of the 9th time step before the current time step k, and k|k-9 represents the interval of 10 consecutive time steps from time k to the previous 9 times. This interval is based solely on the statistics of the most recent 10 valid pulse intervals to ensure a rapid response to instantaneous jitter. For the first Standard deviation of pulse interval at time point and the previous 9 time points. The average pulse interval is given by the value of N, and the window length N=10 ensures a fast response to instantaneous jitter.
8. The method according to claim 3, characterized in that, The serial DMA communication between the microcontroller and the hardware-in-the-loop simulation verification system adopts a data verification mechanism. The data frame format is: [Frame Header 0xAA] + [Data Length] + [Rotation Speed n'(k)] + [Pulse Count in_counter(k)] + [Checksum], where the checksum is... The calculation formula is: in To optimize the speed binary bytes and, pulse count value The binary bytes are used to filter packet loss and bit errors during serial port transmission.
Citation Information
Patent Citations
Method and circuit for measuring rotating speed of rotary machine
CN106645784A
Incremental encoder speed measurement method and device and computer readable storage medium
CN119224365A
Rotating speed detection and closed-loop control system based on photoelectric sensing and PLC
CN121386589A