%% Reduce Memory Consumption by Storing Parameter Value in Small Data Type
% When you use a parameter object (for example, |Simulink.Parameter|) to set block parameter values,
% you can configure the object to appear in the generated code as a tunable
% global variable. By default, the parameter object and the corresponding
% global variable typically uses the same data type as the signal or
% signals on which the block operates. For example, if the input signal of
% a Gain block uses the data type |int16|, the parameter object typically
% use the same data type. To reduce the amount of memory that this
% variable consumes, specify that the variable use a smaller
% integer data type such as |int8|.  
%% Store Parameter Value in Integer Data Type
% Open the model |rtwdemo_configrpinterface| and set the properties of the model.
open_system('rtwdemo_configrpinterface')
set_param('rtwdemo_configrpinterface','HideAutomaticNames','off')
set_param('rtwdemo_configrpinterface','ShowPortDataTypes','on')
set_param('rtwdemo_configrpinterface','SimulationCommand','Update')

%%
% Many of the signals in the model use the data type |double|.
%
% On the *Modeling* tab, click *Model Data Editor*.
%
% In the Model Data Editor, inspect the *Parameters* tab.
%
% Click the *Show/refresh additional information* button. 
%
% Next to the *Filter contents* box, activate the *Filter using selection*
% button.
%
% In the model, click the Gain block. The Model Data Editor shows one row
% that corresponds to the *Gain* parameter of the block and one row that
% corresponds to the MATLAB variable |K1|, which sets the parameter value to |2|.
% |K1| resides in the model workspace.
%
% In the C Code tab, select *Code Interface > Default Code Mappings*.
% In the Code Mappings editor, under *Parameters*, set the storage class for K1 to |ExportedGlobal|.
cm = coder.mapping.api.get('rtwdemo_configrpinterface');
setModelParameter(cm,'K1','StorageClass','ExportedGlobal');
%%
% In the C Code tab, select Code Interface > Default Code Mappings.
% In the Model Data Editor, set *Change view* to |Design| and ensure that the *Data Type* column for |K1| is |int8|.
hws = get_param(bdroot, 'modelworkspace');
hws.setVariablePart('K1.DataType','int8');
%%
% For the row that represents the *Gain* block parameter, in the *Data Type* column, set the drop-down list to
% |Inherit: Inherit from 'Gain'|. With this
% setting, the *Gain* parameter of the block inherits the |int8| data type
% from the parameter object.
set_param('rtwdemo_configrpinterface/Gain','ParamDataTypeStr',...
    'Inherit: Inherit from ''Gain''')
%%
% Generate code from the model.
rtwbuild('rtwdemo_configrpinterface')
%%
% The generated file |rtwdemo_configrpinterface.c| defines the global variable |K1|
% by using the data type |int8_T|, which corresponds to the data type
% |int8| in Simulink.
file = fullfile('rtwdemo_configrpinterface_grt_rtw','rtwdemo_configrpinterface.c');
rtwdemodbtype(file,'/* Exported block parameters */','int8_T K1 = 2;',1,1)
%%
% The code algorithm in the model |step| function uses |K1| to calculate
% the output of the Gain block. The algorithm casts
% |K1| to the data type |real_T| (|double|) because the signals involved in the
% calculation use the data type |real_T|.
rtwdemodbtype(file,'output =',...
    '10U',1,1)
