%% Tune Control Parameter Gains in Hardware and Validate Plant
% 
% This example uses field-oriented control (FOC) to run a three-phase permanent magnet synchronous motor (PMSM) in different modes of operation for plant validation. FOC algorithm implementation needs the real-time feedback of the rotor position. This example uses a 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)>.
% 
% The example runs the motor in these modes:
% 
% * *Stop* - In this mode, the motor stops running because the inverter outputs zero volts.
% 
% * *Open loop* - In this mode, the controller uses open-loop control to run the motor. You can use the *Operating Mode Variables* area of the host model to change the output voltage of the inverter and the rotor speed. Use the *Monitor* area to select the speed and rotor position values to display them on the scope for monitoring.
% 
% * *Torque control* - In this mode, the controller uses a torque control algorithm to run the motor. You can use the *Operating Mode Variables* area of the host model to change the ${I_d}$ reference and ${I_q}$ reference currents.
% 
% You can also lock the rotor by turning the slider switch to the Pos lock position that sets the rotor position to zero. Therefore, in this mode, the controller receives the position feedback as zero because the motor stops running. If you turn the switch to the Unlock position, the motor runs and the controller receives position feedback from the quadrature encoder (you can monitor this value by using the Position_meas signal in the *Monitor* area of host model). You can use the scope to monitor the two debug signals (Monitor Signal #1 and Monitor Signal #2) that you select in the *Monitor* area. Therefore, you can use the slider switch to tune the torque control gain parameters.
% 
% * *Speed control* - In this mode, the controller uses a speed control algorithm to run the motor. You can use the *Operating Mode Variables* area of the host model to change the Speed Reference value (in per-unit) of the rotor. You can use the scope to monitor the two debug signals (Monitor Signal #1 and Monitor Signal #2) that you select in the *Monitor* area. For information related to the per-unit system, see <docid:mcb_gs#mw_e962ec5d-44da-4cd1-889a-c8b45491450f Per-Unit System>.
% 
% To further control the motor, you can also use the *Control loop gains* area of the host model to change the control parameters of the |d|-axis and |q|-axis current controllers and the speed controller.
% 
% You can use this example to run the motor in open-loop control, torque control, and speed control modes. You can also use this example for tuning the hardware gains and validating the plant model. 
% 
% *Caution:* Stop the motor first before transitioning from one operating mode to another.
% 
% You can select one of these operating modes in the Control area of the host model:
% 
% * Stop
% * Open loop run
% * Torque control
% * Speed control
% 

% Copyright 2020 The MathWorks, Inc.

%% Model
% 
% The example includes this model:
% 
% * *mcb_pmsm_operating_mode_f28379d*
% 
% You can use the model for both simulation and code generation. You can use the open_system command to open the Simulink(R) model:
%%
open_system('mcb_pmsm_operating_mode_f28379d.slx');
%%
% 
%% Required MathWorks(R) Products
% 
% *To simulate model:*
% 
% * Motor Control Blockset(TM)
% 
% *To generate code and deploy model:*
% 
% *1.* Motor Control Blockset(TM)
% 
% *2.* Embedded Coder(R)
% 
% *3.* Embedded Coder(R) Support Package for Texas Instruments(TM) C2000(TM) Processors
% 
% *4.* Fixed-Point Designer(TM)
% 
%% 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 only for the Speed Control operating mode. 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 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 model, run (and control) the motor in a selected operating mode, and monitor the debug signals of the model.
% 
% *Required Hardware*
%
% This example supports this hardware configuration. Use the target model name (highlighted in bold) to open the model for the corresponding hardware configuration, from the MATLAB(R) command prompt.
% 
% * LAUNCHXL-F28379D controller + BOOSTXL-DRV8305 inverter: *mcb_pmsm_operating_mode_f28379d*
% 
% 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 for the hardware configuration that you want to use. 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, program that operates the CPU2 blue LED by using GPIO31 (c28379D_cpu2_blink.slx).
% 
% *NOTE:*
% 
% * Do not directly switch between the open-loop run, torque control, and speed control operating modes. Always stop the motor before changing the operating mode.
% 
% * Before you run the motor in speed control mode for the first time, run the motor in open-loop to determine the quadratue encoder index. This helps to start the motor smoothly in the closed-loop speed control mode.
% 
% 
% *Instructions for Open-Loop Run Mode:*
% 
% *1.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *2.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *3.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *4.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *5.* Select *Stop* in the *Control* area to stop the motor.
% 
% *6.* Select *Open loop run* to start the motor.
% 
% 
% *Instructions for Torque Control Mode:*
% 
% *1.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *2.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *3.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *4.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *5.* Select *Stop* in the *Control* area to stop the motor.
% 
% *6.* Enter the value 0 (per-unit) in the *Id Reference* and *Iq Reference* fields in the *Operating Mode Variables* area.
% 
% *7.* Select *Torque control* in the *Control* area.
% 
% *8.* Move the slider switch to *Unlock* position in the *Operating Mode Variables* area.
% 
% *9.* Select *Iq_ref* for *Monitor Signal #1* and *Iq_meas* for *Monitor Signal #2* in the *Monitor* area.
% 
% *10.* Enter the value 0.1 (per-unit) in the in the *Iq Reference* field (in the *Operating Mode Variables* area) to start running the motor.
% 
% *11.* Open the scope in the host model and monitor the Iq_ref and Iq_meas current signals.
% 
% *Note:* The motor can reach high speeds if you run it under no load condition in this operating mode. In addition, the motor will not meet the Iq reference current under no load condition in this operating mode.
% 
% 
% *Instructions for Speed Control Mode:*
% 
% *1.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *2.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *3.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *4.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *5.* Select *Stop* in the *Control* area to stop the motor.
% 
% *6.* Enter the value 0.5 (per-unit) in the *Speed Reference* field in the *Operating Mode Variables* area.
% 
% *7.* Select *Speed control* in the *Control* area.
% 
% *8.* Select *Speed_ref* for *Monitor Signal #1* and *Speed_meas* for *Monitor Signal #2* in the *Monitor* area.
% 
% *9.* Open the scope in the host model and monitor the Speed_ref and Speed_meas output signals.
% 
% 
% *Instructions for Tuning Gain of Torque Controller:*
% 
% *1.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *2.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *3.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *4.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *5.* Select *Stop* in the *Control* area to stop the motor.
% 
% *6.* Select *Torque control* in the *Control* area.
% 
% *7.* Turn the slider switch to *Pos lock* position in the *Operating Mode Variables* area.
% 
% *8.* Select *Id_ref* for *Monitor Signal #1* and *Id_meas* for *Monitor Signal #2* in the *Monitor* area.
% 
% *9.* Enter the value 0.2 (per-unit) in the *Id Reference* field in the *Operating Mode Variables* area.
% 
% *10.* Open the scope and monitor the step response signal.
% 
% *11.* Tune the control gains Kp and Ki for the |d|-axis current controller.
% 
% 
% *Instructions for Tuning Gain of Speed Controller:*
% 
% *1.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *2.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *3.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *4.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *5.* Select *Stop* in the *Control* area to stop the motor.
% 
% *6.* Select *Speed control* in the *Operating Mode Variables* area.
% 
% *7.* Select *Speed_ref* for *Monitor Signal #1* and *Speed_meas* for *Monitor Signal #2* in the *Monitor* area.
% 
% *8.* Enter the value 0.5 (per-unit) in the *Speed Reference* field in the *Operating Mode Variables* area.
% 
% *9.* Open the scope and observe the reference and the measured speed values.
% 
% *10.* Enter the value 0.8 (per-unit) in the *Speed Reference* field.
% 
% *11.* Observe the speed step response in the scope.
% 
% *12.* Tune the control gains Kp and Ki for the speed controller.
% 
% 
% *Instructions for Validating Plant Model:*
% 
% *1.* Click the *host model* hyperlink in the target model to open the associated host model.
% 
% *2.* In the Host Serial Setup block mask of the host model, select a *Port name*.
% 
% *3.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *4.* Open the target model using this command:
%   open_system('mcb_pmsm_operating_mode_f28379d.slx');
% 
% *5.* Change the position of the Manual Switch (in mcb_pmsm_operating_mode_f28379d.slx/Speed Control/Speed_control) to Simulate Step Speed Input.
% 
% *6.* Simulate the model mcb_pmsm_operating_mode_f28379d.slx.
% 
% *7.* Open the scope in the model to observe the step response of the speed controller.
% 
% *8.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *9.* Select *Stop* in the *Control* area of the host model to ensure that the motor is not running.
% 
% *10.* Select *Speed control* in the *Operating Mode Variables* area.
% 
% *11.* Select *Speed_ref* for *Monitor Signal #1* and *Speed_meas* for *Monitor Signal #2* in the *Monitor* area.
% 
% *12.* Enter the value 0.2 (per-unit) in the *Speed Reference* field in the *Operating Mode Variables* area.
% 
% *13.* Open the scope and observe the reference and the measured speed values.
% 
% *14.* Enter the value 0.5 (per-unit) in the *Speed Reference* field.
% 
% *15.* Observe the speed step response in the scope.
% 
% *16.* Compare the speed step responses obtained in steps 6 (with simulation) and 15 (with code generation).
% 
% 
% *Note:* You can also use the open_system command to open the host model:
%%
open_system('mcb_host_mode_control.slx');
%%
% In the *Control loop gains* area, you must enter the gain values that can be represented by the datatype defined in the model initialization script.
% 
% 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>.
% 