brazos-trapezoidal
Macros | Enumerations | Functions | Variables
TrapSless.c File Reference

Implementation of trapezoidal sensorless control for wye connected BLDC motors. More...

#include "TrapSless.h"
#include "includes.h"
Include dependency graph for TrapSless.c:

Go to the source code of this file.

Macros

#define PATTERN_BEMF_U_MASK   (0x04)
 
#define PATTERN_BEMF_V_MASK   (0x02)
 
#define PATTERN_BEMF_W_MASK   (0x01)
 

Enumerations

enum  UVW_STATE_TYPE {
  UVW_STATE_HI_DL_FL = 0 , UVW_STATE_DH_LO_FL , UVW_STATE_DH_FL_LO , UVW_STATE_HI_FL_DL ,
  UVW_STATE_FL_HI_DL , UVW_STATE_FL_DH_LO , UVW_STATE_LO_DH_FL , UVW_STATE_DL_HI_FL ,
  UVW_STATE_DL_FL_HI , UVW_STATE_LO_FL_DH , UVW_STATE_FL_LO_DH , UVW_STATE_FL_DL_HI ,
  UVW_STATE_FL_FL_FL , UVW_STATE_LO_LO_LO
}
 

Functions

void outputPwmExec (void)
 Update the pwm outputs. More...
 
void AvgOffset ()
 
void TrapSless__Init (void)
 Initialization of TrapSless module. More...
 
uint8_t TrapSless__GetTooEarly (void)
 Too early detection mechanism For every commutation there is an expected zcross at a given phase. This algorithm will check if the event has already passed and is missed. More...
 
uint8_t TrapSless__GetCmtToNum (uint8_t cmt, uint8_t dir)
 Returns the index of the given commutation pattern For CW rotation the index matches the commutation pattern. For CCW rotation the index does not match the commutation pattern. More...
 
uint8_t TrapSless__GetBemfToNum (uint8_t bemf, uint8_t after_zc, uint8_t dir)
 Returns the index of the given bemf pattern. More...
 
uint8_t TrapSless__GetCmtFromBemf (uint8_t dir, uint8_t bemf, uint8_t after_zc)
 Returns the commutation pattern given a bemf pattern. More...
 
uint8_t TrapSless__GetBemfFromCmt (uint8_t dir, uint8_t CmtNum, uint8_t after_zc)
 Returns the bemf pattern given a commutation index. More...
 
void TrapSless__Handler ()
 Handler for Trapezoidal control - 16KHz call rate. More...
 
void TrapSless__SlowHandler ()
 Handler for Trapezoidal control - 1KHz call rate. More...
 
uint8_t TrapSless__GetBemfSignal (int32_t va, int32_t vb, int32_t vc)
 Returns the bemf pattern based on pole voltage reading [a,b,c]. More...
 
int32_t TrapSless__CalcSpeed ()
 Speed computation based on Bemf Zcross events. More...
 
void TrapSless__ResetVars ()
 Resetting of trapezoidal key variables. More...
 
void TrapSless__SetSpd (int32_t spd_rpm, int32_t ramp_rpm)
 BLDC motor rotation command. More...
 
void TrapSless__SetPatternAndPwm (uint8_t after_zc)
 Set pwm duty cycle and output pattern. More...
 
void TrapSless__SetPwm (void)
 Set pwm duty cycle. More...
 
void TrapSless__PiCalc (int32_t curr_clamp)
 Pi regulator If curr_clamp is different than zero, then only runs the clamp algorithm If curr_clamp is zero, then execute regular pi. More...
 
void TrapSless__SetVbusIbus (void)
 Read ADC and Set Vbus and Ibus variables. More...
 
void TrapSless__SetVabc (void)
 Read ADC and set pole voltage variables. More...
 
void TrapSless__BemfCnterUpdate (void)
 Accumulate the counter for bemf. More...
 
TRAPSLESS_PATTERN_TYPE * TrapSless__GetVars (void)
 Obtaine the trapezoidal variables. More...
 
void TrapSless__ExecuteCmt (void)
 Execution of commutation. More...
 
void decodeOutput (UVW_STATE_TYPE state)
 Set the outputs according to the pole voltage state. More...
 

Variables

TRAPSLESS_PATTERN_TYPE TrapSlessData
 
volatile uint8_t AUTO_COMM_CNTER = 255
 
volatile uint8_t AUTO_COMM_CNTER_BREAK = 255
 
const uint8_t PortStateCW [8][2]
 
