0% found this document useful (0 votes)
7 views

Pwm Motor Control

This document provides an application note for a 3-Phase Sensorless PMSM Motor Control Kit using the S32K344 processor and Model-Based Design Toolbox (MBDT). It outlines the required software and hardware, demo preparation, and detailed instructions for running the motor control demo, including state machine operations and current measurements. The document serves as a comprehensive guide for developers to implement motor control applications using NXP's tools and libraries.

Uploaded by

paulephraim937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Pwm Motor Control

This document provides an application note for a 3-Phase Sensorless PMSM Motor Control Kit using the S32K344 processor and Model-Based Design Toolbox (MBDT). It outlines the required software and hardware, demo preparation, and detailed instructions for running the motor control demo, including state machine operations and current measurements. The document serves as a comprehensive guide for developers to implement motor control applications using NXP's tools and libraries.

Uploaded by

paulephraim937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

NXP Semiconductors Document Number: AN13902

Application Notes Rev. 0, 03/2023

3-Phase Sensorless PMSM Motor Control


Kit with S32K344 using MBDT Blocks
by: NXP Semiconductors
Contents
1. Introduction 1.
2.
Introduction........................................................................ 1
Motor Control PMSM example ......................................... 2
2.1. Required software ................................................... 2
The Model-Based Design Toolbox (MBDT) enables the 2.2. Required hardware .................................................. 2
Model-Based Design workflow targeting NXP 2.3. Demo Preparation ................................................... 3
processors via the MATLAB® and Simulink® 2.4. 2.4 Running the Demo ............................................ 5
3. Model overview ................................................................. 5
environments. 3.1. Initialize function .................................................... 5
3.2. ADC interrupts and measurements ......................... 7
The toolbox was created to help in the development of 3.3. Board buttons .......................................................... 8
increasingly complex applications. It is a collection of 4. State machine ..................................................................... 9
tools and libraries designed to assist customers with 4.1. Compute event ...................................................... 11
4.2. Init state ................................................................ 13
prototyping and accelerating algorithm development on 4.3. Fault state .............................................................. 15
MCUs and processors. 4.4. Align state ............................................................. 16
4.5. Ready state ............................................................ 19
MBDT integrates NXP tools (Real-Time Drivers, 4.6. Calibration state .................................................... 20
Configuration Tools, Automotive Math and Motor 4.7. Run state ............................................................... 22
Control Library (AMMCLib), Compilers and 4.8. Motor control algorithm overview ........................ 24
5. Current loop (Fast loop) ................................................... 25
Toolchain), offering a complete development solution, 6. Speed loop (Slow loop) .................................................... 32
with the following benefits: 7. Open loop......................................................................... 34
8. RGBLED0 toggle............................................................. 38
• Code/algorithm reusability 9. Updating PWMs............................................................... 39
10. FreeMASTER config GUI ............................................... 40
• Easy to understand/follow diagrams 11. Conclusions...................................................................... 42
12. References ........................................................................ 42
• Automatic code generation
S32K3 is an Arm® Cortex®-M7® based
Microcontrollers family in single, dual, and lockstep
core configurations, supporting up to ASIL (Automotive
Safety Integrity Level) D functional safety automotive
and industrial applications, running up to 240 MHz,
with FPU (Floating Point Unit). S32K3 is suitable for
applications like Motor Control, Battery Management
Motor Control PMSM example

Systems (BMS) or Body Control Module (BCM).

2. Motor Control PMSM example


The MBDT plug-in for S32K3 includes a motor control example (s32k3xx_mc_pmsm.mdl) which
supports the following features:
• 3-phase PMSM speed Field Oriented Control
• Current sensing with two shunt resistors
• Shaft position and speed estimated by sensorless algorithm or encoder position sensor
• Application control user interface using FreeMASTER debugging tool
To install the MBDT plug-in for S32K3, please check the Model-Based Design Toolbox for
S32K3xx - Quick Start Guide under NXP MBDT page.
The s32k3xx_mc_pmsm.mdl example illustrates how the MCSPTE1AK344 BLDC and PMSM Motor
Control Development Kit are used to run the Permanent Magnet Synchronous Motor (PMSM) vector
control using Field Oriented Control (FOC) with two shunt current sensors. This Simulink® model is
configured to work with a Sunrise 42BLY3A78-24110 motor, but users can load the
"s32k3xx_mc_pmsm_data_linix.m" script to use the Linix 45ZWN24-40 motor.
After the application is deployed on target, the RGBLED0_GREEN should stay on. Select the desired
motor speed, utilizing the provided project, and press the "On/Off" button in FreeMASTER.
Alternatively, you can start the motor in CW mode by pressing the USER_SW0 button (SW6 on board),
or in CCW mode by pressing the USER_SW1 button (SW5 on board). Pressing both buttons at the
same time or the reset button stops the motor.
If there is no error, the motor should start to spin and the RGBLED0_GREEN will start to blink. If
there is any error, the motor will not run, and the RGBLED0 will switch from GREEN to solid RED.

