Module 7: Torque Control

Discussion created by dumitru-daniel.popa Employee on Feb 11, 2018
Latest reply on Sep 22, 2019 by Marius-lucian Andrei

EV Powertrain  source: carmagazine.co.uk 





Nowadays, are we witnessing the start of a global revolution within the automotive industry, where more and more car manufacturers are trying to compete in the rising sector of electrical vehicles. At the core of this revolution sits the "work horse" of modern transportation: the humble electric motor. 


In this module of the 3-Phase PMSM Control Workshop with NXP's Model-Based Design Toolbox , we are going to discuss about the FOC for traction motors and how to control the torque produced by PMSM. The control method is generic and may be applicable to all sort of means of transportation: trains, buses, bikes, scooters and modern day cars.  


In a car with internal combustion engine each time you press the gas pedal, the throttle valves open, more fuel and air is injected into cylinders resulting in a stronger explosion that sends the piston down with a stronger force. This linear force pointed downward is converted via the crankshaft into a rotational movement with a certain amount of torque given by the product between the force and rod length. Therefore, each time you press the "pedal to the metal" you control an amount of torque the engine is capable of. Under normal driving conditions the speed of the vehicle is regulated in driver's head that ultimately decides to push the acceleration pedal more or less.    


The purpose of this article is to replicate such control system. The user will be able to set a torque reference and the motor will obey that command speeding up or slowing down based on how much torque is being demanded. In this module we are going to learn how to:

  • Implement FOC by closing the current loop with the values read from ADC;
  • Regulate the Torque and Flux in (dq) coordinate system with standard PI controllers;
  • Use Park transformations to liniarize the control quantities; 
  • Synchronize the SLOW and FAST control loops with the ADC interrupts;
  • Start-up the PMSM from a well known position;
  • Configure the motor and drive protections;


In Fig. 1, which represent the global application mapping diagram as discussed in Module 3: System Partitioning , are shown the new hardware blocks that are going to be configured and used to perform the PMSM FOC Torque Control highlighted in green relative to the ones already configured so far in the previous modules of this workshop. We are going to reuse and enhance the model developed in Module 6: Current Sensing (Part 1/2)  and Module 6: Current Sensing (Part 2/2) which will allow us to test the application in real time on S32K144 3-phase PMSM Development Kit|NXP . The interactions between host PC and hardware will be done via OpenSDA serial communication that will be used to download the code generated from MATLAB and to visualize various control signals with FreeMASTER. In addition, the user can start and control the motor speed from the SW2 and SW3 push button present on the S32K144 Evaluation Board|NXP . A three color LED will be used to signal the control states and faults. 


Fig. 1:  Application Mapping -  new HW & SW modules used for FOC Torque Control are highlighted in green






The Torque Control replicates the behavior of internal combustion engine. As is was explained in Module 2: PMSM and FOC Theory to achieve the adequate motor response we need to:

  • maintain 90 electrical degrees angle between Torque and Flux axis by regulating the currents in Q and D axis.
  • maximize the PMSM torque that is proportional with the stator current vector projection on the Q axis.


In Fig. 2, is shown the block diagram of the Torque Control system proposed for implementation. The Field Oriented Control is executed in the FAST CONTROL LOOP block that is executed based on a ADC trigger at each 100 microseconds. This subsystem receives the references values for currents in Q (torque) and D (flux) axis and the currents measured from the ADC and outputs the PWM duty cycles commands that controls the 3-phase inverter.


The toque and flux references are computed in the SLOW CONTROL LOOP block that is triggered at each 1 milisecond based on the same ADC events as the FAST CONTROL LOOP. The speed and torque commands are received via the UART or from on board push buttons and transformed based on a LUT into actual torque and electric angle increments.


Fig. 2: FOC Torque Control - Block Diagram


As you can see in Fig. 2, we have only one loop that is closed: current loop. The outer speed loop is still open, hence it does not prevent us for implementing the torque control even if this is not ideal. The outer speed loop will be closed in the next module of this workshop.




The block diagram of the functions implemented on SLOW CONTROL LOOP is shown in Fig. 3. At each startup, a special alignment sequence is executed in order to move the PMSM rotor into a particular well known position - rotor magnetic D axis is align with PMSM stator phase A. The rotor alignment is a key aspect in torque control since we want to produce the highest torque available and that can only be done if we maintain a 90 electrical degrees between Q axis and rotor magnetic flux axis D

