10 Practical Considerations for Implementing FOC with Simulink | Motor Control, Part 6
From the series: Motor Control
Melda Ulusoy, MathWorks
This video walks you through the various tasks required for designing and implementing field-oriented control (FOC), ranging from algorithm design to modeling and control tuning to deployment.
FOC requires the knowledge of a motor’s rotor position, which can be either measured using sensors such as quadrature encoder or Hall sensors or estimated using observer-based methods. Using motor parameter estimation techniques, you can estimate parameters of your motor and create motor and inverter models at the needed level of fidelity. You can design controllers against your motor and inverter model and tune them using rule-based or FOC autotuning techniques. You can perform closed-loop simulations to test the performance of your motor control algorithm under different operating conditions. You can generate ANSI C code from your FOC algorithm and deploy to target hardware to spin your motor. Use the reference examples and videos to get started designing and implementing motor control algorithms with Motor Control Blockset™:
Sensor-Based FOC
Field-Oriented Control of PMSM Using Quadrature Encoder
Field-Oriented Control of PMSM Using Hall Sensor
Sensor Offset Calibration
Hall Offset Calibration for PMSM Motor
Quadrature Encoder Offset Calibration for PMSM Motor
Run 3-Phase AC Motors in Open-Loop Control and Calibrate ADC Offset
Sensorless FOC
Sensorless Field-Oriented Control of PMSM
Motor Parameter Estimation
Field-Oriented Control of PMSMs with Simulink, Part 1: Motor Parameter Estimation
Motor and Inverter Modeling
Field-Oriented Control of PMSMs with Simulink, Part 2: Desktop Simulation
FOC Autotuning
Field-Oriented Control of PMSMs with Simulink, Part 4: Controller Autotuning
Spinning Brushless Motors with Simulink
Code Generation and Deployment
Field-Oriented Control of PMSMs with Simulink, Part 3: Deployment
Code Verification and Profiling Using Processor-in-the-Loop Testing
Published: 1 Apr 2021
Previously, we discussed some important concepts behind field-oriented control such as Clarke and Park transforms and space vector modulation. Understanding these fundamental concepts is important when designing an FOC algorithm. However, that’s not going to be sufficient because motor control is a complex process and there’s so much more that you need to consider when designing and implementing FOC algorithms. Motor control engineers perform various tasks ranging from plant modeling to control design and algorithm development to production implementation on a microprocessor or FPGA. In this video, we’re not going to be able to cover everything but focus on a few of these topics. We’ll give you 10 practical considerations for implementing FOC. Throughout this video, I’ll be sharing with you some resources on these topics, including videos and reference examples which use this motor kit from Texas Instruments. Note that reference examples can be used for both simulation and code generation. You can test and verify your control algorithm in desktop simulations before you generate code from your algorithm and deploy it to hardware. Please find the links to shared resources in the video descriptions.
Let’s first talk about the FOC architecture which we introduced in the previous video. We have two feedback loops to control the Iq and Id currents at their desired values. This part of the algorithm represents the current or torque control and forms the innermost loop of the complete motor control algorithm. Other common control modes are speed and position control. Speed control is a cascaded loop around the current control loops. In speed control, we want the motor to follow a speed reference with the use of this controller. The output of the controller generates the input to the innermost current loops, the reference values for Iq and Id currents. Position control is just another cascaded loop around the speed control loop. In position control, the controller tracks a position reference and generates the input for the speed control loop.
To achieve efficient motor control, the control algorithm needs to run at high sampling rates. In a cascaded control structure like this, the inner control loops need to respond much faster than the outer control loops. This example here uses FOC to control the speed of a PMSM motor. The different colors indicate the sample times of the different loops of the algorithm. We see that the innermost current loop runs at 20kHz. As a rule of thumb, the outer loop—in this case the speed loop—needs to run at least 10 times slower than the inner loop. So, the speed loop runs at 2kHz. If we had a position control loop, that would then run at 200Hz that is 10 times slower than the speed control loop.
One of the important things to note here is that FOC algorithm requires the knowledge of the rotor position. These values can be either measured using sensors or they can be estimated using observer-based techniques. Let’s focus on the sensor-based methods first. In some cases, you use a motor with a sensor that lets you measure the rotor position. Commonly used sensors in PMSMs include quadrature encoders and Hall sensors. To understand how these sensors are used to measure the rotor position in FOC, you may want to check out these reference examples. This one lets you implement FOC using a quadrature encoder. Under the current control subsystem, the sensor driver block lets you read the quadrature encoder pulses, which are then converted to angular rotor position and speed by these blocks, respectively. You can generate code from this model and deploy to hardware to get your motor spinning. Here, we change the speed reference to different values and see how the motor tracks the commanded speed.
Next, we’ll talk about sensor calibration. For the sensors you’re using in FOC, you will need to perform sensor calibration in order to remove the sensor offset and accurately calculate the current and position feedback values. Here are a few examples that walk you through the sensor calibration process. Each example comes with a target and a host model. Once we generate code from the target model and download to the processor, we use the host model which is running on our computer to communicate with the target.
Let’s discuss current sensor calibration briefly. Phase currents are measured using analog to digital converters, or ADCs. In order to find the ADC offsets, we need to read the ADC counts when there are no phase currents in the motor. That’s why the motor is disconnected from the inverter. Once we follow the steps provided in this example, the target model will continuously read the ADC counts and send them to the host model. The mean value of the ADC readings will give us the ADC offsets.
We follow a similar workflow for calibrating offsets of Hall sensors and quadrature encoders. In these examples, the target model is a unique algorithm that uses open-loop control to run the motor at a constant speed. The model lets us read the rotor position measured by the position sensor and compares the measured value to the open-loop rotor position, which is the reference generated in the open-loop control algorithm. The difference between the open-loop rotor position and the measured rotor position gives us the position sensor offset.
We talked about the sensor-based methods to measure the rotor position. Next, we’ll discuss sensorless field-oriented control. In some cases, you may want to perform motor control without a sensor, which might be due to costs associated with the use of a sensor. The alternative method to sensor-based control is to use a sensorless approach to estimate rotor position feedback. This example lets you use observer-based techniques such as a sliding mode observer or a flux observer for implementing sensorless field-oriented control. You can choose either of these observers as a position estimator. The rotor position estimation is carried out under the current control subsystem. When we navigate as seen here, we can view the observer blocks used by this FOC algorithm. Both blocks take the same inputs which are the measured voltages and currents. Recall that these voltages and currents were computed by these transforms in the current control section of the FOC algorithm. Similarly, if we now navigate to current control in this example, we see that the inputs to the observer blocks are the voltages computed by the Inverse Park Transform and the currents computed by the Clarke Transform. The observers then output the estimated rotor position of the motor and other estimated values such as the rotor flux and torque.
After discussing the sensor-based and sensorless methods, we’ll now talk about motor and inverter modeling which is essential in developing a FOC algorithm. Control design is an iterative process and requires a lot of testing with the system in closed-loop. Oftentimes, the motor we want to control is driving or a part of a much larger and complex system which might be expensive to operate. But more importantly, without a stabilizing controller we may damage the whole system, which may also lead to safety problems. Because of all these reasons, control engineers typically develop a representative model of the actual physical system, in this case the motor and inverter, and then design and test the controllers against that model using simulation before deploying the controller on the real system.
To create a plant model of our motor, we need to know the motor parameters that are listed here (such as Phase resistance (Rs), d and q axis inductances (Ld and Lq), Back-EMF constant (Ke), Motor inertia (J), Friction constant (F)). Typically, we would find this information in the motor datasheet. But in some cases, the datasheet is not available. Or even though the motor parameters are available, sometimes simulations performed with the model created based on the given parameters doesn’t match the real-world motor behavior.
The solution to this is parameter estimation. This example here lets you estimate the parameters of a PMSM that runs with this particular TI microcontroller and inverter listed here. The model runs a set of instrumented tests on the motor to collect input and output data and then, based on the measured data, estimates the motor parameters such as the stator resistance, d and q axis inductances, back-emf constant, motor inertia, and friction constant. Once the parameters are estimated, you can either save them in a file or create a new model that will include a PMSM block parameterized with the estimated values of the motor parameters. To learn how to run this model and perform motor parameter estimation, check out the video link given in the description.
You can now attach this motor model to an average value inverter for accurate closed-loop simulations of motor dynamics. Based on your needs, you can also create inverter and motor models at different levels of fidelity. Here’s another example that uses this converter which lets us choose the switching device for the inverter. For example, we can use ideal switching or switches like IGBTs or MOSFETs. When we simulate with these low- and high-fidelity models, this is what we will get for the speeds, phase current, voltages, and space vector modulation waveforms. The signals in yellow reflect the effect of switching in the inverter with the high-fidelity model, whereas the responses in blue show the simulation results with the average value inverter. The examples we discussed here use equation-based motor models or lumped parameter models, but you can create simulations to model saturation and spatial harmonics.
Once you complete motor and inverter modeling, in the next step you need to tune the gains for the current and speed control loops. One option is to perform rule-based tuning by using the capabilities available to compute the controller gains of the current and speed loops once you estimate motor parameters. Once the gains are computed, system design can be analyzed with bode, root-locus, and step response plots.
Alternatively, you can leverage autotuning techniques to automatically tune the controller gains of your FOC algorithm. This example runs with the Speedgoat motor control kit, which includes a real-time target machine with the inverter and a PMSM. It uses the FOC Autotuner block to compute the gains of the Iq, Id, and speed control loops that are implemented under this subsystem. As we’ll see shortly, the autotuning is performed in real-time while the deployed model is running on the target driving the actual motor. Once you build the model and load the application to Speedgoat target, the model first uses the initial gain values to control the motor at different speeds. The FOC autotuning starts here and fine-tunes the gains after running a series of experiments that inject perturbation signals to the motor. The resulting motor response with the tuned gains shows us how the tracking has improved compared to the response with the initial gains. For more information on FOC autotuning, feel free to check out these videos.
After testing the control algorithm using closed-loop simulations, the last step is to generate code from your FOC algorithm and deploy it to the embedded microcontroller for spinning your motor. The examples we showed so far used the TI processor, but you can target any processor with ANSI C code. For code generation, you can partition your model into algorithmic and hardware-specific parts. This means you can generate code separately from your FOC algorithm and then sensor and PWM driver peripherals. The driver blocks implement the current and position sensors as well as PWM writes, whereas the FOC algorithm implements the speed and current loops. This is the example we’ve seen before which implements FOC using quadrature encoder for this TI motor kit. Using this example, you can run closed-loop simulations and when you’re ready to move on to production, you can generate code and deploy it to the TI processor. To view the required steps of this process, don’t forget to check out this video. Once the code has been deployed, you can use the host model to change the reference speed to the motor and also view how the reference speed and measured speed are changing. One thing to consider here is that before you deploy and run your algorithm to spin the actual motor, you can also perform processor-in-the-loop, or PIL, testing to test the performance of your controller on the embedded microcontroller. In PIL testing, the controller is deployed and executes on the processor, whereas the motor and inverter model run on the host computer. Let’s use this model to perform PIL simulation with the current control algorithm. You first deploy the current control subsystem to the processor. Once you build code for this subsystem, it creates a new one called PIL. You can now replace the current control subsystem with the PIL block. After deployment, the current control algorithm executes on the processor, but the controller will not be driving the actual motor. Instead, we’ll be performing closed-loop simulations with the motor and inverter model. You can also keep the current control subsystem to compare the algorithm execution on the host machine and in the PIL simulation. You can generate a profiling report that includes the execution times of your control algorithm. The model we used to generate this report has the current loop sample time set to 50 microseconds. And the profiling report shows that the current loop runs in 5 microseconds on the embedded microcontroller, which is efficient for the 50 microsecond sample time that we had specified for this model.
Today, we discussed these 10 practical considerations for implementing field-oriented control. We also shared some examples and videos that can help you get started with designing and implementing your own FOC algorithm. These examples and most of the capabilities we discussed today are available in Motor Contol Blockset. So, you may want to check out this page for more information on Motor Control Blockset. To find the links to all the resources we covered in this video, please don’t forget to check out the video descriptions.