const uint8_t PortStateCCW [8][2]
 
const uint8_t TrapSlessBemfSeqCW [][2] = {{5,4},{4,6},{6,2},{2,3},{3,1},{1,5}}
 
const uint8_t TrapSlessBemfSeqCCW [][2] = {{4,5},{5,1},{1,3},{3,2},{2,6},{6,4}}
 
const uint8_t TrapSlessCmtSeqCW [] = {0,1,2,3,4,5}
 
const uint8_t TrapSlessNxtCmtSeqCW [] = {1,2,3,4,5,0}
 
const uint8_t TrapSlessCmtSeqCCW [] = {0,5,4,3,2,1}
 
const uint8_t TrapSlessNxtCmtSeqCCW [] = {5,4,3,2,1,0}
 
uint16_t OffsetTimeout = 2500
 
mc_t TrapDutyMaxCnts
 
uint32_t PwmHalfMaxCnts
 
uint32_t PwmMaxMinusDtCnts
 
mc_t ParamPwmFreqKHz
 
mc_t ParamDegZcToCmt
 
mc_t ParamMinPreCarrierCnt
 
mc_t ParamMaxPreCarrierCnt
 
int64_t ParamSpdCalcNum
 
int32_t CurrClampValue
 
uint32_t ParamOpenLoopSpdBemfCnt
 
uint32_t ParamMaxSpdBemfCnt
 
int32_t OutMaxVbus
 
uint32_t ParamOpenLoopSpdBemfCntVar
 

Detailed Description

Implementation of trapezoidal sensorless control for wye connected BLDC motors.

This control does not fit to delta type motors.

Version
1.0
Author
pegorf Mar 21, 2022

Definition in file TrapSless.c.

Macro Definition Documentation

◆ PATTERN_BEMF_U_MASK

#define PATTERN_BEMF_U_MASK   (0x04)

< Back-emf reading pattern Mask for U phase bemf signal build

Definition at line 44 of file TrapSless.c.

◆ PATTERN_BEMF_V_MASK

#define PATTERN_BEMF_V_MASK   (0x02)

Mask for V phase bemf signal build

Definition at line 45 of file TrapSless.c.

◆ PATTERN_BEMF_W_MASK

#define PATTERN_BEMF_W_MASK   (0x01)

Mask for W phase bemf signal build

Definition at line 46 of file TrapSless.c.

Enumeration Type Documentation

◆ UVW_STATE_TYPE

Definitions of switches states (12 active states and 2 non active states)
Defines the switches states: Float (FL), Low (LO), Duty at Low (DL), Duty at high (DH) and High (HI).

Enumerator
UVW_STATE_HI_DL_FL 

0

UVW_STATE_DH_LO_FL 

1

UVW_STATE_DH_FL_LO 

2

UVW_STATE_HI_FL_DL 

3

UVW_STATE_FL_HI_DL 

4

UVW_STATE_FL_DH_LO 

5

UVW_STATE_LO_DH_FL 

6

UVW_STATE_DL_HI_FL 

7

UVW_STATE_DL_FL_HI 

8

UVW_STATE_LO_FL_DH 

9

UVW_STATE_FL_LO_DH 

10

UVW_STATE_FL_DL_HI 

11

UVW_STATE_FL_FL_FL 

12

UVW_STATE_LO_LO_LO 

13

Definition at line 53 of file TrapSless.c.

Function Documentation

◆ AvgOffset()

void AvgOffset ( )
@brief  Average the current offset.

@details

This function is called at INIT state of motor statemachine It is used for sampling of ADC current offsets while inverter switches are off. When motor current is zero the ADC is about the half scale of ADC range

Definition at line 1298 of file TrapSless.c.

Here is the caller graph for this function:

◆ decodeOutput()

void decodeOutput ( UVW_STATE_TYPE  state)

Set the outputs according to the pole voltage state.

Parameters
state- pole uvw state [state].

Definition at line 1209 of file TrapSless.c.

Here is the caller graph for this function:

◆ outputPwmExec()

void outputPwmExec ( void  )
inline

Update the pwm outputs.

Definition at line 1277 of file TrapSless.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrapSless__BemfCnterUpdate()

void TrapSless__BemfCnterUpdate ( void  )

Accumulate the counter for bemf.

Definition at line 1104 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__CalcSpeed()

int32_t TrapSless__CalcSpeed ( void  )

Speed computation based on Bemf Zcross events.

Returns
Speed feebdack [speedRad]

