%% Replacing Variable Resistors
% This example shows how to convert a model that is nonlinear due to a
% variable resistor into a switched linear model making it compatible with
% Simscape to HDL Workflow.

%   Copyright 2018 The MathWorks, Inc.
 
%% Introduction
% Simscape to HDL Workflow supports conversion of Simscape switched linear
% models to functionally-equivalent Simulink models that are compatible for
% HDL code generation. Due to the nature of the equations that result from
% variable resistors, blocks such as Piecewise-Constant Resistor can lead
% to nonlinear behavior and must be replaced with equivalent switched
% linear components. Specifically the Piecewise-Constant Resistor contains
% events that are
% <matlab:helpview(fullfile(docroot,'hdlcoder/ug/generate-hdl-code-from-simscape-model.html'))
% not supported> by the Simscape HDL Workflow Advisor.
%
% Open The Simscape(TM) Model. In the MATLAB(R) command prompt, enter:
 
%%
nonlinearModel = 'sschdlexVariableResistorExample';
load_system(nonlinearModel)
open_system([nonlinearModel,'/Simscape Subsystem'])
 
%%
% This model is an RLC circuit with a Piecewise-Constant Resistor acting as
% the resistor or 'load'. For the Piecewise-Constant Resistor, the
% relationship between voltage V and current I is V=I*R where R is the
% numerical value presented at the physical signal port R. 
%
% To ensure a positive value for the resistance, any value below 1e-6 is
% replaced by 1e-6. This resistor is Piecewise-Constant because the resistance
% only changes when the input value differs from the current resistance by
% more than a set tolerance. Thus, a continuously changing input would be
% converted to a discrete set of resistances.  
%
% In this model the signal going into the Piecewise-Constant Resistor is a
% step function that changes from 2 to 3 at t=0.1 thus changing the load
% resistance from 2 $\Omega$ to 3 $\Omega$.
%%
% <<sschdl_tutorial_variable_resistor_variableResistorControl.png>>
%
%%
% To open the Simscape HDL Workflow Advisor at the command-line, enter:
%   sschdladvisor(nonlinearModel)
%%
% Run the workflow to the Get state-space parameters task. This task fails
% because of the presence of the Piecewise-Constant Resistor. 
%%
% <<sschdl_tutorial_variable_resistor_sschdlfailure.png>>
%% Replace Variable Resistor with switches and constant resistors.
% To convert this model to an equivalent switched linear model, replace the
% Piecewise-Constant Resistor with a set of switches and resistors for each
% desired value. To open the switched linear version in the MATLAB(R) command prompt, enter:
switchedLinearModel = 'sschdlexVariableResistorSwitchedLinearExample';
load_system(switchedLinearModel)
open_system(switchedLinearModel)
 
%%
% The variable resistor has been replaced by a resistor and switch for each
% desired resistance. To recreate the behavior of a load resistance that
% changes from 2 $\Omega$ to 3 $\Omega$ at t=0.1 two resistors are used, one
% with a resistance of 2 $\Omega$ and the other with a resistance of 3
% $\Omega$. By closing and opening the switches the load resistance
% switches from 2 $\Omega$ to 3 $\Omega$.
%
%% Controlling the Switches
% The switches must be turned on and off to provide the correct load
% resistance. To view the control signals for the switches in the MATLAB(R)
% command prompt, enter: 
open_system([switchedLinearModel,'/Input'])
%%
% To achieve the correct resistance, create two step functions. One to open
% the switch in series with the 2 $\Omega$ resistor at t=0.1 and another to
% close the switch in series with the 3 $\Omega$ resistor at the same time.
%
%%
% Now that the variable resistor has been replaced with switched linear
% components run the Simscape HDL Workflow Advisor and see that all the tasks run
% to completion.
%%
% <<sschdl_tutorial_variable_resistor_sschdlsuccess.png>>
%
%%
% By changing the variable piecewise resistor to a number of specified
% resistors that switch on and off the model has been changed to a form
% that is compatible with the Simscape to HDL workflow.


