www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/CallExtCCodeFromModelAndGeneratedCodeExample.m
%% Call External C Code from Model and Generated Code % Call existing, external functions from a simulation or % from the generated code by using the Legacy Code Tool. % % Learn how to: % % * Evaluate a C function as part of a Simulink(R) model simulation. % * Call a C function from the code that you generate from a model. % % For information about the example model and other examples in this % series, see <docid:ecoder_examples.example-rtwdemo_pcgd_stage_1_p1_script>. %% Replacement Process % Open the example model, <matlab:RTWDemos.pcgd_open_pcg_model(4,0); % |rtwdemo_PCG_Eval_P4|>. % % For many Model-Based Design applications, in addition to Simulink(R) models, a design includes a set of existing C functions % that have been tested and validated. You can integrate % these functions into a Simulink(R) model and generate code that uses the % functions. % % In this example, you create a custom Simulink(R) block that % calls an existing C function. You then include the block in a model % and test the overall system through model simulation in Simulink(R). % % In the example model, you can replace the Lookup blocks (lookup tables) in the PI controllers % with calls to an existing C function. The function is defined in the % files |SimpleTable.c| and |SimpleTable.h|. % % <matlab:edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos','EmbeddedCoderOverview','stage_4_files','SimpleTable.c')) View |SimpleTable.c|.> % % <matlab:edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos','EmbeddedCoderOverview','stage_4_files','SimpleTable.h')) View |SimpleTable.h|.> % % <<../replace_sim_with_external.jpg>> %% Create Block That Calls C Function % To specify a call to an existing C function, use an S-Function block. % You can automate the creation of the S-Function block by using the % Legacy Code Tool. In the tool, you first specify an interface for your % existing C function. The tool then uses that interface to create an % S-Function block. % % Use the Legacy Code Tool to create an S-Function block for the existing % C function in |SimpleTable.c|. % % 1. <matlab:RTWDemos.pcgd_LCT(1) Create a structure to contain the definition of the function interface.> % % def = legacy_code('initialize') % % You can use the structure |def| to define the function interface to the existing C % code. % % 2. <matlab:RTWDemos.pcgd_LCT(2) Populate the fields of the structure.> % % <<../defineFunctionInterface.jpg>> % % 3. <matlab:RTWDemos.pcgd_LCT(3) Create the S-function.> % % legacy_code('sfcn_cmex_generate',def) % % 4. <matlab:RTWDemos.pcgd_LCT(4) Compile the S-function.> % % legacy_code('compile',def) % % 5. <matlab:RTWDemos.pcgd_LCT(5) Create the S-Function block.> % % legacy_code('slblock_generate',def) % % The generated S-Function block calls the C function in |SimpleTable.c|. You can now use and reuse this S-Function block in models. % % 6. <matlab:RTWDemos.pcgd_LCT(6) Create the TLC file.> % % legacy_code('sfcn_tlc_generate',def) % % This command creates a TLC % file, which is the component of an S-Function that specifies how % to generate code for the block. %% Validate External Code Through Simulation % When you integrate existing C code in a Simulink(R) model, % first validate the generated S-Function block. % % To validate the replacement of the Lookup blocks, compare the simulation results produced by the % Lookup blocks with the results produced by the new S-Function block. % % 1. <matlab:RTWDemos.pcgd_open_pcg_model(2,3); Open the validation model.> % % * The Sine Wave block produces output values from [-2 : 2]. % * The input range of the lookup table is from [-1 : 1]. % * The lookup table outputs the absolute value of the input. % * The lookup table clips the output at the input limits. % % 2. <matlab:sim(pcgDemoData.helper{2}) Run the validation model.> % % The figure shows the validation results. The existing C code % and the Simulink(R) table block produce the same output values. % % <<../S_Fun_Validate.jpg>> % %% Validate C Code as Part of Simulink(R) Model % After validating the existing C function code as a % standalone component, you can validate the S-function in the model. To % complete the validation, use the test harness model. % % 1. <matlab:RTWDemos.pcgd_open_pcg_model(4,1); Open the test harness.> % % 2. <matlab:RTWDemos.pcgd_runTestHarn(1,4) Run the test harness.> % % The simulation results match the golden values. % % <<../FirstPassTest.jpg>> % %% Call C Function from the Generated Code % The code generator uses the TLC file to process the S-Function block like % any other block. The code generator can implement _expression folding_ with the S-Function block, % an operation that combines multiple computations into a single output % calculation. % % 1. <matlab:RTWDemos.pcgd_buildDemo(4,0) Build the full model.> % % 2. <matlab:RTWDemos.pcgd_showSection(4,'Reusable'); Examine the generated % code in % |PI_Control_Reusable.c|.> % % The generated code now calls the |SimpleTable| C function. % % The figures show the generated code before and after the C code % integration. Before the integration, the code calls a generated lookup % routine. After the integration, the generated code calls the C function % |SimpleTable|. % % <<../original_table_call.png>> % % <<../external_scheduler.png>> % % For the next example in this series, see % <docid:ecoder_examples.example-rtwdemo_pcgd_stage_5_p1_script>. % % Copyright 2007-2015 The MathWorks, Inc.