Definition at line 864 of file TrapSless.c.

◆ TrapSless__ExecuteCmt()

void TrapSless__ExecuteCmt ( void  )

Execution of commutation.

Definition at line 1123 of file TrapSless.c.

Here is the call graph for this function:

◆ TrapSless__GetBemfFromCmt()

uint8_t TrapSless__GetBemfFromCmt ( uint8_t  dir,
uint8_t  CmtNum,
uint8_t  after_zc 
)

Returns the bemf pattern given a commutation index.

Parameters
dirdirection
CmtNumCommutation index
after_zcif before (0) or after (1) zero cross .
Returns
Bemf pattern [response]

Definition at line 424 of file TrapSless.c.

◆ TrapSless__GetBemfSignal()

uint8_t TrapSless__GetBemfSignal ( int32_t  va,
int32_t  vb,
int32_t  vc 
)

Returns the bemf pattern based on pole voltage reading [a,b,c].

Parameters
vaPole voltage
vbPole voltage
vcPole voltage
Returns
Bemf pattern [response]

Definition at line 842 of file TrapSless.c.

◆ TrapSless__GetBemfToNum()

uint8_t TrapSless__GetBemfToNum ( uint8_t  bemf,
uint8_t  after_zc,
uint8_t  dir 
)

Returns the index of the given bemf pattern.

Parameters
bemfBemf pattern
after_zcif before (0) or after (1) the zerocross (zc)
dirdirection (CW, CCW)
Returns
Index of the bemf pattern (Num)

Definition at line 296 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__GetCmtFromBemf()

uint8_t TrapSless__GetCmtFromBemf ( uint8_t  dir,
uint8_t  bemf,
uint8_t  after_zc 
)

Returns the commutation pattern given a bemf pattern.

Parameters
dirdirection
bemfBemf pattern
after_zcif before (0) or after (1) zero cross .
Returns
Commutation pattern [response]

Definition at line 410 of file TrapSless.c.

Here is the call graph for this function:

◆ TrapSless__GetCmtToNum()

uint8_t TrapSless__GetCmtToNum ( uint8_t  cmt,
uint8_t  dir 
)

Returns the index of the given commutation pattern For CW rotation the index matches the commutation pattern. For CCW rotation the index does not match the commutation pattern.

Parameters
cmtCommutation.
dirDirection (CW, CCW).
Returns
Index of the commutation (Num)

Definition at line 256 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__GetTooEarly()

uint8_t TrapSless__GetTooEarly ( void  )

Too early detection mechanism For every commutation there is an expected zcross at a given phase. This algorithm will check if the event has already passed and is missed.

Returns
1 in case of too early, 0 otherwise

Definition at line 222 of file TrapSless.c.

◆ TrapSless__GetVars()

TRAPSLESS_PATTERN_TYPE * TrapSless__GetVars ( void  )

Obtaine the trapezoidal variables.

Returns
Pointer for the data structure of trapezoidal control

Definition at line 1116 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__Handler()

void TrapSless__Handler ( void  )

Handler for Trapezoidal control - 16KHz call rate.

< Zero speed for feedback

< Time count for bemf events (for speed calc) is zero

Definition at line 437 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__Init()

void TrapSless__Init ( void  )

Initialization of TrapSless module.

Definition at line 184 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__PiCalc()

void TrapSless__PiCalc ( int32_t  curr_clamp)

Pi regulator If curr_clamp is different than zero, then only runs the clamp algorithm If curr_clamp is zero, then execute regular pi.

Parameters
curr_clampCurrent Clamp value.

Definition at line 993 of file TrapSless.c.

◆ TrapSless__ResetVars()

void TrapSless__ResetVars ( void  )

Resetting of trapezoidal key variables.

Parameters
Trapezoidaldata structure [data].

Definition at line 885 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__SetPatternAndPwm()

void TrapSless__SetPatternAndPwm ( uint8_t  after_zc)

Set pwm duty cycle and output pattern.

Parameters
after_zcif before (0) or after (1) zcross.

Definition at line 924 of file TrapSless.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrapSless__SetPwm()

void TrapSless__SetPwm ( void  )

Set pwm duty cycle.

Definition at line 938 of file TrapSless.c.

Here is the caller graph for this function:

◆ TrapSless__SetSpd()

void TrapSless__SetSpd ( int32_t  spd_rpm,
int32_t  ramp_rpm 
)

BLDC motor rotation command.