2.1. Required software


• S32 Design Studio for S32 Platform 3.4
• EB tresos Studio 27.1
• FreeMASTER 3.2
• MATLAB R2021a with “Embedded Coder for ARM Cortex-M Processors” and
“NXP_Support_Package_S32K3xx”
• Model-Based Design Toolbox for S32K3xx version 1.3.0

2.2. Required hardware


• Personal Computer or Laptop
• MCSPTE1AK344 BLDC and PMSM Motor Control Development Kit contains:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
2 NXP Semiconductors
Motor Control PMSM example

o DEVKIT-MOTORGD board
o S32K3X4EVB-Q172 board
o Sunrise 42BLY3A78-24110 Motor
o 12V Power Supply
o Micro-USB cable

Figure 1. Hardware requirement

2.3. Demo preparation


The MCSPTE1AK344 has some modifications compared with the original S32K3X4EVB-
Q172 board. To see what has changed, check the S32K3X4EVB-Q172 Board Modifications for
MCSPTE1AK344 Motor Control Kit Schematic Addendum on the NXP website.
Check on DEVKIT-MOTORGD if the board jumpers J9, J10, and J11 are in positions 2-3 as shown in
the following figure, to enable phase current measurements by shunt resistors:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 3
Motor Control PMSM example

Figure 2. Dev kit

1. Connect a micro-USB cable between the host PC and the OpenSDA USB port (J40) and switch
the SW1 to position two on the S32K3X4EVB-Q172 board.
2. Connect the 12 V Power supply to the DEVKIT-MOTORGD board and switch the SW1 from
the position two to position one on the S32K3X4EVB-Q172 board.
3. The four LEDs next to SW1 will power on and be solid orange, if they are blinking, move the
switch from position one to two and back to position one to start the SBC without the active
watchdog.
4. Open the s32k3xx_mc_pmsm.mdl in MATLAB®. Users can choose between the Sunrise
42BLY3A78-24110 motor (MCSPTE1AK344 kit) or the Linix 45ZWN24-40 MCSPTE1AK144
development kit. To change from the default Sunrise motor, click on the Linix Motor Parameters
in the Motor Selection Block.
5. Build, Download the program to the target board and the RGBLED0_GREEN lights up.
6. Open the "S32K_PMSM_Sensorless.pmp" FreeMASTER project from the
"FreeMASTER_control" folder and click on the "Tools" menu, then "Connection Wizard," then
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
4 NXP Semiconductors
Model overview

click Next twice, and select the COM port assigned to the S32K3X4EVB-Q172 board
and 115200 bauds.

2.4. Running the demo


1. In the "S32K_PMSM_Sensorless" project, click on the "App Control" tab, select the desired
speed using the gauge speed, and click on the "On/Off" button.
a. Alternatively, to start the motor in CW mode, press the USER_SW0 button, and to start
in CCW mode, press the USER_SW1 button.
b. Pressing both buttons at the same time or the reset button stops the motor.
2. In case of any fault, the motor will stop, the error is signaled in the FreeMASTER project, and the
RGBLED0_RED will light on. To run the motor again, clear the fault by clicking on the "Fault" button, then
start the motor again.
3. The RGBLED0_GREEN will blink once per second when the motor spins.

3. Model overview
The following image shows the model overview with all the blocks present.

Figure 3. Model overview

These are the main blocks:


• Initialize block contains functions for the S32K344 MCU and GD3000 pre-driver

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 5
Model overview

• Hardware_interrupt_handler calls the Measurement and drives the motor control algorithm
• Measurements reads the ADC FIFO buffer and calls the following blocks
• Board buttons control the start (in CW or CCW) and stop of the motor
• State machine subsystem contains the main "StateFlow" of the Motor Control Application,
along with the S functions executed in each of the states.
• Motor selection contains parameters for the Sunrise or the Linix motor.
• RGBLED0 toggle controls the blinking of the green LED
• Update PWM calls the peripheral block to update a new value of duty cycle
• Var. Init. contains most of the variables used in the model and in FreeMASTER
• FreeMASTER Recorder transfers the data from the model to the PC

3.1. Initialize function


The first block to run in the generated code has the purpose of configuring the following:
• Communication via FreeMASTER
• Analog-to-Digital Convertor (ADC) - instances ADC0 and ADC1
• GD3000 invertor board driver
• Six PWM channels
• Trigger the event for “MBD_e_init”

Figure 4. Initialization function


3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
6 NXP Semiconductors
Model overview

