%% Model Switching Dynamics in Inverter Using Simscape Electrical
% 
% This example uses field-oriented control (FOC) to control the speed of a three-phase permanent magnet synchronous motor (PMSM). It gives you the option to use these Simscape Electrical blocks as an alternative to the Average Value Inverter block in Motor Control Blockset(TM):
% 
% * Converter (Three-Phase)
% * Ideal Semiconductor Switch
% 
% The example also gives you the option to use the PMSM block from Simscape(TM) Electrical(TM) as an alternative to the Surface Mount PMSM block from Motor Control Blockset(TM). These Simscape(TM) Electrical(TM) blocks enable you to generate high-fidelity simulations.
% 
% Field-oriented control (FOC) needs a real time feedback of the rotor position. This example uses the quadrature encoder sensor to measure the rotor position. For details about FOC, see <docid:mcb_gs#mw_2d6dd8ca-938f-44d3-8334-f5e42bf4b73b Field-Oriented Control (FOC)>.
% 
% You can use this example to simulate the target model by using different inverters and monitor the feedback current for each inverter. You can also generate the code and use the host model along with the target model.
% 

% Copyright 2020 The MathWorks, Inc.

%% Models
% 
% The example includes the model <matlab:open_system('mcb_ee_pmsm_foc')
% mcb_ee_pmsm_foc>.
% 
% You can use this model for both simulation and code generation. You can also use the open_system command to open the Simulink(R) model. For example, use this command for a F28379D based controller:
%%
open_system('mcb_ee_pmsm_foc.slx');
%%
% 
%% Required MathWorks(R) Products
% 
% *To simulate model:*
% 
% * Motor Control Blockset(TM)
% * Simscape(TM) Electrical(TM)
% 
% *To generate code and deploy model:*
% 
% * Motor Control Blockset(TM)
% * Simscape(TM) Electrical(TM)
% * Embedded Coder(R)
% * Embedded Coder(R) Support Package for Texas Instruments(TM) C2000(TM) Processors
% * Fixed-Point Designer(TM) (only needed for optimized code generation)
% 
%% Prerequisites
% 
% *1.* Obtain the motor parameters. 
% We provide default motor parameters with the Simulink(R) model that you can replace with the values from either the motor datasheet or other sources.
% 
% However, if you have the motor control hardware, you can estimate the parameters for the motor that you want to use, by using the Motor Control Blockset parameter estimation tool. For instructions, see
% <docid:mcb_gs#mw_5020c0a3-3cde-4f36-bebc-21090973f8f5 Estimate Motor Parameters by Using Motor Control Blockset Parameter Estimation Tool>.
% 
% The parameter estimation tool updates the _motorParam_ variable (in the MATLAB(R) workspace) with the estimated motor parameters.
% 
% *2.* If you obtain the motor parameters from the datasheet or other sources, update the motor parameters and inverter parameters in the model initialization script
% associated with the Simulink(R) models. For instructions, see
% <docid:mcb_gs#mw_dd32d1fd-68d8-4cfd-8dea-ef7f7ed008c0 Estimate Control Gains from Motor Parameters>.
% 
% If you use the parameter estimation tool, you can update the inverter parameters, but do not update the motor parameters in the model initialization script. The script automatically extracts motor parameters from the updated _motorParam_ workspace variable.
% 
%% Simulate Model
% 
% This example supports simulation. Follow these steps to simulate the
% model.
% 
% *1.* Open the target model *mcb_ee_pmsm_foc*.
% 
% *2.* Select one of these options in the InverterSelected radio group in the target model to simulate an inverter variant:
% 
% * *Motor Control Blockset average inverter* - Select this option to use the Average Inverter and Surface Mount PMSM blocks.
% 
% * *Simscape Electrical 3 phase converter* - Select this option to use the Converter (Three-Phase) and PMSM blocks.
% 
% * *Simscape Electrical Modular Multilevel converter* - Select this option
% to use the Ideal Semiconductor Switch and PMSM blocks. This option
% simulates the Simscape Electrical modular multilevel converter using a
% low voltage.
% 
% *3.* Select an option from the InverterSelected radio group and click *Run* on the *Simulation* tab to simulate the target model.
% 
% *4.* On the target model, click *Data Inspector* on the *Simulation* tab to view results from the three simulation runs.
% 
% This image shows the simulation results for ${I_a}$ phase current:
% 
% <<../mcb_phys_mod_plot.png>>
% 
% These images show the comparison of rotor speed, ${I_q}$ current, ${I_{ab}}$ phase current, and rotor position for the three inverter types:
% 
% <<../mcb_phys_mod_plot1.png>>
% 
% <<../mcb_phys_mod_plot2.png>>
% 
% These images show the comparison of PWM modulation waveforms for the three inverter types:
% 
% <<../mcb_phys_mod_plot3.png>>
% 
% <<../mcb_phys_mod_plot4.png>>
%
%% Generate Code and Deploy Model to Target Hardware
% This section instructs you to generate code and run the FOC algorithm on
% the target hardware.
% 
% The example uses a host and a target model. The host model is a user interface to the controller hardware board.  You can run the host model on the host computer. The prerequisite to use the host model is to deploy the target model to the controller hardware board. The host model uses serial communication to command the target Simulink(R) model and run the motor in a closed-loop control.
% 
% *Required Hardware*
%
% The example supports this hardware configuration. You can also use the target model name to open the model for the corresponding hardware configuration, from the MATLAB(R) command prompt.
% 
% * LAUNCHXL-F28379D controller + BOOSTXL-DRV8305 inverter: <matlab:open_system('mcb_ee_pmsm_foc') mcb_ee_pmsm_foc>
% 
% For connections related to the preceding hardware configuration, see <docid:mcb_gs#mw_8a869325-5b0d-4631-afd5-05a23622cc5c Hardware Connections for LAUNCHXL board>.
% 
% *Generate Code and Run Model on Target Hardware*
%
% *1.* Simulate the target model and observe the simulation results.
% 
% *2.* Complete the hardware connections.
% 
% *3.* The model automatically computes the ADC (or current) offset values. To disable this functionality (enabled by default), update the value 0 to the variable inverter.ADCOffsetCalibEnable in the model initialization script.
%  
% Alternatively, you can compute the ADC offset values and update it manually in the model initialization scripts. For instructions, see <docid:mcb_gs#mw_2d4f6f28-855c-4e0c-b977-bf5b93a09227 Run 3-Phase AC Motors in Open-Loop Control and Calibrate ADC Offset>.
% 
% *4.* Compute the quadrature encoder index offset value and update it in the model initialization scripts associated with the target model. For instructions, see <docid:mcb_gs#mw_52571b8e-639e-4a24-a8bf-b644eb78edc1 Quadrature Encoder Offset Calibration for PMSM Motor>.
% 
% *5.* Open the target model. If you want to change the default hardware configuration settings for the model, see <docid:mcb_gs#mw_3286e9a5-4b65-4b84-9133-a92130b252bc Model Configuration
% Parameters for Sensors>.
% 
% *6.* To ensure that CPU2 is not mistakenly configured to use the board peripherals intended for CPU1, load a sample program to CPU2 of LAUNCHXL-F28379D, for example, a program that operates the CPU2 blue LED by using GPIO31 (c28379D_cpu2_blink.slx).
% 
% *7.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *8.* Click the *host model* hyperlink in the target model to open the associated host model. You can also use the open_system command to open the host model. For example, use this command for a F28379D based controller:
%%
open_system('mcb_pmsm_foc_host_model_f28379d.slx'); 
%%
% For details about the serial communication between the host and target
% models, see <docid:mcb_gs#mw_7d703f4b-0b29-4ec7-a42b-0b300f580ccc Communication between Host and Target>.
% 
% *9.* In the Host Serial Setup block mask of the host model, select a *Port name*.
%  
% *10.* Update the *Reference Speed* value in the host model.
% 
% *11.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *12.* Change the position of the Start / Stop Motor switch to On, to start running the motor.
%  
% *13.* Observe the debug signals from the RX subsystem, in the Time Scope and Display blocks of the host model.
% 
% *Note*: In the host model, you can also select the debug signals that you want to monitor.
% 
%% Other Things to Try
% 
% You can also use SoC Blockset(TM) to implement a closed-loop motor control
% application that addresses challenges related to ADC-PWM synchronization,
% controller response, and studying different PWM settings. You can use
% Simscape(TM) Electrical(TM) to implement high fidelity inverter
% simulation. For details, see <docid:mcb_gs#mw_70df71b5-79de-4644-89b5-ed230e50718b Integrate MCU
% Scheduling and Peripherals in Motor Control Application>.
% 