Parameters
spd_rpmSpeed in rpm
ramp_rpmSpeed ramp in rpm/s

Definition at line 899 of file TrapSless.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrapSless__SetVabc()

void TrapSless__SetVabc ( void  )

Read ADC and set pole voltage variables.

Definition at line 1087 of file TrapSless.c.

◆ TrapSless__SetVbusIbus()

void TrapSless__SetVbusIbus ( void  )

Read ADC and Set Vbus and Ibus variables.

Definition at line 1052 of file TrapSless.c.

◆ TrapSless__SlowHandler()

void TrapSless__SlowHandler ( void  )

Handler for Trapezoidal control - 1KHz call rate.

Definition at line 694 of file TrapSless.c.

Here is the call graph for this function:

Variable Documentation

◆ AUTO_COMM_CNTER

volatile uint8_t AUTO_COMM_CNTER = 255

Number of commutations until auto commutation is reached.

Definition at line 48 of file TrapSless.c.

◆ AUTO_COMM_CNTER_BREAK

volatile uint8_t AUTO_COMM_CNTER_BREAK = 255

Above this value zero voltage is applied until the counter reaches AUTO_COMM_CNTER

Definition at line 49 of file TrapSless.c.

◆ CurrClampValue

int32_t CurrClampValue

[A] Current at which the control will limit the operation

Definition at line 167 of file TrapSless.c.

◆ OffsetTimeout

uint16_t OffsetTimeout = 2500

2,5 seconds, slot time 1ms, time for sampling the offset level of current reading

Definition at line 158 of file TrapSless.c.

◆ OutMaxVbus

int32_t OutMaxVbus

[V] Hold the maximum voltage the pi controller will output.

Definition at line 170 of file TrapSless.c.

◆ ParamDegZcToCmt

mc_t ParamDegZcToCmt

Electrical angle in [deg] from a zero cross to a commutation instant

Definition at line 163 of file TrapSless.c.

◆ ParamMaxPreCarrierCnt

mc_t ParamMaxPreCarrierCnt

This is the carrier counting expected, for one commutation [60deg], when operating at lowest speed

Definition at line 165 of file TrapSless.c.

◆ ParamMaxSpdBemfCnt

uint32_t ParamMaxSpdBemfCnt

This is the carrier bemf counting expected, for one commutation [60deg], when operating at highest speed

Definition at line 169 of file TrapSless.c.

◆ ParamMinPreCarrierCnt

mc_t ParamMinPreCarrierCnt

This is the carrier counting expected, for one commutation [60deg], when operating at highest speed

Definition at line 164 of file TrapSless.c.

◆ ParamOpenLoopSpdBemfCnt

uint32_t ParamOpenLoopSpdBemfCnt

This is the carrier bemf counting expected, for one commutation [60deg], when operating at lowest speed

Definition at line 168 of file TrapSless.c.

◆ ParamOpenLoopSpdBemfCntVar

uint32_t ParamOpenLoopSpdBemfCntVar

This variable limits the time counts for the commutation instant at startup [counts]

Definition at line 172 of file TrapSless.c.

◆ ParamPwmFreqKHz

mc_t ParamPwmFreqKHz

Hold the pwm frequency in [KHz]

Definition at line 162 of file TrapSless.c.

◆ ParamSpdCalcNum

int64_t ParamSpdCalcNum

[rad.s-1] Constant for speed computation

Definition at line 166 of file TrapSless.c.

◆ PortStateCCW

const uint8_t PortStateCCW[8][2]
Initial value:
@ UVW_STATE_FL_FL_FL
Definition: TrapSless.c:72
@ UVW_STATE_LO_DH_FL
Definition: TrapSless.c:63
@ UVW_STATE_DL_FL_HI
Definition: TrapSless.c:66
@ UVW_STATE_FL_DH_LO
Definition: TrapSless.c:61
@ UVW_STATE_FL_HI_DL
Definition: TrapSless.c:60
@ UVW_STATE_HI_FL_DL
Definition: TrapSless.c:58
@ UVW_STATE_FL_LO_DH
Definition: TrapSless.c:69
@ UVW_STATE_DL_HI_FL
Definition: TrapSless.c:64
@ UVW_STATE_FL_DL_HI
Definition: TrapSless.c:70
@ UVW_STATE_DH_LO_FL
Definition: TrapSless.c:55
@ UVW_STATE_HI_DL_FL
Definition: TrapSless.c:54
@ UVW_STATE_DH_FL_LO
Definition: TrapSless.c:57
@ UVW_STATE_LO_FL_DH
Definition: TrapSless.c:67
@ UVW_STATE_LO_LO_LO
Definition: TrapSless.c:73