3.2. ADC interrupts and measurements


The hardware interrupt handler, which is set up to be activated by the ADC (Analog to Digital
Converter) and to make a recurrent call to sample FOC state variables with a 100 μs period, serves as the
primary driver for this motor control application.
The shunt resistors on the DEVKIT-MOTORGD are used to measure the motor stator phase currents.
Current flows through each leg of the invertor (R56, R57, and R58) and produces a voltage drop that is
fed into the Operational Amplifier for adding gain, signal filtering and read using the Analog-to-Digital
Converter (ADC) peripheral. R60 is used as the low-side shunt resistor for the DC bus current.
Even though S32K344 includes three ADCs, and the board has three shunt resistors (R56, R57, and
R58), only two currents are monitored concurrently in this application to illustrate ADC Single-shot
mode and BCTU control mode in parallel. The following equation is used to get the third stator current
:

More details about the specific hardware for the current measurements can be found in Chapter 3.3,
"Phase Current Measurement," of the document “3-Phase Sensorless PMSM Motor Control Kit with
S32K344 using RTD Low Level API” in "Application Note AN13767".
The hardware interrupt handler calls the Measurements block subsystem.

Figure 5. Hardware interrupt handler

The Measurement block reads the ADC FIFO (First In First Out) buffer, and using the MUX, it creates a
bus with three signals, each containing the values from the respective ADC channel:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 7
Model overview

Figure 6. ADC channels

This subsystem also generates function calls for the other subsystems:
• Board Buttons
• State Machine

3.3. Board buttons


The block provides functionality to start the motor in a clockwise (CW) or counter-clockwise (CCW)
direction and stop the motor.

Figure 7. Starting motor


3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
8 NXP Semiconductors
State machine

When the USER_SW0 button is pressed, the motor starts in the CW direction, and when the
USER_SW1 button is pressed, it starts in the CCW direction.
Pressing both buttons stops the motor and rearm the system to start again.
The FreeMASTER tool can read the two global variables (btSpeedUp and btSpeedDown) for starting or
stopping the motor in CW or CCW.
The speed step increase/decrease is done by sequentially pressing the same button and this triggers the If
Action subsystem below.

Figure 8. Speed control

If the “cntSpeedUp” value is greater than the “MBD_SPEED_UP_CNT,” the counter value is reset to 0.
Otherwise, it increases with each run of the subsystem.

4. State machine
This is the main motor control block and the most complex one from the MATLAB model.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 9
State machine

Figure 9. Main control block

The main state machine block controls the flow of the motor control application.

Figure 10. Motor control application flow

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
10 NXP Semiconductors
State machine

The application state machine consists of the following six states:


• INIT – local_MBD_init
• FAULT – local_MBD_fault
• READY – local_MBD_ready
• CALIB – local_MBD_calib
• ALIGN – local_MBD_align
• RUN – local_MBD_run
The INIT state, in which the PWM outputs are disabled, and the variables are reset to zero, is the first to
execute.
Following initialization, the RGBLED0_GREEN is set to "On" and the "MBD_ready" is set before the
device enters the "READY" state.
After the system enters CALIB state, where the offsets for phase A and phase B currents are measured.
Continuing with the ALIGN phase of the motor, the start-up ramp is initiated, and the motor enters in
RUN state, where the motor switches from open loop to closed loop.
Every state will make a call to a model subsystem; therefore, we will examine these building elements
next.

4.1. Compute event


This block calculates all the stator currents based on the raw ADC readings and computes the Event and
DC bus voltage.

Figure 11. Compute event

Looking into the Compute Events and ADCs block.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 11
State machine

Figure 12. Compute event with ADC blocks

An event is generated only when the motor starts with OnOffState set to OFF and the OnOffSwitch set to
true, or when the motor stops with OnOffState set to ON and the OnOffSwitch set to false.

Figure 13. On and off state

This subsystem checks if the OnOffState is equal to 1, and if it is true, it will set MBD_e_app_on as the
output for the event; otherwise, it will be off.
Calc_iABC obtains the ADC raw values and converts them to real currents.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
12 NXP Semiconductors
State machine

It also determines the third stator current, or , using the following formula:

The UDC_filt block gets the raw ADC value and computes the raw and filtered DC bus voltage:

The raw ADC values are shifted and multiplied with the constant MBD_U_DCB_MAX that is present in
the MATLAB script “s32k3xx_mc_pmsm_data.m”.
The AMMCLib function GDFLIB_FilterMA implements an exponential moving average filter. The
filter will initialize the internal state according to the preset value.
The purpose of this preset is to avoid excessive transients and, in this case, the constant 12 is used
because the nominal DC voltage is 12 volts.
The output of UDC_filt block updates two global variables:
• Udcb_Filt
• Udcb_Raw
These variables are particularly important because they are used in the current loop in the fault state to
check for overvoltage, the align state, and to eliminate the DC bus ripple.

