# Modeling Commutation Logic | How to Design Motor Controllers Using Simscape Electrical, Part 3

From the series: How to Design Motor Controllers Using Simscape Electrical

*Melda Ulusoy, MathWorks*

In this video, you’ll learn how to model commutation logic and also build a feedback control loop to control the speed of a BLDC motor using Simscape Electrical™.

Download the model used in this video.

Check out this video to learn more on tuning PID gains for BLDC motor control and this Tech Talk video to understand how the different components of the BLDC speed control algorithm, such as commutation logic, sensors, and a three-phase inverter, work.

**Published:
10 Dec 2019**

In the previous video, we modeled a three-phase inverter that converts the DC power to three-phase currents to control a BLDC motor. The input to the three-phase inverter is a switching pattern that controls the on and off states of the phase pairs of the motor. In the previous video, we used a static switching pattern to energize phases A and C, and observed that the rotor aligned with the stator magnetic field at 30 degrees. In this video, we’re going add commutation logic to this model to dynamically change the switching pattern for a continuous rotation of the rotor.

As we discussed in our second Tech Talk video, we need a Hall effect sensor to determine which sector the rotor is in. Commutation logic then uses the current sector to select the corresponding switching pattern. Let’s start with modeling the Hall effect sensor. In practice, Hall effect sensors sense the magnetic field around each phase to determine the current sector. For simulation purposes, however, we’ll assume that we know the angular rotor position from which we’ll compute the sectors. The logic for the Hall effect sensor model should be the following: If the rotor is between 0 and 60 degrees, then it means the rotor is in the first sector, so we should output 1. Similarly, there are five more cases until we complete the full rotation of the rotor.

Let’s keep this table here and try to model the same logic in Simulink. The angular position theta is always between 0 and 360 degrees, which means that after every full rotation of the rotor we should reset theta to 0 degrees. We can do this by using the remainder function available in the Math Function block. We’ll input theta and the constant value of 360 to this block, which will then return the remainder from the division of theta by 360 degrees. We’ll also insert a Gain block here and enter the number of pole pairs p which is 1 in our case and already defined in the MATLAB workspace. This way we’re representing the rotor position in electrical degrees. Now we’re ready to work on this logic.

For each case, there are two conditions we need to check for. To implement the first check, we add a Constant block, which we set to 0. We then grab a Relational Operator block and choose the right operator we want to use for comparing theta with 0. Similarly, we model the second condition. When both of these conditions are met, we want to set the sector to 1. We can do this by using an AND gate along with a gain representing the sector number. Note that the logical operator outputs a Boolean value, which we need to convert to the same data type as the gain. We can do so with the Data Type Conversion block, which takes the Boolean value and converts it to the the data type that it inherits from the Gain block.

According to this logic, when both conditions are met, the AND operator will return 1 and the sector will be set to 1. If either or both of the conditions are not met, the output will be 0, as this will mean that the rotor is in another sector.

To implement the rest of the conditions, we can simply copy and paste this part and then adjust the values as in the picture. Now the sum of resulting values will give us the sector number. Note that at each time only one of these outputs will be positive and the rest will be 0. Let’s select this part and create a subsystem that we’ll call sensor.

Now that we’re done with computing the sector, we can use it to model the commutation logic. As we discussed in the second Tech Talk video, the commutation logic is basically like a table containing all of the possible switching patterns and outputs them in the right sequence for properly rotating the rotor based on the sector information.

Here we have the first switching pattern. Let’s disconnect this and add the remaining switching patterns that we see in the picture. In order to choose a pattern based on the sector, we’re going to use a switch. We use the Multiport Switch block for this. We need six inputs, which we connect to the switching patterns we just created. The first input is controlling this switch by telling it what pattern to choose, so here we need to connect the sector. Let’s select all of this and create a subsystem which we can call “Commutation Logic”. With that we closed a loop around the motor, which lets us energize the correct phases for continuous rotation based on the sector that we determine by using Hall effect sensors.

We’ll now log the switching pattern as well as “Theta”. We’ll use the same script from the previous video which uses the logged signals to animate the simulation results. We first run the simulation and then call our script by typing its name in the command line. We see that everything’s working as expected. This means we’re calculating the sector correctly, and based on that the commutation logic selects the right switching pattern. Now we’re controlling our motor, but only at a constant speed, as seen in here. Because the supply voltage is constant. To be able to run the motor at varying speeds, we need a feedback controller that will adjust the supply voltage to the three-phase inverter. To build this control loop, we first need to compute the error between a desired and measured speed and then feed it to a controller to adjust the voltage level. We measure the speed under the Sensor block. Let’s first output the measured speed with an Outport block. We will compare it to a desired speed which we can model using a repeating sequence changing gradually from 0 to 900 RPMs. We insert a Sum block to compute the error between desired and measured speeds which we then input into a PID controller. For speed control, we choose to use a discrete PI-controller. As I tuned the gains before and already know what values work well for my system, I’ll just enter them here. Next, we add a unit delay to prevent any algebraic loop that may occur in this model. Now, we need to feed the voltage computed by the controller into the three-phase inverter. So, we remove the current voltage source and replace it with an ideal voltage source which provides the commanded voltage regardless of the current passing through it.

Next, we will log different signals to be able to view them after we simulate the system. Now, we can run the model and take a look at the desired and measured speeds as well as the voltage computed by the controller. We see the voltage to the three-phase inverter is being regulated by the controller, and measured speed successfully tracks the desired speed. Note how voltage and motor speed change proportionally.

To summarize, in this video we showed you how to model commutation logic and also control the motor speed with a feedback controller. In this model, we used an ideal voltage source to adjust the voltage level. But in reality, the supply voltage is fixed, and we need to adjust it using a technique called PWM or pulse width modulation. Next time, we’ll discuss about PWM and different architectures for implementing PWM control.

#### Download Code and Files

#### Related Products

#### Learn More

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)