www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/GenerateTunableInitialConditionStructExample.m
%% Generate Tunable Initial Condition Structure for Bus Signal % When you use a MATLAB(R) structure to specify initialization % values for the signal elements in a bus, you can create a tunable global % structure in the generated code. % % If you set *Configuration Parameters > Optimization > Signals and % Parameters > Default parameter behavior* to |Tunable|, the initial % condition appears as a tunable substructure of the global parameters % structure. % % Whether you set *Default parameter behavior* to |Tunable| or |Inlined|, % you can specify the initial condition by using a tunable |Simulink.Parameter| object whose % value is a structure. If you apply a storage class other than |Auto| to % the parameter object, the structure is tunable in the generated % code. % % To generate efficient code by avoiding data type % mismatches between the structure and the bus signal, use either: % % * Typed expressions to specify the values of the structure fields. Match the % data type of each field with the data type of the % corresponding signal element. % * A |Simulink.Bus| object to control the data types of the structure % fields and the signal elements. % % For basic information about using structures to initialize bus signals, % and to decide how to control field data types, % see <docid:simulink_ug.bsff8zh>. %% Generate Tunable Initial Condition Structure % This example shows how to use a tunable structure parameter to initialize % a virtual bus signal. % % Open the example model <matlab:open_system('rtwdemo_tunable_init_struct') |rtwdemo_tunable_init_struct|>. load_system('rtwdemo_tunable_init_struct') set_param('rtwdemo_tunable_init_struct','SimulationCommand','Update') open_system('rtwdemo_tunable_init_struct') %% % In the Inport block dialog boxes, open the *Signal Attributes* tab. Each Inport uses a % different output data type. % % Open the Bus Creator block dialog box. The block output is a virtual bus. % % In the Configuration Parameters dialog box, open the *Optimization > % Signals and Parameters* pane. The configuration parameter *Default % parameter behavior* is set to |Tunable|. By default, block parameters, % including initial conditions, appear in the generated code as tunable fields of the global % parameters structure. % % Open the Unit Delay block dialog box. Set *Initial condition* to a % structure that specifies an initial condition for each of the three % signal elements. To generate efficient code, match the data types of the % structure fields with the data types of the corresponding signal % elements. % % set_param('rtwdemo_tunable_init_struct/Unit Delay','InitialCondition',... % 'struct(''thermocpl'',15.23,''magFlow'',uint32(79),''posSwitch'',false)') set_param('rtwdemo_tunable_init_struct/Unit Delay','InitialCondition',... 'struct(''thermocpl'',15.23,''magFlow'',uint32(79),''posSwitch'',false)') %% % Generate code from the example model. rtwbuild('rtwdemo_tunable_init_struct') %% % In the code generation report, view the file |rtwdemo_tunable_init_struct_types.h|. % The code defines a structure type whose fields use the data % types that you specified in the |struct| expression. file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct_types.h'); rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_struct_','}',1,1) %% % View the file |rtwdemo_tunable_init_struct.h|. The |struct| type definition of % the global parameters structure contains a substructure, % |UnitDelay_InitialCondition|, which represents the *Initial condition* % parameter of the Unit Delay block. file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct.h'); rtwdemodbtype(file,'struct P_rtwdemo_tunable_init_struct_T_ {','UnitDelay_InitialCondition;',1,1) %% % View the file |rtwdemo_tunable_init_struct_data.c|. This source file % allocates memory for the global parameters structure. The substructure % |UnitDelay_InitialCondition| appears. file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct_data.c'); rtwdemodbtype(file,'/* Block parameters (auto storage) */','}',1,1) %% % View the file |rtwdemo_tunable_init_struct.c|. The model initialization function uses the fields of the substructure to initialize the block states. file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct.c'); rtwdemodbtype(file,'/* InitializeConditions for UnitDelay: ''<Root>/Unit Delay'' */',... 'rtwdemo_tunable_init_struct_P.UnitDelay_InitialCondition.posSwitch',1,1) %% Use Bus Object to Specify Data Types % If you create a bus object, you can use it to specify the % data type of the bus signal and the tunable initial condition structure. % Before code generation, the % |Simulink.Parameter| object casts the values of the structure fields to the data types of the % signal elements. % For basic information about creating bus objects and using them % in models, see <docid:simulink_ug.f15-109955>. % % Open the example model <matlab:open_system('rtwdemo_init_struct_busobj') |rtwdemo_init_struct_busobj|>. open_system('rtwdemo_init_struct_busobj') load_system('rtwdemo_init_struct_busobj') set_param('rtwdemo_init_struct_busobj','SimulationCommand','Update') open_system('rtwdemo_init_struct_busobj') %% % In the base workspace, double-click the |Simulink.Bus| object % |ComponentData|. The object defines three signal elements: |thermocpl|, % |magFlow|, and |posSwitch|. The elements each use a different data type. % % Open the block dialog box for the Inport block DataIn. The output data % type is set to |Bus: ComponentData|. % % Create the tunable structure parameter |initStruct|. You can specify the % field values by using untyped expressions. To improve readability, % specify the field |posSwitch| with a Boolean value. Specify the % |DataType| property of the parameter object as |'Bus: ComponentData'|. % % initStruct = struct(... % 'thermocpl',15.23,... % 'magFlow',79,... % 'posSwitch',false ... % ); % % initStruct = Simulink.Parameter(initStruct); % initStruct.StorageClass = 'ExportedGlobal'; % initStruct.DataType = 'Bus: ComponentData'; initStruct = struct(... 'thermocpl',15.23,... 'magFlow',79,... 'posSwitch',false... ); initStruct = Simulink.Parameter(initStruct); initStruct.StorageClass = 'ExportedGlobal'; initStruct.DataType = 'Bus: ComponentData'; %% % In the Unit Delay block dialog box, specify *Initial condition* as % |initStruct|. set_param('rtwdemo_init_struct_busobj/Unit Delay','InitialCondition','initStruct') %% % Generate code from the example model. rtwbuild('rtwdemo_init_struct_busobj') %% % In the code generation report, view the file |rtwdemo_init_struct_busobj_types.h|. % The code creates a structure type |ComponentData| whose fields use the data % types in the bus object. file = fullfile('rtwdemo_init_struct_busobj_grt_rtw','rtwdemo_init_struct_busobj_types.h'); rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_ComponentData_','}',1,1) %% % View the file |rtwdemo_init_struct_busobj.c|. The code creates a global variable to represent the tunable % parameter object |initStruct|. file = fullfile('rtwdemo_init_struct_busobj_grt_rtw','rtwdemo_init_struct_busobj.c'); rtwdemodbtype(file,'/* Exported block parameters */','Variable: initStruct',1,1) %% % The model initialization function uses the structure fields to initialize the block states. rtwdemodbtype(file,'/* InitializeConditions for UnitDelay:',... '= initStruct.posSwitch;',1,1) %% % To change the data type of any of the three signal elements, specify the % new type in the bus object. The signal element in the model uses % the new type. Before simulation and code generation, the parameter object |initStruct| casts the % corresponding structure field to the new type.