4.2. Init state


This block resets all variables to their default init state and calls a function to disable the PWMs.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 13
State machine

Figure 14. Init state

InitLeds block sets to zero the RGBLED0, along with OnOffState, OnOffSwitch, and SwitchFaultClear.

Figure 15. Variable setting

After all the variables are set, MBD_e_init_done is sent to the global variable Event.
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
14 NXP Semiconductors
State machine

4.3. Fault state


This block checks if the MBD_State is faulted and then check for various errors that might have
appeared, such as Overcurrent, Overvoltage, and Undervoltage.

Figure 16. Fault state

Overcurrent is also checked for each phase A, B, or C, as well as overDCBusVoltage and overcurrent on
the DC bus.

Figure 17. Overcurrent check


3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 15
State machine

If any of the limits are reached, then a function call to disable PWM's outputs will be triggered.

4.4. Align state


This subsystem aligns the motor shaft by setting a constant value for the D and Q references.

Figure 18. Align state

The SetStates subsystem sets variables and resets the counter needed for the align process.

Figure 19. Reset after variable setting

The inverse Park transformation is used by the Calc_uAIBReq to calculate the Ud and Uq voltages.
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
16 NXP Semiconductors
State machine

Figure 20. Ud and Uq voltage calculation

The Wait_to_align block uses a counter to wait until the motor shaft is mechanically aligned.

Figure 21. Motor shaft alignment

The align subsystem is using a timer to wait until the counter is negative, and then it resets all the
following variables: DutyCycleValues(:), uDQReq1, uDQReq2, iDQReq1, iDQReq2, Rst_FastLoop,
Rst_SlowLoop, trackingToSensorless, speedLoopCntr, openLoop_wRotEl, pospeControl_wRotEl,
sensorless_wRotEl, sensorless_thRotEl, openLoop_thRotEl:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 17
State machine

After all values are set, the MBD_e_align_done will be sent to the Event global variable.
The last subsystem eliminates the DC bus ripple and calculates the PWM duty cycle:

It uses two AMMCLib functions “GMCLIB_ElimDcBusRip” and “GMCLIB_SvmStd_FLT”.


The “GMCLIB_ElimDcBusRip” function provides a computational method for the recalculation of the
direct Uα and Uβ quadrature components of the required stator voltage vector, to compensate for voltage
ripples on the DC bus of the power stage.
The “GMCLIB_SvmStd_FLT” function transforms the values from the two-phase (α-β) orthogonal
coordinate system to the six-phase system consisting of the two sets of star connected three phase stator
windings (f32A-f32B-f32C) and (f32D-f32E-f32F) using the Space Vector Modulation (SVM)
algorithm.
To illustrate the transformation from the Stator Reference voltages (alpha and beta) to the modified
Space Vector Modulation SVM, we can take a closer look at the waveforms below:
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
18 NXP Semiconductors
State machine

We are using Modified Space Vector Modulation in order to achieve full DC bus voltage utilization by
continuously shifting up and down the 3-phase voltage system.

This shifting is called “3rd harmonic injection” and allows us to change the amplitude of the first
harmonic by 15.5%, which will allow us to get to a higher speed region or deliver more torque compared
to the standard sinusoidal PWM modulation.

4.5. Ready state


This state contains two blocks GreenLedOn and ReadyStateSetStates described in the picture below.
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 19
State machine

Figure 22. Ready state

The GreenLedOn sets the RGBLED0 to a solid GREEN color and turns off the other two color LEDs.
The “ReadyStateSetStates” sends the value “MBD_e_ready” to Event and the constant “MBD_ready” to
the “MBD_State” global variable.
The Ready state subsystem also creates a function call to disable the PWM outputs.

4.6. Calibration state


This block calculates the offsets for phase A and phase B currents and also uses a counter to wait until it
can reset variables.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
20 NXP Semiconductors
State machine

Figure 23. Calibration state

The “Calculate_I_ABCoffsets” uses the “IphaseAB_raw” values and the constant “MBD_I_MAX” from
the MATLAB script as inputs for the AMMCLib blocks.

The “GDFLIB_FilterMA_FLT” function block implements an exponential moving average filter used to
calculate the offset between the raw phase currents and the maximum current “MBD_I_MAX” set as a
constant in the script “s32k3xx_mc_pmsm_data.m” file.
The function returns a single precision floating-point value, representing the filtered value of the input
signal in step (k).
The counter implementation is simple, using a decrement for each step and comparing.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 21
State machine

If the “CalibCnt” counter value is higher than the “CalibTimer” value, it will trigger the subsystem
“ResetVariables”:

