Position Control of a PMSM with Simulink and Microchip 32-bit MCUs
Overview
Field-oriented control (FOC) is a common technique for precise position control of permanent magnetic synchronous motors (PMSM). This webinar shows how to use Simulink to implement FOC algorithms for positioning a PMSM and deploy them on Microchip’s ATSAME70, 32-bit microcontroller (MCU). Starting with an FOC algorithm from Motor Control Blockset, we will use Embedded Coder to generate optimized C-code for a Microchip microcontroller. The code generation workflow will feature the use of the MPLAB Device Blocks for Simulink, which provides an integrated development environment and toolchain for configuring and generating all the necessary software to execute complex applications on Microchip MCUs.
Highlights
- Simulating an FOC based position control algorithm in Simulink and Motor Control Blockset
- Generating optimized, production-ready C-code with Embedded Coder
- Deploying code to Microchip PIC32, and SAM controllers using MPLAB Device Blocks for Simulink
About the Presenters
Swathy Pillai: MathWorks
Swathy Pillai is a Product Manager at MathWorks focusing on Motor Control. She holds a master’s degree in Electrical Engineering with specialization in Power Electronics & Drives. Before MathWorks, she worked with L&T Electrical & Automation, Schneider Electric India in R&D division where she was involved in control algorithm development and coding for Low and Medium Voltage industrial drives and other power electronic converters.
Brett Novak: Microchip
Brett is the Motor Control Marketing Manager for Microchip’s MCU32 Division, including responsibility for PIC32 and acquired SAM ARM based devices. He has over 20 years of experience working in the semiconductor industry focused on control theory-based applications including motor and power control.
Purushothamreddy Madduru: Microchip
Purushothamreddy is the principal applications engineer for Microchip’s MCU32 division, specializing in Machine Drives & Power Electronics, and has 9 years of industry experience in Embedded software development for Motor Control applications. He is responsible for MCU32 MPLAB Simulink Device Blocks development and motor control algorithms development for PMSM, BLDC and induction motor drives suitable for industrial and traction control applications. He has a vast knowledge of model-based design including simulation, hardware-in-the-loop, and code generation.
Recorded: 13 Jul 2023
Hello, everyone. And welcome to this webinar. I am Swathi , a product manager at MathWorks, supporting motor control development. And prior to joining MathWorks, I worked as a power electronics and controls engineer focused on building control algorithms and code development for industrial drives and various other power converters.
Today I will be joined by Greg Novak and from Microchip. And together, we will talk about developing a position control algorithm for using Simulink and deploying it on Microchips 32-bit MCU. So with that, let's have a quick overview.
So here's our agenda for today. I will start with a brief overview of position control for implementing magnet synchronous motor and how motor control block set helps you to jump-start your motor control development.
Next, Brett from Microchip will give a quick introduction to Microchip and an overview of their controller supporting motor control applications, following which will walk us through a demo which will cover how to use device blocks with the help of a pre-built reference example in Simulink, which will be deployed onto a SAM E 70 32-bit microcontroller unit from Microchip which is connected to a permanent magnet synchronous motor .
So why are we talking about PMSM and specifically position control? Now, we know that PMSMs are popular because of their high talk density, efficiency, and small size. And hence, they are used in many applications from electric vehicles, industrial drives, to medical devices, and so on.
Now, PMSMs are gaining popularity in servo systems, but precise position control is needed. In the case of applications like CNC machines, solar panel positioning, cranes, and even robotics as well.
So we know that these oriented control algorithm is a popular control mechanism when it comes to PMSMs. And this is because of the good dynamic performance and good overall control capability which it provides. And hence, engineers are developing field-oriented control, base position control, for applications requiring precise position control of motors.
So why Simulink for motor control? Because with Simulink and design, you can accelerate your motor control development process. With Simulink, you can quickly put together a control system and start testing and verifying it in closed-loop desktop simulation, thus validating your specifications in simulation months before your actual hardware is available.
You can then proceed to automatically generate code from this Simulink model, thus quickly moving from design to implementation. To minimize development time further, we provide pre-built reference examples to get you quickly started with your next motor control project.
So yes, definitely, faster development times is a big reason why our customers choose Simulink for motor control projects. So while we're on the topic of accelerating motor control development, let me introduce you to motor control block set, which is an add-on to Simulink.
So now, with motor control, we are aware that we have advanced algorithms involved with high computational efforts. And so, we need to work from modeling and simulating the algorithms for the motor to deploy it on our target hardware to finally get our motor spinning.
And this is exactly where motor control block set comes in. By helping you develop and deploy the control algorithms to target microcontrollers like your Microchip bit-32, or SAM devices, or even FPGAs.
So let's have a look at some of the key features. So motor control block set provides you with pre-built blocks which are optimized for both C and HTL code generation. And these blocks can be used in workflows involving and ISO 26262 functional safety standards.
Motor control block set library includes blocks, the map transform, and controlled-related blocks, to censor decoders, as well as observers to help you develop sensor-based and sensorless motor control algorithms.
And in addition, there are features like parameter estimation and control auto-tuning capability included to ease your motor control development. On top of this, the block set provides multiple fully assembled reference examples, like the one which we are covering today, which can be quickly deployed to your target hardware, which in this case is your Microchip 16-bit or 32-bit microcontrollers.
And when we are talking about motor control, we know that an important step here is tuning a controller to meet our gain, phase margin, or bandwidth requirements. So to help with this, motor control block set provides you with different methods as shown. So we provide basic methods such as empirical calculation where your PI values are calculated from your motor parameter information using basic formulas.
In addition, we have other methods to tune control gains like auto-tuning with the field-oriented control auto-tuner block, which helps you to further refine and tune your controller directly on your hardware. Similarly, you can also view your frequency response and accordingly tune your system using the PIU tuner app.
And lastly, these are available as reference examples so that they can be easily tried out. And while we are talking about reference examples, let's have a look at the reference example that motor control block set provides to accelerate your motor control development process.
So motor control block set provides reference examples covering a wide range of motor control algorithms so you can work on and explore conventional algorithms like sensorless field-oriented control and direct control to nonlinear control strategies for field weakening and advanced controls like model predictive control with these reference examples.
In addition to motor control algorithms for permanent magnet synchronous motor, motor control block set also includes reference examples for different motor types like induction motors, switch motors, as well as brushless DC motors.
For example, you can try out this reference example to implement six-step computation control for brushless DC motors. For more details, please check out the motor control block set product page.
And today, with this webinar, we will be focusing on a reference example covering design and deployment part of a field-oriented control-based position control for a PMSM, which would be deployed on Microchip SAM E 70 32-bit microcontroller.
So here is a quick preview of where we will end up at. As discussed, we will be using the reference example for motor control block set. And once we are happy with the simulation, we proceed to generate code, which is optimized for the SAM E 70 32-bit microcontroller unit with the help of embedded coder.
And as we see, the microcontroller unit sits on top of the low-voltage motor control development board, which is connected to a permanent magnet synchronous motor. So the reference position would be provided with the help of the rotary potentiometer on the development board. Let's have a quick look at this video.
So as the blue plot, which is the motor position, tracks the reference position accurately. And the reference position is provided with the help of the rotary potentiometer. Next up is Brett, who will walk us through Microchip and their family of controllers and development boards supporting motor control applications.
Hello, folks. As Swathi mentioned, my name is Brett Novak, and represent the marketing side of Microchip's 32-bit microcontroller division for industrial and motor control type applications. I have a few quick slides to cover. But in the interest in time, I won't read every bullet as we do make a copy of the slides available for reference
Microchip has had a long-standing business focus on motor control applications spanning a huge variety of both levels of performance and motor types. Home appliances, of course, make up a huge percentage of the overall market. But smart control for more consumer type gadgets is quickly catching up. Industry has always been primarily focused on efficiency in performance. And these demands are quickly being incorporated into automotive and consumer-based applications.
As I pointed out on the previous slide, Microchip has a very wide performance range of MCUs targeting motor control directly in both 16-bit DS PIC devices and our 32-bit families, including both ARM and MIPS core devices. Included in these families are devices with true 5 volt support as well, highlighted in the middle.
In reality, we have a solution for just about every application out there. And that includes some automotive qualified devices for high-temp under-the-hood applications as well.
Focusing on our 32-bit families, you can see that we have a wide breadth of choices for both peripheral mix, processor performance, memory sizes, different interfaces, and even major differentiators such as touch and security features. Investments and functional safety software and documentation will continue to expand while future devices will also further develop the security functions.
Filtering down a layer, you can see the devices we generally target for motor control. And by that I mean direct control-- loop control for a power stage. Although the slide goes top to bottom for performance with the lower performance on the very top and the highest performance on the very bottom, you can see how broad the portfolio is, scaling from 25 megahertz with the PIC 32 MM to a 300 megahertz with our SAM 7x devices.
Of course, just having silicon doesn't mean you have a solution. Supporting both 16 and 32-bit products, Microchip provides a gamut of additional support, including application notes and tuning guides, modeling tools, which we're here to talk about today, and, of course, physical hardware for folks to start getting-- start spinning motors with. If we don't have prototype boards, it's kind of difficult to get started.
This is just a quick snapshot of our current motor control development boards. Our primary platforms consist of a base board with both high and low voltage versions available and individual plug-in modules for each main parent MCU per family. This modularity reduces overall cost by enabling developers to have one base board but be able to swap in and out different MCUs to test and build different performance configurations.
For today's application setup, we'll be focusing on the SAM E 70 Cortex M7 based MCU. This is one of our highest performing devices in the motor control family and offers 300 megahertz performance with a broad peripheral mix and large memory configurations.
As Swathi has already introduced, of course, the main focus today is how to use the MATLAB models and Simulink block sets. These include pure models such as flux observers and sliding mode observers, combined with device-level block sets. Tied together in Simulink, this allows us to develop, test in real-time, and generate production-ready code.
From the previous slide, the device block sets for use within Simulink are provided by Microchip developers. These are essentially the glue that works between the device peripherals and the programming model within Simulink. These are provided for free within the MathWorks File Exchange.
We're constantly adding support for new devices in both 16 and 32-bit domains. And, actually, we do support our 8-bit devices as well. And on that note, I'm going to hand it over to Parush, and on with the show.
Thank you, Brett. Welcome, everyone. I am Parush . I am part of Microchip MC Database applications. In this part of presentation, I'll briefly discuss the amazing position control and its implementation using Microchip to use on Simulink.
Next, I would simply add the position control example model using motor inverter plant. Once the simulation completes, I will use the same model to generate code and program the same SEM database. Finally, I will show the hardware demo.
Let us see the system level of PMSM position control. This block diagram illustrates the system level of sensor-based VMs and position control. position control system is a closed-loop control system, whose output is the desired angular position of your motor.
Here, motor is connected in a closed loop system. Here, controller takes the motion feedback from motor's quadrature encoder and compares it with user reference position to carry out the position control.
Here, reference input can be taken from either serial communication or potentiometer reference input. Field-oriented control is one of the most efficient control techniques to achieve high-performance in motor control applications. In this demo example, your position control is implemented to achieve VMs and position control.
Coming to microcontroller, Microchip provides a broad range of 32-bit microcontrollers for motor control applications, which includes SAM E7, SAM E5, SAM C2, D2, PIC 32 EMK, and PIC 32 CM series devices.
We have used SAM E 70 device for this demo application. SAM E 70 is a 300 megahertz amp codecs M7 core device. Let us discuss important peripherals of SAM E 70, which are used for motor control applications.
AWM controller, SAM E 70 has two four-channel 6-bit PBMW suite with complimentary outputs. It also has a generator with independent channels. It supports both left align and central align AWM modes with two event lines to trigger conversion synchronous to AWM cycles.
Like you've already seen, SAM E 70 is called an undocumented controller. In short, Epic has 16-bit resolution. It can be used in single , differential input voltage mode. It supports simultaneous sampling with dual sample and hold. It has a feature of automatic correction of offset and gain errors. DMA also supported.
Next, timer counter. SAM E 70 timer counter model has quadrature decoder connected in front of timers which is used to decode the quadrature signals to calculate positional speed. It provides revolution command and direction change detection. Missing path detection and auto-correction are also available.
Now, let us discuss how the algorithm is implemented to achieve PMSM position control. Following is the operation flow for sensor-based PMSM position control. The phase currents in two phases are measured and that phase current is calculated using each of current .
The currents in the three-phase stationary reference frame are transformed to two-phase stationary reference frame, I-alpha and I-beta using cloud transformation. Position decoder is used to calculate the rotor position from quadrature input signals. Using the rotor position, I-alpha, I-beta are transformed to rotating reference frame ID IQ currents.
Rotor speed is calculated from the measured position information. Position control loop, here rotor position is compared with the user reference position, and the corresponding error is controlled through propositional controller to obtain speed reference.
Speed control-- motor speed is compared with the reference speed and the resultant error is fed to PA controller to generate data component IQ reference. Current controller, here ID reference is zero till base speed. Here, ID and IQ currents are compared with ID IQ reference.
Corresponding errors are controlled through PA controller to obtain VD VQ components. VD and VQ inverse part transform to obtain V alpha and V beta. This are used to generate PWM signals using space vector PWM. The PWM inputs are used by a three-phase inverter to generate the voltage to be applied to the motor.
Now, we will see the software and hardware tools that are required to run this demo. Software required. These are the MathWorks toolboxes that are mentioned here. MATLAB, Simulink, motor control block set, control system toolbox, and code generation toolboxes.
Coming to Microchip products, ID, it is an integrated development environment for Microchip devices. Exit that to the compiler and device block set Simulink. Hardware required, we just pick them, MCLV2 development board. It is a low-voltage motor control development board. SAM E 70 motor control plugin module. 3 PMSM is a motor with encoder.
Coming to the debugger. ICD 4 debugger are picked for debugger to program the device. Adapter board is required when use base inc. 24 volts power supply is required to power the MCL with the board. You would need everyday cable to transmit the data between target and host model.
Now, let us discuss how the implementation of PMSM position control is done using MATLAB Simulink. First, let us install the MP lab blocks. Open MATLAB, go to the add-ons, type in lab here, click on the MP lab device blocks for Simulink. Click on the Add. MP lab device blocks will be installed.
When it prompts, select here to open the startup guide. Click on the compiler link to download and install the compiler. If you are working with 32-bit MCUs, install XT 32 compiler. If you are working with 16-bit MCUs, install 16 compiler.
In this webinar, we'll be using SAM E 70 microcontroller as an example. Download XP 32-bit compiler. Click here to download and install mp level ICD.
Once the installations are done, go to the location where mp level block sets are installed. Here you can see various examples of Microchip devices that are supported by mp level device blocks. You can use these example models as reference models for your application development.
Next, let us go to the demo folder location. These are the files that we will be using for our example project in the webinar. Software and hardware requirements document. This will show all the software and hardware that are required to run this demo, like part numbers, reference links, and instructions to set up the hardware.
There is a position control doc and selects file, which is a position control example model that is implemented using Simulink blocks and mp level blocks. There is a host model dot XLS file, which is used to communicate data from host to target. Data dot file. It is a parameter file. You will see various parameters which we will use in the example.
Open position control Simulink file from the demo folder. This model can be used for both simulation and code generation. Run the serial communication block. As you can see here, using this variant of system block, course can be taken either from simulation block or code generation block.
Go to the base model. For code generation, let us see the configurations of SAM E 70. Run configurations. You can master. Make sure that SAM E 70 device is chosen as a target device. If you are working on any other device, you can choose the respective device from the dropdown list.
Close the block. Open the compiler block. Make sure that always use latest compiler is selected. The configurations are as shown. You can choose configurations as per application requirements. Your configurations can be found in your pluck. Means the data used for this example project are shown here.
Well, if I can do the configuration, go to base model, open current control. Go to the sensor driver blocks, open ADC block. These are the areas configurations. Simultaneous sampling option is selected for phase current measurement.
Open cubic block. Record the quadrature signal model, timer contact zero is used. For PWM configuration, go to current control. Open the PWM block. The configuration study as shown. PWM is selected. Time period of 50 microseconds is selected. Central island PW mode is selected. The pins that have been used are shown here. You can choose the configurations as per your requirements.
Seral and receive blocks are used to transmit the data between target and host model. If you want to use any other blocks other than mention here, go to Simulink Library browser. Click on mp live device blocks for Simulink. As you can see, various peripheral blocks for different aims used listed out here. Go through all of these blocks and use the blocks as per your requirements.
We have discussed the hardware configuration. Now let us have a look at the position control algorithm. Position speed control takes position reference, speed, and motion feedback as inputs, provides ID IQ reference currency as outputs.
Open the position and speed control block. You can see, position control and speed control implementation over here. Current controller takes ID IQ reference, actual currents, and quadrature signals as inputs, provides the debug data, duty cycles, position and speed measured as the outputs.
Open the current controller block. Go to closed loop. You can see you post the implementation over here. If we want to know the implementation details of Simulink blocks. Right-click here. Go to help. You can find the implementation documentation.
Go to current control. For code generation, these duty cycles are fed to PWM block. For simulation, this duty cycles are fed to in order plan model. For input scaling, click here. You can see how the speed and position are measured from encoder counts. Inverter and motor plan model subsystem, which is used for simulation.
Now, let us simulate the model. Click on the Run button here. This will simulate the model. Once the simulation completes, let us click on the data inspector. This will show simulation results. Select position reference and position feedback. You can see, position feedback tracks reference properly.
This concludes the simulation. Let us see the code generation and hardware results now. Follow the instructions specified in the hardware and software requirement document. Make the appropriate hardware connections. Open Simulink model. Go to Microchip. Make sure that flash after build is selected so that code is built as well as programmed onto the device. Select programmer as mp level ID 4.
Go to apps. Click on embed encoder. Go to C code and click on Build. It will generate code and program the device. Once the code generation is done, it will generate data code generation report. Click on some part of code, which will help to navigate between generator code and MATLAB model. Click here to navigate to the model.
To trace from model to code, right-click on Simulink block. Click on navigate to see C++ code. Let us go to the host model and start communicating. Open the serial communication blocks and update the serial port. Click on Run to start communication between host model and target.
Click the Start button to start the position control. In this demo example project, rotor is aligned to start of initial position. Here, position reference is taken from onboard potentiometer of board.
Let me rotate the potentiometer now. You can observe the motion tracking on motor as well as Simulink scope. You can observe that rotor follows position reference. I will force the rotor to rotate. But it is locked on holding the reference position.
Enlarge the scope to see position tracking for the entire operation. You can see how smoothly rotor position is tracking with the reference.
This concludes the demo. Now I'll pass to Swathi for a wrap-up.
Thank you, Now for a quick wrap-up. So we saw how to design and implement a field-oriented control-based position control for the PMSM on a SAM E 70 32-bit controller from Microchip using Simulink and motor control block set.
So we went through Microchips and built device blocks for Simulink, which is the free package available in the MathWorks add-on explorer. This provides Simulink library blocks to help program peripherals for your Microchip controller.
So we started with the pre-built reference example, with using motor control block set and MP lab device blocks. This model was then used to automatically generate optimized code, which was then deployed onto the SAM E 70 32-bit controller from Microchip.
We proceeded to verify the control algorithm, wherein we could see that the mode of position was tracking the reference position accurately. And all this was done within the Simulink environment. An important point to note is that the same workflow, which we saw for the 32-bit microcontroller unit, could also be used with the 16-bit TSP controllers from Microchip.
So in conclusion, you can select the Microchip controller that fits your application and use this workflow to quickly program the device from Simulink. Thank you.