Commutation patterns for Clockwise (CCW)
HI is high side active permanently within one commutation.
LO is low side switch active permanently within one commutation.
DH is the high side active with Pwm
DL is the low side active with Pwm
FL is the phase floating

    (before zc) U   V   W   ||  (after zc)  U   V   W

Commutation #0: DH LO FL || HI DL FL (active)
Commutation #1: FL DL HI || FL LO DH (active)
Commutation #2: LO FL DH || DL FL HI (active)
Commutation #3: DL HI FL || LO DH FL (active)
Commutation #4: FL DH LO || FL HI DL (active)
Commutation #5: HI FL DL || DH FL LO (active)
Commutation #6: FL FL FL || FL FL FL (non active)
Commutation #7: LO LO LO || LO LO LO (non active)

Definition at line 121 of file TrapSless.c.

◆ PortStateCW

const uint8_t PortStateCW[8][2]
Initial value:

Commutation patterns for Clockwise (CW)
HI is high side active permanently within one commutation.
LO is low side switch active permanently within one commutation.
DH is the high side active with Pwm
DL is the low side active with Pwm
FL is the phase floating

    (before zc) U   V   W   ||  (after zc)  U   V   W

Commutation #0: HI DL FL || DH LO FL (active)
Commutation #1: DH FL LO || HI FL DL (active)
Commutation #2: FL HI DL || FL DH LO (active)
Commutation #3: LO DH FL || DL HI FK (active)
Commutation #4: DL FL HI || LO FL DH (active)
Commutation #5: FL LO DH || FL DL HI (active)
Commutation #6: FL FL FL || FL FL HI (non active)
Commutation #7: LO LO LO || LO LO LO (non active)

Definition at line 94 of file TrapSless.c.

◆ PwmHalfMaxCnts

uint32_t PwmHalfMaxCnts

Half of maximum pwm count value when at duty cycle max

Definition at line 160 of file TrapSless.c.

◆ PwmMaxMinusDtCnts

uint32_t PwmMaxMinusDtCnts

Maximum pwm count value minus deadtime when at duty cycle max

Definition at line 161 of file TrapSless.c.

◆ TrapDutyMaxCnts

mc_t TrapDutyMaxCnts

Maximum pwm count value when at duty cycle max

Definition at line 159 of file TrapSless.c.

◆ TrapSlessBemfSeqCCW

const uint8_t TrapSlessBemfSeqCCW[][2] = {{4,5},{5,1},{1,3},{3,2},{2,6},{6,4}}

Actual Commutation sequence for clockwise operation - CW

Definition at line 143 of file TrapSless.c.

◆ TrapSlessBemfSeqCW

const uint8_t TrapSlessBemfSeqCW[][2] = {{5,4},{4,6},{6,2},{2,3},{3,1},{1,5}}

< Bemf sequence based on commutation CW 4 6 2 3 1 5 Bemf sequence based on commutation CCW 5 1 3 2 6 4

Definition at line 141 of file TrapSless.c.

◆ TrapSlessCmtSeqCCW

const uint8_t TrapSlessCmtSeqCCW[] = {0,5,4,3,2,1}

Next Commutation sequence for counter clockwise operation - CCW
Based on bemf information

Definition at line 152 of file TrapSless.c.

◆ TrapSlessCmtSeqCW

const uint8_t TrapSlessCmtSeqCW[] = {0,1,2,3,4,5}

Next Commutation sequence for clockwise operation - CW

Definition at line 146 of file TrapSless.c.

◆ TrapSlessData

TRAPSLESS_PATTERN_TYPE TrapSlessData

-----------------------------— Theta Power Systems - CONFIDENTIAL --------------------------------------—

< Struct variable carry all motor control related informatin

Definition at line 21 of file TrapSless.c.

◆ TrapSlessNxtCmtSeqCCW

const uint8_t TrapSlessNxtCmtSeqCCW[] = {5,4,3,2,1,0}

Variables for algorithm handling

Definition at line 155 of file TrapSless.c.

◆ TrapSlessNxtCmtSeqCW

const uint8_t TrapSlessNxtCmtSeqCW[] = {1,2,3,4,5,0}

Actual Commutation sequence for counter clockwise operation - CCW
Based on bemf information

Definition at line 148 of file TrapSless.c.