Fig. 3: Slow Control Loop - Block diagram for Torque Control


The alignment is achieved by injecting a current into the D-axis, forcing this way the rotor magnets to align with the magnetic field produced in the stator. For the rotor alignment we need two parameters:

  • the magnitude of the current in the D-axis, which depending on the load it should be closed to the motor rated current or even a bit higher;
  • alignment time that gives the time period while the current is maintained thru stator coils ensuring the rotor is being dragged into the correct position;  


Once the rotor alignment is completed the speed command is transformed into the torque command via a LUT similar with the method from Module 5: V/F Scalar Control 

In the same time with torque reference computation, the actual electric angle increment is computed in order to rotate the motor.




The block diagram of the functions implemented on FAST CONTROL LOOP is shown in Fig. 4. As it can be noticed this is the complete diagram of the Field Oriented Control as discussed in Module 3: System Partitioning 


Fig. 4: Fast Control Loop - Block diagram for Torque Control


Based on the reference input values of Torque (Iq), Flux (Id) and the PMSM measured phased currents, after PARK forward transformation the appropriate control voltages for (dq) reference system are computed with the help of two independent PI current controllers. Once the command voltages are obtained, the inverse PARK and CLARKE transformations are used to compute the PWM duty factors via the Space Vector Modulation technique to control the 3-phase inverter MOSFETs.


The theory behind the PI controllers was discussed in Motor Control Class: Lecture 11 - Closed Loop Control 

In case of motor electric transfer function the procedure of computing the current controller Kp and Ki gains is the same as the one presented for the speed controller with the observation that Inertia (J) is replaced with Inductance (L) and Friction coefficient (b) is replaced with Resistance (R). The rest of the computations remains similar. 


Note, how in the Fig. 4, we are demanding the rotor to move in a desired position based on the computation of the electrical angle (theta) that is derived from the speed reference computed in SLOW CONTROL LOOP. Since there is no loading torque on the motor, we are going to use for demonstration this method of controlling the motor torque in open loop since it is safe and will produce good results. In case there is a loading torque and the rotor alignment is not correct the motor might lose the synchronism and will stall. For such cases you will need:

  • actual rotor position sensor (we will address that in M8)
  • estimate the rotor position based on measurements and mathematical model (we will address that in M9)


The Simulink model used to implement all these functionalities is shown in Fig. 5:

Fig. 5: Simulink model for PMSM FOC Torque Control


Video training: How To Implement and Test Torque Control System for PMSM Using S32K144EVB

Please watch this video before going further. The rest of the article is a summary of the video.






In this section we are going thru the high level details regarding the implementation of Toque Control model shown in Fig. 5. The model is available for download at the end of the article. In the figures below we are going to discuss only new functionalities added or changed compared to the model implemented in Module 6: Current Sensing (Part 1/2) 



ADC Interrupts

Since we need the FOC software routines (SLOW CONTROL LOOP & FAST CONTROL LOOP) to be synchronized with the ADC measurements as it was shown in Fig. 9 from Module 6: Current Sensing (Part 1/2) we are going to modify the PDB1_IRQ to trigger a function call based on ADC conversion complete (COCO) interrupt that corresponds to the AD15 - PMSM Phase B current. For this we are going to use the ADC_ISR block that allows us to read the conversion results and trigger an interrupt in the same time.


Fig. 6: ADC Interrupt trigger for SLOW and FAST Control Loops



Interrupt Dispatcher

Because the ADC interrupts are synchronized with the PWM frequency, it means that we will get an interrupt at each 100 micro-seconds for a 10kHz switching frequency. That is OK for the FAST CONTROL LOOP which we want to be triggered at each 100 micro-seconds, but for the SLOW CONTROL LOOP that must be scheduled at each 1 mili-seconds we need to implement a sort of dispatching mechanism. This is done in the IRQ_Dispatcher subsystem as shown in Fig. 7.


Fig. 7: Dispatching interrupts to FAST and SLOW CONTROL LOOPS based on ADC HW COCO interrupt


This block implements a two stage logic:

  • StartUp sequence - when there are no ADC interrupts prior of enabling PWM but still need to execute the FAST CONTROL LOOP in order to configure the MOSFET pre-driver MC34GD3000
  • normal RunState - when ADC interrupts are active and we need to delay the triggers for the SLOW CONTROL LOOP leaving unchanged the timings for the FAST CONTROL LOOP in order to insure the 10:1 execution rate between FAST vs. SLOW loops.


