8#ifndef CWLIB_LIB_TRAPMC_TRAPSLESS_H_
9#define CWLIB_LIB_TRAPMC_TRAPSLESS_H_
32#define PWM_CNTER_FREQ_MHZ 40
34#define PWM_MAXDUTY_PERC 0.97
35#define PWM_MINDUTY_PERC (1-PWM_MAXDUTY_PERC)
38#define BEMF_CNTER_FREQ_HZ 1600000
39#define BEMF_CNTER_FREQ_KHZ (PWM_CNTER_FREQ_MHZ*1000)
41#define BEMF_CNTER_FREQ_HZ (PWM_CNTER_FREQ_MHZ*1000000)
42#define BEMF_CNTER_FREQ_KHZ (PWM_CNTER_FREQ_MHZ*1000)
49#define K2_TWOPI (2*3.1415926535897932384626433832795f)
50#define K2_SPEED_CALC_BASE (K2_TWOPI*(BEMF_CNTER_FREQ_HZ))
53#define K2_SPD_RPM_BASE (3500)
55#define K2_SPD_RAD_BASE (K2_SPD_RPM_BASE*K2_TWOPI/60*CAL_GET_POLE_PAIRS(0))
57#define K2_VOLT_BASE (400)
59#define K2_CURR_BASE (7)
61#define K2_KP_BASE (K2_VOLT_BASE/K2_SPD_RAD_BASE)
63#define K2_KI_BASE (K2_VOLT_BASE/K2_SPD_RAD_BASE*1000)
66#define K2_SPD_RPM_BASE_FMT (20)
68#define K2_SPD_RAD_BASE_FMT (20)
70#define K2_VOLT_BASE_FMT (15)
72#define K2_KP_BASE_FMT (15)
74#define K2_KI_BASE_FMT (20)
76#define K2_CURR_BASE_FMT (15)
79#define K2_MIN_PRE_CARRIER_CNT (K2_TRAP_CTRL_PWM_FREQ_HZ/(CAL_GET_REF_SPDMAX_RPM(0)*CAL_GET_POLE_PAIRS(0)*6/60))
81#define K2_MAX_PRE_CARRIER_CNT (K2_TRAP_CTRL_PWM_FREQ_HZ/(CAL_GET_FBK_SPDMIN_RPM(0)*CAL_GET_POLE_PAIRS(0)*6/60))
84#define K2_MAX_BEMF_CNT (BEMF_CNTER_FREQ_HZ/(CAL_GET_FBK_SPDMIN_RPM(0)*CAL_GET_POLE_PAIRS(0)*6/60))
86#define K2_OPENLOOP_SPD_BEMF_CNT (BEMF_CNTER_FREQ_HZ/(IQ_TO_RPM(CAL_GET_REF_SPDMIN_RPM(0))*CAL_GET_POLE_PAIRS(0)*6/60))
88#define K2_MAX_SPD_BEMF_CNT (BEMF_CNTER_FREQ_HZ/(CAL_GET_REF_SPDMAX_RPM(0)*CAL_GET_POLE_PAIRS(0)*6/60))
91#define K2_SPDMIN_RPM_FXP (CAL_GET_REF_SPDMIN_RPM(0))
94#define K2_MINRATE_EVERY_MS_RPM_FXP (K2_SPDMIN_RPM_FXP/1000)
97#define K2_TRAP_CTRL_PWM_FREQ_HZ (K2_PWM_FREQ_KHZ*1000)
101 #define K2_TRAP_PWM_AT_DUTY_MAX_CNTS (uint32_t)(0.97*((1600000/16000)/2))
103 #define K2_TRAP_PWM_MAX_CNTS (uint32_t)((1600000/16000)/2)
106 #define K2_TRAP_PWM_AT_DUTY_MAX_CNTS (uint32_t)(PWM_MAXDUTY_PERC*((PWM_CNTER_FREQ_MHZ*1000000/K2_TRAP_CTRL_PWM_FREQ_HZ)/2))
108 #define K2_TRAP_PWM_MAX_CNTS (uint32_t)((PWM_CNTER_FREQ_MHZ*1000000/K2_TRAP_CTRL_PWM_FREQ_HZ)/2)
113#define K2_TRAP_PWM_HALF_MAX_CNTS (K2_TRAP_PWM_MAX_CNTS/2)
115#define K2_TRAP_PWM_DT_CNTS (PWM_DT_US * PWM_CNTER_FREQ_MHZ)
117#define K2_TRAP_PWM_MAX_MINUS_DT_CNTS (K2_TRAP_PWM_MAX_CNTS - K2_TRAP_PWM_DT_CNTS)
120#define K2_MAX_VREF_Q15 (uint32_t)(PWM_MAXDUTY_PERC*32768)
122#define K2_MIN_VREF_Q15 (uint32_t)(PWM_MINDUTY_PERC*32768)
125#define K2_SPEED_LOOP_FMT_KP 15
127#define K2_SPEED_LOOP_FMT_KI 15
129#define K2_SPEED_LOOP_FMT_IN 27
131#define K2_SPEED_LOOP_FMT_OUT 15
133#define K2_SPEED_LOOP_FMT_PROPSUM 42
137#define K2_VBUS_MIN_VOLT 40
139#define K2_VBUS_MAX_VOLT 400
141#define K2_PWM_FREQ_KHZ 16
147 #define GET_PARAM_FXP(value, base) ((value) * (1 << (base ## _FMT)) / (base))
152 PATTERN_UH_ON_V_PWM = 0,
169 STATE_TRAP_CLOSEDLOOP,
233extern int32_t Vdc_Ad_Forced_Value[2];
234extern int32_t Iu_Ad_Forced_Value[2];
235extern int32_t Iv_Ad_Forced_Value[2];
236extern int32_t Iw_Ad_Forced_Value[2];
237extern int32_t Vu_Ad_Forced_Value[2];
238extern int32_t Vv_Ad_Forced_Value[2];
239extern int32_t Vw_Ad_Forced_Value[2];
void TrapSless__SlowHandler()
Handler for Trapezoidal control - 1KHz call rate.
void TrapSless__SetSpd(int32_t spd_rpm, int32_t ramp_rpm)
BLDC motor rotation command.
uint8_t TrapSless__GetBemfFromCmt(uint8_t dir, uint8_t CmtNum, uint8_t after_zc)
Returns the bemf pattern given a commutation index.
void TrapSless__ResetVars()
Resetting of trapezoidal key variables.
int32_t TrapSless__CalcSpeed()
Speed computation based on Bemf Zcross events.
uint8_t TrapSless__GetBemfToNum(uint8_t bemf, uint8_t after_zc, uint8_t dir)
Returns the index of the given bemf pattern.
TRAPSLESS_PATTERN_TYPE * TrapSless__GetVars(void)
Obtaine the trapezoidal variables.
void TrapSless__Init(void)
Initialization of TrapSless module.
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].
void TrapSless__BemfCnterUpdate(void)
Accumulate the counter for bemf.
void TrapSless__SetPatternAndPwm(uint8_t after_zc)
Set pwm duty cycle and output pattern.
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 ...
uint8_t TrapSless__GetCmtFromBemf(uint8_t dir, uint8_t bemf, uint8_t after_zc)
Returns the commutation pattern given a bemf pattern.
void TrapSless__SetVbusIbus(void)
Read ADC and Set Vbus and Ibus variables.
void TrapSless__Handler()
Handler for Trapezoidal control - 16KHz call rate.
void TrapSless__ExecuteCmt(void)
Execution of commutation.
void TrapSless__SetPwm(void)
Set pwm duty cycle.
uint8_t TrapSless__GetTooEarly(void)
Too early detection mechanism For every commutation there is an expected zcross at a given phase....
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 i...
void TrapSless__SetVabc(void)
Read ADC and set pole voltage variables.
Contains generic typedefs that are able to change in case of different processors.
uint32_t openLoopPatternTimeCnt
uint8_t actual_bemf_pattern
uint32_t blankTimeDownCnter
uint32_t TimeoutToolateForCmt
uint32_t openLoopTimeThld
uint32_t TimeoutForCmtSaved
uint32_t TimeoutForReload