www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/ExportingFunctionsExample.m
%% Generate Code for Functions Exported to a Larger System % % This example shows how to generate function code for individual Simulink % function blocks or function-call subsystems without generating scheduling code. % % To generate function code for export: % % # Create a model that contains the functions for export. % # Create a test harness model that schedules execution of the functions % during simulation. % # Simulate the model that contains the functions by using the test harness model. % # Generate code for the model that contains the functions. % %% Create Model That Contains Functions for Export % % The model with functions for export must satisfy architectural constraints at % the model root level. Blocks that are valid at the root level are: % % * Inport % * Outport % * Function-Call Subsystem % * Simulink Function % * Goto % * From % * Merge % % The code generator produces function code for Function-Call Subsystem and % Simulink Function blocks as well as Initialize and Reset Function blocks. For % a Function-call Subsystem block, you connect the block input ports to root % Inport blocks that assert function-call signals. The subsystem executes based % on the function-call signal that it receives. A Simulink Function block % executes in response to the execution of a corresponding Function Caller block % or Stateflow chart. An Initialize Function block executes on a model % initialize event and a Reset Function block executes on a user-defined reset % event. % % Model |rtwdemo_functions| contains two function-call subsystems (|f1_alg| and % |f2_alg|) and a Simulink Function block (|f3_alg|) for exporting % functions. The model also contains an Initialize Function block (|Initialize % Function|) and a Reset Function block (|Reset Function|). The State Writer % blocks are used inside the Initialize and Reset Function blocks to calculate % initial conditions for blocks with state in other parts of the model. open_system('rtwdemo_functions') %% Create Model That Contains Function Caller % % Use a Function Caller block to invoke a Simulink Function block. The Function % Caller block can be in the same model as the Simulink Function block or in % a different model. % % Multiple Function Caller blocks can invoke a Simulink Function block. % You can place the Function Caller block inside a function-call subsystem. % During code generation, the code generator exports a function from the % function-call subsystem. % % The model |rtwdemo_caller| exports a function-call subsystem that contains a Function % Caller block. open_system('rtwdemo_caller') %% Create Test Harness Model for Simulation % % When you export functions, the generated code does not include a scheduler. % Create a test harness model to handle scheduling during simulation. Do % not use the test harness model to generate code that you deploy. % % Model |rtwdemo_export_functions| is a test harness. The model: % % * Schedules the Simulink Function block with the Function Caller block in % |rtwdemo_caller|. % * Provides function-call signals to other models in this example to schedule % the model contents, including the model initialize and reset events. open_system('rtwdemo_export_functions') %% Simulate the Test Harness Model % % Verify that the model containing the functions that you want to export % executes as you expect by simulating the test harness model. For example, % simulate |rtwdemo_export_functions|. sim('rtwdemo_export_functions') %% Generate Function Code and Report % % Generate code and a code generation report for the functions that you want % to export. For example, generate code for |rtwdemo_functions|. rtwbuild('rtwdemo_functions') %% Review Generated Code % From the code generation report, review the generated code. % % * |ert_main.c| is an example main program (execution framework) for the % model. This code shows how to call the exported functions. The code also % shows how to initialize and execute the generated code. % * |rtwdemo_functions.c| calls the initialization function, including % |Initialize Function|, and exported functions for model components |f1_alg|, % |f2_alg|, and |f3_alg|. % * |rtwdemo_functions.h| declares model data structures and a public % interface to the exported entry-point functions and data structures. % * |f3.h| is a shared file that declares the call interface for % the Simulink function |f3_alg|. % * |rtwtypes.h| defines data types, structures, and macros that the % generated code requires. % %% Code Interface % Open and review the Code Interface Report. To write the interface code % for your execution framework, use the information in that report. % % # Include the generated header files by adding directives |#include rtwdemo_functions.h|, % |#include f3.h|, and |#include rtwtypes.h|. % # Write input data to the generated code for model Inport blocks. % # Call the generated entry-point functions. % # Read data from the generated code for model Outport blocks. % % Input ports: % % * |rtU.U1| of type |real_T| with dimension 1 % * |rtU.U2| of type |real_T| with dimension 1 % % Entry-point functions: % % * Initialize entry-point function, |void % rtwdemo_functions_initialize(void)|. At startup, call this function once. % * Reset entry-point function, |void rtwdemo_functions_reset(void)|. Call this % function as needed. % * Exported function, |void f1(void)|. Call this function as needed. % * Exported function, |void f2(void)|. Call this function as needed. % * Simulink function, |void f3(real_T rtu_u, real_T *rty_y)|. Call this % function as needed. % % Output ports: % % * |rtY.Accumulator1| of type |int8_T| with dimension [2] % * |rtY.Accumulator2| of type |int8_T| with dimension [2] % * |rtY.TicToc10| of type |int8_T| with dimension 1 % %% Create Model with Functions for Export with C++ Class Generation % % You can use exported functions with a C++ model class interface. % For C++ class generation, blocks that are valid at the model root level are: % % * Inport % * Outport % * Function-Call Subsystem % * Goto % * From % * Merge % % When generating C++ code, the code generator produces public class methods % for Function-Call Subsystem blocks. For a Function-call Subsystem block, % you connect the block input ports to root Inport blocks that produce % function-call signals. % % Model |rtwdemo_cppclass_functions| exports functions as C++ code. % The model is a modified version of |rtwdemo_functions|. open_system('rtwdemo_cppclass_functions'); %% More About % % * <docid:ecoder_ug.bqnrwky-1> % * <docid:ecoder_ug.br54_tp> % * <docid:ecoder_ug.f6010dfi4> % * <docid:rtw_ug.bukgv5h> % * <docid:rtw_ug.bukg55s-1> %% % Close Example Models bdclose('rtwdemo_export_functions') bdclose('rtwdemo_functions') bdclose('rtwdemo_caller') bdclose('rtwdemo_cppclass_functions')