├── 1 Simulating back emf voltage of a BLDC motor ├── BLDC_back_EMF.slx ├── README.md └── images │ ├── algorithm.png │ ├── backEMF.png │ └── model.png ├── 2 Modeling a three phase inverter ├── Modeling_three_phase_inverter.slx ├── README.md ├── animateRotorPosition.m └── images │ ├── GS_part2.gif │ ├── algorithm.png │ └── model.png ├── 3 Modeling commutation logic ├── Modeling_commutation_logic.slx ├── README.md ├── animateRotorPosition.m └── images │ ├── algorithm.png │ ├── animation.gif │ └── model.png ├── 4 Modeling a PWM controlled buck converter ├── BLDC_control_buck_converter.slx ├── README.md ├── bldcData.mat └── images │ ├── algorithm.png │ └── model.png ├── 5 PWM control of a BLDC motor ├── BLDC_PWM_control.slx ├── README.md ├── bldcData.mat └── images │ ├── algorithm.png │ └── model.png ├── LICENSE.md ├── README.md └── SECURITY.md /1 Simulating back emf voltage of a BLDC motor/BLDC_back_EMF.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/1 Simulating back emf voltage of a BLDC motor/BLDC_back_EMF.slx -------------------------------------------------------------------------------- /1 Simulating back emf voltage of a BLDC motor/README.md: -------------------------------------------------------------------------------- 1 | # Simulating Back-EMF Voltage of a BLDC Motor 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | This repository contains the Simulink model used in the [How to Design Motor Controllers Using Simscape Electrical, Part 1: Simulating Back-EMF Voltage of a BLDC Motor](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html) video. Check out the rest of the videos in [this section](#videos-and-files). 5 | 6 | ## Model and Setup 7 | The provided model shown below lets you simulate a three-phase BLDC motor and observe its back-EMF voltage. 8 | 9 | ![](images/model.png) 10 | 11 | In this simulation, we create a scenario where we turn the motor shaft while having open terminals at all three phases and then measure the voltage produced at one of the phases to observe the back-EMF which is shown in the below picture. 12 | 13 |

14 | 15 |

16 | 17 | To run the simulation, open the “BLDC_back_EMF” Simulink model, run simulation by pressing the “Run” button on the simulation tab and observe the back-EMF voltage using the provided scope. 18 | 19 | ## Videos and Files 20 | "How to Design Motor Controllers Using Simscape Electrical" video series consists of 5 videos. Click the links below to watch the videos and download the files. 21 | 22 | - Part 1: Simulating Back-EMF Voltage of a BLDC Motor [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor)] 23 | - Part 2: Modeling a Three-Phase Inverter [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter)] 24 | - Part 3: Modeling Commutation Logic [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic)] 25 | - Part 4: Model PWM-Controlled Buck Converters [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter)] 26 | - Part 5: An Alternative implementation of PWM Control [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor)] 27 | 28 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 29 | 30 | - How brushless DC motors differ from brushed DC motors and how they work 31 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 32 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor. 33 | 34 | ![](algorithm.png) 35 | 36 | -------------------------------------------------------------------------------- /1 Simulating back emf voltage of a BLDC motor/images/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/1 Simulating back emf voltage of a BLDC motor/images/algorithm.png -------------------------------------------------------------------------------- /1 Simulating back emf voltage of a BLDC motor/images/backEMF.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/1 Simulating back emf voltage of a BLDC motor/images/backEMF.png -------------------------------------------------------------------------------- /1 Simulating back emf voltage of a BLDC motor/images/model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/1 Simulating back emf voltage of a BLDC motor/images/model.png -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/Modeling_three_phase_inverter.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/2 Modeling a three phase inverter/Modeling_three_phase_inverter.slx -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/README.md: -------------------------------------------------------------------------------- 1 | # Modeling a Three-Phase Inverter 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | This repository contains the MATLAB and Simulink files used in the [How to Design Motor Controllers Using Simscape Electrical, Part 2: Modeling a Three-Phase Inverter](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html) video. Check out the rest of the videos in [this section](#videos-and-files). 5 | 6 | ## Model and Setup 7 | The provided model lets you simulate a BLDC motor which is driven by a three-phase inverter. A snapshot of the model is given below. 8 | 9 | ![](images/model.png) 10 | 11 | In the model, the BLDC motor is set up to have a single pole pair in the rotor and the three-phase inverter is driven with a static switching pattern simultaneously energizing phases A and C. The following animation shows the resulting rotor alignment. 12 | 13 |

