Automatic Generation Method for Brushless DC Motor Control Code Based on STM32-MAT
Automatically generates brushless DC motor control codes through Matlab&Simulink and STM32CubeMX toolboxes, solving the problem of low efficiency in STM32 brushless DC motor code development, achieving efficient code generation and chip transplantation, reducing development costs.
Patent Information
- Application Number
- CN202210386345.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-13
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2042-04-13
AI Technical Summary
The code development efficiency of the STM32 brushless DC motor is low, has a long development cycle, and is difficult to transplant code.
Matlab&Simulink and STM32CubeMX toolbox are used to build a brushless DC motor control model through graphical configuration and algorithm modules, and automatically generate control codes.
It improves development efficiency, simplifies the code writing process, realizes code porting between different chips, and reduces the development cost and threshold of embedded systems.
Smart Images

Figure CN114759844B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of brushless DC motor control, and particularly relates to a method for automatically generating portable C code for brushless DC motor control based on STM32-MAT, so as to facilitate the implementation of control algorithms and accelerate the process from design to implementation of motor control. Background Art
[0002] With the rapid development of integrated circuit technology, 32-bit MCUs with characteristics such as high performance, multi-function, and low power consumption have gradually replaced 8-bit MCUs in embedded system design and become the first choice. However, problems such as more command operations for 32-bit MCUs and more complex software and hardware design of the system have also emerged. STM32 series microcontrollers, as embedded application products based on the ARM Cortex-M3 kernel, also face the same problems. For this reason, STMicroelectronics (ST) has launched the STM32CubeMX software to improve the development efficiency of developers. This software is a tool for realizing graphical configuration of STM32 series single-chip microcontrollers. Developers can use its provided visualization peripheral, pin and other configuration functions to quickly realize the initialization of projects.
[0003] Subsequently, the STM32-MAT toolbox jointly developed based on Matlab and STM32CubeMX was launched. This toolbox is a plug-in for graphically developing STM32 in Matlab&Simulink. It provides the MCUCONFIG module for configuring STM32 chips and contains almost all resources on STM32 chips, such as GPIO, ADC, DAC, CAN, I2C, I2S, REGISTER, SPI, TIMERS, USART, xWDG, and so on. In this way, on the basis of the initialization configuration of STM32CubeMX, the control algorithm can be implemented and the code generation model can be built by using the STM32-MAT toolbox and the existing modules in Matlab&Simulink. Finally, readable and portable C code can be generated by Embedded Coder, thus realizing automatic code generation.
[0004] In the traditional development process, it is necessary to establish an algorithm model and write a program to implement the corresponding algorithm, and there is also complicated initialization configuration. However, by using Matlab&Simulink, STM32-MAT, and STM32CubeMX, the establishment of the algorithm model and the corresponding programming can be integrated, greatly reducing the amount of manual code writing, and greatly accelerating the design and development process while ensuring the code quality.
[0005] The basic structure of a brushless direct current (BLDC) motor consists of three parts: the motor body, the power drive circuit, and the HALL position sensor. Structurally, the BLDC motor body is similar to a permanent magnet synchronous motor, mainly composed of a stator containing an armature winding and a rotor with permanent magnets. As the name indicates, the BLDC motor does not use a brush for commutation but uses electronic commutation. The position sensor in the BLDC motor plays the role of detecting the rotor pole position and providing correct commutation information for the logic switch circuit. The control logic first obtains the rotor pole position through the HALL position sensor and converts it into an electrical signal to be transmitted to the logic switch circuit. The power drive circuit distributes the power output of the power supply to the three-phase windings of the BLDC motor stator according to the transmitted electrical signal in a specific timing pattern, so that the current in the motor armature commutes in a corresponding order as the rotor position changes, thereby generating a stepped rotating magnetic field to drive the permanent magnet rotor to rotate continuously, ultimately achieving the effect of driving the motor. Among them, the power drive circuit adopts a mode of two-by-two conduction, with one upper bridge arm and one lower bridge arm conducting at each moment, thus realizing the conduction of two phases of the motor and leaving the third phase floating.
[0006] Since the control algorithm of the brushless direct current motor is easy to implement on Matlab, and the modules required to establish the code generation model can be found in both STM32-MAT and Matlab&Simulink, the above tools are used here to realize the model construction and code generation of the speed and current double closed-loop control of the brushless direct current motor. Summary of the Invention
[0007] The purpose of the present invention is to provide a method for generating the speed and current double closed-loop control code of STM32 for a brushless direct current motor by using Matlab&Simulink and STM32CubeMX in view of the current deficiency in the STM32 code development efficiency of the brushless direct current motor.
[0008] The present invention is implemented by adopting the following technical solutions:
[0009] An automatic generation method for the control code of a brushless direct current motor based on STM32-MAT includes the following steps:
[0010] Step S1: First establish an.ioc project file in STM32CubeMX to complete the relevant settings of the relevant models and pin functions of the STM32 chip.
[0011] Step S2: Design the algorithm module of the control system on Matlab&Simulink, that is, write the motor control algorithm using Matlab function.
[0012] Step S3: Create a blank Model in Matlab&Simulink, set the relevant software parameters and save it.
[0013] Step S4: In the Simulink Library Browser of the newly created Model, find the STM32-MAT toolbox, drag the STM32_Config module in MCU CONFIG into the model file to associate it with the.ioc project generated by STM32CubeMX.
[0014] Step S5: Use the Matlab function module written in Step S2 and the components of STM32-MAT to establish a model for automatically generating code, and click Build to generate the corresponding code.
[0015] Step S6: The generated code is located in the.ioc project folder. Use relevant development tools such as Keil5 to directly open and compile it, and burn it into the hardware chip for verification.
[0016] The beneficial effects of the present invention are as follows:
[0017] The present invention solves the problem of slow development cycle of current STM32 projects. The present invention configures peripherals, pins, etc. graphically through STM32CubeMX software, and at the same time realizes the corresponding control algorithms with the help of Matlab. Finally, use the STM32-MAT toolbox to build a graphical model on Simulink and automatically generate control code, which can help developers free from complex register programming, cumbersome peripherals and the coding of various control algorithms, greatly improving the development efficiency; at the same time, it can realize code transplantation between different chips, improve the transplantation efficiency, and reduce the development cost and threshold of the embedded system. Description of the Drawings
[0018] Figure 1 It is the flowchart for automatic generation of motor code;
[0019] Figure 2 It is the single-resistance sampling circuit diagram;
[0020] Figure 3 It is the algorithm model and corresponding output configuration diagram;
[0021] Figure 4 It is the processing diagram of three-way Hall signals;
[0022] Figure 5 It is the speed and current double closed-loop control diagram;
[0023] Figure 6 It is the complete code generation model diagram. Detailed Embodiment
[0024] Next, the present invention will be described in detail in conjunction with specific embodiments and the accompanying drawings.
[0025] The development boards adopted by the present invention are X-NUCLEO-IHM07M1 3SH and NUCLEO-F103RB launched by STMicroelectronics. Among them, X-NUCLEO-IHM07M1 3SH is a motor drive board dedicated to PMSM and BLDC, and the motor drive board drive chip model is L6230. The brushless DC motor model is 2836. The motor has two sets of wires (Hall sensor wires and motor three-phase wires), the working voltage is 24V, and the maximum speed is 12000rpm.
[0026] As Figure 1 shown, the automatic generation method of the brushless DC motor control code based on STM32-MAT includes the following steps:
[0027] Step 1: Open the STM32CubeMX software, select the dedicated NUCLEO-F103RB project template, create a new Cube project, and select STM32F103RB based on the ARM Cortex-M3 kernel for the chip model.
[0028] Step 2: Configuration of the RCC interface: For High Speed Clock (HSE), select BYPASS Clock Source; for Low Speed Clock (LSE), select Crystal / Ceramic Resonator. For SYS configuration, select SysTick (tick timer) for Timebase Source (HAL's timebase source). For USART2 configuration: Select Asynchronous (asynchronous communication) for Mode (mode), and the software will automatically assign USART2_RX and USART2_TX to PA3 and PA5 pins respectively.
[0029] Step 3: TIM1 configuration: Configure three-way PWM mode 1 output, where the PWM carrier frequency is 20Khz = 72M / (2 + 1) / (1199 + 1), the initial PWM wave of each channel is 500, and enable the TIM1 update interrupt. This configuration is used to output PWM waves to the power drive circuit.
[0030] Step 4: TIM2 Configuration: Enable the three-way selection Input Capture direct mode, and enable the XOR function. Select Rising Edge for Polarity Selection, Direect for IC Selection, and enable the timer interrupt. This configuration is used to implement the function of collecting and analyzing Hall signals.
[0031] Step 5: Configure ADC1: Select IN11 for the Mode of ADC1, that is, configure it on the PC1 pin. Select Right alignment for Data Alignment, Enable for Continue Conversion Mode, and 7.5Cycles for Sampling Time. At the same time, enable the DMA function of ADC1. In the present invention, the ADC operates in the single-resistor sampling mode for the motor current. As Figure 2 shown, the relational expression between the motor current and the ADC sampling current is (JP1 and JP2 are not connected):
[0032] V = 3 * 0.33 * I
[0033] where: 0.33 is the resistance value of the sampling resistor, I is the motor current, and V is the motor current amplified by 3 times. This configuration is used to sample the motor current pin and can realize the feedback of the motor speed.
[0034] Step 6: Configure ADC2: Select IN9 for the Mode of ADC2, that is, configure it on the PB1 pin. Select Right alignment for Data Alignment, Enable for Continue Conversion Mode, and 239.5Cycles for Sampling Time. This configuration is used to configure the input of the speed reference potentiometer, that is, to sample the set value of the motor speed.
[0035] Step 7: Configure the general I / O functions of PC10, PC11, and PC12 as GPIO_OUTPUT mode, with the default output being low. This configuration is used to input the corresponding level signals to the power drive circuit.
[0036] Step 8: After completing the above initialization configuration, name the project BLDC_Control, and select MDK-ARM V5 for the integrated development tool (Toolchain / IDE). In the Code Generator, select the Generatedfiles box, and check the box for Generated periphera initialization as a pair of '.c / .h' files per peripheral (generate corresponding.c files and.h header files for each peripheral). In HAL Settings, check Set all free pins as analog (to optimize the power consumption) to set the unused pins to analog mode and optimize the power consumption of the system. Finally, save the project File->Save Project, and do not generate code for now, only generate the corresponding BLDC_Control.ioc file.
[0037] Step 9: Install the STM32-MAT software, add the installation path to the Matlab search path, and switch the Matlab working environment to the storage address of the BLDC_Control.ioc. Then start Simulink, create a new model named BLDC_Control, and perform basic settings for the model: In the Code Generation (code generator), select Stm32.tlc for the System target file; in the STM32 Options, check the STM32CubeMX Path update and add the installation directory of the STM32CubeMX software; in Solvor, select Fixed-step for Solvor options->Type, and set the Additional parameters->Fixed-step size to 1 / 10000; for the STM32Project Files (STM32 project files): add the source file getBuffPtr.c and the corresponding header file getBuffPtr.h to the BLDC_Control project file. Then click Save to generate the blank BLDC_Control.slx model, and its save address should be the same as that of the BLDC_Control.ioc.
[0038] Step 10: Open the Simulink library Browser (Simulink library), find Target support package - STM32 Adapter (i.e., STM32 - MAT), and add the MCU CONFIG module in it to the BLDC_Control.slx model. Double - click to open this module, and select and load the BLDC_Control.ioc file generated by the STM32CubeMX software in the Select STM32 configuration file (STM32 configuration file).
[0039] Step 11: Establish a Matlab Function algorithm model according to the six - step commutation sequence of the brushless DC motor, and add it to BLDC_Control.slx. The six - step commutation sequence rules of the motor are shown in Table 1 below.
[0040] Table 1 Commutation sequence table
[0041]
[0042]
[0043] Step 12: As Figure 3 shown, take out the TIM module in the Target support package - STM32 Adapter toolbox, configure it as TIM1. The three channels of TIM1 correspond to those configured in STM32CubeMX. Connect the three pins to CH1, CH2, and CH3 of the Matlab Function respectively. This operation connects the three - phase PWM waves generated by the Matlab Function to the upper bridge arm of the corresponding power drive circuit. Then take out the GPIO module, configure it as GPIOC, select the pins Pin10, Pin11, and Pin12, and connect these three pins to CH1N, CH2N, and CH3N of the Matlab Function respectively. This operation transmits the three - phase level signals generated by the Matlab Function to the lower bridge arm of the corresponding power drive circuit.
[0044] Step 13: As Figure 4As shown, also take out the TIM module from the above toolbox and configure it as TIM2. This module has six pins, among which three are used to capture the CHx Freq of the position signal of the Hall sensor, and the remaining three CHx Reg Val are used to count the number of high-level signals of the Hall sensor. First connect the CHx Freq pin to the uint16 module for data type conversion, and then connect the converted data to the Bitiwise Operator module and the ArithShift module for corresponding bit operations, and finally connect them to the ha, hb, and hc pins of the Matlab Function respectively. The data bit operations are for the convenience of algorithm writing in the Matlab Function.
[0045] Step 14: Then add up the counting numbers of the three CHx Reg Val on the TIM2 module in the previous step, and determine the real-time speed of the motor through its proportional relationship with the motor speed. Therefore, use the Gain module to convert it into the range of 0-1 and then use it as the speed feedback value of the outer speed loop of the speed and current double closed-loop control link. Then take out the ADC module and configure it as ADC2, and select the pin as ch9. This module is used to read the ADC signal of the speed reference potentiometer. Similarly, first use the Gain module to convert it into the range of 0-1, and then use it as the speed set value of the outer speed loop.
[0046] Step 15: Use the output of the outer speed loop PID as the input of the inner current loop, that is, as the set value of the current loop. Then take out the ADC module and configure it as ADC2, and select the pin as ch11. This module samples the motor current. Similar to the above steps, first use the Gain module to convert it into the range of 0-1, and then use it as the current feedback value of the inner speed loop. Use the output of the inner speed loop as the Duty input of the Matlab Function, and cooperate with the algorithm of the Matlab Function to realize the adjustment of the duty cycle of the PWM wave. The double closed-loop control module is as Figure 5 shown, and both the speed and current closed-loop control links adopt the PID control method.
[0047] Step 16: So far, the construction of the code generation model of the present invention is completed, and the complete model diagram is as Figure 6 shown. Click Build in Embedded Coder to generate code, and finally download it to the compilation software for code modification and verification, and the code generation model can be modified accordingly.
Claims
1. An automatic generation method for the control code of a brushless DC motor based on STM32-MAT, characterized in that, It includes the following steps: Step S1: Create an.ioc project file in STM32CubeMX to complete the settings of the STM32 chip model and pin functions; Step S2: Design the algorithm module of the control system on Matlab&Simulink, that is, write the motor control algorithm using Matlab function; Step S3: Create a blank Model in Matlab&Simulink, set the software-related parameters and save; Step S4: In the Simulink Library Browser of the newly created Model, find the STM32-MAT toolbox, drag the STM32_Config module in MCU CONFIG into the model file to associate it with the.ioc project generated by STM32CubeMX; Step S5: Use the Matlab function module written in Step S2 and the components of STM32-MAT to establish a model for automatically generating code, and click Build to generate the corresponding code; Step S6: The generated code is located in the.ioc project folder. Use the Keil5 development tool to directly open and compile it, and burn it into the hardware chip for verification; In Step S4, take out the TIM module from the Target support package-STM32 Adapter toolbox and configure it as TIM1. The three channels of TIM1 correspond to those configured in STM32CubeMX. Connect the three pins to CH1, CH2, and CH3 of MatlabFunction respectively. This operation connects the three-phase PWM wave generated by Matlab Function to the upper bridge arm of the corresponding power drive circuit; then take out the GPIO module, configure it as GPIOC, select the pins Pin10, Pin11, and Pin12, and connect these three pins to CH1N, CH2N, and CH3N of Matlab Function respectively. This operation transmits the three-phase level signals generated by MatlabFunction to the lower bridge arm of the corresponding power drive circuit; In Step S4, take out the TIM module from the Target support package-STM32 Adapter toolbox and configure it as TIM2. This module has six pins, three of which are used to capture the position signals of the Hall sensors, CHx Freq, and the remaining three CHx Reg Val are used to count the number of high-level signals of the Hall sensors; First connect the CHx Freq pin to the uint16 module for data type conversion, then connect the converted data to the Bitiwise Operator module and the ArithShift module for corresponding bit operations, and finally connect them to the ha, hb, and hc pins of MatlabFunction respectively.
2. The automatic generation method of the brushless DC motor control code based on STM32-MAT according to claim 1, wherein: In the step S1, the chip model is selected as STM32F103RB based on the ARM Cortex-M3 kernel.
3. The automatic generation method of the brushless DC motor control code based on STM32-MAT according to claim 1, wherein: In the step S2, a Matlab Function algorithm model is established according to the six-step commutation sequence of the brushless DC motor.
4. The automatic generation method of the brushless DC motor control code based on STM32-MAT according to claim 1, wherein: The counting numbers of the three CHx Reg Val on the TIM2 module are added together, and the real-time speed of the motor is determined through its proportional relationship with the motor speed. After being converted into the range of 0-1 by the Gain module, it is used as the speed feedback value of the outer speed loop of the speed and current double closed-loop control link.
5. The automatic generation method of the brushless DC motor control code based on STM32-MAT according to claim 4, characterized in that: The output of the outer speed loop is used as the input of the inner current loop; the ADC2 module is configured to sample the motor current, and after being converted into the range of 0-1 by the Gain module, it is used as the current feedback value of the inner speed loop. The output of the inner speed loop is used as the Duty input of Matlab Function, and the duty ratio of the PWM wave is adjusted in cooperation with the algorithm of Matlab Function.
6. The automatic generation method of the brushless DC motor control code based on STM32-MAT according to claim 4 or 5, wherein: Both the speed and current closed-loop control links adopt the PID control method.
Citation Information
Patent Citations
Direct current brushless control system and method based on model design
CN109768745A
BLDC motor control device with can communication interface
KR100786636B1