%% Six-Step Commutation of BLDC Motor Using Sensor Feedback
%
% This example uses 120-degree conduction mode to implement the six-step commutation technique to control 
% speed and direction of rotation of a three-phase brushless DC (BLDC) motor. The example uses the switching sequence 
% generated by the Six Step Commutation block to control three-phase stator voltages, and therefore, control the rotor speed and direction. 
% For more details about this block, see
% <docid:mcb_ref#mw_e3591137-bd90-4528-bd5b-223b39a8ede9 Six Step Commutation>. 
% 
% The six-step commutation algorithm requires a Hall sequence or a rotor position feedback value (which is obtained from either a quadrature encoder or a Hall sensor).
% 
% The quadrature encoder sensor consists of a disk with two tracks or
% channels that are coded 90 electrical degrees out of phase. This creates
% two pulses (A and B) that have a phase difference of 90 degrees and an
% index pulse (I). The controller uses the phase relationship
% between the A and B channels and the transition of channel states to
% determine the speed, position, and direction of rotation of the motor.
% 
% A Hall effect sensor varies its output voltage based on the strength of
% the applied magnetic field. According to the standard configuration, a
% BLDC motor consists of three Hall sensors located electrically 120 degrees
% apart. A BLDC with the standard Hall placement (where the sensors are
% placed electrically 120 degrees apart) can provide six valid combinations
% of binary states: for example, 001,010,011,100,101, and 110. The sensor
% provides the angular position of the rotor in degrees in the multiples of 60, which
% the controller uses to determine the 60-degree sector
% where the rotor is present.
% 
% The controller controls the motor by using the Hall sequence or the rotor
% position. It energizes the next two phases of the stator winding,
% so that the rotor always maintains a torque angle (angle between rotor
% d-axis and stator magnetic field) of 90 degrees with a deviation of 30
% degrees.
% 
% <<../mcb_bldc_sixstep_qep_hall.png>>
% 

% Copyright 2020 The MathWorks, Inc.

%% Models
% 
% The example includes the model *mcb_bldc_sixstep_f28379d*.
% 
% You can use this model for both simulation and code generation. To open the Simulink(R) model, use the open_system command at the MATLAB command prompt.
%%
open_system('mcb_bldc_sixstep_f28379d.slx');
%%
% 
% For details of the supported hardware configuration, see Required Hardware in the Generate Code and Deploy Model to Target Hardware section.
% 
%% 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) (only needed for optimized code generation)
% 
%% Prerequisites
% 
% *1.* Obtain the motor parameters. 
% We provide default motor parameters with the Simulink model that you can replace with 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 a motor datasheet or from other sources, update the motor parameters and the inverter parameters in the model initialization script
% associated with the Simulink 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 the motor parameters from the updated _motorParam_ workspace variable.
% 
%% Simulate Model
% 
% This example supports simulation. Follow these steps to simulate the
% model.
% 
% *1.* Open the model included with this example.
% 
% *2.* Select either the QEP or the Hall Speed_Feedback radio button in the model.
% 
% *3.* Click *Run* on the *Simulation* tab to simulate the model.
% 
% *4.* Click *Data Inspector* on the *Simulation* tab to view and analyze the simulation
% results.
% 
%% Generate Code and Deploy Model to Target Hardware
% This section shows you 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 model and run the motor in a closed-loop control.
% 
% *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_bldc_sixstep_f28379d*
% 
% For connections related to the 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 computes the ADC (or current) offset values by default. To disable this functionality, 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 script. 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.* If you are using a quadrature encoder, compute the quadrature encoder index offset value and update it in the model initialization script 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.* If you are using a Hall sensor, compute the Hall sequence value and
% update it in the _bldc.hallsequence_ variable in the model initialization script associated with the target
% model. For instructions, see
% <docid:mcb_gs#mw_ac526d91-7b0a-429b-beed-b32ed16ccd64 Hall Sensor Sequence Calibration of BLDC Motor>.
% 
% *6.* 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>.
%
% *7.* Select either the QEP or the Hall Speed_Feedback radio button in the target model.
% 
% *8.* Load a sample program to CPU2 of LAUNCHXL-F28379D. For example, you can use the program that operates the CPU2 blue LED by using GPIO31 (c28379D_cpu2_blink.slx), and ensure that CPU2 is not mistakenly configured to use the board peripherals intended for CPU1.
% 
% *9.* Click *Build, Deploy & Start* on the *Hardware* tab to deploy the target model to the hardware.
% 
% *10.* 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. Use this command for a F28069M based controller.
%%
open_system('mcb_bldc_host_model_f28379d.slx'); 
%%
% For on the serial communication between the host and target
% models, see <docid:mcb_gs#mw_7d703f4b-0b29-4ec7-a42b-0b300f580ccc Communication between Host and Target>.
% 
% *11.* In the Host Serial Setup block mask in the host model, select a *Port name*.
%  
% *12.* Update the reference speed value in the _Reference Speed (RPM)_ field in the host model.
% 
% *13.* In the host model, select the debug signals that you want to monitor.
% 
% *14.* Click *Run* on the *Simulation* tab to run the host model.
% 
% *15.* Change the position of the Start / Stop Motor switch to On, to start running the motor.
%  
% *16.* Observe the debug signals from the RX subsystem, in the Scope and Display blocks in the host model.
% 