The dispatching mechanism that keeps a relative execution rate of 10:1 is implemented as:

Fig. 8: Interrupt dispatcher implementation for execution rate 10:1 between FAST vs. SLOW loops



Slow Control Loop

This subsystem was modified as shown in Fig. 9, to allow:

  • rotor alignment during the startup sequence. This procedure is using an AlignTimer to count how many seconds the rotor should be kept under alignment.
  • speed to torque transformation via a dedicated LUT


As Fig. 9 shows, we have kept the same mechanism used for Module 5: V/F Scalar Control to generate the electrical angle profile and the the references for the (dq) system commands.

Fig. 9: SLOW CONTROL LOOP implementation



Fast Control Loop

This subsystem was enhanced with two PI controllers for each of (dq) axis and the forward PARK and CLARKE transformations that are now used to compute the currents in (dq) reference system starting from the values measured from ADC.

As can be noticed, during the alignment process the PI controllers are bypassed while the commands from SLOW CONTROL LOOP are directly applied as voltage commands to the FOC transformations. This ensure a faster and stronger response, forcing the rotor to align more precisely into the correct positions without additional delays cause by the PI controllers. 

Fig. 10: FAST CONTROL LOOP implementation



Board Controls

The subsystem shown in Fig. 11, implements the logic of reading the push buttons from the S32K144 Evaluation Board|NXP that allows us to change the motor speed manually and the mechanism of LED notification. The LED logic is implemented as:

  • after reset the GREEN LED is ON signaling the application is ready for receiving commands;
  • during the alignment the GREEN LED is toggling;
  • during normal operation the BLUE LED is ON signaling the application is operating normally;
  • if a fault occurs the RED LED is ON and the motor will stop automatically. The only way to get out of this state is by pressing the RESET button;


Fig. 11: I/O board controls




The motor and drive protections are implemented in the ADC interrupt routine immediately after the measurements in order to make sure that any fault is addressed as soon as possible. Currently there are three protections implemented but in next modules, additional ones will be added:

  • under-voltage protection if the DC bus voltage is below 8V;
  • over-voltage protection if  the DC bus voltage is above 18V;
  • DC link over current if the value of the current on DC link exceeds 4A;


If one of these conditions occurs the application will disable the PWM generation immediately and the application will not be able to be resumed until a RESET is issued by the user.

Fig. 12: Protections





Using the FreeMASTER attached project we can check the system responses and tune the controllers to responds however we wish on the fly in real time. For demonstration purposes the controller are de-tuned in order to increase the respond time and have easy to understand pictures.


Fig. 13, shows the system response during the alignment sequence, startup and normal operation. You can see that the alignment sequence is lasting for 3 seconds. In this time interval ~1.1 amps are injected in the flux axis in order to force the motor alignment with stator phase A. The correspondence between the (dq) currents and the actual (abc) currents can be clearly observed in Fig. 14.

Fig. 13: Startup sequence: alignment, startup and normal operation showing:

- Torque reference (IQ_REF - red) and Actual Torque (IQ - orange)

- Flux reference (ID_REF - blue) and Actual Flux (ID - cyan) 


Fig. 14: Startup sequence: alignment, rump-up and normal operation comparison between the (dq) reference system and (abc) reference system

- Actual Torque (IQ - orange) and Flux Flux (ID - cyan) in the upper part

- Actual PMSM phase current Ia, Ib, Ic


Fig. 15, shows the PI controllers responses during startup and normal operation. As you can see in order to maintain almost 1.5 amps thru the motor we need only a small amount of control voltage (0.5 volts - on the right axis). 

Fig. 15: PI Current Controller behavior: inputs vs outputs

- Torque axis response (upper)

- Flux axis response (lower)


Fig. 15, shows that there is plenty of room to play of current controller tuning parameters and improve the system response since the voltage reserve is not reached.


How would you tune the current controllers ? Do you prefer a sharp or a mild response and why is that ?  



Update revisions:

March 19, 2018


Update January 28th 2019 - This Simulink model is now available on MATLAB 2018b and MBDT for S32K14x 2018.R1 release

Before using the new models make sure you apply all the hot-patches from here: HotFix: MBD Toolbox 2018.R1 for S32K 


Update revisions:

March 18, 2019

May 06, 2020