This design reference manual describes the software portion (hardware / chip independent) of the 16-bit implementation of a sensorless threephase Brushless DC (BLDC) motor control application. The application software provides uses the concept of an isolated motor control algorithm software and hardware. This software approach enables easy porting of an application across the other devices or hardware platforms.
This design reference manual describes the software portion (hardware / chip independent) of the 16-bit implementation of a sensorless threephase Brushless DC (BLDC) motor control application. The application software provides uses the concept of an isolated motor control algorithm software and hardware. This software approach enables easy porting of an application across the other devices or hardware platforms.
This design reference manual describes the software portion (hardware / chip independent) of the 16-bit implementation of a sensorless threephase Brushless DC (BLDC) motor control application. The application software provides uses the concept of an isolated motor control algorithm software and hardware. This software approach enables easy porting of an application across the other devices or hardware platforms.
2014 Freescale Semiconductor, Inc. ___________________________________________________________________ Three-Phase BLDC Sensorless Motor Control Application by: Petr Staszko
1 Introduction This design reference manual describes the software portion (hardware/chip independent) of the 16-bit implementation of a sensorless three- phase Brushless DC (BLDC) motor control application. The application software provided uses the concept of an isolated motor control algorithm software and hardware. This software approach enables easy porting of an application across the other devices or hardware platforms. The application software is divided in the following parts: BLDC motor control algorithm process input variables to output variables and control flags. Concrete hardware and microprocessor serves as a bridge between hardware peripheral modules and BLDC motor control software algorithm. This design reference manual describes the hardware independent BLDC motor control algorithm part of code only. It does not cover individual implementation to respective device. This DRM is supplemented by AN4862: Three- Phase BLDC Sensorless Control Using the MKV10x application note, describing implementation of hardware dependent part of code to the concrete devices.
Contents 1 Introduction ......................................................1 2 Control theory basics ........................................2 2.1 Brushless DC motor (BLDC motor) .........2 2.2 Digital control of BLDC motor ................3 2.3 Sensorless BLDC motor control ...............4 3 Application concept ..........................................6 3.1 System concept .........................................6 3.2 BLDC motor control .................................7 4 Software design ................................................9 4.1 Application software main process ...........9 4.2 I/O values processing ..............................10 5 Application details ..........................................12 5.1 Application state machine function ........12 5.2 Slow control loop ISR ............................17 5.3 Fast control loop ISR ..............................20 5.4 Time event ISR .......................................23 6 Acronyms and abbreviations ..........................28 7 References ......................................................29 8 Revision history ..............................................29
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 2 Freescale Semiconductor, Inc.
The BLDC motor control algorithm is a speed closed-loop motor control algorithm using a Back-EMF voltage integration method for a sensorless motor control. It serves as an example of a BLDC motor control design. It is focused on a simple and easy to understand control approach to BLDC. This reference manual includes a basic BLDC motor theory, system concept, and software design description. The control algorithm includes: rotor alignment open startup sensorless position detection using Back-EMF voltage integration method speed closed-loop current/torque limitation fault protection 2 Control theory basics 2.1 Brushless DC motor (BLDC motor) The brushless DC (BLDC) motor is a rotating electric machine where the stator is a classical three-phase stator similar to an induction motor and the rotor has surface-mounted permanent magnets (see Figure 1). The motor can have more than one pole pair per phase (On the picture can be seen motor with 2 pole pairs per phase). The number of pole pairs per phase defines the ratio between the electrical revolution and the mechanical revolution.
Figure 1. BLDC motor cross section
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 3
2.2 Digital control of BLDC motor The BLDC motor is driven by rectangular voltage strokes coupled with the given rotor position (see Figure 2). The generated stator flux interacts with the rotor flux, which is generated by a rotor magnet, defines the torque and thus the speed of the motor. The voltage strokes must be properly applied to the two phases of the three-phase winding system so that the angle between the stator flux and the rotor flux is kept close to 90, to get the maximum generated torque. Due to this fact, the motor requires electronic control based on actual rotor angle position for proper operation.
Figure 2. Voltage strokes applied to the three-phase BLDC motor For the common three-phase BLDC motor, a standard three-phase power stage is used, as illustrated in Figure 3. The power stage utilizes six power transistors.
Figure 3. Three-phase BLDC power stage The three-phase power stage energizes two motor phases concurrently and the third phase is unpowered (see Figure 2). Thus, we get six possible voltage vectors that are applied to the BLDC motor using a PWM switching signals. The unpowered phase can be used for rotor position evaluation in sensorless motor control. The most common BLDC control topology uses the power stage with a constant power source DC voltage. Therefore, the three-phase average voltage amplitude is controlled by a PWM duty cycle.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 4 Freescale Semiconductor, Inc. 2.3 Sensorless BLDC motor control 2.3.1 BLDC motor Back-EMF voltage shape The Back-EMF sensing technique is based on the fact that only two phases of a Brushless DC motor are energized at a time. The third phase is a disconnected phase that can be used to sense the Back-EMF voltage. The Figure 4 shows branch and motor phase winding voltages during a 0-360 electrical interval. The yellow interval (4) means a conduction interval of a phase. During this time period, current flows through the winding, and Back-EMF voltage is impossible to measure. Cyan lines (3) determine the time period when the Back-EMF voltage can be sensed as phase is unpowered. Gray lines (5) show the transient pulse measured on the phase voltages right after the commutation event, which is produced by the current recirculation when the fly-back diodes are conducting. Green lines (6) determine the time period before the zero-cross event, the red line (7) shows the time after zero-cross event, in this period the Back-EMF voltage is integrated, and at the end of the red interval, the integrated value of the Back- EMF voltage reaches the Back-EMF threshold value, and this is the signal to perform a commutation.
Figure 4. Single phase voltage waveform For more detailed information about BEMF sensing, see References. 2.3.2 Back-EMF voltage integration method One of the sensorless BLDC motor control methods is Back-EMF voltage integration. It is based on simple principle, the integrated value (triangle area) of the non-fed phases Back-EMF voltage after the zero cross is approximately the same at all speeds (S1 S2 S3), as shown in Figure 5.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 5
Figure 5. BEMF integration method The integration starts when the non-fed phases Back-EMF voltage crosses the zero value. Finding of the exact time of zero-cross point is not very important as the Back-EMF voltage around the zero cross is relatively low, integration of these low voltage values around the zero-cross has small effect to the final integrated value. Commutation is performed when the integrated value reaches a predefined threshold value. If a flux weakening operation is required, current advance can be achieved by decreasing the threshold value. In comparison to the standard sensorless technique based on BEMF zero-cross findings, the integration approach is less sensitive to switching noise and offset voltage problems (resistance precisions, noise, and so on). This brings more precise control in low speeds of motor. On the other hand, integration approach is less effective in very high speeds of motor (with only few PWM periods per commutation period), in this case the sensorless technique based on the BEMF zero-cross findings, with simple ADC samples interpolation implemented in, demonstrates more precise motor control. 2.3.3 Back-EMF voltage sensing limitations Accuracy of the sensorless BLDC motor control algorithm based on the Back-EMF voltage sensing is mostly limited by the precision of measured Back-EMF voltage on non-fed motors phase. For example, ADC accuracy, precision of phase voltage sensing circuitry, signal noise and distortion caused by the power switching modules, all these effects need to be taken on mind. Noise generated by power switching modules can be eliminated by correct setting of measurement event to be far away from switching edges (PWM to ADC synchronization). There still exists limitation that cannot be eliminated, named a decay period or freewheeling period. As soon as the phase is disconnected from the power by commutation event, there is still current flowing through the freewheeling diode. The conduction freewheeling diode connects the released phase to either a positive or a negative DC bus voltage. The conduction time depends on the momentary load of the motor. In some circumstances, the conduction time is so long that it doesnt allow for detection of Back-EMF voltage, as represented in Figure 6.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 6 Freescale Semiconductor, Inc.
Figure 6. BEMF decay period In the sensorless BLDC motor control, the period after the Back-EMF voltage crosses zero is important.. If decay period is too long, then it is difficult to control motor with sensorless algorithm based on Back- EMF technique. It is also important to differentiate Back-EMF voltage generated by the motor and phase voltage tied to positive or negative DC bus voltage during the decay period. For this purpose, application uses some blank period after commutation, named T OFF period parameter, and during this T OFF period the Back-EMF voltage is not sensed and used for sensorless control. T OFF period is calculated as proportional value to the previous commutation period using uw16PeriodToff parameter (for example, 0.25 means 25%). Value of this parameter should reflect the motor, load, and dynamical application parameters. 3 Application concept 3.1 System concept This application is developed with a focus on easy porting of software across the devices or platforms. For this reason, the application software is divided into two separate parts of code: Hardware dependent code (dependent on the hardware boards used and the MCU devices peripheral modules). Includes CPU and peripheral modules initialization, I/O control drivers, and interrupt service routines handling. Hardware independent code (pure BLDC motor control application)
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 7
Figure 7. System concept overview
An overview of the system concept of application is shown in Figure 7. Input and output variables and output control flags are used as a bridge between the hardware specific part of code and motor control algorithm. Most of the application input and output variables are stored in a fractional number representation (in range from -1 to 1), scaling of these variable from/to the real values is calculated in drivers (hardware related part of code). 3.2 BLDC motor control Sensorless BLDC motor control method based on Back-EMF voltage needs measurable value of Back- EMF voltage. This voltage is proportional to the actual rotor speed, thus it is not measurable in zero and very low speeds. Therefore, in order to start and run the BLDC motor, the control algorithm has to go through the following states: Alignment: initial rotor position setting Startup: forced commutations in open-loop mode Run: close-loop control with BEMF acquisition
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 8 Freescale Semiconductor, Inc. Figure 8 shows the timing of individual BLDC motor control states.
Figure 8. BLDC motor control states 3.2.1 Alignment As discussed in the earlier sections, the main task for sensorless control of a BLDC motor is position estimation. Before starting the motor, however, the rotor position is not known. The aim of the alignment state is to align the rotor to a known position. This known position enables starting the rotation of the shaft in the desired direction and generating the maximal torque during startup. During the alignment state, all three phases are powered in order to get the required rotor position alignment vector. Phase A and Phase B are powered with the positive voltage and Phase C is powered with negative voltage. The alignment time depends on the mechanical constant of the motor, including load, and also on the applied motor current. In this state, the motor current (torque) is controlled by the PI controller. 3.2.2 Startup In the startup state, the motor commutations are controlled in an open-loop mode without any rotor position feedback. The open-loop start is required only until the shaft speed is high enough (approximately 3-10% of nominal motor speed) to produce an identifiable Back-EMF voltage. The open-loop commutation periods are calculated based on required startup acceleration parameter and number of startup commutations is predefined constant. 3.2.3 Run The block diagram of run state is represented in Figure 9. It includes the BEMF acquisition with Back- EMF integration to control the commutations. The motor speed is based on commutation time periods. time s p e e d Desired Speed Alignment Period Start speed in sensorless close loop Run Open Loop Start Real speed Open Loop: Commutation time calculated base on acceleration equation Alignment Closed Loop: Commutation time calculated using Back- EMF voltage integration Start-up, forced open loop commutations Close loop, commutations based on Back-EMF voltage processing Required speed
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 9
Speed PI controller process the difference between the demanded and estimated speeds. The output of the speed PI controller is proportional to the voltage to be applied to the BLDC motor. The motor current is measured and filtered in fast loop ISR and is used as feedback into the current controller. The output of the current PI controller limits the output of the speed PI controller. The limitation of speed PI controller output protects the motor current from exceeding maximal allowed motor current.
Figure 9. Speed control with current limitation 4 Software design This section provides the overview of application architecture. In addition, it explains the bridge between hardware independent and hardware dependent portions of code. 4.1 Application software main process The whole BLDC motor control application is designed to run in the Interrupt Service Routines only. In the main routine, there is only initialization of the microcontroller, application initialization, and an endless loop with a FreeMASTER poll function to control and monitor the application. Application uses three interrupts with different timing needs: Fast Control loop ISR: executed at every PWM period Slow Control loop ISR: executed with 1 ms period Time Event ISR: executed according to the current application needs The application data flow diagram with the main processes is shown in Figure 10. The interrupt service routines are described in detail in the Application details section. 3-phase Inverter 3-phase BLDC Motor Measured Values Ramp PI Required DC Bus Current Limit Required Speed + + Commutation Control - - Position Recognition & Revolution Period 1/T Volt age L i m i t Act ual Speed Act ual DC Bus Current PI
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 10 Freescale Semiconductor, Inc.
Figure 10. Application main process overview
Fast control loop ISR and time event ISR uses the same interrupt priority level; Slow control loop ISR uses the lower priority level then previous two ISRs. Application State machine function is executed in Fast and Time event interrupt service routines. 4.2 I/O values processing This application uses the concept of a separate MCU specific part of code and hardware independent BLDC motor control algorithm. The hardware related code reads the input values from the peripheral modules (timer counter, ADC result, and others), scales them to the fractional number representation if needed and stores as the input variables; these input variables are then processed in the hardware independent BLDC motor control algorithm part of software. Similarly, the BLDC algorithm output variables are processed and updated into the respective peripheral modules in hardware related code. If there is a need to update output hardware parameter the corresponding request flag is set in the variable uw16HwUpdateRequests during the application control algorithm execution. Function UpdateBldcHwModules() is used to check the status of request flags and perform the appropriate hardware modules update. The following figure illustrates the overview of application I/O data flow: main() (background loop) Peripherals initialization Application initialization FreeMASTER poll Endless loop Fast control loop ISR (every PWM cycle) Slow control loop ISR (1 ms period) Time event ISR (predefined period)
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 11
Figure 11. Application input/output data flow overview 4.2.1 Input application values Input application variables are updated at the beginning of respective interrupt service routines: uw16TimeCurrent actual time value. If timer is used for timing purpose, actual timer counter register value is stored in this variable. uw16TimeCurrentEvent time of last Time Event. If timer is used for timing purpose, timer value register is stored in this variable. f16DcBusCurrent DC bus current value in fractional number representation is stored in this 16- bit variable. -1 means the maximal negative DC Bus current; +1 means maximal positive DC Bus current. f16DcBusCurrentOffset DC bus current offset value in fractional number representation is stored in this 16-bit variable. Value is measured during the calibration state after the application initialization. f16DcbVoltage DC bus voltage value in fractional number representation is stored in this 16-bit variable. 0 means 0 Volts, +1 means maximal measurable DC bus voltage. f16PhaseVoltage phase voltage value in fractional number representation is stored in this 16- bit variable. 0 means 0 Volts, +1 means maximal measurable DC bus voltage. Analog Module PWM Module Dri ver S/W H/W & device specific S/W part Trigger Unit Dri ver S/W Device independent S/W part GPIO, Ti mer, Dri ver S/W Sensorless BLDC motor control algorithm Anal og Measurements Commutati on vector Ti mer val ues PWM duty cycl e I / O
V a r i a b l e s
&
C o n t r o l
F l a g s GPIO, uw16TimeCurrent uw16TimeCurrentEvent UPDATE_BRAKE_RESISTOR_REQ UPDATE_PWM_DISABLE_REQ UPDATE_PWM_CONFIG_REQ w16CmtSector UPDATE_PWM_ALIGNMENT_REQ UPDATE_DUTYCYCLE_REQ f16DutyCycl e UPDATE_TIME_EVENT_REQ uw16TimeNextEvent f16DcBusCurrent f16PhaseVoltage f16DcbVoltage f16PhaseBemf
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 12 Freescale Semiconductor, Inc. f16PhaseBemf calculated Back-EMF voltage value in fractional number representation is stored in this 16-bit variable. The following equation is used to calculate the value (f16DcbVoltage / 2 is considered as a middle point of the motor windings): f16PhaseBemf = f16PhaseVoltage f16DcbVoltage 2
4.2.2 Output application values and flags There can be pending request for hardware output update after the execution of BLDC motor control algorithm. For this purpose UpdateBldcHwModules() function is executed. This function checks all the request flags stored in uw16HwUpdateRequests variable and do the appropriate action based on hardware configuration: UPDATE_TIME_EVENT_REQ request to set new time of Time Event. In variable uw16TimeNextEvent there is stored new time to be set. If timer is used for timing purpose, timer value register is updated with the value of uw16TimeNextEvent. UPDATE_PWM_DISABLE_REQ request to disable all PWM outputs. Powerstage outputs have to be switched off. UPDATE_DUTYCYCLE_REQ request to update duty cycle generated with PWM module. In variable f16DutyCycle there is stored new value of duty cycle in the fractional number representation. Fractional number value in range <-1; +1) is then processed and scaled in appropriate PWM driver function to fit the actual PWM module configuration. UPDATE_PWM_CONFIG_REQ request to update PWM outputs to set the new commutation vector stored in variable w16CmtSector. Integer value in w16CmtSector is in range from 0 to 5. UPDATE_PWM_ALIGNMENT_REQ request to apply alignment vector on the PWM outputs. Alignment vector can be applied using special case of PWM module configuration. UPDATE_BRAKE_RESISTOR_ON_REQ if this bit is set then output MOSFET for brake resistor control is turned on; if it is cleared the output MOSFET for brake resistor control is turned off. 5 Application details This chapter describes the detail use of application state machine and its states, slow and fast control loop interrupt service routines, time event ISR, and its respective functionality based on actual state of state machine. 5.1 Application state machine function Global application state and BLDC motor state are controlled using two state machine functions: Main application state machine Motors substate machine 5.1.1 Main application state machine Main application state machine function globally controls the whole application. It serves as a global application switch including the fault state processing.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 13
It consists of four state machine functions and seven transition functions between the states. State functions are as follows: Fault Init Stop Run Following are the transition functions between the states: Init to Stop Stop to Run Run to Stop Fault to Init Init to Fault Stop to Fault Run to Fault After a reset an Init state function is executed. After successful application initialization execution continues to the Stop and then to Run state. In normal operation (if any fault is not detected), application operates in Stop and Run states only according to the main application switch value (variable SwitchAppOnOff ). In Run state the respective motors substate machine is called. If any fault is detected during the operation application enters the Fault state, the flow of main application state machine diagram is represented in Figure 12.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 14 Freescale Semiconductor, Inc.
Figure 12. Application state machine diagram 5.1.1.1 Init state Application enters Init state from a reset or Fault state. In this state all the variables are loaded with the default values, including PI regulators, Speed Ramp, and Filter parameters. After that application switches to the Stop state using TransInitStop() transition function. 5.1.1.2 Stop state Application enters Stop state from an Init state, or from the Run state. If main application switch (SwitchAppOnOff) is nonzero then application switches to the Run state using TransStopRun() transition function. This transition function prepares application for calibration substate, which is the first substate entered after changing main state from Stop to Run state. 5.1.1.3 Run state Application enters Run state from a Stop state if the main application switch (SwitchAppOnOff) is nonzero. In Run state, motors sub state machine is called, covering all control states required by used motor. If main application switch is set to zero then application switches to the Stop state using TransRunStop() transient function. This transition function checks the actual motors substate, disables PWM output generation and enters the Freewheel substate if motor is spinning, and after the freewheel period it acknowledges system can continue to the Stop state. INIT FAULT RUN STOP RESET SM_CTRL_INIT_DONE SM_CTRL_FAULT SM_CTRL_FAULT SM_CTRL_START SM_CTRL_STOP SM_CTRL_FAULT_CLEAR Transition Init ->Stop Transition Stop ->Run SM_CTRL_RUN_ACK Transition Run ->Stop SM_CTRL_STOP_ACK Transition Run ->Fault SM_CTRL_FAULT Transition Stop ->Fault Transition Init ->Fault Transition Fault ->Init
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 15
5.1.1.4 Fault state Application enters Fault state when system faced a fault condition. If any fault is detected one of three transition functions is called (TransInitStop (), TransStopFault (), and TransRunFault()). All these three transition functions disable the global application switch (SwitchAppOnOff); on top of that TransRunFault() transient function disables PWM output generation, disables brake resistor, and enters the Freewheel motors substate. State machine remains in Fault state until all detected faults are cleared by user or software. If Fault state was entered from the Run state with motor spinning, state machine remains in Fault state at least for freewheel period (until uw16FreewheelPeriodCurrentMs variable value becomes zero). After all faults are cleared application switches to Init state using TransFaultInit() transient function. 5.1.2 Motors substate machine Motors Substate Machine function controls the actual motor state. Motors substate machine function is called in the main application state machine in Run state. If more than one motor is driven by application, each motor uses its own application substate machine. Substate machine consists of six states: Calibration Ready Alignment Startup Spin Freewheel When main state machine enters the Run state then Calibration substate is entered as first. After calibration, substate machine enters Ready state and remain in one of five states - Ready, Alignment, Startup, Spin, and Freewheel according to the required speed or actual motor speed values. See motors substate machine diagram on Figure 13.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 16 Freescale Semiconductor, Inc.
Figure 13. Motors substate machine diagram 5.1.2.1 Calibration state Application enters Calibration substate state when main state machine switches from Stop state to Run state. Calibration state is used for the zero DC Bus current offset calibration. During the calibration, DC Bus current is measured (and accumulated to f32DcBusCurrentAcc variable) every period defined in microseconds using CALIB_ONE_MEASURE_PERIOD_US constant; this constant should be at least one PWM period long. Number of calibration measurements is defined with CALIB_MEASURES_CNT constant which is stored in uw16CalibCounter variable at the beginning. After all the measurements are done (variable uw16CalibCounter becomes zero), DC Bus current offset is calculated from accumulated value and stored to f16DcBusCurrentOffset variable. Then Ready substate is entered. 5.1.2.2 Ready state Application enters Ready state from Calibration, Alignment, or Freewheel state. If required speed (f16SpeedRequired) is below the minimal allowed speed (f16SpeedMinimal) application substate remains in Ready state. If required speed is higher application is going to be switched to Alignment state. According to the required speed value (if positive or negative) direction of rotation (uw16Direction) is set and is used further in commutation function. Alignment duration variable uw16AlignmentPeriodMs is loaded with predefined constant ALIGN_DURATION. Current PI controller output limitation parameters are configured with alignment output parameters (CURRENT_CONTROLLER_ALIGN_LIM_HIGH, CURRENT_CONTROLLER_ALIGN_LIM_LOW) and Alignment vector (output PWM) is being applied. Then application substate is switched to Alignment state. CALIB READY ALIGN FREEWHEEL SPIN STARTUP Stop ->Run Alignment time passed Startup ok Calibration time elapsed required speed min speed < required speed min speed < Freewheel period elapsed required speed min speed > actual speed min speed <
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 17
5.1.2.3 Alignment state Application enters Alignment state from Ready state. During the Alignment state, if required speed (f16SpeedRequired) is below the minimal allowed speed (f16SpeedMinimal) application disables PWM output and returns back to the Ready substate. After Alignment period passes (uw16AlignmentPeriodMs becomes zero) application is going to switch to Startup state. First commutation period (defined with STARTUP_CMT_PER constant) is set to uw16PeriodCmtNext variable, number of open-loop commutations STARTUP_CMT_CNT is stored to uw16StartCmtCounter variable. First commutation vector w16CmtSector is set according to the actual direction of rotation. Then application substate is switched to Startup state. 5.1.2.4 Startup state Application enters Startup state from Alignment state. During the Startup state, if required speed (f16SpeedRequired) is below the minimal allowed speed (f16SpeedMinimal) application disables PWM output and enters Freewheel substate with period stored in uw16FreewheelPeriodShortMs variable. When all open-loop commutations were performed (uw16StartCmtCounter becomes zero) application is going to switch to Spin state. T OFF period is calculated from the last commutation period (uw16PeriodCmtNext) using fractional multiplication by constant CMT_T_OFF (which is fractional number less than 1) and result is stored to uw16PeriodToff variable. Last six commutation period variables uw16PeriodCmt [x] are loaded with current commutation period uw16PeriodCmtNext. Current PI controller is configured for Spin parameters and speed ramp function is synchronized to the actual motor speed (target startup speed N_START_TRH). Then application substate is switched to Spin state. 5.1.2.5 Spin state Application enters Spin state from Startup state. Application remains in Spin state until the actual motor speed is higher than minimal allowed speed. This check is done in Slow Control Loop ISR where actual motor speed is evaluated. If motor speed is below minimal speed application substate is switched to Freewheel state. 5.1.2.6 Freewheel state Application enters Freewheel state from Spin or Startup state. Remaining Freewheel period is defined in uw16FreewheelPeriodCurrentMs variable and when it is zero application substate is switched to Ready state. 5.2 Slow control loop ISR Slow control loop ISR is executed at every 1 ms and is used for speed and torque evaluation and control. Slow control loop ISR uses lower priority level in comparison with other ISRs used in the application, therefore, it can be used for the functions that consumes more time to run, for example, MC33927_readStatus()function can be used to read actual status of power stage driver over SPI. Overview of slow control loop ISR is represented in Figure 14.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 18 Freescale Semiconductor, Inc.
Figure 14. Slow control loop ISR 5.2.1 Slow control loop function In the slow control loop ISR there can be any part of code that is touching the hardware (in our case it is function to read power stage status), and there is a slow control loop function which is hardware independent. This function is hardware independent; it uses only the input and output variables and flags to communicate with the outer world. The detailed diagram of slow control loop function is represented in Figure 15. This function is only used in Spin and Alignment states. Read power stage status (MC33937 over SPI) Slow control loop ISR (1 ms period) Return Slow control loop function
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 19
Figure 15. Slow control loop function 5.2.1.1 Spin state In Spin state, actual motor speed (f16SpeedMeasured) is calculated using the last six commutation periods (uw16PeriodCmt[n]). Library GFLIB_DivSat function is used for fractional division. The result cannot overflow because of the saturation feature used in this function. In case, the actual speed is lower than minimal allowed speed (f16SpeedMinimal), then the application enters the Freewheel state. The speed ramp output (f16SpeedRampOutput) is calculated using the library GFLIB_Ramp function. Note that 32-bit implementation of ramp function is used to provide the better speed step resolution in 1 ms call period, therefore 32-bit to 16-bit and opposite library conversion functions are used Slow control loop Return Calculate actual speed StateRun = SPIN? StateRun = ALIGN? actual speed < minimal speed Disable PWM StateRun =FREEWHEEL Return Speed Ramp Speed PI Controller Current PI Controller CurrentPiOutput > SpeedPiOutput DutyCycle =SpeedPiOutput DutyCycle =CurrentPiOutput Current PI Controller DutyCycle =CurrentPiOutput + - + - + - + - Current limiting Speed control
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 20 Freescale Semiconductor, Inc. (MLIB_ConvertPU_F32F16 and MLIB_ConvertPU_F16F32). According to the actual direction of rotation the sign of f16SpeedMeasured variable is set and input to the speed PI controller (f16SpeedPiErr) is calculated. Then speed PI controller and current PI controller are calculated using library GFLIB_ControllerPIpAW function. If speed PI controller output is higher than the output of current PI controller then current limiting is applied to the output, it means the current PI controller output is used instead of the speed PI controller output. This will reduce the speed of motor but the phase current do not exceeds the limit value in f16DcBusCurrentLimit variable. 5.2.1.2 Alignment state In Alignment state, slow control loop function controls the phase (which is equal to DC Bus) current to required alignment current value stored in f16DcBusCurrentAlign variable. From this value and the actual DC Bus current value (f16DcBusCurrentNoFiltered) the input to the current PI controller is calculated; then the output duty cycle (f16DutyCycle) is calculated using PI controller. 5.3 Fast control loop ISR Fast control loop ISR is executed at every PWM cycle and is used for a rotor position recognition and commutation. Fast control loop ISR uses the same priority level as of time event ISR (so it should not be interrupted by time event ISR) but it is higher than the slow control loop ISR. Overview of fast control loop ISR is represented in the following figure:
Figure 16. Fast control loop ISR Inside the fast control loop ISR there is a portion of the code that is connecting with the hardware, and hardware independent fast control loop function. At the beginning of ISR the actual time (timer counter value) is stored in the uw16TimeCurrent variable. Then results of analog measurements are scaled in 16- bit fractional number representation and stored to the following variables: Read and scale: DC Bus voltage DC Bus current phase voltage Read actual time Fast control loop ISR (every PWM cycle) Filter DC Bus current Calculate back-EMF voltage Call fast control Loop function Configure next analogue measurements Check Fault states Call State machine function Update peripheral modules setting based on requests Return
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 21
f16DcBusCurrent f16PhaseVoltage f16DcbVoltage DC Bus current is filtered using library GDFLIB_FilterMA function (Moving average filter) and stored in f16DcBusCurrent variable. Phase Back-EMF voltage f16PhaseBemf is calculated using phase voltage and DC Bus voltage values. Then fast control loop function is called (is described in Fast control loop function). Then the analog measurement configuration for next PWM cycle module is set according to the updated BLDC sector. Faults are tested in ReadSwFaults()function, this function checks the measured analog values for DC bus overcurrent, overvoltage, and undervoltage limits; respective fault is stored in the global fault mintM1_Fault variable. Then application state machine SM_StateMachine() function , is called (for more details, see Main application state machine). At the end, hardware modules are updated in UpdateBldcHwModules() function (for more details, see I/O values processing). 5.3.1 Fast control loop function The main function in fast control loop ISR is fast control loop function. This function is hardware independent; it uses only input and output variables and flags to communicate with the outer world. Detailed diagram of a slow control loop function is shown in Figure 17. This function is used in Spin state only.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 22 Freescale Semiconductor, Inc.
Figure 17. Fast control loop function In the beginning of a slow control loop function the actual motors substate is tested; if actual state is different than Spin state then the function exits. Brake resistor is updated according to the actual DC bus voltage stored in f16DcbVoltage variable and limit value defined with U_DCB_TRIP constant. T OFF
period is compared with a period of last commutation, which is calculated from actual time (uw16TimeCurrent) and time of last commutation (uw16TimeOfCommutation). Exit if the T OFF period was not elapsed. Then according to the actual BLDC sector applied and direction of rotation, the Back- EMV voltage is inverted in f16PhaseBemf variable; from this point the Back-EMF voltage in f16PhaseBemf is always raising from negative values to positive. Afterwards, the simple zero-cross detection condition is used and the Back-EMF voltage is integrated to f32IntegralBemfSumvariable if it - + Fast control loop Return StateRun = SPIN? - Return Process Brake resistor + T OFF period elapsed? Return + - Falling back-EMF? invert back-EMF voltage + - back-EMF >0 Back-EMF voltage Integration + - back-EMF integral > threshold Disable PWM StateRun =FREEWHEEL + - CMT period <minimal Return Commutation - Save CMT time, periods - Perform commutation - Calculate T OFF period - Set next timed safety commutation
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 23
is positive (after the Back-EMF zero-cross). If integrated value of Back-EMF voltage, stored in f32IntegralBemfSumvariable, is greater than the threshold value in f32IntegralBemfThreshold then it is time to perform a commutation. Actual commutation period is calculated using the actual time stored in uw16TimeCurrent and previous commutation time in uw16TimeOfCommutation, is compared to the half of the minimal commutation period defined with CMT_PER_MIN constant; this constant value is calculated from the maximal motors speed, number of motors pole-pairs, and timer frequency. Application enters the Freewheel state in case the commutation period is going to be too short. Actual commutation time variable uw16TimeOfCommutation is updated, commutation period variable is uw16PeriodCmt[n] is calculated from the actual and previous commutation times and commutation is performed calling BldcCommutation() function. BldcCommutation() function simply increases or decreases the actual commutation sector stored in w16CmtSector variable according to the actual direction of rotation in uw16Direction; PWM output is then updated in function UpdateBldcHwModules() (for more details, see I/O values processing). Then new value of T OFF period is calculated from the last commutation period using fractional multiplication by constant CMT_T_OFF (which is a fractional number less than 1) and result is stored to uw16PeriodToff variable. Integrated value of Back-EMF voltage (f32IntegralBemfSum) is cleared, and it is marked that application just commutated using sensorless algorithm - variable uw16CommutatedSensorless is set to one; this is useful for case the Time event ISR is going to be executed and performing the safety commutation (will be described later). Safety commutation (uw16TimeNextEvent) is set to double of last commutation period. 5.4 Time event ISR Time event ISR is used to fulfill the following purposes: Individual application states timing Open-loop commutations timing in Startup state Safety commutation in Spin state If sensorless motor control algorithm fails in rotor position recognition then the time event ISR protects motor from overcurrent. Overview of time event ISR is represented in Figure 18.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 24 Freescale Semiconductor, Inc.
Figure 18. Time event ISR At the beginning of time event ISR, the actual time and event time are stored in uw16TimeCurrent and uw16TimeCurrentEvent variables. The value of uw16TimeCurrent is saved by the ISR software at the beginning of this ISR and the value of uw16TimeCurrentEvent variable is saved when the ISR is originated by MCU hardware. For precise timing uw16TimeCurrentEvent variable is considered as the right time value. After that the Time event function is called (will be described in next chapter). Then application state machine function SM_StateMachine() is called (for more details, see Main application state machine). At the end hardware modules are updated in UpdateBldcHwModules() function (for more details, see I/O values processing). 5.4.1 Time event function Time event function has a different functionality based on the actual motors substate machine state. It provides simple timing to Alignment, Calibration, and Frewheel states. In Startup state, the time event function is used for the open-loop commutations timing, where each following commutation period is calculated from the previous commutation period. In Spin state, if a sensorless motor control algorithm cannot recognize rotors position (for any reason) then the time event function serves for the safety commutations. The detailed diagram of time event function is represented in Figure 19 and Figure 20.
Read actual time Time event ISR (predefined period) Time event function Update peripheral modules setting based on requests Call State machine function Return
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 25
Figure 19. Time event function (Spin and Startup states)
- + + - Time event function StateRun = SPIN? - StateRun = STARTUP? Perform Commutation Decrement Start CMT counter + - Start CMT counter >0? Set next safety commutation period Set next Start commutation period Return Still start-up + - Commutated sensorless? Return + CmtErrorCnt <max Disable PWM StateRun =FREEWHEEL Return Increment CmtErrorCnt Commutation - Save CMT time, periods - Perform commutation - Calculate T OFF period - Set next timed safety commutation Return continue
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 26 Freescale Semiconductor, Inc. 5.4.1.1 Spin state In Spin state, if a sensorless BLDC motor control algorithm is working fine, the Time event ISR will never be executed, because the commutations are performed in a fast control loop function executed in fast control loop ISR. But in case the sensorless BLDC motor control algorithm cannot recognize rotors position correctly, the time event ISR is used for the safety commutation of the motor. Period to the next (safety) commutation is set to double of last commutation period. In case of normal operation the sensorless algorithm should recognize a correct rotor's position and commutate normally within this period (before this safety commutation is executed), in this case the safety commutation is canceled and again postponed to double of last commutation period. In case the sensorless algorithm cannot recognize rotors position correctly, the time event ISR is used for the safety commutation of the motor. Safety commutations limit the overcurrents in a motor if sensorless algorithm fails. At the beginning of time event function uw16CommutatedSensorless variable is checked, if it is set then the commutation was performed in fast control loop just before the time event ISR was originated, in that case function exits because the sensorless motor control algorithm works still fine, even it was commutated just at the end of the window established for the sensorless commutations. Otherwise the safety commutation is going to be performed. Every time the safety commutation is performed uw16CmtErrorCnt variable is increased by 3, conversely when motor is commutated in fast control loop using sensorless algorithm uw16CmtErrorCnt is decreased. If uw16CmtErrorCnt is higher than maximal allowed number of commutation fails application enters Freewheel start. If number of errors is less than limit then application continues same way as in fast control loop. Actual commutation time variable uw16TimeOfCommutation is updated, commutation period variable is uw16PeriodCmt[n] is calculated from the actual and previous commutation times and commutation is performed calling BldcCommutation() function. New value of T OFF period uw16PeriodToff is calculated from the last commutation period. Integrated value of Back-EMF voltage (f32IntegralBemfSum) is cleared and next safety commutation (uw16TimeNextEvent) is set to double of last commutation period. 5.4.1.2 Startup state In Startup state the time event function is used for the open-loop commutations timing. The commutation is performed at the beginning of the time event function in Startup state. Then number of open-loop commutations is decreased and checked (uw16StartCmtCounter). If it is still higher than zero the next commutation period is calculated from the previous commutation period multiplied with an acceleration coefficient, f16McatStartCmtAcceleration. Acceleration coefficient is a fractional constant number (number a little bit less than 1), and defines the ratio between lengths of two consequential commutation periods. MCLIB_MUL library function is used for fractional multiplication. Acceleration coefficient is calculated from the first commutation period after an alignment and the target Startup speed. When all Startup (open-loop) commutations were performed, application will switch to the Spin state (sensorless close-loop motor control); period to the next (safety) commutation is set to double of last Startup commutation period.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 27
Figure 20. Time event function (Alignment, Calibration, and Freewheel states) + + - StateRun = ALIGN? - Alignment period >0? Decrement Alignment period Set period to next Time Event (1 ms) Return + + - StateRun = CALIB? - Calibration counter >0? Decrement Calibration counter Accumulate DC Bus current Set period to next Time Event Return + + - StateRun = FREEWHEEL? - Freewheel period >0? Decrement Freewheel period Set period to next Time Event (1 ms) Return Return continued
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 28 Freescale Semiconductor, Inc. 5.4.1.3 Alignment state In Alignment state, the time event function is used for timing purpose. uw16AlignmentPeriodMs variable is decreased every 1 ms down to zero. It is then tested for zero in Alignment state of motors substate machine. 5.4.1.4 Calibration state In Calibration state, the time event function is used for timing purpose. Variable uw16CalibCounter is decreased every period defined by CALIB_ONE_MEASURE_PERIOD_US constant in microseconds (this constant should be at least one PWM period long). During the calibration, DC Bus current is measured and accumulated to f32DcBusCurrentAcc variable. After all the measurements are done (variable uw16CalibCounter becomes zero), then the DC Bus current offset is calculated from accumulated value and stored to f16DcBusCurrentOffset variable in Calibration state of motors substate machine. 5.4.1.5 Freewheel state In Freewheel state, the time event function is used for timing purpose. uw16FreewheelPeriodCurrentMs variable is decreased every 1 ms down to zero. It is then tested for zero in Freewheel state of motors substate machine. 6 Acronyms and abbreviations Table 1. Acronyms Term Meaning ADC Analog-to-digital converter. BEMF Back-electromotive force. BLDC Brushless DC motor. CPU Central processing unit. DC Direct current. DRM Design reference manual. I/O Input/output interfaces between a computer system and the external world a CPU reads an input to sense the level of an external signal and writes to an output to change the level of an external signal. ISR Interrupt Service Routine. MCU Microcontroller. PI controller Proportional-integral controller. PWM Pulse-width modulation. SPI Serial peripheral interface module.
Three-Phase BLDC Sensorless Motor Control Application, Rev. 0, 02/2014 Freescale Semiconductor, Inc. 29
7 References Following references are available on freescale.com:
2. Set of General Math and Motor Control Functions for ARM Cortex M0+ Core, CM0MCLUG, by Freescale Semiconductor, Inc., 2013.
3. 3-Phase BLDC Motor Sensorless Control using MC9S08AW60, DRM086, by Freescale Semiconductor, Inc., 2005.
4. 3-Phase BLDC Motor Sensorless Control using MC9S08MP16, DRM117, by Freescale Semiconductor, Inc., 2009.
8 Revision history Table 2. Revision history Revision number Date Substantial changes 0 02/2014 Initial release
How to Reach Us: Home Page: freescale.com Web Support: freescale.com/support
Information in this document is provided solely to enable system and software implementers to use Freescale products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits based on the information in this document. Freescale reserves the right to make changes without further notice to any products herein. Freescale makes no warranty, representation, or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. Typicalparameters that may be provided in Freescale data sheets and/or specifications can and do vary in different applications, and actual performance may vary over time. All operating parameters, including typicals, must be validated for each customer application by customers technical experts. Freescale does not convey any license under its patent rights nor the rights of others. Freescale sells products pursuant to standard terms and conditions of sale, which can be found at the following address: freescale.com/SalesTermsandConditions.
Freescale, and the Freescale logo are trademarks of Freescale Semiconductor, Inc., Reg. U.S. Pat. & Tm. Off. All other product or service names are the property of their respective owners. 2014 Freescale Semiconductor, Inc.