This sets all variables above and sends the value “MBD_e_calib_done” to the Event global state.

4.7. Run state


This the main application block and contains the reset function, and the motor control loops.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
22 NXP Semiconductors
State machine

The “Rst_FastLoop” variable is used to reset the motor control algorithm when the user wants to stop
and restart the motor.
The motor control algorithm is pictured below, going from the current acquisition, Clark+Park
transforms, BEMF observer, Current loop, Inverse Park plus DC bus ripple compensation and PWM out.

The schematic above is similar to the motor control application from the document “3-Phase Sensorless
PMSM Motor Control Kit with S32K344 using RTD Low Level API” AN13767.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 23
State machine

4.8. Motor control algorithm overview


To simplify a bit the control system, we can take a “linear” look at this motor control algorithm from a
block-set point of view:

The first step is the acquisition of the motor stator phase current.
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
24 NXP Semiconductors
Current loop (Fast loop)

The voltage drop on the shunt resistors is sent into the Op-Amp for amplification and signal filtering
before being input for the ADC.
Then the ADC is used to sense two of the phase currents, and then the motor control algorithm
calculates the third current based on Kirchhoff's Law, which states that the sum of currents entering a
node is equal to zero.

The forward Clark transform is then used to convert the three-phase system to a two-phase orthogonal
system (α, β).
The stationary system is then translated into a rotating d-q reference frame using the Park transform,
after which it is fed into the back-EMF and Angle tracking Observer to compute the command voltages
for the next PWM commutation.

The d-q coordinates are transformed to the alpha and beta stationary frames using the inverse Park.
The space vector modulation (SVM) block uses the inverse Clark transformation to produce the three-
phase voltages for the motor windings, completing the motor control loop from current acquisition to the
generated voltage command.
For more information about the equations, stationary and rotating reference frames, please read Chapter
3.2, “PMSM model in quadrature phase synchronous reference frame,” in AN13767.

5. Current loop (Fast loop)


Current control is essential in a variable-frequency drive control method known as field-oriented control
(FOC). The transformation of the stator phase currents into the orthogonal rotational two-phase system
results in two independent current components.
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 25
Current loop (Fast loop)

The direct (d-axis) current component is known as the flux-producing component, and the orthogonal
(q-axis) component to the magnetic flux is known as the torque-producing component.

The current control loop is used to control the torque and magnetic flux of the 3-phase motor with high
accuracy, dynamics, and bandwidth.
Looking back into the MATLAB model, let’s follow the same logic from the ADC signal acquisition
until the PWM output voltage commands.
First, we have the ADC raw values fed into the “Calc_iABC“ block that computes the three-phase
currents, which are then converted to single-precision floating point using the
“CurrentSenseProcessing” block.
Second, we have the Forward Clark transformation subsystem.

This uses the “GMCLIB_Clark_FLT” single precision floating point function, which is used to
transform values from the three-phase (A-B-C) coordinate system to the two-phase (α-β) orthogonal
coordinate system according to the following equations:

Next is the Park transformation, which uses “GMCLIB_Park_FLT” function:


3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
26 NXP Semiconductors
Current loop (Fast loop)

This transforms values from the two-phase (α-β) stationary orthogonal coordinate system to the two-
phase (d-q) rotational orthogonal coordinate system, according to these equations:

Were (theta_e) represents the electrical position of the rotor flux.


The Current loop block gets the d-q current coordinates as input, see below.

The “AMCLIB_CurrentLoop” function implements the current control loop, which is an integral part of
FOC and represents the most inner loop in the motor control structure.
The current control loop contains two PI controllers in recurrent form, and the parameters are
configurable inside the AMCLib block:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 27
Current loop (Fast loop)

These parameters can be modified in the script “s32k3xx_mc_pmsm_data.m” from the model folder.
The output of the Current Loop is the Ud and Uq required command voltages, which are fed into the
Invers Park block:

The “GMCLIB_ParkInv” function calculates the Inverse Park Transformation, which transforms
quantities from the two-phase (d-q) rotational orthogonal coordinate system to the two-phase (α-β)
stationary orthogonal coordinate system using the equations:

Next, the two signals feed the DC bus Ripple Compensation block.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
28 NXP Semiconductors
Current loop (Fast loop)

Next, these signals are passed into the PWM modulation block:

The DutyCycleValues values are used to update the MCU timer that generates the PWM signals, which
are fed into the GD3000 MOSFETs driver.
The “sin-cos” block calculates the sine, cosine of the rotor position (theta_e):

These signals are needed for the angle estimation and are fed into the Park and inverse Park blocks. The
top part of the back-EMF and Angle tracking Observer block contains the following.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 29
Current loop (Fast loop)