14 | 15 |

16 | 17 | You can do the following with the provided files: 18 | 19 | - Run Simulink model (Modeling_three_phase_inverter.slx) and observe angular position and speed responses of the BLDC motor using the provided scopes. 20 | 21 | - Run MATLAB file (animateRotorPosition.m) to create the BLDC motor animation seen above (also at 7:23 in the video). After running the MATLAB file, press any button to start the animation. The MATLAB file runs the Simulink model, and uses the simulated data to animate the BLDC motor. 22 | 23 | ## Videos and Files 24 | "How to Design Motor Controllers Using Simscape Electrical" video series consists of 5 videos. Click the links below to watch the videos and download the files. 25 | 26 | - Part 1: Simulating Back-EMF Voltage of a BLDC Motor [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor)] 27 | - Part 2: Modeling a Three-Phase Inverter [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter)] 28 | - Part 3: Modeling Commutation Logic [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic)] 29 | - Part 4: Model PWM-Controlled Buck Converters [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter)] 30 | - Part 5: An Alternative implementation of PWM Control [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor)] 31 | 32 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 33 | 34 | - How brushless DC motors differ from brushed DC motors and how they work 35 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 36 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor. 37 | 38 | ![](algorithm.png) 39 | -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/animateRotorPosition.m: -------------------------------------------------------------------------------- 1 | %Copyright 2019 The MathWorks, Inc. 2 | %% animateRotorPosition 3 | % 4 | % This script will run a Simulink model of a BLDC that is energized in 5 | % one coil and animate the movement of the rotor. Once the animation figure 6 | % is rendered, there is a 5 second pause before the animation begins. 7 | 8 | close all 9 | clear 10 | 11 | 12 | % Below parameters are defined in the Simulink model 13 | % Sample time 14 | % Ts = 1e-5; 15 | % Number of pole pairs 16 | % p = 1; 17 | % Initial rotor angle in degrees 18 | % th0 = 0; 19 | % Sector 20 | % sector = 6; 21 | 22 | 23 | mdl = 'Modeling_three_phase_inverter'; 24 | 25 | open_system(mdl); 26 | 27 | sim(mdl) 28 | 29 | try 30 | % 31 | r = 1.2; 32 | theta = linspace(0,2*pi); 33 | x = cos(theta); 34 | y = sin(theta); 35 | 36 | x1 = 0.8*cos(theta); 37 | y1 = 0.8*sin(theta); 38 | 39 | xa = cos(0); 40 | ya = sin(0); 41 | 42 | xb = cos(2*pi/3); 43 | yb = sin(2*pi/3); 44 | 45 | xc = cos(-2*pi/3); 46 | yc = sin(-2*pi/3); 47 | 48 | xat = r*cos(0); 49 | yat = r*sin(0); 50 | 51 | xbt = r*cos(2*pi/3); 52 | ybt = r*sin(2*pi/3); 53 | 54 | xct = r*cos(-2*pi/3); 55 | yct = r*sin(-2*pi/3); 56 | 57 | hf = figure(1);h = plot(x,y,'k-',[0 xa],[0 ya],'k-',[0 xb],[0 yb],'k-',[0 xc],[0 yc],'k-',xa,ya,'ko',xb,yb,'ko',xc,yc,'ko',x1,y1,'k-');grid 58 | 59 | set(hf,'Color',[1 1 1]) 60 | 61 | ha = gca; 62 | 63 | set(ha,'Visible','off') 64 | 65 | ht1 = text(xat,yat,'A'); 66 | ht2 = text(xbt,ybt,'B'); 67 | ht3 = text(xct,yct,'C'); 68 | 69 | set(ht1,'FontSize',14); 70 | set(ht2,'FontSize',14); 71 | set(ht3,'FontSize',14); 72 | 73 | 74 | axis([-1.2 1.2 -1.2 1.2]) 75 | axis equal 76 | 77 | set(h(5),'MarkerSize',20) 78 | set(h(6),'MarkerSize',20) 79 | set(h(7),'MarkerSize',20) 80 | 81 | switch_pattern = logsout{2}.Values.Data(1:20:end,:); 82 | 83 | init_switch_pattern = num2str(switch_pattern(1,:)); 84 | 85 | 86 | switch init_switch_pattern 87 | 88 | case num2str([1 0 0 0 0 1]) 89 | 90 | set(h(5),'MarkerFaceColor',[1 0 0]) 91 | set(h(7),'MarkerFaceColor',[0 0 1]) 92 | 93 | case num2str([0 0 1 0 0 1]) 94 | 95 | set(h(6),'MarkerFaceColor',[1 0 0]) 96 | set(h(7),'MarkerFaceColor',[0 0 1]) 97 | 98 | case num2str([0 1 1 0 0 0]) 99 | 100 | set(h(5),'MarkerFaceColor',[0 0 1]) 101 | set(h(6),'MarkerFaceColor',[1 0 0]) 102 | 103 | case num2str([0 1 0 0 1 0]) 104 | 105 | set(h(5),'MarkerFaceColor',[0 0 1]) 106 | set(h(7),'MarkerFaceColor',[1 0 0]) 107 | 108 | case num2str([0 0 0 1 1 0]) 109 | 110 | set(h(6),'MarkerFaceColor',[0 0 1]) 111 | set(h(7),'MarkerFaceColor',[1 0 0]) 112 | 113 | case num2str([1 0 0 1 0 0]) 114 | 115 | set(h(5),'MarkerFaceColor',[1 0 0]) 116 | set(h(6),'MarkerFaceColor',[0 0 1]) 117 | 118 | end 119 | 120 | 121 | l1 = 0.7; 122 | ro = logsout{1}.Values.Data(1)*pi/180; 123 | 124 | xr1 = l1*cos(ro); 125 | yr1 = l1*sin(ro); 126 | xr2 = -l1*cos(ro); 127 | yr2 = -l1*sin(ro); 128 | 129 | hold on,hl1 = plot([xr1 xr2],[yr1 yr2],'m-'); 130 | 131 | set(hl1,'LineWidth',3) 132 | 133 | hpr = plot(xr2,yr2,'ko'); 134 | hpb = plot(xr1,yr1,'ko'); 135 | 136 | 137 | set(hpr,'MarkerSize',20) 138 | set(hpb,'MarkerSize',20) 139 | set(hpr,'MarkerFaceColor',[1 0 0]) 140 | set(hpb,'MarkerFaceColor',[0 0 1]) 141 | 142 | 143 | pause 144 | 145 | ro1 = logsout{1}.Values.Data(1:40:end); 146 | 147 | for l = 1:numel(ro1) 148 | 149 | switch_pattern1 = num2str(switch_pattern); 150 | 151 | ro = ro1(l)*pi/180; 152 | 153 | 154 | xr1 = l1*cos(ro); 155 | yr1 = l1*sin(ro); 156 | xr2 = -l1*cos(ro); 157 | yr2 = -l1*sin(ro); 158 | 159 | set(hpr,'XData',xr2); 160 | set(hpr,'YData',yr2); 161 | set(hpb,'XData',xr1); 162 | set(hpb,'YData',yr1); 163 | 164 | set(hl1,'XData',[xr1 xr2]); 165 | set(hl1,'YData',[yr1 yr2]); 166 | 167 | 168 | switch switch_pattern1 169 | 170 | case num2str([1 0 0 0 0 1]) 171 | 172 | set(h(5),'MarkerFaceColor',[1 0 0]) 173 | set(h(7),'MarkerFaceColor',[0 0 1]) 174 | 175 | case num2str([0 0 1 0 0 1]) 176 | 177 | set(h(6),'MarkerFaceColor',[1 0 0]) 178 | set(h(7),'MarkerFaceColor',[0 0 1]) 179 | 180 | case num2str([0 1 1 0 0 0]) 181 | 182 | set(h(5),'MarkerFaceColor',[0 0 1]) 183 | set(h(6),'MarkerFaceColor',[1 0 0]) 184 | 185 | case num2str([0 1 0 0 1 0]) 186 | 187 | set(h(5),'MarkerFaceColor',[0 0 1]) 188 | set(h(7),'MarkerFaceColor',[1 0 0]) 189 | 190 | case num2str([0 0 0 1 1 0]) 191 | 192 | set(h(6),'MarkerFaceColor',[0 0 1]) 193 | set(h(7),'MarkerFaceColor',[1 0 0]) 194 | 195 | case num2str([1 0 0 1 0 0]) 196 | 197 | set(h(5),'MarkerFaceColor',[1 0 0]) 198 | set(h(6),'MarkerFaceColor',[0 0 1]) 199 | 200 | end 201 | 202 | 203 | 204 | drawnow 205 | 206 | end 207 | 208 | catch 209 | 210 | disp("The animation was closed before the end of the simulated data") 211 | 212 | end 213 | -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/images/GS_part2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/2 Modeling a three phase inverter/images/GS_part2.gif -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/images/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/2 Modeling a three phase inverter/images/algorithm.png -------------------------------------------------------------------------------- /2 Modeling a three phase inverter/images/model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/2 Modeling a three phase inverter/images/model.png -------------------------------------------------------------------------------- /3 Modeling commutation logic/Modeling_commutation_logic.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/3 Modeling commutation logic/Modeling_commutation_logic.slx -------------------------------------------------------------------------------- /3 Modeling commutation logic/README.md: -------------------------------------------------------------------------------- 1 | # Modeling Commutation Logic 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | This repository contains MATLAB and Simulink files used in the [How to Design Motor Controllers Using Simscape Electrical, Part 3: Modeling Commutation Logic](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html) video. Check out the rest of the videos in [this section](#videos-and-files). 5 | 6 | ## Model and Setup 7 | 8 | The Simulink model provides the control algorithm that lets you control a BLDC motor at different speeds and simulate motor response. The following picture shows a snapshot of the model which consists of subsystems such as PI-controller, three-phase inverter, commutation logic and sensor. 9 | 10 | ![](images/model.png) 11 | 12 | The provided MATLAB file runs the Simulink model, and uses the simulated data to create an animation of the rotor and phase energizations of the BLDC motor as seen below. 13 | 14 |

