Accelerating Motor Control Development with Embedded Coder Support for STM32 - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 30:23
Loaded: 0.00%
Stream Type LIVE
Remaining Time 30:23
 
1x
  • descriptions off, selected
  • en (Main), selected
    Video length is 30:23

    Accelerating Motor Control Development with Embedded Coder Support for STM32

    Overview

    In this webinar, MathWorks will demonstrate the capabilities enabled by STM32 support from Embedded Coder. Attendees will see how to use a model for simulation and tuning of an FOC PMSM motor control algorithm, and then convert it to C code running on the STM32 and controlling a motor. ST will discuss the advantages of the new STM32G4 for motor control and power conversion. 

    Highlights

    • Understand how to generate C code and automatically deploy it to the STM32
    • Use ST CubeMX for deep configuration of hardware peripherals and use those peripherals from Simulink
    • Apply processor specific optimizations like CMSIS to generated code
    • Estimate parameters of a motor controlled by STM32 using parameter estimation tools
    • Simulate and tune FOC controller with motor model before deployment
    • Hardware used for demo: STM32 NUCLEO G474RE with NUCLEO IHM07M1

    About the Presenters

    John Kluza

    Partner Manager

    John Kluza manages strategic partnerships with embedded hardware companies for MathWorks. He previously was a consulting engineer and application engineer at MathWorks. He has a background in product management of electrical products, including inverters, batteries and protection devices. John earned a B.S. and M.S. in mechanical engineering from Lehigh and Penn State University, and an M.S. in management and engineering from the Sloan School at MIT.

    Venkatesh Chilapur

    Embedded Targets Developer

    Venkatesh Chilapur has been building customer solutions that combine MathWorks software with embedded hardware for over 6 years. He previously developed embedded software at Delphi Automotive and Robert Bosch. Venkatesh holds a B.E. in Electronics and Communication Engineering from Visvesvaraya Technical University and Masters in Embedded Systems from Manipal University.

    Jean-Marc Mathieu

    STM32 Digital Power and Motor Control Marketing Manager

    Jean-Marc Mathieu is the Digital Power and Motor Control Marketing Manager in the Microcontroller Division within ST’s General Purpose Microcontroller Sub-Group (CMG). Jean-Marc held this position since 2010.

    Jean-Marc started his career in STMicroelectronics in 1995 working in the Microcontroller division as product engineer in Italy, then in France. In 2000 he moved to TV division as Application engineer where he became Application manager. Jean-Marc joined Microcontroller division again in 2008 as Product Line Manager within Marketing team where he launched the first STM32 Ultra Low Power product series (STM32 L1) in 2009. Then he took the head of the Digital Power and Motor Control verticals as Marketing manager and he launched the STM32 Mixed-Signals microcontroller product series - STM32F3 in 2014 and G4 series in 2019 - addressing mainly analog rich, motor control and digital power applications.

    Jean-Marc graduated in Electronic Engineering from the Polytech Montpellier, France.

    Recorded: 27 Sep 2022

    Good day, everyone, and thanks to all of you for attending. And thanks to ST for joining us on this webinar. We appreciate the opportunity to speak with you about using the STM32 with model based design and MathWorks tools. I'm John Kluza and I'm the Partner Manager for MathWorks relationship with ST.

    We'll start the session off today with a quick overview of MathWorks, and the value of model based design, and how STM32 is integrated with the MathWorks tool chain. Then, John Marc from ST will tell us a bit about which STM32s are best for motor control and the overall STM32 ecosystem. Finally, Venkatesh from MathWorks will walk us through a demonstration of how to use Simulink to develop a field oriented control algorithm for motor control and automatically deploy it to an STM32 for live tuning and refinement.

    First, I wanted to fill you in a little on the MathWorks, especially if the last time you used our tools was in an engineering class in college. MathWorks has grown a great deal since it was founded in 1984, and our tools are now commonly used in complex, safety critical, and other difficult applications. We've expanded worldwide to have offices in 16 countries and have over $1.3 billion in revenue and over 100 products.

    Our applications have also expanded over the years. Beyond our stronghold with most of the major automotive companies and aerospace and defense, we've also become a major player in many industrial and consumer segments. A few relevant projects and appliances, power conversion, and vehicles are shown here that are seeing the benefits of 50% time savings in development or testing using model based design and MathWorks tools. We have these and many other insightful user stories available online that share the common thread of large savings and time and cost, and benefits in quality and performance.

    So what is model based design? And how does STM32 fit into it? In model based design, first the user does research and algorithm development in MATLAB or a text based programming language. Those algorithms are built into a Simulink graphical model. That Simulink model is the one source of truth for linking requirements, simulating on the host machine, executing test cases, and generating C code. And rapidly iterating as more as learned to refine and optimize the design.

    Embedded Coder does the conversion to ANSI or optimized C code. And the STM32 target enables processor specific optimizations, peripheral drivers, deployment, and live tuning. The STM32 target moves the processor into the model based design workflow for one seamless experience that accelerates development all the way from ideas to code, running on the real hardware system.

    There are three STM32 specific solutions that offer specialized support for STM32. We offer two hardware support packages for STM32, also known as STM32 targets. The first one listed here, Embedded Coder Support Package for STM32, is more flexible and offers two workflows-- a new one released last year that supports many STM32 devices using configurations created in CubeMX and legacy one preconfigured for discovery boards only. In this webinar, we'll focus on the newer CubeMX based workflow.

    The second package, Simulink Coder Support Package for Nucleo, supports specific Nucleo boards only and focuses on ease of use. This uses Embed to offer a good selection of drivers, but it's limited in configurability. That's great for rapid prototyping.

    Additionally, we offer Motor Control Blockset, which allows users to rapidly develop Field Oriented Control, or FOC motor control algorithms, and we offer several example models using STM32.

    I'll zoom in a little on the Embedded Coder Support Package for STM32's CubeMX workflow. We'll address three main capabilities here. And first, you'll see two on the diagram. On the left side of the diagram, you'll see where we can start with a Simulink model. And then you can use the algorithm export workflow to generate C and H files from it, and then manually pull them into an existing project in your ID, and deploy them onto an STM32. This offers the most flexibility and optimization. So it's good for advanced users in production situations.

    The next capability, rapid prototyping, allows you to automatically build, deploy, and execute with one click, which is very useful for research and development, educational, and even low volume production.

    For rapid prototyping, we offer external mode, which lets you tune parameters and log data in real time. And finally, not shown in the chart, is Processor in the Loop, or PIL, which is available to automatically run generated C code algorithms on the STM32 hardware while interfacing it to the Simulink software plant model on the host machine. Now, this is useful for evaluating execution time and code behavior on production hardware and is commonly used for higher safety levels in IEC 61508.

    We also offer a variety of peripheral driver blocks, with more planned as this new workflow matures. These are used in rapid prototyping. This supports many key STM32 devices, including the motor control oriented ones. And if you have Embedded Decoder already, this is actually a free add on from our add on explorer.

    The other STM32 specific product I'll focus on is Motor Control Blockset, which will also be included in the demo. Users can apply sophisticated field oriented control algorithms to brushless and induction motors. It's part of the model based design workflow, so you can tune them in simulation and generate efficient code for hardware implementation. Blocks include Park and Clarke transforms, sensorless observers, and an FOC auditor.

    You can verify control algorithms in closed loop simulation using the motor and inverter models included in the blockset. The block set parameter estimation tool runs on your motor hardware for accurate estimation of parameters like static resistance, inductance, You can incorporate these motor parameters into a closed loop simulation to analyze your controller design.

    Motor Control Blockset integrates with related MathWorks products for advanced applications. You can use Simscape Electrical to customize and improve motor and inverter models in your system, use Fixed Point Designer to optimize data types, and use Simulink Control Design to help tune control loops.

    So that's the MathWorks overview. And now let's shift gears to John Marc from ST to discuss the STM32.

    Thanks John for the introduction. Let's talk now about the STM32 motor control ecosystem. My name is Jean Marc Mathieu. I work for STMicroelectronics in the microcontroller division. I'm the marketing manager in charge of motor control and digital core segments within the general purpose microcontroller subgroup.

    So now let's do a quick overview of the STM32 portfolio. I will not go into the detail, but just keep in mind that we have different families-- the wireless, the ultra-low, the mainstream, the high-performance, and the MCU. And each of them are made of different product series. The STM32WL, the L4, the G4, the F4, et cetera, et cetera.

    And as you can see in the big and red highlighted boxes on this line, most of these product series embed one or several advanced model control timers. This advanced motor control timer is designed to address various monocultural application, thanks to the different building features you have on the specific timer. You can refer to the STM32 online training to get more technical details.

    As presented on the previous slide, we have a large STM32 portfolio. But if we have to focus on the motor control flagship, I would mention the G0 to address cost effective applications. The STM32G4, the mixed signal MCU which combines excel with the rich and advanced analog peripheral set. And the STM32H7, our high performance MCU.

    Any large silicon portfolio will not be relevant without a complete ecosystem. So on this slide, you have an overview of the rich STM32 ecosystem. First, on the STM32 embedded software solution from HAL, included in the different STM32 Cube MCU packages. Up to system solution for AI, cloud, graphic, motor control, and many others. We offer many other development tools and boards for prototyping up to full evaluation and development.

    Regarding software development tools, we offer free tools to configure, debug, program, and monitor your solution, thanks to the STM32Cube software toolset that you can see on the left hand side of the slide.

    So now let's focus on the STM32 model control ecosystem. First, I want to talk about ST-MC-Suite that you can see on the top of the slide. This online tool will help you to select the most appropriate materials-- are libraries, software tools, documentation-- according to your needs.

    However, the backbone of the motor control ecosystem is the motor control software development kit called X-CUBE-MCSDK, in which you have a full featured firmware library available in full source code. And its graphical user interface, the Motor Control Workbench, which will help you to configure this motor control library.

    The STM32 Motor Control Workbench interfaces with the STM32 Cube MX to generate the code of your project compatible with the STM32 Cube so long.

    As you know, ST and its microcontroller division have invested in motor control for years. For instance, our first MCSDK the solution that we discussed on the last slide, was released in 2008 with the STM32. MathWorks is our partner for years. And today we are glad that MathWorks offers expanded support for the STM32.

    This new support of the STM32 in MATLAB and Simulink integrates MathWorks' industry standard engineering workflow with ST's popular STM32. The support package allows users to increase engineering productivity by using MATLAB and model based design for motor control development and then easily deploy it to STM32 for further refinement testing and even low volume production.

    So thanks for your attention. And now I'm passing the ball to Venkatesh for the next portion of this presentation. Venkatesh, the floor is yours.

    Thank you, Jean Marc for that overview on STM32 devices and tools.

    Let me start with a question. As part of rapid prototyping, are you looking for a quick and simplified way of developing an advanced motor control algorithm on a STM32 device? If your answer is yes, then using MATLAB Simulink can help you achieve that.

    My name is Venkatesh Chilapur. I'm working as a Senior Embedded Developer at MathWorks based in Bangalore, India. And in this part of the webinar, we will cover our modeling and code generation workflow for the field oriented control algorithm development in MATLAB Simulink. And deploy it on an STM32G474 microcontroller. We will be using the MathWorks Embedded Coder hardware support package and Motor Control Blockset to achieve the same.

    Now let us look at the hardware involved and the shipping examples on motor control that we will use today. To demonstrate the working of a sensorless field oriented control of a three phase BLDC motor. We need a driver expansion board X-NUCLEO-IHM07M1 from SD Microelectronics mounted on an STM32 NUCLEO-G474 board. And we will be controlling a 24 volt, 4,000 RPM BLDC motor.

    There are more examples, mainly consisting of an open loop model that enables the users to quickly verify the hardware setup by spinning the motor at a constant reference speed. We then have the sensorless field oriented control that combines both the simulation and core generation capabilities in the same model.

    So we have an open loop and closed loop model files. And we will begin with the open loop first. There are reasons to do it, which I'll explain now. To launch the open loop model, type the model name-- mcb_open_loop_control_nucleo_g646re-- at the MATLAB command prompt. And press Enter.

    Once the model is open, we notice that we are spinning the motor at a constant reference speed. This helps to quickly validate the integrity of the hardware connections. This is all the more important if the user is starting with a new setup of motor and inverter.

    On a high level, the open loop model is configured to generate an ADC end of conversion interrupt at a rate equal to the PWM switching frequency that triggers the open loop control subsystem. For the desired switching frequency, among other parameters here. Inside the open loop control subsystem we read the motor currents using the ADC driver block. This also helps to check for ADC offset correction.

    The motor phase voltages and position information are generated inside the control system using the blocks' promoter control block set-- namely the position generator and the three phase sine rotation generator. The generator phase voltages after scaling are input to the PWM driver block. That is able to generate the PWM outputs to the inverter to generate the phase voltages and turn the motor at the desired speed.

    With that overview of the model, one can run the model in simulation and monitor, for example, the three phase voltage is generated from the sine voltage generator block. To run the model in simulation, go to the Simulation tab and click on the Run button.

    When the model is running in simulation, click on the data inspector button. This enables us to view the logged signals. Expand on the signals and enable the phase voltages. The open log simulation actually starts with a press of the Push button. This Push button mimics the hardware equivalent user button that controls the motor start stop operation.

    One can notice after the Push button is enabled, the MCB block is able to generate the three phase voltages. The model can also be deployed to hardware by going to the Hardware tab and clicking on the Monitor and Tune button. This essentially is running the model in an external mode.

    We can also click on this button-- Build, Deploy, and Start-- to run the model in standalone execution. Running the model on hardware ensures the on chip peripheral configurations like ADC and PWM synchronization, that is critical for motor control, is working as expected.

    So as we can see, the open loop model helps to break down the complexity of the task by ensuring the basic hardware and software environment is operational. We can then proceed to the design and implementation involving closed loop model. The shipping example on closed loop can be launched by typing the model name-- mcb_pmsm_foc_sensorless_nucleo_g474re-- from the MATLAB command prompt.

    The shipping examples on motor control are configured with variant so that we can use the model to both simulate and generate code. This essentially lets us use the same model for simulating our field oriented control algorithm against the inverter and blank model, and for generating the code to program the microcontroller.

    Let's begin with the blank model implementation involving the inverter and motor, which enables the simulation of the model. We are using the average value inverter and surface mount PMSM blocks from the motor control blockset. That helps us achieve the low fidelity simulation. However, users looking for high fidelity simulation can leverage the capabilities of Simscape electrical, which enables one to model inverters with IGBTRS enabling analysis of nonlinear effects of harmonics and saturation.

    We can also quickly take a look at the different subsystems that implement the field oriented motor control algorithm. On the outer loop, we have a speed control and a current control that is part of the inner loop. These subsystems correspond to the two controller loops-- the speed controller and the current control loop of the field oriented control.

    The speed control is pretty straightforward. Note we are using a PI controller here to regulate the tracking error between the reference speed and the actual motor speed. The current control is a little different. We want to control the motor space current to maximize torque generation.

    But here, the phase currents are easy signals and not easy to be controlled with PI controllers. So we transform the phase currents into DQ frame and get the ID and IQ current, which are DC signals. Clarke and Park transform blocks from Motor Control Blockset help us achieve this transformation.

    Inside the current controllers, we are using two PI controllers. We know that current IQ contributes to the torque generation, but the current ID does not. So to get the maximum torque, we need two PI controllers-- one to zero the ID, the other one to maximize the IQ.

    The controller outputs will be the voltages in DQ frame. So we need to reverse it back using the inverse pack transform. And use a space vector PWM generator to generate duty cycles to control the inverter. We are using the Clarke transform, Park transform, inverse Park transform, space vector generator, and the sine cosine lookup table block from the motor control block set through closed loop control.

    The assocated algorithm needs current position and speed of the motor. In the current example, sliding mode observer technique is used for sensorless position estimation by using the sliding motor observer block from Motor Control Blockset. Given this overview of the model, as a next step to prepare the model to run in simulation is to update the motor and inverter parameters.

    It is important to note that the simulation phase is helpful when we start the development even before the hardware is available. So one can import motor parameters from the datasheet and update the values in the model script.

    Various system and hardware parameters can be updated in the script. These parameters are then accessed within the model blocks and configuration settings. As an example, the surface mount PMSM block is using the parameters as defined in the script.

    We are now ready for the simulation of the model to verify the performance of the controller against the blank model. Simulate the model using the Simulation tab and pressing the Run button.

    When the model is simulating, one can open the Data Inspector and visualize the signal of interest in the model. As an example, I have enabled the reference speed and feedback speed in the Data Inspector, including the load profile. We start with the open loop simulation with a press of the Push button. That controls the motor start stop operation.

    We run open loop until we get the motor position and speed details. We then switch to the closed loop simulation. We can see the reference speed and the feedback speed are matching. Any change in the load profile would mean the feedback speed varies. But the controller is able to recover it to the reference speed by generating the design torque to the motor.

    You can log and visualize other signals of interest in a model such as motor speed, motor torque, ID, IQ currents, phase voltages, space vector modulation signals, et cetera. You can verify the controller performance by modifying the control parameters, by editing the motor and inverter parameters and running multiple simulations until you reach an acceptable level of performance.

    Note that while in our shipping examples, the script is set up to determine loop gains of the current and speed controller via empirical calculations, we can also leverage an FOC auto-tuner that computes the loop gains based on the bandwidth and phase margin. We can also use other tools from Simulink control design to tune the control gains.

    Now that we have verified our control algorithm in desktop simulation, deployment of the model to the target consists of additional components like the STM32 CubeMX configuration tool and peripheral device driver blocks from the Embedded Coder hardware support package. The STM32 CubeMX tool enables the configuration of microcontroller clock and peripherals, while the peripheral device driver blocks or ADC and PWM from the Embedded Coder Support Package provide access to the key microcontroller peripheral resources required in motor control.

    The generated code from the assembly model and the CubeMX is combined to create the final executable and deploy it to the hardware board. Notice a different color scheme is used for FOC designed with MCB blocks. This is highlighting the fact that code generated for a Field Oriented Controller that is developed using the MCB blocks is standard ANSI C code, which means you can target any processor. This code is separate from the hardware dependent components in a blue box. So a given model can be set up to run on any other supported STM32 device just by replacing the configuration device driver blocks specific to that device.

    We can now look into the hardware dependent part of the model. Taking a closer look at the timing legend with colors reveals that the inner current loop, which is shown in green here, runs at 20 kilohertz. The interaction between the PWM and the ADC module controls the rate of triggering of the inner current loop. This ADC PWM synchronization setting is taken care of in the CubeMX configuration.

    This can be quickly verified by launching the CubeMX tool from the Hardware Configuration Settings. Click on the Launch button under the Build Options. Once the STM32 CubeMX configuration tool is launched, one can change the settings for the clock configuration as well as the peripheral configuration.

    The timer run is configured with three output channels to drive the three phase inverter. The timer operates in center aligned mode with a counter period value set such that a PWM switching frequency of 20 kilohertz is optimal.

    Inside the ADC, when the PWM counter reaches a count value equal to the PWM counter period, it triggers the start of conversion event. It is the end of conversion-- 20 kilohertz in transcript the current control loop. The outer speed control, shown in blue, runs 10 times slower at 2 kilohertz.

    We can also quickly take a look at the driver blocks that are used to enable the model to run on the hardware. The driver blocks can be located the code generation path of the model. The hardware interrupt block implements the ADC end of conversion interrupter, which runs the current loop.

    The motor currents are red, using the ADC block, as shown here. The PWM outputs are generated from the timer one module, using the PWM output block. We are also using the GPIO block to sense the user button to enable the motor operation. One can find these blocks under the Simulation tab and by clicking on the Library button.

    Navigate to the Embedded Coder support package for STM32 end loop and click on the device of interest. They're constantly adding, updating newer blocks and features to the support package with every release of the MATLAB.

    Having covered the specifics of the model related to core generation that enables it to run on the hardware, we have the following options to verify our control algorithm by executing it on the STM32 device. One can run the model as a standalone executable on the hardware device by clicking on the Build, Deploy, and Start button. This generates code from the Simulink model, deploys it on STM32 processor hardware, and starts the execution.

    Alternatively one can also use the external mode simulation feature to run the generated code from the model on the hardware, and monitor the signals and tune parameters from the same model running on the desktop. You can find more details from the MathWorks documentation on external mode simulation.

    One can also leverage the build simulation capabilities provided by the Embedded Coder. You have the control code running on the target and code simulated with your motor and inverter model running in the Simulink. Finally, we have the algorithm export workflow, which involves generating code for the control algorithm by using Simulink and Embedded Coder. And then integrating it with either the manually written or externally generated hardware driver code.

    For the hardware demo that we have today, we will use the external motor option. And we have the results.

    With the model now running in external mode on the target hardware, one can control the start and stop operation of the motor by pressing the user button on the STM32G474 board.

    We are now looking at the external mode simulation results that are obtained when the model was running on the hardware. I have added the reference speed and feedback speed signals to monitor once the user button is pressed on the board. As captured by the enable signal, we run the motor in the open loop until we have the motor position and speed data available. We then get into the closed loop, and we can see the reference speed and the feedback speed are matching.

    The results of the hardware run match with the simulation results of the model, making Simulink an important debugging tool as we can ensure correctness of the algorithm before deploying it to the hardware. User can also vary a potentiometer to vary the reference speed and observe that the feedback speed catches the reference speed almost seamlessly.

    We can generate a code execution profiling report if we have it enabled in our code generation verification by enabling this checkbox. With profiling enabled, we can see the profiling report is generated at the end of the external mode simulation.

    Note that the current control is running at a high sample rate of 50 microseconds. In this case, we can see our current control is taking about 21 microseconds, which is pretty efficient. Some of this efficiency comes from the optimized code generation by the Embedded Coder support package, with capabilities like code replacement libraries.

    As an example of the code replacement, we can see from the code generation report the Clarke transform function from the optimized STMS library is used. This is handled automatically during the code generation by the Embedded Coder.

    With that information, I will now hand over the control to John to take it forward. Thank you.

    Thanks, Venkatesh.

    Just to summarize, today we've discussed how you can increase your engineering productivity by developing motor controllers with MATLAB and Simulink using model based design. MathWorks Embedded Coder support for STM32 allows you to quickly deploy those designs to STM32 devices and further refine them. MathWorks users for many leading companies have found this workflow valuable for achieving their ever increasing performance and cost goals on tight timelines.

    If you'd like to give it a try, go to our website at mathworks.com/st to learn more and to download the free add on for STM32 CubeMX. To make it as easy as possible for you, we've also included video guides for installation and your first program. And more in-depth examples are available in the documentation.

    Thanks for your attention.

    View more related videos