The “AMCLIB_BemfObsrvDQ” function returns the phase error between the real and the estimated
rotating reference frames:

The bottom part of the back-EMF and Angle tracking Observer block contains the
“AMCLIB_TrackObsrv_FLT” function.

The position tracking observer has a PI controller that is calculating a linear approximation around the
operating point Θestim = Θe.
The parameters of the block are:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
30 NXP Semiconductors
Current loop (Fast loop)

The Observer feeds the “Omega_e” to the Speed Loop, called Slow Loop in this model.
All the above blocks are part of the “Fast Loop” of the motor control algorithm and are essential for the
current control logic.
This function takes the phase error (Θerr) calculated by the “AMCLIB_BemfObsrvDQ” and uses it to
estimate the angular velocity and position of the rotor.
The estimated rotor position is obtained by driving the phase error (Θerr) of the γ-δ reference frame
towards zero in a phase-locked loop (PLL).
The PLL structure is depicted in the following diagram.

Further, we will present the other blocks or subsystems needed.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 31
Speed loop (Slow loop)

6. Speed loop (Slow loop)


The Speed Loop subsystem is running at a much lower speed than the Current Loop:

The counter “speedLoopCntr” is used to run the Slow Loop subsystem when it is equal to the variable
“MBD_SPEED_LOOP_CNTR”, executed 10 times slower than the Fast Loop:

First, the counter “speedLoopCntr” is reset to zero upon entering this subsystem, then it checks:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
32 NXP Semiconductors
Speed loop (Slow loop)

If “PosMode” equals two, it means that the motor is in sensorless mode, and it will set the speed:

Inside “PosMode1” there is a small state machine that works as a speed limiter:

This limits the speed output between two limits: “-HighLimSpeed” and “+HighLimSpeed”. The Speed
Loop block implements the function “AMCLIB_FWSpeedLoop”:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 33
Open loop

The “AMCLIB_FWSpeedLoop” function implements the speed PI controller in the FOC outer control
loop and the field-weakening algorithm for permanent magnet synchronous motors:

The speed PI controller and the field weakening controller are implemented by the "AMCLIB
FWSpeedLoop" function in the outer control loop shown in the preceding diagram.

7. Open loop
This block calculates the open loop “theta_e” using a couple of AMMCLib functions:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
34 NXP Semiconductors
Open loop

In order to calculate theta, we know that omega (ω) or angular velocity is equal to the rate of change of
the angular position θ with respect to time:

Furthermore, we can integrate omega based on the formula:

Now let us look at the MATLAB implementation:

This divides the "FwSpeedLoop_pRamp_fltState" variable value by the constant "MBD_WEL_MAX"


and converts it to a 32-bit fractional data type.
The second part of the Open Loop is comprised of:

The function “GFLIB_IntegratorTR” implements a discrete integrator using the trapezoidal (bilinear)
transform.
The result is converted to a 32-bit fractional data type and multiplied with the “pi” constant.
There are four running modes in the system:
3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 35
Open loop

Depending on the scenario, each mode modifies different parameters.


The force mode imposes the open loop omega, and zero for the other variables:

The tracking mode sets control omega to zero and theta to open loop:

When the motor is running in a closed loop (sensorless), the position mode is in case two.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
36 NXP Semiconductors
Open loop

This divides “theta_e” from the variable “sensorless_thRotEl” with the constant “pi” and convert it to
a 32-bit fractional data type.
The default mode is:

The Automatic Mode consists of a state machine that switches the “PosMode” based on the open loop
speed:

The function “MLIB_Abs_FLT” returns the absolute value of the open loop omega electrical.
The “PosMode” state machine has the following states:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 37
RGBLED0 toggle

There are three states based on the Open Loop Speed:


• Force mode = 0
• Tracking mode = 1
• Sensorless mode = 2
This ensures that the motor control algorithm automatically switches the operating mode based on the
speed.

8. RGBLED0 toggle
This block implements a timer that toggles the RGBLED0_GREEN.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
38 NXP Semiconductors
Updating PWMs

The second function of this subsystem is to also shut down the other two LEDs, giving the value 0 for
the NXP function block: Dio_WriteChannel for RGBLED0_BLUE and RGBLED0_RED.
The timer uses the variable "LEDCounter," which is incremented each period of the fast loop and
compares it with the value 2500.
If it is higher, it will reinitialize the counter value to 0, and it will trigger the If Action Subsystem.

In this subsystem, the LEDState is compared with logic “1” and masked bitwise XORed, giving the
resulting value to the variable and function Dio_WriteChannel for RGBLED0_GREEN.

9. Updating PWMs
This block generates the corresponding PWM duty cycle values based on the hardware scaling and the
pre-calculated values for each channel.

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 39
FreeMASTER config GUI