%% Store Fixed-Point Parameter Value in Smaller Integer Data Type
% Suppose you configure the signals in your model to use fixed-point data
% types. You want a gain parameter to appear in the generated code as a
% tunable global variable. You know the range of real-world values that you expect the
% parameter to assume (for example, between 0 and 4). If you can meet your application requirements
% despite reduced precision, you can reduce memory consumption by configuring the
% parameter to use a different data type than the input and output signals
% of the block.
%
% Open the example model |fxpdemo_direct_form2| and configure it to show the generated names of blocks.
load_system('fxpdemo_direct_form2')
set_param('fxpdemo_direct_form2','HideAutomaticNames','off')
open_system('fxpdemo_direct_form2')
%%
% Update the block diagram. Signals in this model use signed fixed-point data
% types with a word length of 16 and binary-point-only scaling.
% 
% Open the Gain5 block dialog box. The *Gain* parameter is set to |1.85|.
% Suppose you want to configure this parameter.
%
% Set *Gain* to |myGainParam| and click *Apply*. 
%
% Click the action button (with three vertical dots) next to the parameter value. Select *Create*.
%
% In the Create New Data dialog box, set *Value* to
% |Simulink.Parameter(1.85)| and click *Create*. The |Simulink.Parameter|
% object |myGainParam| appears in the base workspace.
%
% Open the *Simulink Coder* app.
% In the C Code tab, select *Code Interface > Default Code Mappings*.
% In the Code Mappings editor, under *Parameters*, set the storage class for |myGainParam| to |ExportedGlobal|.
% With this setting, |myGainParam| appears in the
% generated code as a global variable.
%
% In the block dialog box, on the *Parameter Attributes* tab, set
% *Parameter minimum* to |0| and *Parameter maximum* to |4|.
%
% Set *Parameter data type* to |fixdt(0,8)| and click *Apply*.
%
% Click the *Show Data Type Assistant* button. The Data Type Assistant
% shows that the expression |fixdt(0,8)| specifies an unsigned fixed-point
% data type with a word length of 8 and best-precision scaling. When you
% simulate or generate code, the block parameter chooses a fraction length
% (scaling)
% that enables the data type to
% represent values between the parameter minimum and maximum (0 and 4) with the best possible
% precision.
%
% In the Data Type Assistant, set *Scaling* to |Binary point|. Click
% *Calculate Best-Precision Scaling*, *Fixed-point details*, and *Refresh Details*. The information
% under *Fixed-point details* shows that a fraction length of 5 can
% represent the parameter values with a precision of 0.03125.
%
% Set *Scaling* back to |Best precision| and click *OK*. In this example, when you simulate or generate
% code, the block parameter chooses a fraction length of 5.
%
% Alternatively, you can use these commands at the command prompt to create the object and
% configure the block:
myGainParam = Simulink.Parameter(1.85);
myGainParam.CoderInfo.StorageClass = 'ExportedGlobal';
set_param('fxpdemo_direct_form2/Gain5','Gain','myGainParam')
set_param('fxpdemo_direct_form2/Gain5','ParamMin','0','ParamMax','4')
set_param('fxpdemo_direct_form2/Gain5','ParamDataTypeStr','fixdt(0,8)')
%%
% Configure the model to produce a code generation report. To reduce clutter in the Command Window, clear the
% configuration parameter *Verbose build*.
set_param('fxpdemo_direct_form2','GenerateReport','on',...
    'LaunchReport','on','RTWVerbose','off')
%%
% Generate code from the model.
evalc('rtwbuild(''fxpdemo_direct_form2'')');
%%
% The generated file |fxpdemo_direct_form2.c| defines the global variable
% |myGainParam| by using the data type |uint8_T|, which corresponds to the
% specified word length, 8. The code initializes the variable by using an
% integer value that, given the fraction length of 5, represents the
% real-world parameter value 1.85.
file = fullfile('fxpdemo_direct_form2_grt_rtw','fxpdemo_direct_form2.c');
rtwdemodbtype(file,'/* Exported block parameters */','uint8_T myGainParam = 59U;',1,1)
%%
% The code algorithm uses |myGainParam| to calculate the output of the Gain5
% block. The algorithm uses a C shift to scale the result of the
% calculation.
rtwdemodbtype(file,'/* Gain: ''<Root>/Gain5'' incorporates:',...
'/* Gain: ''<Root>/Gain'' incorporates:',1,0)
%%
rtwdemoclean;


%% 
% Copyright 2012 The MathWorks, Inc.