www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/LUTObjCondCompiledDimLengthsExample.m
%% Generate Code That Uses Conditionally Compiled Dimension Lengths % Suppose your handwritten code conditionally allocates memory and % initializes a lookup table based on dimension lengths that you specify % as |#define| macros. This example shows how to generate code that uses % your custom table and breakpoint data. % % Symbolic dimensions require that you use an ERT-based system target file, % which requires Embedded Coder(R). %% Explore Custom Code % In your current folder, copy these macro definitions into a header file % named |myHdr_LUT.h|. % % <include>myHdr_LUT.h</include> % % Copy this static initialization code into a source file named |mySrc_LUT.c|. % % <include>mySrc_LUT.c</include> % % To generate code that imports this data, create |bp1Len| and |bp2Len| as % |Simulink.Parameter| objects in MATLAB. Create |LUTObj| as a |Simulink.LookupTable| object. Use the parameter objects % to specify the dimension lengths for the table and breakpoint set % data in the |Simulink.LookupTable| object. %% Create Example Model % Create the example model |ex_LUTObj| by using an n-D Lookup Table block. % In the Lookup Table block dialog box, on the *Table and Breakpoints* tab, % set *Number of table dimensions* to |2|. open_system('ex_LUTObj') %% Create |Simulink.LookupTable| Object % In the Model Explorer *Model Hierarchy* pane, select *Base Workspace*. % % Select *Add > Add Custom*. % % In the Select Object dialog box, set *Object class* to % |Simulink.LookupTable|. Set *Object name* to |LUTObj|. Click *OK*. The % |Simulink.LookupTable| object |LUTObj| appears in the base workspace. % % Alternatively, create the object at the command prompt: LUTObj = Simulink.LookupTable; %% Configure |Simulink.LookupTable| Object % In the *Contents* pane, select the new object |LUTObj|. The property % dialog box appears in the *Dialog* pane. % % Set *Number of table dimensions* to |2|. % % Under *Table*, set *Value* to |[3 4; 2 1]|. % % In the first row under *Breakpoints*, set *Value* to |[1 2]|. % % In the second row under *Breakpoints*, set *Value* to |[3 4]|. Click % *Apply*. % % Under *Struct Type definition*, set *Data scope* to |Imported|. Set % *Header file* to |myHdr_LUT.h|. Set *Name* to |LUTObj_Type|. % % In the Lookup Table block dialog box, set *Data specification* to % |Lookup table object|. Set *Name* to |LUTObj|. Click % *Apply*. % % Alternatively, to configure the object and the blocks, use these commands: LUTObj.Breakpoints(1).Value = [1 2]; LUTObj.Breakpoints(2).Value = [3 4]; LUTObj.Table.Value = [3 4; 2 1]; LUTObj.StructTypeInfo.DataScope = 'Imported'; LUTObj.StructTypeInfo.HeaderFileName = 'myHdr_LUT.h'; LUTObj.StructTypeInfo.Name = 'LUTObj_Type'; set_param('ex_LUTObj/Lookup Table','LookupTableObject','LUTObj') set_param('ex_LUTObj/Lookup Table',... 'DataSpecification','Lookup table object') %% % Enable the code generator to use |Simulink.Parameter| objects as macros % that specify dimension lengths. Select the configuration parameter *Allow % symbolic dimension specification*. set_param('ex_LUTObj','AllowSymbolicDim','on') %% % Create the |Simulink.Parameter| objects that represent the macros % |bp1Len| and |bp2Len|. To generate code that imports the macros from your % header file |myHdr_LUT.h|, apply the custom storage class |ImportedDefine|. bp1Len = Simulink.Parameter(2); bp1Len.Min = 2; bp1Len.Max = 3; bp1Len.DataType = 'int32'; bp1Len.CoderInfo.StorageClass = 'Custom'; bp1Len.CoderInfo.CustomStorageClass = 'ImportedDefine'; bp1Len.CoderInfo.CustomAttributes.HeaderFile = 'myHdr_LUT.h'; bp2Len = Simulink.Parameter(2); bp2Len.Min = 2; bp2Len.Max = 3; bp2Len.DataType = 'int32'; bp2Len.CoderInfo.StorageClass = 'Custom'; bp2Len.CoderInfo.CustomStorageClass = 'ImportedDefine'; bp2Len.CoderInfo.CustomAttributes.HeaderFile = 'myHdr_LUT.h'; %% % Configure the existing |Simulink.LookupTable| object |LUTObj| to use the % |Simulink.Parameter| objects. Set the dimension lengths of the breakpoint % set data and the table data by using the names of the parameter objects. LUTObj.Breakpoints(1).Dimensions = '[1 bp1Len]'; LUTObj.Breakpoints(2).Dimensions = '[1 bp2Len]'; LUTObj.Table.Dimensions = '[bp1Len bp2Len]'; %% % Configure |LUTObj| as imported data by applying the custom storage class % |ImportFromFile|. To import your definition of |LUTObj|, add the name of % the file |mySrc_LUT.c| to the model configuration parameter *Configuration % Parameters > Code Generation > Custom Code > Additional Build Information % > Source files*. LUTObj.CoderInfo.StorageClass = 'Custom'; LUTObj.CoderInfo.CustomStorageClass = 'ImportFromFile'; LUTObj.CoderInfo.CustomAttributes.HeaderFile = 'myHdr_LUT.h'; set_param('ex_LUTObj','CustomSource','mySrc_LUT.c') %% Generate and Inspect Code % Configure the model to compile an executable from the generated code. set_param('ex_LUTObj','GenCodeOnly','off') %% % Generate code from the model. rtwbuild('ex_LUTObj') %% % In the code generation report, view the generated file % |ex_LUTObj.h|. The file imports the macro definitions and the structure type definition by % including your header file |myHdr_LUT.h|. file = fullfile('ex_LUTObj_ert_rtw','ex_LUTObj.h'); rtwdemodbtype(file,'#include "myHdr_LUT.h"','#include "myHdr_LUT.h"',1,1) %% % In the source file |ex_LUTObj.c|, the code algorithm in the model |step| % function passes the breakpoint and table data to the function that % performs the table lookup. The algorithm also passes |bp1Len| so the % lookup function can traverse the rows and columns of the table data, % which appear in the generated code as a serialized 1-D array. file = fullfile('ex_LUTObj_ert_rtw','ex_LUTObj.c'); rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)