The duty cycles are stored in a data variable as an array for the three channels.
They are multiplied by the constant "32768" for the duty cycle shift and then fed into the input for each
PWM channel.
Also, the “DutyCycleValues” are adjusted for the Phase Shift input of the same PWM blocks and the
Sync Update is fed with a constant "1," meaning that each channel is synchronized.
This subsystem uses the NXP PWM block and the function "PWM_SetDutyPhaseShift" for alignment
of the three channels: PWM0, PWM1, and PWM2.

10. FreeMASTER config GUI


Open the "S32K_PMSM_Sensorless" project, click on the "App Control" page, select the desired
speed using the gauge speed, and click on the "On/Off" button:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
40 NXP Semiconductors
FreeMASTER config GUI

Furthermore, the user can look at various parameters, for example the IDQ currents below:

We can also take a close look at the three-phase motor currents:

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 41
References

The 3-phase currents are represented in RGB colors and show a peak of 1.4 [A] depending on the motor
load.

11. Conclusions
The MCSPTE1AK344 Development Kit offers a complete set of hardware features that work together
with the s32k3xx_mc_pmsm.mdl example, presented above, and creates a complete out-of-the-box
experience for modeling the motor control algorithm using the NXP Model-Based Design Toolbox for
S32K3.
MBDT enables the NXP tools like Automotive Math and Motor Control Library (AMMCLib),
Compilers, Configuration Tools, Real-Time Drivers (RTD), and Toolchain in the Simulink®
environment. Together with the Embedded Coder®, it generates optimized code for the microcontroller,
ensuring that the most advanced software loops can run on the NXP MCUs with maximum performance.
Please visit the MBDT Community Page to learn more about the features of the Model-Based Design
Toolbox for S32K3. You may start a topic here and have a direct conversation with the engineers that
created MBDT toolboxes. Aside from typical application demos concentrating on Battery Management
Systems, AUTOSAR, or Motor Control Applications, you can also follow the NXP MBDT training and
courses such as the Beginner's Guide Series.
You might also visit NXP.com for additional information on the development ecosystem that NXP
offers.

12. References
1. Application Note AN13767
2. The workshop “3-Phase PMSM Control Workshop with NXP's Model-Based Design Toolbox”
3. Module 4: Space Vector Modulation

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
42 NXP Semiconductors
References

4. Automotive Math and Motor Control Library Set for S32K14x

3-Phase Sensorless PMSM Motor Control Kit with S32K344 using MBDT Blocks, Rev. 0, 03/2023
NXP Semiconductors 43
NXP Semiconductors
Legal information

Legal information
Definitions Applications — Applications that are described herein for any of these
products are for illustrative purposes only. NXP Semiconductors makes no
Draft — A draft status on a document indicates that the content is still
representation or warranty that such applications will be suitable for the
under internal review and subject to formal approval, which may result
specified use without further testing or modification.
in modifications or additions. NXP Semiconductors does not give any
representations or warranties as to the accuracy or completeness of Customers are responsible for the design and operation of their applications

information included in a draft version of a document and shall have no and products using NXP Semiconductors products, and NXP Semiconductors

liability for the consequences of use of such information. accepts no liability for any assistance with applications or customer product
design. It is customer’s sole responsibility to determine whether the NXP
Semiconductors product is suitable and fit for the customer’s applications and
Disclaimers products planned, as well as for the planned application and use of customer’s
Limited warranty and liability — Information in this document is believed third party customer(s). Customers should provide appropriate design and
to be accurate and reliable. However, NXP Semiconductors does not give operating safeguards to minimize the risks associated with their applications
any representations or warranties, expressed or implied, as to the accuracy and products.
or completeness of such information and shall have no liability for the NXP Semiconductors does not accept any liability related to any default,
consequences of use of such information. NXP Semiconductors takes no damage, costs or problem which is based on any weakness or default in the
responsibility for the content in this document if provided by an information customer’s applications or products, or the application or use by customer’s
source outside of NXP Semiconductors. third party customer(s). Customer is responsible for doing all necessary testing
In no event shall NXP Semiconductors be liable for any indirect, incidental, for the customer’s applications and products using NXP Semiconductors
punitive, special or consequential damages (including - without limitation - products in order to avoid a default of the applications and the products or of the
lost profits, lost savings, business interruption, costs related to the removal or application or use by customer’s third party customer(s). NXP does not accept
replacement of any products or rework charges) whether or not such damages any liability in this respect.
are based on tort (including negligence), warranty, breach of contract or any
Terms and conditions of commercial sale — NXP Semiconductors products
other legal theory.
are sold subject to the general terms and conditions of commercial sale,
Notwithstanding any damages that customer might incur for any reason
as published at http://www.nxp.com/profile/terms, unless otherwise agreed
whatsoever, NXP Semiconductors’ aggregate and cumulative liability towards
in a valid written individual agreement. In case an individual agreement
customer for the products described herein shall be limited in accordance with
is concluded only the terms and conditions of the respective agreement
the Terms and conditions of commercial sale of NXP Semiconductors.
shall apply. NXP Semiconductors hereby expressly objects to applying the
customer’s general terms and conditions with regard to the purchase of NXP
Right to make changes — NXP Semiconductors reserves the right to make
Semiconductors products by customer.
changes to information published in this document, including without limitation
specifications and product descriptions, at any time and without notice. This
Suitability for use in automotive applications — This NXP product has been
document supersedes and replaces all information supplied prior to the
qualified for use in automotive applications. If this product is used by customer
publication hereof.
in the development of, or for incorporation into, products or services (a)
used in safety critical applications or (b) in which failure could lead to death,
personal injury, or severe physical or environmental damage (such products
and services hereinafter referred to as “Critical Applications”), then customer
makes the ultimate design decisions regarding its products and is solely
responsible for compliance with all legal, regulatory, safety, and security
related requirements concerning its products, regardless of any information
or support that may be provided by NXP. As such, customer assumes all risk
related to use of any products in Critical Applications and NXP and its suppliers
shall not be liable for any such use by customer. Accordingly, customer will
indemnify and hold NXP harmless from any claims, liabilities, damages and
associated costs and expenses (including attorneys’ fees) that NXP may incur
related to customer’s incorporation of any product in a Critical Application.