15 | 16 |

17 | 18 | You can do the following with the provided files: 19 | 20 | - Run Simulink model (Modeling_commutation_logic.slx) and open up the Data Inspector to view the logged signals such the desired and measured speeds and supplied voltage to the three-phase inverter. 21 | 22 | - Run MATLAB file (animateRotorPosition.m) to create the BLDC motor animation. After running the MATLAB file, press any button to start the animation. The MATLAB file runs the Simulink model, and uses the simulated data to animate the BLDC motor. 23 | 24 | ## Videos and Files 25 | "How to Design Motor Controllers Using Simscape Electrical" video series consists of 5 videos. Click the links below to watch the videos and download the files. 26 | 27 | - Part 1: Simulating Back-EMF Voltage of a BLDC Motor [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor)] 28 | - Part 2: Modeling a Three-Phase Inverter [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter)] 29 | - Part 3: Modeling Commutation Logic [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic)] 30 | - Part 4: Model PWM-Controlled Buck Converters [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter)] 31 | - Part 5: An Alternative implementation of PWM Control [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor)] 32 | 33 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 34 | 35 | - How brushless DC motors differ from brushed DC motors and how they work 36 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 37 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor. 38 | 39 | 40 | ![](algorithm.png) 41 | -------------------------------------------------------------------------------- /3 Modeling commutation logic/animateRotorPosition.m: -------------------------------------------------------------------------------- 1 | % Copyright 2019, The MathWorks, Inc. 2 | %% animateRotorPosition 3 | % 4 | % This script will run a Simulink model of a BLDC that is energized in 5 | % one coil and animate the movement of the rotor. Once the animation figure 6 | % is rendered, there is a 5 second pause before the animation begins. 7 | 8 | close all 9 | clear 10 | 11 | % Below parameters are defined in the Simulink model 12 | % Sample time 13 | % Ts = 2e-5; 14 | % Number of pole pairs 15 | % p = 1; 16 | % Initial rotor angle in degrees 17 | % th0 = 0; 18 | % Sector 19 | % sector = 6; 20 | 21 | mdl = 'Modeling_commutation_logic.slx'; 22 | open_system(mdl); 23 | 24 | sim(mdl) 25 | try 26 | 27 | % 28 | r = 1.2; 29 | theta = linspace(0,2*pi); 30 | x = cos(theta); 31 | y = sin(theta); 32 | 33 | x1 = 0.8*cos(theta); 34 | y1 = 0.8*sin(theta); 35 | 36 | xa = cos(0); 37 | ya = sin(0); 38 | 39 | xb = cos(2*pi/3); 40 | yb = sin(2*pi/3); 41 | 42 | xc = cos(-2*pi/3); 43 | yc = sin(-2*pi/3); 44 | 45 | xat = r*cos(0); 46 | yat = r*sin(0); 47 | 48 | xbt = r*cos(2*pi/3); 49 | ybt = r*sin(2*pi/3); 50 | 51 | xct = r*cos(-2*pi/3); 52 | yct = r*sin(-2*pi/3); 53 | 54 | hf = figure(1);h = plot(x,y,'k-',[0 xa],[0 ya],'k-',[0 xb],[0 yb],'k-',[0 xc],[0 yc],'k-',xa,ya,'ko',xb,yb,'ko',xc,yc,'ko',x1,y1,'k-');grid 55 | 56 | set(hf,'Color',[1 1 1]) 57 | 58 | ha = gca; 59 | 60 | 61 | set(ha,'Visible','off') 62 | 63 | ht1 = text(xat,yat,'A'); 64 | ht2 = text(xbt,ybt,'B'); 65 | ht3 = text(xct,yct,'C'); 66 | 67 | set(ht1,'FontSize',14); 68 | set(ht2,'FontSize',14); 69 | set(ht3,'FontSize',14); 70 | 71 | 72 | axis([-1.2 1.2 -1.2 1.2]) 73 | axis equal 74 | 75 | set(h(5),'MarkerSize',20) 76 | set(h(6),'MarkerSize',20) 77 | set(h(7),'MarkerSize',20) 78 | 79 | switch_pattern = switchPattern.signals.values(1:40:end,:); 80 | 81 | init_switch_pattern = num2str(switch_pattern(1,:)); 82 | 83 | 84 | switch init_switch_pattern 85 | 86 | case num2str([1 0 0 0 0 1]) 87 | 88 | set(h(5),'MarkerFaceColor',[1 0 0]) 89 | set(h(7),'MarkerFaceColor',[0 0 1]) 90 | set(h(6),'MarkerFaceColor',[1 1 1]) 91 | 92 | case num2str([0 0 1 0 0 1]) 93 | 94 | set(h(6),'MarkerFaceColor',[1 0 0]) 95 | set(h(7),'MarkerFaceColor',[0 0 1]) 96 | set(h(5),'MarkerFaceColor',[1 1 1]) 97 | 98 | case num2str([0 1 1 0 0 0]) 99 | 100 | set(h(5),'MarkerFaceColor',[0 0 1]) 101 | set(h(6),'MarkerFaceColor',[1 0 0]) 102 | set(h(7),'MarkerFaceColor',[1 1 1]) 103 | 104 | case num2str([0 1 0 0 1 0]) 105 | 106 | set(h(5),'MarkerFaceColor',[0 0 1]) 107 | set(h(7),'MarkerFaceColor',[1 0 0]) 108 | set(h(6),'MarkerFaceColor',[1 1 1]) 109 | 110 | case num2str([0 0 0 1 1 0]) 111 | 112 | set(h(6),'MarkerFaceColor',[0 0 1]) 113 | set(h(7),'MarkerFaceColor',[1 0 0]) 114 | set(h(5),'MarkerFaceColor',[1 1 1]) 115 | 116 | case num2str([1 0 0 1 0 0]) 117 | 118 | set(h(5),'MarkerFaceColor',[1 0 0]) 119 | set(h(6),'MarkerFaceColor',[0 0 1]) 120 | set(h(7),'MarkerFaceColor',[1 1 1]) 121 | 122 | end 123 | 124 | 125 | l1 = 0.7; 126 | ro = thetaSim.signals.values(1)*pi/180-pi/2; 127 | 128 | xr1 = l1*cos(ro); 129 | yr1 = l1*sin(ro); 130 | xr2 = -l1*cos(ro); 131 | yr2 = -l1*sin(ro); 132 | 133 | hold on,hl1 = plot([xr1 xr2],[yr1 yr2],'m-'); 134 | 135 | 136 | set(hl1,'LineWidth',3) 137 | 138 | hpr = plot(xr2,yr2,'ko'); 139 | hpb = plot(xr1,yr1,'ko'); 140 | 141 | 142 | set(hpr,'MarkerSize',20) 143 | set(hpb,'MarkerSize',20) 144 | set(hpr,'MarkerFaceColor',[1 0 0]) 145 | set(hpb,'MarkerFaceColor',[0 0 1]) 146 | 147 | 148 | pause 149 | 150 | ro1 = thetaSim.signals.values(1:40:end)-90; 151 | 152 | for l = 1:numel(ro1) 153 | 154 | switch_pattern1 = num2str(switch_pattern(l,:)); 155 | ro = ro1(l)*pi/180; 156 | 157 | 158 | xr1 = l1*cos(ro); 159 | yr1 = l1*sin(ro); 160 | xr2 = -l1*cos(ro); 161 | yr2 = -l1*sin(ro); 162 | 163 | set(hpr,'XData',xr2); 164 | set(hpr,'YData',yr2); 165 | set(hpb,'XData',xr1); 166 | set(hpb,'YData',yr1); 167 | 168 | set(hl1,'XData',[xr1 xr2]); 169 | set(hl1,'YData',[yr1 yr2]); 170 | 171 | switch switch_pattern1 172 | 173 | case num2str([1 0 0 0 0 1]) 174 | 175 | set(h(5),'MarkerFaceColor',[1 0 0]) 176 | set(h(6),'MarkerFaceColor',[1 1 1]) 177 | set(h(7),'MarkerFaceColor',[0 0 1]) 178 | 179 | case num2str([0 0 1 0 0 1]) 180 | 181 | set(h(6),'MarkerFaceColor',[1 0 0]) 182 | set(h(7),'MarkerFaceColor',[0 0 1]) 183 | set(h(5),'MarkerFaceColor',[1 1 1]) 184 | 185 | case num2str([0 1 1 0 0 0]) 186 | 187 | set(h(5),'MarkerFaceColor',[0 0 1]) 188 | set(h(6),'MarkerFaceColor',[1 0 0]) 189 | set(h(7),'MarkerFaceColor',[1 1 1]) 190 | 191 | case num2str([0 1 0 0 1 0]) 192 | 193 | set(h(5),'MarkerFaceColor',[0 0 1]) 194 | set(h(7),'MarkerFaceColor',[1 0 0]) 195 | set(h(6),'MarkerFaceColor',[1 1 1]) 196 | 197 | case num2str([0 0 0 1 1 0]) 198 | 199 | set(h(6),'MarkerFaceColor',[0 0 1]) 200 | set(h(7),'MarkerFaceColor',[1 0 0]) 201 | set(h(5),'MarkerFaceColor',[1 1 1]) 202 | 203 | case num2str([1 0 0 1 0 0]) 204 | 205 | set(h(5),'MarkerFaceColor',[1 0 0]) 206 | set(h(6),'MarkerFaceColor',[0 0 1]) 207 | set(h(7),'MarkerFaceColor',[1 1 1]) 208 | 209 | 210 | end 211 | 212 | drawnow 213 | 214 | end 215 | catch 216 | 217 | disp("The animation was closed before the end of the simulated data") 218 | 219 | end 220 | 221 | 222 | -------------------------------------------------------------------------------- /3 Modeling commutation logic/images/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/3 Modeling commutation logic/images/algorithm.png -------------------------------------------------------------------------------- /3 Modeling commutation logic/images/animation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/3 Modeling commutation logic/images/animation.gif -------------------------------------------------------------------------------- /3 Modeling commutation logic/images/model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/3 Modeling commutation logic/images/model.png -------------------------------------------------------------------------------- /4 Modeling a PWM controlled buck converter/BLDC_control_buck_converter.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/4 Modeling a PWM controlled buck converter/BLDC_control_buck_converter.slx -------------------------------------------------------------------------------- /4 Modeling a PWM controlled buck converter/README.md: -------------------------------------------------------------------------------- 1 | # Modeling a PWM-Controlled Buck Converter 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | This repository contains MATLAB and Simulink files used in the [How to Design Motor Controllers Using Simscape Electrical, Part 4: Modeling a PWM-Controlled Buck Converter](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html) video. Check out the rest of the videos in [this section](#videos-and-files). 5 | 6 | ## Model and Setup 7 | The Simulink model shown in the picture below uses a PWM-controlled buck converter to step down a DC source voltage to the required voltage values for running a BLDC motor at different reference speeds. 8 | 9 | ![](images/model.png) 10 | 11 | You can run the Simulink model by pressing “Run” button on the simulation tab and then use the data inspector to view the logged signals such as desired and measured speeds, DC source voltage and modulated DC voltage. 12 | 13 | ## Videos and Files 14 | "How to Design Motor Controllers Using Simscape Electrical" video series consists of 5 videos. Click the links below to watch the videos and download the files. 15 | 16 | - Part 1: Simulating Back-EMF Voltage of a BLDC Motor [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor)] 17 | - Part 2: Modeling a Three-Phase Inverter [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter)] 18 | - Part 3: Modeling Commutation Logic [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic)] 19 | - Part 4: Model PWM-Controlled Buck Converters [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter)] 20 | - Part 5: An Alternative implementation of PWM Control [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor)] 21 | 22 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 23 | 24 | - How brushless DC motors differ from brushed DC motors and how they work 25 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 26 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor. 27 | 28 | ![](algorithm.png) 29 | 30 | 31 | -------------------------------------------------------------------------------- /4 Modeling a PWM controlled buck converter/bldcData.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/4 Modeling a PWM controlled buck converter/bldcData.mat -------------------------------------------------------------------------------- /4 Modeling a PWM controlled buck converter/images/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/4 Modeling a PWM controlled buck converter/images/algorithm.png -------------------------------------------------------------------------------- /4 Modeling a PWM controlled buck converter/images/model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/4 Modeling a PWM controlled buck converter/images/model.png -------------------------------------------------------------------------------- /5 PWM control of a BLDC motor/BLDC_PWM_control.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/5 PWM control of a BLDC motor/BLDC_PWM_control.slx -------------------------------------------------------------------------------- /5 PWM control of a BLDC motor/README.md: -------------------------------------------------------------------------------- 1 | # PWM Control of a BLDC Motor 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | This repository contains MATLAB and Simulink files used in the [How to Design Motor Controllers Using Simscape Electrical, Part 5: An Alternative Implementation of PWM Control](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html) video. Check out the rest of the videos in [this section](#videos-and-files). 5 | 6 | ## Model and Setup 7 | In the Simulink model which is shown in the following snapshot, the three-phase voltages are modulated directly using PWM that is implemented under the commutation logic subsystem. 8 | 9 | ![](images/model.png) 10 | 11 | You can run the Simulink model by pressing the “Run” button on the simulation tab and then use the data inspector to view the logged signals such as desired and measured speeds, DC source voltage and three-phase voltages supplied to the BLDC motor. 12 | 13 | ## Videos and Files 14 | "How to Design Motor Controllers Using Simscape Electrical" video series consists of 5 videos. Click the links below to watch the videos and download the files. 15 | 16 | - Part 1: Simulating Back-EMF Voltage of a BLDC Motor [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-1-simulating-back-emf-voltage-of-a-bldc-motor-1565241566392.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor)] 17 | - Part 2: Modeling a Three-Phase Inverter [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-2-modeling-a-three-phase-inverter-1567758371716.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter)] 18 | - Part 3: Modeling Commutation Logic [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-3-modeling-commutation-logic-1576044161917.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic)] 19 | - Part 4: Model PWM-Controlled Buck Converters [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-4-modeling-a-pwm-controlled-buck-converter-1578478768258.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter)] 20 | - Part 5: An Alternative implementation of PWM Control [[Watch video](https://www.mathworks.com/videos/how-to-design-motor-controllers-using-simscape-electrical-part-5-an-alternative-implementation-of-pwm-control-1579758063226.html), [Download files](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor)] 21 | 22 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 23 | 24 | - How brushless DC motors differ from brushed DC motors and how they work 25 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 26 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor. 27 | 28 | ![](algorithm.png) 29 | -------------------------------------------------------------------------------- /5 PWM control of a BLDC motor/bldcData.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/5 PWM control of a BLDC motor/bldcData.mat -------------------------------------------------------------------------------- /5 PWM control of a BLDC motor/images/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/5 PWM control of a BLDC motor/images/algorithm.png -------------------------------------------------------------------------------- /5 PWM control of a BLDC motor/images/model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/307832a100418f6e9241f2a5cc0f01b9c171ba41/5 PWM control of a BLDC motor/images/model.png -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020, The MathWorks, Inc. 2 | All rights reserved. 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 5 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 6 | 3. In all cases, the software is, and all modifications and derivatives of the software shall be, licensed to you solely for use in conjunction with MathWorks products and service offerings. 7 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Motor Controllers with Simscape Electrical 2 | ### Copyright 2019 The MathWorks, Inc. 3 | 4 | [![View Design-motor-controllers-with-Simscape-Electrical on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/74826-design-motor-controllers-with-simscape-electrical) 5 | 6 | This repository consists of folders containing the MATLAB and Simulink files that are used in the [How to Design Motor Controllers Using Simscape Electrical](https://www.mathworks.com/videos/series/how-to-design-motor-controllers-using-simscape-electrical.html) video series: 7 | 8 | - [Simulate back-EMF voltage of a BLDC motor](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/1%20Simulating%20back%20emf%20voltage%20of%20a%20BLDC%20motor) 9 | - [Modeling a three-phase inverter](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/2%20Modeling%20a%20three%20phase%20inverter) 10 | - [Modeling commutation logic](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/3%20Modeling%20commutation%20logic) 11 | - [Modeling a PWM-controlled buck converter](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/4%20Modeling%20a%20PWM%20controlled%20buck%20converter) 12 | - [PWM Control of a BLDC motor](https://github.com/mathworks/Design-motor-controllers-with-Simscape-Electrical/tree/master/5%20PWM%20control%20of%20a%20BLDC%20motor) 13 | 14 | Check out [this tech talk video series](https://www.mathworks.com/videos/series/brushless-dc-motors.html) to understand: 15 | 16 | - How brushless DC motors differ from brushed DC motors and how they work 17 | - How BLDC motors can be controlled using six-step commutation (trapezoidal control) 18 | - The different components of a BLDC motor control algorithm such as PWM control, commutation logic, three-phase inverter and sensor 19 | 20 | ![](algorithm.png) 21 | 22 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Reporting Security Vulnerabilities 2 | 3 | If you believe you have discovered a security vulnerability, please report it to 4 | [security@mathworks.com](mailto:security@mathworks.com). Please see 5 | [MathWorks Vulnerability Disclosure Policy for Security Researchers](https://www.mathworks.com/company/aboutus/policies_statements/vulnerability-disclosure-policy.html) 6 | for additional information. --------------------------------------------------------------------------------