A C MEX S-function modeling method suitable for new energy converters
By using C language programming in the S-Function module of MATLAB/Simulink, an interrupt function and main function code architecture similar to digital chips is built, which solves the problem of the long development cycle of the new energy converter control system in the existing technology and the inability to imitate the actual digital chip code architecture, and realizes efficient modeling and rapid simulation, reducing development and verification risks.
Patent Information
- Application Number
- CN202411137560.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-19
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2044-08-19
AI Technical Summary
When implementing a new energy converter control system, the development cycle is long, the configuration verification steps are cumbersome, and the actual digital chip code architecture cannot be effectively imitated, resulting in the inability to port, verify and reuse.
Programming in C language, an interrupt function and main function code architecture similar to that of digital chips is built in the S-Function module of MATLAB/Simulink. Through the timing trigger mechanism and millisecond time-based processing, the precise simulation of the digital chip code architecture is achieved.
It improves code execution efficiency and simulation speed, achieves higher modeling accuracy, brings control effects closer to actual chips, simplifies the development and verification process, reduces potential risks, and realizes the joint development of MATLAB software, real-time controllers and digital chips.
Smart Images

Figure CN119025089B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of code development for new energy converters, and more specifically, relates to a C MEX S function modeling method suitable for new energy converters. Background Art
[0002] It aims to address the problems existing in the traditional digital chip development process, such as long development cycle, cumbersome configuration verification steps and potential risks in the verification stage.
[0003] There are currently three main ways to implement the control system of new energy converters in MATLAB / Simulink: using existing Simulink modules, using S functions of the M script language, and using C MEX S functions. However, the existing module construction method of Simulink requires a large number of modules to handle complex logical judgments, arrays, or loops, and the steps are cumbersome and complicated; the M script language is less efficient than the C language. Moreover, the first two methods cannot imitate the actual digital chip code architecture and cannot be transplanted, verified, or reused.
[0004] The existing literature and patents on the development of C MEX S functions are still oriented towards modeling a specific model or function, and have not achieved a complete simulation of the entire code architecture of the digital chip. Summary of the invention
[0005] In order to solve the above technical problems, the present invention provides a C MEX S-function modeling method suitable for new energy converters. Through C language programming, the S-function simulation of the digital chip code architecture is simply and quickly realized.
[0006] To achieve the above object, the present invention adopts the following technical solutions:
[0007] A C MEX S-function modeling method suitable for new energy converters uses C language programming in the S-Function module of MATLAB software to realize the modeling of the new energy converter control system, and constructs a code architecture similar to the main function and interrupt function of a digital chip by designing a timing trigger mechanism and processing a millisecond time base. The method includes the following steps:
[0008] Step 1, according to the code writing rules of C MEX S function, configure the callback function, define multiple header files and C source files, define the required variables and functions in multiple header files and C source files, write the function code and call the custom function in the corresponding callback function, and add the path of the header file and C source file before calling the custom function;
[0009] Step 2, build the main function and interrupt function in the mdlOutputs function, set the discrete time of the S-Function module to the interrupt design time of the DSP, and implement the main function based on the millisecond time base flag. If the main function execution time plus the interrupt function running time exceeds the interrupt design time, execute the main function in steps, and the actual execution time of the main function and interrupt function in each execution cycle does not exceed the interrupt design time;
[0010] Step 3, simulating multiple digital chips in parallel by calling multiple S-Function modules simultaneously in a MATLAB / Simulink simulation;
[0011] Step 4: After applying the MATLAB / Simulink simulation model in step 3 to the real-time controller for verification and debugging, the entire code architecture is transplanted to the DSP.
[0012] The beneficial effects of the present invention are:
[0013] Using C language programming, the code execution efficiency is high and the simulation speed is fast; the S function modeling is more accurate, and the code architecture and control effect are closer to the actual chip; the C language code of the real-time platform can be directly transplanted to the digital chip that supports C language, shortening the development and verification cycle, reducing potential risks, and realizing the joint development of MATLAB software, real-time controller and digital chip. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 This is a flow chart of a C MEX S function modeling method applicable to a new energy converter of the present invention;
[0015] Figure 2 This is a schematic diagram of the DSP main function and interrupt function;
[0016] Figure 3 Schematic diagram of simulating DSP main function and interrupt function for C MEX S function;
[0017] Figure 4 Schematic diagram of calling two S-Function modules for a MATLAB / Simulink simulation model. DETAILED DESCRIPTION
[0018] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0019] The purpose of the present invention is to use S-Function modules in the MATLAB / Simulink environment through C language programming to realize the control of new energy converters, and to make it closer to the real digital chip code architecture by constructing interrupt functions and main functions.
[0020] The technical solution adopted is to use standard C language modeling in the S-Function module and build an interrupt function and main function architecture similar to that of a digital chip.
[0021] First, according to the code writing rules of C MEX S function, configure the callback function, define the header file and C source file, and define the required variables and functions in the file, write the function code and call the custom function in the corresponding callback function, and add the path of the header file and C source file before calling the custom function.
[0022] Then, in order to make the model built by S-Function closer to the control model built by digital chips, the interrupt function and the main function are constructed in the mdlOutputs function. In order to imitate the triggering mode of the DSP interrupt function being triggered at a fixed point by Enhanced Pulse Width Modulation (ePWM), a fixed interval is set to trigger the S-Function module, and the trigger frequency is consistent with the DSP, that is, the discrete time of the S-Function module is set to the interrupt time of the DSP. The main function is implemented with the help of the millisecond time base flag. If the amount of code in the main function is large, the execution time of the main function plus the interrupt function running time will exceed the interrupt design time. The main function needs to be executed in steps, and it must be ensured that the actual execution time of the main function and the interrupt function in each execution cycle does not exceed the interrupt design time.
[0023] In addition, in a MATLAB / Simulink simulation, multiple digital chips can be simulated in parallel by calling multiple S-Function modules at the same time. In each S-Function module, add the required header file and C source file path and build their own interrupt function and main function, but make sure that the name of each S-Function module cannot be repeated. Each module can run independently at the same time without affecting each other, and can also pass internal variables to each other.
[0024] Finally, the MATLAB / Simulink simulation model built according to the method of the present invention can be simply converted into an RTLAB real-time model, verified and debugged in a real-time platform, and then the code can be transplanted into the DSP.
[0025] Specifically, Figure 1As shown, the first step is C MEX S function modeling. C MEX S function writing adopts the callback function method. Simulink Engine calls the corresponding callback function in different simulation stages to perform the specified task. First, configure the callback function parameters of the S function according to actual needs, mainly configuring the input dimension, output dimension, data type and discrete time in some callback functions related to initialization; then create multiple custom header files and C source files in the folder of this project, customize the file name, variable, structure and sub-function according to different functions, and add the custom header file and C source file path required by this project, and then perform custom variable initialization and custom sub-function initialization in the mdlStart callback function. Finally, in the mdlOutputs callback function, first link the input signal with the custom variable, including sampling signals, logic commands, online debugging parameters and variables that interact with the outside; then call the required function at the corresponding position. Before calling, you need to configure the input interface of the function, and after calling, configure the output interface of the function.
[0026] Step 2: Construct interrupt function and main function. In order to make the model built by S function closer to the control model of real digital chip, it is necessary to construct interrupt function and main function. New energy converter controlled by DSP generally uses a certain ePWM as the trigger source, and triggers the interrupt function at a fixed point of ePWM. Figure 2 The figure shows that the interrupt function is triggered at the zero crossing point of the triangular carrier, and the time between the two fixed points is the interrupt design time. The relationship between the DSP main function and the interrupt function is that after entering the main function entrance, the main function code begins to execute. If an interrupt request is encountered during the execution of the main function code, the execution of the main function code is stopped and the interrupt function begins to execute. The actual execution time of the interrupt function must be less than the interrupt design time. The duty cycle is updated before the interrupt function ends, and then the interrupt is exited and the execution continues at the position where the main function was interrupted last time. When the next interrupt request is encountered, the execution order is the same, and this cycle repeats. Therefore, the interrupt function of the S function is triggered at a fixed interval, and the trigger frequency is consistent with the DSP. Therefore, the discrete time of the S-Function module is set to the interrupt design time of the DSP, which can ensure that the mdlOutputs function is executed at a frequency consistent with the DSP interrupt frequency.
[0027] In addition, in actual projects, the execution time of the DSP main function is generally in the millisecond level. In the mdlOutputs function, a millisecond time base can be processed by accumulating and clearing the counter, and the millisecond time base is used as a flag to execute the code in the main function. Figure 3As shown in the figure, the main function time of the S function is designed with reference to the execution time of the DSP main function. If the main function has a large amount of code, the running time of all the main function codes plus the interrupt function will exceed the interrupt design time. The main function needs to be executed in steps, and it must be ensured that the actual execution time of the main function and the interrupt function in each execution cycle does not exceed the interrupt design time.
[0028] like Figure 4 As shown, the third step: calling multiple S-Function modules at the same time. Multiple S-Function modules can be called in a MATLAB / Simulink simulation model. The present invention takes the simultaneous calling of two S-Function modules (S-Function1 and S-Function2) as an example. First, configure each S-Function module with its own Input signal (sampling signal, logic command online debugging parameters and interactive variables) and Output signal (modulation wave, debugging variable, logic variable and interactive variable) according to its own needs, and then add the required header files (Function1.h, Function2.h and Function3.h) and C source files (Function1.c, Function2.c and Function3.c) paths in each S-Function module, and construct their own interrupt functions (ISR1 and ISR2) and main functions (Main1 and Main2). In each simulation step, Simulink executes each S-Function module in sequence according to the execution order determined in the initialization phase. They run in parallel and their internal variables do not conflict with each other, but the names of the S-Function modules must not be repeated, such as Figure 3 The name of the S-Function1 module is Inv1, and the name of the S-Function2 module is Inv2. They are compiled to generate their own executable files Inv1.mexw64 and Inv2.mexw64, which do not affect each other. To this end, the variables that need to be called in multiple places are defined as structures, and the structure names contain functional characteristics so that they can be called by multiple S-Function modules at the same time.
[0029] Step 4: code transplantation. The MATLAB / Simulink offline simulation model built according to the method of the present invention can be simply converted into an RTLAB real-time model, and the RTLAB real-time model is compiled and downloaded to the RTLAB real-time controller. The code is verified in the real-time platform composed of the RTLAB real-time controller and the actual circuit to obtain the optimal control parameters. Then the header file, C source file, main function and interrupt function verified on the real-time platform are transplanted to the DSP, and after simple secondary development, it can be used normally.
[0030] The C MEX S-function modeling method of the present invention is not only applicable to the modeling of the control part of topological circuits such as various new energy converters (rectifier, inverter, DCDC), but also applicable to the accurate modeling of complex models (such as motors, photovoltaic models, battery models and power electronic switch devices, etc.), modeling of complex logical functions (such as EMS, BMS, etc.); the S-function modeling method of the present invention can not only adopt C language programming, but also use M scripting language, C++, FORTRAN and other computer languages, but the corresponding compiler must be configured; The method of calling external files by an S-Function module of the present invention is not only applicable to the offline and real-time verification of the handwritten code of the developer, but also to the verification of the code automatically generated by Simulink and the existing code of the digital chip; the method of constructing the interrupt function and the main function of the present invention is not only applicable to the simulation of the DSP code architecture, but also to the simulation of other digital chips supporting C / C++ language, such as ARM, MCU, FPGA, etc.; the method of calling multiple S-Function modules by one simulation of the present invention can not only simulate the parallelism between digital chips of the same model that realize the same function, but also simulate the parallelism between digital chips of different models, and also simulate the parallelism between chips that realize different functions (such as monitoring, scheduling system and control system); the code transplantation technology of the present invention is not limited to the transplantation of codes verified by the real-time control platform into the digital chip, but also can be the transplantation of codes verified only by MATLAB simulation into the digital chip, or can be the transplantation of codes of the digital chip into MATLAB simulation; the real-time control technology of the present invention is not only applicable to the RTLAB real-time simulation platform, but also to any other real-time simulation platform compatible with the MATLAB / Simulink environment.
[0031] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A C MEX S-function modeling method suitable for new energy converters, characterized in that: The C language programming is used in the S-Function module of the MATLAB software to realize the modeling of the new energy converter control system. The code architecture similar to the main function and interrupt function of the digital chip is constructed by designing the timing trigger mechanism and processing the millisecond time base. The method includes the following steps: Step 1, according to the code writing rules of C MEX S function, configure the callback function, define multiple header files and C source files, define the required variables and functions in multiple header files and C source files, write the function code and call the custom function in the corresponding callback function, and add the path of the header file and C source file before calling the custom function; Step 2, construct the main function and interrupt function in the mdlOutputs function, set the discrete time of the S-Function module to the interrupt design time of the DSP, implement the main function based on the millisecond time base flag, if the main function execution time plus the interrupt function running time exceeds the interrupt design time, execute the main function in steps, and the actual execution time of the main function and the interrupt function in each execution cycle does not exceed the interrupt design time; wherein, in the mdlOutputs function, a millisecond time base is processed by accumulating and clearing the counter, and the code in the main function is executed with the millisecond time base as the flag; Step 3, simulating multiple digital chips in parallel by calling multiple S-Function modules simultaneously in a MATLAB / Simulink simulation; Step 4: After applying the MATLAB / Simulink simulation model in step 3 to the real-time controller for verification and debugging, the entire code architecture is transplanted to the DSP.
Citation Information
Patent Citations
Modbus communication library adaptation method in MATLAB and CCS combined development mode
CN116450109A
DAC adaptation layer in MATLAB and CCS combined development mode and design method thereof
CN116578286A