Export control — This document as well as the item(s) described herein may be
subject to export control regulations. Export might require a prior authorization
from competent authorities.
NXP Semiconductors
Legal information

Translations — A non-English (translated) version of a document, including Synopsys — Portions Copyright © 2021 Synopsys, Inc. Used with permission.
the legal information in that document, is for reference only. The English All rights reserved.
version shall prevail in case of any discrepancy between the translated and
English versions.

Security — Customer understands that all NXP products may be subject to


unidentified vulnerabilities or may support established security standards or
specifications with known limitations. Customer is responsible for the design
and operation of its applications and products throughout their lifecycles
to reduce the effect of these vulnerabilities on customer’s applications
and products. Customer’s responsibility also extends to other open and/or
proprietary technologies supported by NXP products for use in customer’s
applications. NXP accepts no liability for any vulnerability. Customer should
regularly check security updates from NXP and follow up appropriately.

Customer shall select products with security features that best meet rules,
regulations, and standards of the intended application and make the
ultimate design decisions regarding its products and is solely responsible
for compliance with all legal, regulatory, and security related requirements
concerning its products, regardless of any information or support that may be
provided by NXP.

NXP has a Product Security Incident Response Team (PSIRT) (reachable


at [email protected]) that manages the investigation, reporting, and solution
release to security vulnerabilities of NXP products.

Trademarks
Notice: All referenced brands, product names, service names, and
trademarks are the property of their respective owners.

NXP — wordmark and logo are trademarks of NXP B.V.

AMBA, Arm, Arm7, Arm7TDMI, Arm9, Arm11, Artisan, big.LITTLE, Cordio,


CoreLink, CoreSight, Cortex, DesignStart, DynamIQ, Jazelle, Keil, Mali,
Mbed, Mbed Enabled, NEON, POP, RealView, SecurCore, Socrates, Thumb,
TrustZone, ULINK, ULINK2, ULINK-ME, ULINK-PLUS, ULINKpro, μVision,
Versatile — are trademarks or registered trademarks of Arm Limited (or its
subsidiaries) in the US and/or elsewhere. The related technology may be
protected by any or all of patents, copyrights, designs and trade secrets. All
rights reserved.

Bluetooth — the Bluetooth wordmark and logos are registered trademarks


owned by Bluetooth SIG, Inc. and any use of such marks by NXP
Semiconductors is under license.

EdgeLock — is a trademark of NXP B.V.

elQ — is a trademark of NXP B.V.

I2C-bus — logo is a trademark of NXP B.V.

NXP SECURE CONNECTIONS FOR A SMARTER WORLD — is a trademark


of NXP B.V.

SafeAssure — is a trademark of NXP B.V.

SafeAssure — logo is a trademark of NXP B.V.

SuperFlash This product uses SuperFlash®technology. SuperFlash® is a


registered trademark of Silicon Storage Technology, Inc.

Synopsys & Designware — are registered trademarks of Synopsys, Inc.


Please be aware that important notices concerning this document and the product(s) described
herein, have been included in section 'Legal information'.

© NXP B.V. 2023. All rights reserved.


For more information, please visit: http://www.nxp.com
For sales office addresses, please send an email to: [email protected]
Date of release: 03/2023
Document identifier: AN13902

You might also like