%% Sensorless Field-Oriented Control of Induction Motor
%
% This example uses sensorless position estimation to implement the field-oriented control (FOC) technique to control 
% the speed of a three-phase AC induction motor (ACIM). 
% For details about FOC, see
% <docid:mcb_gs#mw_2d6dd8ca-938f-44d3-8334-f5e42bf4b73b Field-Oriented Control (FOC)>.
%
% This example uses rotor Flux Observer block to estimate the position of
% rotor flux.
% 
% The block uses stator voltages $({V_\alpha },{V_\beta })$ and
% currents $({I_\alpha },{I_\beta })$ as inputs and estimates the rotor
% flux, generated torque, and the rotor flux position.
% 
% The sensorless observers and algorithms have known limitations regarding
% motor operations beyond the base speed. We recommend that you use the
% sensorless examples for operations upto base speed only.
% 
% *NOTE:* The speed estimated by the Flux Observer block has an error
% against the actual rotor speed. This error is within tolerance of one percent of
% the base speed. You can introduce an offset compensation to the position
% output of the Flux Observer block to minimize this error.
% 

% Copyright 2020 The MathWorks, Inc.

%% Model
% 
% The example includes the model <matlab:open_system('mcb_acim_foc_sensorless_f28379d') mcb_acim_foc_sensorless_f28379d>.
% 
% 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.
%%
open_system('mcb_acim_foc_sensorless_f28379d.slx');
%%
% 
% For details on the supported hardware configuration, see the Required Hardware section under Generate Code and Deploy Model to Target Hardware.
% 
%% Required MathWorks(R) Products
% 
% *To simulate model:*
% 
% * Motor Control Blockset(TM)
% 
% *To generate code and deploy model:*
% 
% * Motor Control Blockset(TM)
% * Embedded Coder(R)
% * Embedded Coder(R) Support Package for Texas Instruments(TM) C2000(TM) Processors
% * Fixed-Point Designer(TM) (needed only for optimized code generation)
% 
%% Prerequisites
% 
% *1.* Obtain the motor parameters. 
% We provide the default motor parameters with the Simulink(R) model that you can replace with the values from either the motor datasheet or other sources.
% 
% *2.* If you obtain the motor parameters from the datasheet or other sources, update the motor 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>.
% 
% *3.* The initialization script also computes the derived parameters. For
% example, total leakage factor, rated flux, rated torque, stator and rotor
% inductances of the induction motor.
% 
%% Simulate Model
% 
% This example supports simulation. Follow these steps to simulate the
% model.
% 
% *1.* Open the model included with this example.
% 
% *2.* Click *Run* on the *Simulation* tab to simulate the model.
% 
% *3.* Click *Data Inspector* on the *Simulation* tab to view and analyze the simulation
% results.
% 
%% Generate Code and Deploy Model to Target Hardware
% This section instructs you on how to generate code and run the FOC algorithm on
% the target hardware.
% 
% This 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 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_acim_foc_qep_f28379d') mcb_acim_foc_qep_f28379d>
% 
% For connections related to this 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 Analog-to-Digital Converter (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 them 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.* Open the target model. If you want to change the default hardware configuration settings in the model, see <docid:mcb_gs#mw_3286e9a5-4b65-4b84-9133-a92130b252bc Model Configuration
% Parameters for Sensors>.
%
% *5.* Load a sample program to CPU2 of the LAUNCHXL-F28379D, for example program that operates the CPU2 blue LED, using the GPIO31 pin (_c28379D_cpu2_blink.slx_), to ensure that CPU2 is not mistakenly configured to use the board peripherals intended for CPU1.
% 
% *6.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *7.* In the target model, click the *host model* hyperlink to open the associated host model. You can also use the open_system command to open the host model.
%%
open_system('mcb_acim_foc_host_model.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>.
% 
% *8.* In the Host Serial Setup block mask of the host model, select a *Port name*.
%  
% *9.* Update the _Reference Speed_ value in the host model.
% 
% *10.* In the *Debug signals* section, select a signal that you want to monitor.
% 
% *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 in the open-loop condition (by default, the motor spins at 10% of the base speed).
% 
% *Note*: Do not run the motor (using this example) in the open-loop condition for long. The motor may draw high currents and produce excessive heat.
% 
% We designed the open-loop control to run the motor with a Reference Speed that is less than or equal to 10% of base speed.
%  
% *13.* Increase the motor _Reference Speed_ beyond 10% of the base speed to switch from open-loop to closed-loop control.
% 
% *NOTE:* To change the motor's direction of rotation, reduce the motor _Reference Speed_ to a value less than 10% of the base speed. This brings the motor back to the open-loop condition. 
% Change the direction of rotation, but keep the _Reference Speed_ magnitude constant. Then transition to the closed-loop condition.
% 
% *14.* Observe the debug signals from the RX subsystem in the *SelectedSignals* time scope of the host model.
% 