소스 검색

feat:温度限制Iq,电量限制功率,测试通过

CN\guohui27 1 년 전
부모
커밋
f2f2e5f7c1

+ 1 - 1
User project/1.FrameLayer/Source/tbc.c

@@ -105,7 +105,7 @@ void tbc_voUpIsr(void)
 void tbc_voDownIsr(void)
 {
     /* ADC Sample with high priority */
-    adc_voSampleUp_High(&adc_stCof, &adc_stUpOut);
+    adc_voSampleUp(&adc_stCof, &adc_stUpOut);
     
     /* Alarm detect */
     alm_stIn.blADCInitOvrFlg = sysfsm_stFlg.blADCInitOvrFlg;

+ 2 - 2
User project/2.MotorDrive/Include/adc.h

@@ -188,7 +188,7 @@ _ADCDRV_EXT UWORD adc_RdsonADCGainSum;
 *************************************************************************/
 #ifdef _ADCDRV_C_
 _ADCDRV_EXT void adc_voCalibration(ADC_COF *cof, ADC_DOWN_OUT *out1, ADC_UP_OUT *out2); // Phase A and B current zero point, other A/D sample value
-_ADCDRV_EXT void adc_voSampleUp_High(const ADC_COF *cof, ADC_UP_OUT *out);
+_ADCDRV_EXT void adc_voSampleUp(const ADC_COF *cof, ADC_UP_OUT *out);
 _ADCDRV_EXT void adc_voSampleUp_Low(const ADC_COF *cof, ADC_UP_OUT *out);
 _ADCDRV_EXT void adc_voSampleDown(const ADC_COF *cof, ADC_DOWN_OUT *out);
 _ADCDRV_EXT void adc_voSampleCoef(ADC_COF *cof);
@@ -196,7 +196,7 @@ _ADCDRV_EXT void adc_voSampleInit(void);
 _ADCDRV_EXT void adc_voSRCalibration(ADC_COF *cof, const ADC_UP_OUT *up_out, ADC_DOWN_OUT *down_out);
 #else
 _ADCDRV_EXT void adc_voCalibration(ADC_COF *cof, ADC_DOWN_OUT *out1, ADC_UP_OUT *out2); // Phase A and B current zero point, other A/D sample value
-_ADCDRV_EXT void adc_voSampleUp_High(const ADC_COF *cof, ADC_UP_OUT *out);
+_ADCDRV_EXT void adc_voSampleUp(const ADC_COF *cof, ADC_UP_OUT *out);
 _ADCDRV_EXT void adc_voSampleUp_Low(const ADC_COF *cof, ADC_UP_OUT *out);
 _ADCDRV_EXT void adc_voSampleDown(const ADC_COF *cof, ADC_DOWN_OUT *out);
 _ADCDRV_EXT void adc_voSampleCoef(ADC_COF *cof);

+ 46 - 49
User project/2.MotorDrive/Include/pwrlim.h

@@ -3,15 +3,15 @@
  Filename:            pwrlim.h
  Partner Filename:    pwrlim.c
  Description:         The header file of pwrlim.c
- Complier:            IAR Embedded Workbench for ARM 7.80, IAR Systems.
+ Complier:            IAR Embedded Workbench for ARM 8.40, IAR Systems.
  CPU TYPE :           GD32F3x0
 *************************************************************************
- Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
+ Copyright (c) 2023 Welling Motor Technology(Shanghai) Co. Ltd.
  All rights reserved.
 *************************************************************************
 *************************************************************************
  Revising History (ECL of this file):
- Fjy create this file;
+
 ************************************************************************/
 
 /************************************************************************
@@ -36,65 +36,62 @@ extern "C" {
 /************************************************************************
  Definitions & Macros
 ************************************************************************/
-#define PWRLIM_COF_DEFAULT     \
-    {                          \
-        0, 0, 0, 0, 0, 0, 0, 0 \
+#define PWRLIM_COF_DEFAULT        \
+    {                             \
+        0, 0, 0, 0, 0, 0, 0, 0, 0 \
     } // Default value of PWRLIM_COF
 #define PWRLIM_OUT_DEFAULT \
     {                      \
-        0                  \
+        0, 0, 0, 0, 0      \
     } // Default value of PWRLIM_OUT
+
 /************************************************************************
  TypeDefs & Structure Defines
 ************************************************************************/
 typedef struct // Input of " pwr_voPwrLimCof "
 {
-    SWORD swPwrLimW; // Q0, Power limit value
-    UWORD uwPwrErrW; // Q0, Start power limit when "VAL - ERR"
-    SWORD swIqMaxAp; // Q0, Max phase current (peak value)
-    UWORD uwIBaseAp; // Q0, Base Current
-    UWORD uwUbVt;    // Q0, Voltage base
-    UWORD uwPwrLimPIKp;
-    UWORD uwPwrLimPIKi;
-    UWORD uwPwrLimSTARTCe;
-    UWORD uwPwrLimENDCe;
+    SWORD swPwrLimW;           /**< 功率限制值,单位:W */
+    UWORD uwPwrErrW;           /**< 功率开始限制值与功率限制值差值,单位:W */
+    SWORD swIqMaxAp;           /**< q轴电流最大值,单位:0.01A */
+    UWORD uwIBaseAp;           /**< 电流基值,单位:0.01A */
+    UWORD uwUbVt;              /**< 电压基值,单位:0.1V */
+    UWORD uwPwrLimPIKp;        /**< PI功率限制Kp,PuQ15 */
+    UWORD uwPwrLimPIKi;        /**< PI功率限制Ki,PuQ15 */
+    SWORD swPwrLimStartTempCe; /**< 开始限制功率的温度,单位:摄氏度 */
+    SWORD swAlarmTempCe;       /**< 结束限制功率的温度,单位:摄氏度 */
 } PWRLIM_COFIN;
 
 typedef struct // Output of " pwr_voPwrLimCof "
 {
-    UWORD uwPwrLimKp;        // Q5, q-axis Current regulation Kp
-    UWORD uwPwrLimPIKp;      // Q15, q-axis Current
-    UWORD uwPwrLimPIKi;      // Q15, q-axis Current
-    SWORD swIqMaxPu;         // Q14, Max phase current (peak value)
-    SWORD swMotorPwrLimitPu; // Q15, Power limit value
-
-    SWORD swPwrLimStartThresholdTemp;
-    SWORD swPwrLimENDThresholdTemp;
-    SWORD swPwrLimTempGain;
-
+    UWORD uwPwrLimKp;          /**< 根据功率线性限制电流系数,PuQ5 */
+    UWORD uwPwrLimPIKp;        /**< 根据功率PI限制电流系数Kp,PuQ15 */
+    UWORD uwPwrLimPIKi;        /**< 根据功率PI限制电流系数Ki,PuQ15 */
+    SWORD swIqMaxPu;           /**< q轴电流最大值,PuQ14 */
+    SWORD swPwrLimPu;          /**< 功率限制值,PuQ15 */
+    SWORD swPwrLimStartTemp;   /**< 开始限制功率的温度,单位:摄氏度 */
+    SWORD swPwrLimEndTemp;     /**< 结束限制功率的温度,单位:摄氏度 */
+    SWORD swPwrLimTempCof;     /**< 根据温度线性限制功率系数,PuQ15 */
+    SWORD swIqLimTempCof;      /**< 根据温度线性限制功率系数,PuQ14 */
+    UWORD uwPwrLimStartBatCap; /**< 开始限制功率的电池电量,单位:100% */
+    UWORD uwPwrLimEndBatCap;   /**< 结束限制功率的电池电量,单位:100% */
+    UWORD uwPwrlimBatCapCof;   /**< 根据电量线性限制功率系数,PuQ15 */
 } PWRLIM_COF;
 
 typedef struct // Input of " pwr_voPwrLim "
 {
-    SWORD swSpdPu;      // Q15, Motor Speed
-    SWORD swMotorPwrPu; // Q15, Input power of motor after LPF
-    SWORD swIqrefPu;
-    SWORD PCBTemp;
+    SWORD swSpdPu;      /**< 电机转速,PuQ15 */
+    SWORD swMotorPwrPu; /**< 电机计算功率,PuQ15 */
+    SWORD swPCBTemp;    /**< 电机计算功率,单位:摄氏度 */
+    UWORD uwBatCap;     /**< 电池电量 */
 } PWRLIM_IN;
 
 typedef struct // Output of " pwr_voPwrLim "
 {
-    SWORD swMotorPwrLimitActualPu;
-
-    SWORD swIqLimPu; // Q14, q-axis Current Reference
-    SWORD swErrorZ1;
-    SLONG slIqLimDetaSum;
-    SWORD swIqLimDetaSum;
-
-    SWORD swLastPCBTemp;
-    UWORD uwPCBChangeCnt;
-    BOOL  blPwrZeroFlg;
-    BOOL  blPCBTempChangeFlg;
+    SWORD swPwrLimActualPu; /**< 实际功率限制值,PuQ15 */
+    SWORD swIqLimPu;        /**< q轴电流限制值,PuQ14 */
+    SWORD swPwrErrZ1;       /**< 功率限制值与功率间的误差,PuQ15 */
+    SLONG slIqLimDetaSum;   /**< q轴电流限制减小值,PuQ30 */
+    SWORD swIqLimDetaSum;   /**< q轴电流限制减小值,PuQ14 */
 } PWRLIM_OUT;
 
 /************************************************************************
@@ -105,12 +102,12 @@ Constant Table
  Exported Variables
 ************************************************************************/
 #ifdef _PWRLIM_C_
-_PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut = PWRLIM_OUT_DEFAULT;
-_PWRLIM_EXT PWRLIM_COF pwr_stPwrLimCof = PWRLIM_COF_DEFAULT;
+_PWRLIM_EXT PWRLIM_COF pwr_stPwrLimCof  = PWRLIM_COF_DEFAULT;
+_PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut  = PWRLIM_OUT_DEFAULT;
 _PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut2 = PWRLIM_OUT_DEFAULT;
 #else
-_PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut;
 _PWRLIM_EXT PWRLIM_COF pwr_stPwrLimCof;
+_PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut;
 _PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut2;
 #endif
 
@@ -127,12 +124,12 @@ _PWRLIM_EXT PWRLIM_OUT pwr_stPwrLimOut2;
 _PWRLIM_EXT void pwr_voPwrLimInit(void);
 _PWRLIM_EXT void pwr_voPwrLimCof(PWRLIM_COFIN *in, PWRLIM_COF *out);
 _PWRLIM_EXT void pwr_voPwrLim(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
-_PWRLIM_EXT void pwr_voPwrLim2(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
+_PWRLIM_EXT void pwr_voPwrLimPI(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
 #else
-_PWRLIM_EXT void pwr_voPwrLimInit(void);
-_PWRLIM_EXT void pwr_voPwrLimCof(PWRLIM_COFIN *in, PWRLIM_COF *out);
-_PWRLIM_EXT void pwr_voPwrLim(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
-_PWRLIM_EXT void pwr_voPwrLim2(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
+_PWRLIM_EXT void       pwr_voPwrLimInit(void);
+_PWRLIM_EXT void       pwr_voPwrLimCof(PWRLIM_COFIN *in, PWRLIM_COF *out);
+_PWRLIM_EXT void       pwr_voPwrLim(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
+_PWRLIM_EXT void       pwr_voPwrLimPI(const PWRLIM_IN *in, const PWRLIM_COF *cof, PWRLIM_OUT *out);
 #endif
 
 /************************************************************************

+ 2 - 0
User project/2.MotorDrive/Include/spdctrmode.h

@@ -131,6 +131,7 @@ _SPDCTRMODE_EXT LPF_OUT scm_stSpdFbkLpf = LPF_OUT_DEFAULT;    // Speed feedback
 _SPDCTRMODE_EXT UWORD   scm_uwSpdFbkLpfAbsPu = 0;             // Q15, Speed feedback LPF absolute
 _SPDCTRMODE_EXT SWORD   scm_swMotorPwrInPu = 0;               // Q15, Input power of motor
 _SPDCTRMODE_EXT LPF_OUT scm_stMotoPwrInLpf = LPF_OUT_DEFAULT; // Input power of motor after LPF
+_SPDCTRMODE_EXT LPF_OUT scm_stPCBTempLpf = LPF_OUT_DEFAULT; // Input power of motor after LPF
 _SPDCTRMODE_EXT SWORD   scm_swMotorPwrInLpfWt = 0;            // unit: 0.1w, Input power of motor after LPF
 _SPDCTRMODE_EXT UWORD   scm_uwMotorPwrInAvgPu = 0;            // Q15, Input power of motor after average filter
 _SPDCTRMODE_EXT BOOL    scm_blDtcEnFlg = FALSE;               // Dead band compansation enable flag
@@ -214,6 +215,7 @@ _SPDCTRMODE_EXT LPF_OUT scm_stSpdFbkLpf;
 _SPDCTRMODE_EXT UWORD   scm_uwSpdFbkLpfAbsPu;
 _SPDCTRMODE_EXT SWORD   scm_swMotorPwrInPu;
 _SPDCTRMODE_EXT LPF_OUT scm_stMotoPwrInLpf;
+_SPDCTRMODE_EXT LPF_OUT scm_stPCBTempLpf;
 _SPDCTRMODE_EXT SWORD   scm_swMotorPwrInLpfWt;
 _SPDCTRMODE_EXT UWORD   scm_uwMotorPwrInAvgPu;
 _SPDCTRMODE_EXT BOOL    scm_blDtcEnFlg;

BIN
User project/2.MotorDrive/Lib/WLMCP_LIB.lib


+ 1 - 1
User project/2.MotorDrive/Source/adc.c

@@ -326,7 +326,7 @@ void adc_voSampleDown(const ADC_COF *cof, ADC_DOWN_OUT *out) /* parasoft-suppres
     out->uwIpeakPu = uwIpeakPu;
 }
 
-void adc_voSampleUp_High(const ADC_COF *cof, ADC_UP_OUT *out)
+void adc_voSampleUp(const ADC_COF *cof, ADC_UP_OUT *out)
 {
     /* Register value */
     out->uwVdcReg = iAdc_GetResultPointer(0)[HW_ADC_UDC_CH]; 

+ 1 - 1
User project/2.MotorDrive/Source/packed

@@ -1 +1 @@
-Subproject commit 06bfb0cb963b8440fb7b29c7c6413b4242d90014
+Subproject commit 3bc9db31141c162b2fd8d52f9de4649740ebe48c

+ 3 - 4
User project/2.MotorDrive/Source/spdctrFSM.c

@@ -122,11 +122,10 @@ void ClzLoop_TbcupHook(void)
     /*=======================================================================
                                      Power Limit
       =======================================================================*/
-    // Q15
+    mth_voLPFilter(adc_stUpOut.PCBTemp, &scm_stPCBTempLpf);
     pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; // Q15
-    pwr_stPwrLimIn.swIqrefPu = swCurRefrompu;                   // asr_stSpdPIOut.swIqRefPu;
-    pwr_stPwrLimIn.PCBTemp = adc_stUpOut.PCBTemp;
-    pwr_voPwrLim2(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
+    pwr_stPwrLimIn.swPCBTemp = scm_stPCBTempLpf.slY.sw.hi;  
+    pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
 
     /*======================================================================= 
              Set Iq and Id reference for Constant Voltage Break

+ 6 - 2
User project/2.MotorDrive/Source/spdctrmode.c

@@ -125,6 +125,7 @@ void scm_voSpdCtrMdInit(void)
     scm_uwSpdFbkLpfAbsPu = 0;      // Q15, Speed feedback LPF absolute
     scm_swMotorPwrInPu = 0;        // Q15, Input power of motor
     scm_stMotoPwrInLpf.slY.sl = 0; // Input power of motor after LPF
+    scm_stPCBTempLpf.slY.sl = 0;
     scm_swMotorPwrInLpfWt = 0;     // unit: 0.1w, Input power of motor after LPF
     scm_uwMotorPwrInAvgPu = 0;     // Q15, Input power of motor after average filter
 
@@ -335,6 +336,9 @@ void scm_voSpdCtrMdCoef(void)
         ulLpfTm = 1000000 / cp_stControlPara.swPwrLimitLPFFre;
         mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stMotoPwrInLpf.uwKx);
 
+        ulLpfTm = 1000000 / 10;
+        mth_voLPFilterCoef(ulLpfTm, FTBC_HZ, &scm_stPCBTempLpf.uwKx);
+
         pwr_stPwrLimCofIn.swPwrLimW = cp_stControlPara.swPwrLimitValWt; // Q0, unit: 0.1w, Power limit value
         pwr_stPwrLimCofIn.uwPwrErrW = cp_stControlPara.swPwrLimitErrWt; // Q0, unit: 0.1w, Start power limit when "VAL - ERR"
         pwr_stPwrLimCofIn.swIqMaxAp = cp_stMotorPara.swIpeakMaxA;       // Q0, unit: 0.01A, Max phase current (peak value)
@@ -342,8 +346,8 @@ void scm_voSpdCtrMdCoef(void)
         pwr_stPwrLimCofIn.uwUbVt = VBASE;                               // Q0,unit: 0.1V, Voltage base
         pwr_stPwrLimCofIn.uwPwrLimPIKp = cp_stControlPara.swPwrLimitKpPu;
         pwr_stPwrLimCofIn.uwPwrLimPIKi = cp_stControlPara.swPwrLimitKiPu;
-        pwr_stPwrLimCofIn.uwPwrLimSTARTCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
-        pwr_stPwrLimCofIn.uwPwrLimENDCe = cp_stControlPara.swAlmOverHeatCeVal;
+        pwr_stPwrLimCofIn.swPwrLimStartTempCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
+        pwr_stPwrLimCofIn.swAlarmTempCe = cp_stControlPara.swAlmOverHeatCeVal;
         pwr_voPwrLimCof(&pwr_stPwrLimCofIn, &pwr_stPwrLimCof);
 
         /*Accelaration&Decelaration limit*/

+ 8 - 0
User project/3.BasicFunction/Include/AssistCurve.h

@@ -14,6 +14,10 @@
 
 #include "asr.h"
 #include "mathtool.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
 /****************************************
  *
  *          Definitions & Macros
@@ -437,4 +441,8 @@ void ass_voMoveAverageFilter(MAF_IN *in);
 void ass_voMoveAverageFilterClear(MAF_IN *in);
 //void ass_voAssitTorqPI(ASS_TORQ_PI_IN *in, ASS_TORQ_PI_OUT *out);
 
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
 #endif

+ 7 - 0
User project/3.BasicFunction/Include/bikebrake.h

@@ -16,6 +16,10 @@
 #define BIKEBRAKE_H
 
 #include "typedefine.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
 /****************************************
  *
  *          Definitions & Macros
@@ -59,6 +63,9 @@ void bikebrake_voBikeBrakeInit(void);   // api function
 void bikebrake_voBikeBrakeDetect(void); // api function
 BOOL BikeBrake_blGetstate(void);
 /************************************************************************/
+#ifdef __cplusplus
+}
+#endif // __cplusplus
 
 #endif
 /************************************************************************

+ 1 - 28
User project/3.BasicFunction/Source/AssistCurve.c

@@ -1069,30 +1069,6 @@ static void ass_voAssitCurLim(UWORD gear, UWORD uwBikeSpeedHzPu, UWORD uwCurMaxP
     ass_stCurLimOut.uwIqlimit = uwIqLimitTemp1;
 }
 
-/**
- * @brief  
- *
- * @param 
- * @return 
- */
-static void ass_voAssistCurLimBMS(UWORD uwSOCvalue)
-{
-    /* Limit the Output Current according to Bike SOC */
-    if (uwSOCvalue < ass_stCurLimCalBMSCoef.uwIqLimitStartSoc && uwSOCvalue > ass_stCurLimCalBMSCoef.uwIqLimitEndSoc)
-    {
-        ass_stCurLimitCalBMSOut.uwIqLimitAbs =
-            ass_stCurLimCalBMSCoef.uwIqLimitInitAbs - ((ass_stCurLimCalBMSCoef.uwIqLimitStartSoc - uwSOCvalue) * ass_stCurLimCalBMSCoef.swIqLImitK);
-    }
-    else if (uwSOCvalue <= ass_stCurLimCalBMSCoef.uwIqLimitEndSoc)
-    {
-        ass_stCurLimitCalBMSOut.uwIqLimitAbs = 0;
-    }
-    else
-    {
-        ass_stCurLimitCalBMSOut.uwIqLimitAbs = ass_stCurLimCalBMSCoef.uwIqLimitInitAbs;
-    }
-}
-
 /**
  * @brief  Assist function
  *
@@ -1119,15 +1095,12 @@ void ass_voAssist(void)
     if (ass_stCalCoef.blAssistflag == TRUE)
     {
         /* Calculate Iqref Limit */
-        UWORD uwIqLimitTemp;
         ass_voAssitCurLim(ass_stCalIn.uwGearSt, ass_stCalIn.uwbikespeed, ass_stParaCong.uwCofCurMaxPu);
-        ass_voAssistCurLimBMS(ass_stCalIn.SOCValue);
 
-        uwIqLimitTemp = (ass_stCurLimOut.uwIqlimit < ass_stCalIn.swFlxIqLimit)
+        ass_stCalCoef.uwCurrentMaxPu = (ass_stCurLimOut.uwIqlimit < ass_stCalIn.swFlxIqLimit)
                           ? (ass_stCurLimOut.uwIqlimit < ass_stCalIn.swPwrIqLimit ? ass_stCurLimOut.uwIqlimit : ass_stCalIn.swPwrIqLimit)
                           : (ass_stCalIn.swFlxIqLimit < ass_stCalIn.swPwrIqLimit ? ass_stCalIn.swFlxIqLimit : ass_stCalIn.swPwrIqLimit);
 
-        ass_stCalCoef.uwCurrentMaxPu = (uwIqLimitTemp < ass_stCurLimitCalBMSOut.uwIqLimitAbs) ? uwIqLimitTemp : ass_stCurLimitCalBMSOut.uwIqLimitAbs;
         ass_stCalCoef.swCurrentmax_torAssPu = (SWORD)(((SLONG)ass_stCalCoef.uwCurrentMaxPu * (SWORD)ass_stParaSet.uwTorWeight) >> 12); // Q14
         ass_stCalCoef.swCurrentmax_cadAssPu = (SWORD)(((SLONG)ass_stCalCoef.uwCurrentMaxPu * (SWORD)ass_stParaSet.uwCadenceWeight) >> 12);
         

+ 0 - 1
User project/3.BasicFunction/Source/Temp.c

@@ -16,7 +16,6 @@
 #include "syspar.h"
 #include "typedefine.h"
 #include "mathtool.h"
-#include "gd32f30x.h"
 #include "Temp.h"
 
 /******************************

+ 105 - 105
User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include/user.h

@@ -42,27 +42,27 @@ Update Time
 /*======================================================================*
    Motor Number Selection
 *=======================================================================*/
-#define MOTOR_WELLING_CITY_36V    0x20
-#define MOTOR_WELLING_MTB_36V     0x21
-#define MOTOR_WELLING_CITY_48V    0x30
-#define MOTOR_WELLING_MTB_48V     0x31
+#define MOTOR_WELLING_CITY_36V 0x20
+#define MOTOR_WELLING_MTB_36V  0x21
+#define MOTOR_WELLING_CITY_48V 0x30
+#define MOTOR_WELLING_MTB_48V  0x31
 
-#define MOTOR_ID_SEL              MOTOR_WELLING_MTB_36V  
-#define GEARSENSOR_ENABLE         1 //0-Uart,1-GearSensor
+#define MOTOR_ID_SEL      MOTOR_WELLING_MTB_36V
+#define GEARSENSOR_ENABLE 1 // 0-Uart,1-GearSensor
 
-//#define TEST  //only use for testing
+// #define TEST  //only use for testing
 
 /*======================================================================*
    ADC Maximum Value
 *=======================================================================*/
 #if (MOTOR_ID_SEL == MOTOR_WELLING_MTB_36V || MOTOR_ID_SEL == MOTOR_WELLING_CITY_36V)
-#define ADC_IPHASE_CUR_MAX_AP  15600 
-#define ADC_IPHASE_CUR_OVER_K  780   // SWORD最大电流/采样最大电流=120A/156A=0.769=787(Q10) 
+#define ADC_IPHASE_CUR_MAX_AP 15600
+#define ADC_IPHASE_CUR_OVER_K 780 // SWORD最大电流/采样最大电流=120A/156A=0.769=787(Q10)
 #else
-#define ADC_IPHASE_CUR_MAX_AP  10355  
-#define ADC_IPHASE_CUR_OVER_K  1024
+#define ADC_IPHASE_CUR_MAX_AP 10355
+#define ADC_IPHASE_CUR_OVER_K 1024
 #endif
-  
+
 /*======================================================================*
     Motor Parameters
 *=======================================================================*/
@@ -70,13 +70,13 @@ Update Time
 #define M_POLE_PAIRS        7       // unit:Null,Pole pairs
 #define M_RS_OHM            170     // unit: 0.1mOhm, Phase resistance
 #define M_MATERIAL          Al      // unit:Null
-#define M_LD_NOLOAD_MH      11000    // unit: 0.01uH, D axis inductance
+#define M_LD_NOLOAD_MH      11000   // unit: 0.01uH, D axis inductance
 #define M_LQ_NOLOAD_MH      11500   // unit: 0.01uH, Q axis inductance
 #define M_LD_TURN1_ID_AP    5000    // unit: 0.01A,
-#define M_LD_TURN1_LD_MH    11000    // unit: 0.01uH,D axis inductance
+#define M_LD_TURN1_LD_MH    11000   // unit: 0.01uH,D axis inductance
 #define M_LD_TURN2_ID_AP    700     // unit: 0.01A,
-#define M_LD_TURN2_LD_MH    11000    // unit: 0.01uH,D axis inductance
-#define M_LD_MIN_MH         11000    // unit: 0.01uH,D axis inductance
+#define M_LD_TURN2_LD_MH    11000   // unit: 0.01uH,D axis inductance
+#define M_LD_MIN_MH         11000   // unit: 0.01uH,D axis inductance
 #define M_LQ_TURN1_IQ_AP    200     // unit: 0.01A,
 #define M_LQ_TURN1_LQ_MH    11500   // unit: 0.01uH,Q axis inductance
 #define M_LQ_TURN2_IQ_AP    700     // unit: 0.01A,
@@ -99,13 +99,13 @@ Update Time
 #define M_POLE_PAIRS        7       // unit:Null,Pole pairs
 #define M_RS_OHM            170     // unit: 0.1mOhm, Phase resistance
 #define M_MATERIAL          Al      // unit:Null
-#define M_LD_NOLOAD_MH      11000    // unit: 0.01uH, D axis inductance
+#define M_LD_NOLOAD_MH      11000   // unit: 0.01uH, D axis inductance
 #define M_LQ_NOLOAD_MH      11500   // unit: 0.01uH, Q axis inductance
 #define M_LD_TURN1_ID_AP    5000    // unit: 0.01A,
-#define M_LD_TURN1_LD_MH    11000    // unit: 0.01uH,D axis inductance
+#define M_LD_TURN1_LD_MH    11000   // unit: 0.01uH,D axis inductance
 #define M_LD_TURN2_ID_AP    700     // unit: 0.01A,
-#define M_LD_TURN2_LD_MH    11000    // unit: 0.01uH,D axis inductance
-#define M_LD_MIN_MH         11000    // unit: 0.01uH,D axis inductance
+#define M_LD_TURN2_LD_MH    11000   // unit: 0.01uH,D axis inductance
+#define M_LD_MIN_MH         11000   // unit: 0.01uH,D axis inductance
 #define M_LQ_TURN1_IQ_AP    200     // unit: 0.01A,
 #define M_LQ_TURN1_LQ_MH    11500   // unit: 0.01uH,Q axis inductance
 #define M_LQ_TURN2_IQ_AP    700     // unit: 0.01A,
@@ -142,7 +142,7 @@ Update Time
 #define M_LQ_MIN_MH         10200   // unit: 0.01uH,Q axis inductance
 #define M_JD                2500    // unit: 10-7Kg*m2, Rotational inertia
 #define M_MAGNETIC_MATERIAL Ferrite // unit:Null
-#define M_FLUX_WB           4800//5056    // unit: 0.001mWb, Flux linkage
+#define M_FLUX_WB           4800    // 5056    // unit: 0.001mWb, Flux linkage
 #define M_ID_MAX_AP         0       //-25      // unit: 0.01A, Max d axis current
 #define M_ID_MIN_AP         -300    // unit: 0.01A, Min d axis current
 #define M_IS_PEAK_MAX_AP    5500    // unit: 0.01A, Max phase current(below base speed)
@@ -171,7 +171,7 @@ Update Time
 #define M_LQ_MIN_MH         10200   // unit: 0.01uH,Q axis inductance
 #define M_JD                2500    // unit: 10-7Kg*m2, Rotational inertia
 #define M_MAGNETIC_MATERIAL Ferrite // unit:Null
-#define M_FLUX_WB           4800//5056    // unit: 0.001mWb, Flux linkage
+#define M_FLUX_WB           4800    // 5056    // unit: 0.001mWb, Flux linkage
 #define M_ID_MAX_AP         0       //-25      // unit: 0.01A, Max d axis current
 #define M_ID_MIN_AP         -300    // unit: 0.01A, Min d axis current
 #define M_IS_PEAK_MAX_AP    5500    // unit: 0.01A, Max phase current(below base speed)
@@ -225,12 +225,12 @@ Update Time
 
 #elif ((MOTOR_ID_SEL == MOTOR_WELLING_CITY_48V) || (MOTOR_ID_SEL == MOTOR_WELLING_MTB_48V))
 /* Over voltage */
-#define ALM_OVR_VLT_LVL1_VAL 585 // unit: 0.1V, Threshold value level1
-#define ALM_OVR_VLT_LVL1_TM  2   // unit: s, Time of duration level1(TBS)
-#define ALM_OVR_VLT_LVL2_VAL 598 // unit: 0.1V, Threshold value level2
-#define ALM_OVR_VLT_LVL2_TM  100 // unit: ms, Time of duration level2(TBS)
-#define ALM_OVR_VLT_LVL3_VAL 611 // unit: 0.1V, Threshold value level3
-#define ALM_OVR_VLT_LVL3_TM  5   // unit: ms, Time of duration level3(TBS)
+#define ALM_OVR_VLT_LVL1_VAL  585 // unit: 0.1V, Threshold value level1
+#define ALM_OVR_VLT_LVL1_TM   2   // unit: s, Time of duration level1(TBS)
+#define ALM_OVR_VLT_LVL2_VAL  598 // unit: 0.1V, Threshold value level2
+#define ALM_OVR_VLT_LVL2_TM   100 // unit: ms, Time of duration level2(TBS)
+#define ALM_OVR_VLT_LVL3_VAL  611 // unit: 0.1V, Threshold value level3
+#define ALM_OVR_VLT_LVL3_TM   5   // unit: ms, Time of duration level3(TBS)
 /* Under voltage */
 #define ALM_UNDR_VLT_LVL1_VAL 390 // unit: 0.1V, Threshold value level1
 #define ALM_UNDR_VLT_LVL1_TM  2   // unit: s, Time of duration level1(TBS)
@@ -246,9 +246,8 @@ Update Time
 #define ALM_ADC_DETE_LOW_VAL 155 // unit: 0.01V, ADC offset Voltage min
 #define ALM_ADC_DETE_HIG_VAL 175 // unit: 0.01V, ADC offset Voltage max
 /* IPM over heat */
-#define ALM_IPM_OVR_HEAT_VAL        105//50 //105 // unit: Ce, Threshold value
-#define ALM_IPM_OVR_TM              2  // unit: s, Time of duration(TBS)
-#define PWRLIM_START_THRESHOLD_TEMP 85//40 //80 // Q0,Ce
+#define ALM_IPM_OVR_HEAT_VAL 105 // 50 //105 // unit: Ce, Threshold value
+#define ALM_IPM_OVR_TM       2   // unit: s, Time of duration(TBS)
 
 /* Stop time of three phase short force  */
 #define ALM_THR_PHS_SHRT_FRC_TM 200 // unit: ms, Time of duration(TBC)
@@ -266,40 +265,40 @@ Update Time
 /* Recover time of IPM OC */
 #define ALM_IPM_OC_REC_TM 200 // unit: ms, Time of duration(TBC)
 /* Recover time & value of over voltage */
-#define ALM_OVR_VLT_REC_VAL (ALM_OVR_VLT_LVL1_VAL - 30)  // unit: 0.1V
-#define ALM_OVR_VLT_REC_TM  100 // unit: ms, Time of duration(TBC)
-#define ALM_OVR_VLT_REC_TM1 150 // unit: ms, Time of duration(TBC)
+#define ALM_OVR_VLT_REC_VAL (ALM_OVR_VLT_LVL1_VAL - 30) // unit: 0.1V
+#define ALM_OVR_VLT_REC_TM  100                         // unit: ms, Time of duration(TBC)
+#define ALM_OVR_VLT_REC_TM1 150                         // unit: ms, Time of duration(TBC)
 /* Recover time & value of under voltage */
 #define ALM_UNDR_VLT_REC_VAL (ALM_UNDR_VLT_LVL1_VAL + 30) // unit: 0.1V
-#define ALM_UNDR_VLT_REC_TM  200 // unit: ms, Time of duration(TBC)
-#define ALM_UNDR_VLT_REC_TM1 400 // unit: ms, Time of duration(TBC)
+#define ALM_UNDR_VLT_REC_TM  200                          // unit: ms, Time of duration(TBC)
+#define ALM_UNDR_VLT_REC_TM1 400                          // unit: ms, Time of duration(TBC)
 /* Recover time & value of IPM over heat */
 #define ALM_IPM_OVR_HEAT_REC_VAL 90  // unit: Ce, Threshold value
 #define ALM_IPM_OVR_REC_TM       60  // unit: s, Time of duration(TBC)
 #define ALM_IPM_OVR_REC_TM1      120 // unit: s, Time of duration(TBC)
 
 /* Ebike sensor fault detect*/
-#define ALM_MOTORSPD_MIN_RPM  50   //unit: rpm
-#define ALM_BIKESPD_FLT_TS    10   //unit: s
-#define ALM_CADENCE_FLT_TS    10    //unit: s
-#define ALM_TORQ_MAX_VOL      30   //unit: 0.1V
-#define ALM_TORQ_MIN_VOL      1    //unit: 0.1V
-#define ALM_TORQ_FLT_TS       1    //unit: s  
-#define ALM_THROTTLE_MAX_VOL  30   //unit: 0.1V
-#define ALM_THROTTLE_MIN_VOL  1    //unit: 0.1V
-#define ALM_THROTTLE_FLT_TS   1    //unit: s
-#define ALM_NTC_MAX_VOL       32   //unit: 0.1V 
-#define ALM_NTC_MIN_VOL       3    //unit: 0.1V
-#define ALM_MOTORNTC_FLT_TS   1    //unit: s
-#define ALM_PCBNTC_FLT_TS     1    //unit: s
+#define ALM_MOTORSPD_MIN_RPM 50 // unit: rpm
+#define ALM_BIKESPD_FLT_TS   10 // unit: s
+#define ALM_CADENCE_FLT_TS   10 // unit: s
+#define ALM_TORQ_MAX_VOL     30 // unit: 0.1V
+#define ALM_TORQ_MIN_VOL     1  // unit: 0.1V
+#define ALM_TORQ_FLT_TS      1  // unit: s
+#define ALM_THROTTLE_MAX_VOL 30 // unit: 0.1V
+#define ALM_THROTTLE_MIN_VOL 1  // unit: 0.1V
+#define ALM_THROTTLE_FLT_TS  1  // unit: s
+#define ALM_NTC_MAX_VOL      32 // unit: 0.1V
+#define ALM_NTC_MIN_VOL      3  // unit: 0.1V
+#define ALM_MOTORNTC_FLT_TS  1  // unit: s
+#define ALM_PCBNTC_FLT_TS    1  // unit: s
 /* Ebike sensor fault recover*/
-#define ALM_BIKE_REC_ALL_TM   1000 //unit: ms
-#define ALM_BIKESPD_REC_TM    200  //unit: ms
-#define ALM_CADENCE_REC_TM    200  //unit: ms
-#define ALM_TORQ_REC_TM       200  //unit: ms
-#define ALM_THROTTLE_REC_TM   200  //unit: ms
-#define ALM_MOTORNTC_REC_TM   200  //unit: ms
-#define ALM_PCBNTC_REC_TM     200  //unit: ms
+#define ALM_BIKE_REC_ALL_TM 1000 // unit: ms
+#define ALM_BIKESPD_REC_TM  200  // unit: ms
+#define ALM_CADENCE_REC_TM  200  // unit: ms
+#define ALM_TORQ_REC_TM     200  // unit: ms
+#define ALM_THROTTLE_REC_TM 200  // unit: ms
+#define ALM_MOTORNTC_REC_TM 200  // unit: ms
+#define ALM_PCBNTC_REC_TM   200  // unit: ms
 /*======================================================================*
    Motor start
 *=======================================================================*/
@@ -310,15 +309,15 @@ Update Time
 #define INITPOS_ALIGN 0             // Motor start with three stage
 #define INITPOS_MODE  INITPOS_ALIGN // Motor initposdet mode
 /* Park time & current */
-#define ALIGN_CUR_AP     1000  // unit: 0.01A, Park current value
+#define ALIGN_CUR_AP     1000 // unit: 0.01A, Park current value
 #define ALIGN_RAMP_TM_MS 500  // unit: ms, Time of current ramp
 #define ALIGN_HOLD_TM_MS 2000 // unit: ms, Time of current hold
 #define ALIGN_ANG_INIT   0    // unit: deg, Park angle init, can not be 90deg
 
 /* Drag speed & current */
-#define DRAG_CUR_AP         1000          // 1500 //100 Huawei      // unit: 0.01A, Drag current value
-#define DRAG_VOL_AP         18           // 0.1v
-#define DRAG_SPD_HZ         20           // unit: Hz, Final speed of drag
+#define DRAG_CUR_AP         1000 // 1500 //100 Huawei      // unit: 0.01A, Drag current value
+#define DRAG_VOL_AP         18   // 0.1v
+#define DRAG_SPD_HZ         20   // unit: Hz, Final speed of drag
 #define DRAG_SPD_RAMP_TM_MS 4000 // unit: ms, Time of speed from 0Hz to target Hz
 /* Open to close */
 #define OPEN2CLZ_CUR_RAMP_TM_MS 5000 // 5000//1000 Huawei      // unit: ms, Time of current from drag value to 0
@@ -328,10 +327,10 @@ Update Time
 /*======================================================================*
    Flux observer
 *=======================================================================*/
-#define OBS_SPD_PLL_M            2  // Flux observer m coefficient
-#define OBS_SPD_PLL_BANDWIDTH_HZ 150       // Flux observer PLL bandwidth
-#define OBS_FLUX_PI_DAMPRATIO    10   // unit:0.1, Flux observer PI regulator dampratio
-#define OBS_FLUX_PI_CROSSFREQ_HZ 10   // unit:Hz, Flux observer PI regulator crossfrequency
+#define OBS_SPD_PLL_M            2   // Flux observer m coefficient
+#define OBS_SPD_PLL_BANDWIDTH_HZ 150 // Flux observer PLL bandwidth
+#define OBS_FLUX_PI_DAMPRATIO    10  // unit:0.1, Flux observer PI regulator dampratio
+#define OBS_FLUX_PI_CROSSFREQ_HZ 10  // unit:Hz, Flux observer PI regulator crossfrequency
 
 /*======================================================================*
    Current loop PI regulation
@@ -345,14 +344,14 @@ Update Time
 /*======================================================================*
    Speed loop PI regulation
 *=======================================================================*/
-#define ASR_PI_BANDWIDTH    3 // unit: Hz, Speed loop bandwidth
-#define ASR_PI_M            4 //
+#define ASR_PI_BANDWIDTH    3  // unit: Hz, Speed loop bandwidth
+#define ASR_PI_M            4  //
 #define ASR_SPD_FBK_LPF_FRQ 50 //
 #define ASR_SPD_INER_RATE   1  //
 /*=====================================================================*
    PWM generation
 *=======================================================================*/
-#define PWM_MAX_DUTY_CYLE_IPM     880 //890                        // 938      // unit: 0.1%,Max duty cyle for compare value
+#define PWM_MAX_DUTY_CYLE_IPM     880                        // 890                        // 938      // unit: 0.1%,Max duty cyle for compare value
 #define PWM_7SVM_TO_5SVM_DUTY_IPM 700                        // unit: 0.1%, Switch ratio from 7 to 5 svpwm
 #define PWM_MIN_SAMPLE_DUTY1_IPM  5 * 10000 / PWM_PERIOD_US  // unit: 0.1%, 5us TWO MIN ZERO VECTOR = two sample current steady time
 #define PWM_MIN_SAMPLE_DUTY2_IPM  10 * 10000 / PWM_PERIOD_US // unit: 0.1%, 10us TWO (one sample current steady time + one sample time)
@@ -360,10 +359,10 @@ Update Time
     ((ULONG)6 * 10000 / PWM_PERIOD_US) // unit: 0.1%, 6 us TWO (one Singel Resistance sample current steady time + one sample time)
 #define PWM_SAMPLE_TOSTEADY_IPM    ((ULONG)8 * 10000 / PWM_PERIOD_US) // unit: 0.1%, 4us TWO sample current to steady time
 #define PWM_SAMPLE_SINGELRESIS_IPM ((ULONG)1 * 10000 / PWM_PERIOD_US) // unit: 0.1%, 1us TWO singel Resistance sample time
-#define PWM_1ST_SAMPLE_CTS_IPM     25//43                        // 25       // unit: Null, first current sample counts
-#define PWM_2ND_SAMPLE_CTS_IPM     220//280                       // 220      // unit: Null, second current sample counts
-#define PWM_MIN_EFF_VECTOR_CTS_IPM 245//323                       // unit: Null, Min effective vector counts
-#define PWM_MIN_EFF_VECTOR_PU_IPM  892//1176                       // 600      // unit: Q14, Min effective vector pu
+#define PWM_1ST_SAMPLE_CTS_IPM     25  // 43                        // 25       // unit: Null, first current sample counts
+#define PWM_2ND_SAMPLE_CTS_IPM     220 // 280                       // 220      // unit: Null, second current sample counts
+#define PWM_MIN_EFF_VECTOR_CTS_IPM 245 // 323                       // unit: Null, Min effective vector counts
+#define PWM_MIN_EFF_VECTOR_PU_IPM  892 // 1176                       // 600      // unit: Q14, Min effective vector pu
 
 #define PWM_OVR_MDL_MIN_PHS 0                   // SVPWM over modulation: min phase error
 #define PWM_OVR_MDL_MIN_AMP 1                   // SVPWM over modulation: min amplitude error
@@ -373,30 +372,31 @@ Update Time
 /*======================================================================*
    Flux Weakening
 *=======================================================================*/
-#define FW_VDC_MIN_CALC_TM       20  // unit: ms,50Hz=>20ms, period of Vdc fluctuate
-#define FW_VDC_LPF_FRQ           50  // unit: Hz, Frequency of Vdc LPF
-#define FW_PWM_MAX_DUTY_CYLE_IPM 880 // unit: 0.1%,PWM max duty cyle for flux weakening
+#define FW_VDC_MIN_CALC_TM       20   // unit: ms,50Hz=>20ms, period of Vdc fluctuate
+#define FW_VDC_LPF_FRQ           50   // unit: Hz, Frequency of Vdc LPF
+#define FW_PWM_MAX_DUTY_CYLE_IPM 880  // unit: 0.1%,PWM max duty cyle for flux weakening
 #define FW_ID_PI_OUT_MIN         1000 // unit: 0.01A, Min Id of PI outunit: 0.01A, Current Limit
-#define FW_ID_MIN_LIM_RATIO      80  // Q0:1%, Ratio of Min d axis current (Charactoristic current)
-#define FW_ID_KP_PU              10  // Q16, Kp for d axis current
-#define FW_ID_KI_PU              5   // Q16, Ki for d axis current
-#define FW_CHAR_CUR_CROSS_FREQ   2   // Q0,unit: SQRT(1/2piR)
-#define FW_CHAR_CUR_DAMP_RATIO   2   // Q0,unit: SQRT(pi/2R)
+#define FW_ID_MIN_LIM_RATIO      80   // Q0:1%, Ratio of Min d axis current (Charactoristic current)
+#define FW_ID_KP_PU              10   // Q16, Kp for d axis current
+#define FW_ID_KI_PU              5    // Q16, Ki for d axis current
+#define FW_CHAR_CUR_CROSS_FREQ   2    // Q0,unit: SQRT(1/2piR)
+#define FW_CHAR_CUR_DAMP_RATIO   2    // Q0,unit: SQRT(pi/2R)
 
 /*======================================================================*
    Constant Voltage Braking Parameter define
 *=======================================================================*/
-#define CVB_CONSTANT_VOL_BRAKE_V (ALM_OVR_VLT_LVL1_VAL - 30)   // unit:0.1V,Voltage limit of Constant Voltage Brake
-#define CVB_CONSTANT_SPD_LOW_RPM 4500 // unit:rpm,
+#define CVB_CONSTANT_VOL_BRAKE_V (ALM_OVR_VLT_LVL1_VAL - 30) // unit:0.1V,Voltage limit of Constant Voltage Brake
+#define CVB_CONSTANT_SPD_LOW_RPM 4500                        // unit:rpm,
 
 /*======================================================================*
    Power limit
 *=======================================================================*/
-#define PWRLIM_VAL         6500 // unit: 0.1w, Power limit value
-#define PWRLIM_ERR         500  // unit: 0.1w, Start power limit when "VAL - ERR"
-#define PWRLIM_LPF_FRQ     8000  // unit: Hz, Frequency of LPF in input power of motor
-#define PWR_IQ_LIMIT_KP_PU 20  // Q15
-#define PWR_IQ_LIMIT_KI_PU 3000   // Q15
+#define PWRLIM_VAL                  6500 // unit: 0.1w, Power limit value
+#define PWRLIM_ERR                  500  // unit: 0.1w, Start power limit when "VAL - ERR"
+#define PWRLIM_LPF_FRQ              8000 // unit: Hz, Frequency of LPF in input power of motor
+#define PWR_IQ_LIMIT_KP_PU          20   // Q15
+#define PWR_IQ_LIMIT_KI_PU          3000 // Q15
+#define PWRLIM_START_THRESHOLD_TEMP 90   // Ce
 /*======================================================================*
    Deadband compensation
 *=======================================================================*/
@@ -417,12 +417,12 @@ Update Time
 #define USER_MOTOR_200RPM2PU       SPD_RPM2PU(200)
 #define USER_MOTOR_300RPM2PU       SPD_RPM2PU(300)
 #define USER_MOTOR_400RPM2PU       SPD_RPM2PU(400)
-#define USER_MOTOR_600RPM2PU       SPD_RPM2PU(600)     // 600rpm,Q15
-#define USER_MOTOR_800RPM2PU       SPD_RPM2PU(800)     // 800rpm,Q15
-#define USER_MOTOR_1000RPM2PU      SPD_RPM2PU(1000)    // 1000rpm,Q15
-#define USER_MOTOR_1500RPM2PU      SPD_RPM2PU(1500)    // 6000rpm,Q15
-#define USER_MOTOR_4000RPM2PU      SPD_RPM2PU(4000)    // 6000rpm,Q15
-#define USER_MOTOR_5500RPM2PU      SPD_RPM2PU(5500) 
+#define USER_MOTOR_600RPM2PU       SPD_RPM2PU(600)  // 600rpm,Q15
+#define USER_MOTOR_800RPM2PU       SPD_RPM2PU(800)  // 800rpm,Q15
+#define USER_MOTOR_1000RPM2PU      SPD_RPM2PU(1000) // 1000rpm,Q15
+#define USER_MOTOR_1500RPM2PU      SPD_RPM2PU(1500) // 6000rpm,Q15
+#define USER_MOTOR_4000RPM2PU      SPD_RPM2PU(4000) // 6000rpm,Q15
+#define USER_MOTOR_5500RPM2PU      SPD_RPM2PU(5500)
 #define USER_MOTOR_100RPMPS2PU_Q29 SPD_ACC_RPM2PU(100) // 100rpm/s,(0x40000000/FTBS_Hz/cof_uwVbRpm*100)
 /*=======================================================================
                       TBS&TBC Time Cnts Define
@@ -479,18 +479,18 @@ Update Time
                       Deadband Compensation parameters define
 ==============================================================================*/
 /* Api Device Index Definations */
-#define ADC_IA_CH                       0
-#define ADC_IB_CH                       1
-#define ADC_IC_CH                       2 
-      
-#define ADC_IDC_CH                      0
-
-#define ADC_UDC_CH                      0
-#define ADC_TEMP_CH                     1
-#define ADC_UAC_CH                      2
-
-#define GPIO_LED_PIN                    0
-#define GPIO_CAP_PIN                    1      
+#define ADC_IA_CH 0
+#define ADC_IB_CH 1
+#define ADC_IC_CH 2
+
+#define ADC_IDC_CH 0
+
+#define ADC_UDC_CH  0
+#define ADC_TEMP_CH 1
+#define ADC_UAC_CH  2
+
+#define GPIO_LED_PIN 0
+#define GPIO_CAP_PIN 1
 /************************************************************************
  TypeDefs & Structure defines
 *************************************************************************/
@@ -514,7 +514,7 @@ Update Time
 /************************************************************************
  Head files (N/A)
 *************************************************************************/
-//#include <stdlib.h>
+// #include <stdlib.h>
 #include "typedefine.h"
 #include "CodePara.h"
 #include "classB.h"

+ 67 - 50
tests/unit_test/test_pwrlim.cpp

@@ -12,8 +12,8 @@ class PwrLimTest : public testing::Test
 {
 protected:
     PmsmSimUtModelClass *sys1;
-    McStatus      McStatus1;
-    McPuBase      McPuBase1;
+    McStatus             McStatus1;
+    McPuBase             McPuBase1;
 
     virtual void SetUp() override
     {
@@ -64,50 +64,41 @@ protected:
     }
 };
 
-class PwrLimTest1 : public PwrLimTest, public testing::WithParamInterface<double>
-{};
-
-TEST_P(PwrLimTest1, BandCal)
-{
-
-}
 
-INSTANTIATE_TEST_SUITE_P(DiffBand, PwrLimTest1, testing::Values(20, 50, 100));
-
-class PwrLimTest2 : public PwrLimTest, public testing::WithParamInterface<int>
+class PwrLimTest2 : public PwrLimTest, public testing::WithParamInterface<::std::tuple<int,int>>
 {};
 
 TEST_P(PwrLimTest2, PwrLim)
 {
     /* Coef Cal */
-    pwr_stPwrLimCofIn.swPwrLimW = cp_stControlPara.swPwrLimitValWt; 
-    pwr_stPwrLimCofIn.uwPwrErrW = cp_stControlPara.swPwrLimitErrWt; 
-    pwr_stPwrLimCofIn.swIqMaxAp = cp_stMotorPara.swIpeakMaxA;       
-    pwr_stPwrLimCofIn.uwIBaseAp = IBASE;                            
-    pwr_stPwrLimCofIn.uwUbVt = VBASE;                               
-    pwr_stPwrLimCofIn.uwPwrLimPIKp = cp_stControlPara.swPwrLimitKpPu;
-    pwr_stPwrLimCofIn.uwPwrLimPIKi = cp_stControlPara.swPwrLimitKiPu;
-    pwr_stPwrLimCofIn.uwPwrLimSTARTCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
-    pwr_stPwrLimCofIn.uwPwrLimENDCe = cp_stControlPara.swAlmOverHeatCeVal;
+    pwr_stPwrLimCofIn.swPwrLimW           = cp_stControlPara.swPwrLimitValWt;
+    pwr_stPwrLimCofIn.uwPwrErrW           = cp_stControlPara.swPwrLimitErrWt;
+    pwr_stPwrLimCofIn.swIqMaxAp           = cp_stMotorPara.swIpeakMaxA;
+    pwr_stPwrLimCofIn.uwIBaseAp           = IBASE;
+    pwr_stPwrLimCofIn.uwUbVt              = VBASE;
+    pwr_stPwrLimCofIn.uwPwrLimPIKp        = cp_stControlPara.swPwrLimitKpPu;
+    pwr_stPwrLimCofIn.uwPwrLimPIKi        = cp_stControlPara.swPwrLimitKiPu;
+    pwr_stPwrLimCofIn.swPwrLimStartTempCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
+    pwr_stPwrLimCofIn.swAlarmTempCe       = cp_stControlPara.swAlmOverHeatCeVal;
     pwr_voPwrLimCof(&pwr_stPwrLimCofIn, &pwr_stPwrLimCof);
 
     mth_voLPFilterCoef((1000000 / cp_stControlPara.swPwrLimitLPFFre), FTBC_HZ, &scm_stMotoPwrInLpf.uwKx);
 
-    asr_stSpdPICoefIn.uwUbVt = VBASE;
-    asr_stSpdPICoefIn.uwIbAp = IBASE;
-    asr_stSpdPICoefIn.uwFbHz = FBASE;
-    asr_stSpdPICoefIn.uwFTbsHz = FTBS_HZ;
-    asr_stSpdPICoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
-    asr_stSpdPICoefIn.uwMtJm = cp_stMotorPara.swJD;
+    asr_stSpdPICoefIn.uwUbVt    = VBASE;
+    asr_stSpdPICoefIn.uwIbAp    = IBASE;
+    asr_stSpdPICoefIn.uwFbHz    = FBASE;
+    asr_stSpdPICoefIn.uwFTbsHz  = FTBS_HZ;
+    asr_stSpdPICoefIn.uwPairs   = cp_stMotorPara.swMotrPolePairs;
+    asr_stSpdPICoefIn.uwMtJm    = cp_stMotorPara.swJD;
     asr_stSpdPICoefIn.uwMtFlxWb = cp_stMotorPara.swFluxWb;
-    asr_stSpdPICoefIn.uwMcoef = cp_stControlPara.swAsrPIM;
-    asr_stSpdPICoefIn.uwWvcHz = cp_stControlPara.swAsrPIBandwidth;
+    asr_stSpdPICoefIn.uwMcoef   = cp_stControlPara.swAsrPIM;
+    asr_stSpdPICoefIn.uwWvcHz   = cp_stControlPara.swAsrPIBandwidth;
     asr_stSpdPICoefIn.uwRatioJm = cp_stControlPara.swAsrSpdInerRate;
     asr_voSpdPICoef(&asr_stSpdPICoefIn, &asr_stSpdPICoef);
 
     /* Custom Spd Ctrl */
-    sys1->PmsmSimUt_P.Params.CustomSpdCtrl  = 1;
-    int16_t spdRefRpm = 3000;
+    sys1->PmsmSimUt_P.Params.CustomSpdCtrl = 1;
+    int16_t spdRefRpm                      = 3000;
 
     for (int i = 0; i < 10 / sys1->PmsmSimUt_P.Params.Ts; i++)
     {
@@ -125,8 +116,7 @@ TEST_P(PwrLimTest2, PwrLim)
         // {
         //     sys1->PmsmSimUt_U.CtrlIn.Tm = 2;
         // }
-
-        sys1->PmsmSimUt_U.CtrlIn.Tm = 2;
+        sys1->PmsmSimUt_U.CtrlIn.Tm = 1;  ///< 注意:加载需加到恒功率区以测试功率是否被限制,但是太大会导致仿真中转速负增加最终溢出,需要算一下
 
         /* Pu Value to Named Value */
         MotorSimHelper::SetModelICmdPu(sys1, 0, asr_stSpdPIOut.swIqRefPu, McStatus1.Base);
@@ -140,35 +130,62 @@ TEST_P(PwrLimTest2, PwrLim)
         /* Power Cal */
         double ualpha = 0.6667 * (sys1->PmsmSimUt_Y.Out.Uabc[0] - 0.5 * sys1->PmsmSimUt_Y.Out.Uabc[1] - 0.5 * sys1->PmsmSimUt_Y.Out.Uabc[2]);
         double ubeta  = 0.57735 * (sys1->PmsmSimUt_Y.Out.Uabc[1] - sys1->PmsmSimUt_Y.Out.Uabc[2]);
-        double ud = ualpha * cos(sys1->PmsmSimUt_Y.Out.Theta) + ubeta * sin(sys1->PmsmSimUt_Y.Out.Theta);
-        double uq = -ualpha * sin(sys1->PmsmSimUt_Y.Out.Theta) + ubeta * cos(sys1->PmsmSimUt_Y.Out.Theta);
-        double udPu = ud * 10 * 16384 / VBASE;
-        double uqPu = uq * 10 * 16384 / VBASE;
+        double ud     = ualpha * cos(sys1->PmsmSimUt_Y.Out.Theta) + ubeta * sin(sys1->PmsmSimUt_Y.Out.Theta);
+        double uq     = -ualpha * sin(sys1->PmsmSimUt_Y.Out.Theta) + ubeta * cos(sys1->PmsmSimUt_Y.Out.Theta);
+        double udPu   = ud * 10 * 16384 / VBASE;
+        double uqPu   = uq * 10 * 16384 / VBASE;
 
-        //int16_t power1 = ((int32_t)McStatus1.Pu.swIalpha * McStatus1.Pu.swUalpha + (int32_t)McStatus1.Pu.swIbeta * McStatus1.Pu.swUbeta) >> 13; // Q15
+        // int16_t power = ((int32_t)McStatus1.Pu.swIalpha * McStatus1.Pu.swUalpha + (int32_t)McStatus1.Pu.swIbeta * McStatus1.Pu.swUbeta) >> 13; //Q15
         int16_t power = ((int32_t)udPu * McStatus1.Pu.swId + (int32_t)uqPu * McStatus1.Pu.swIq) >> 13; // Q15
         mth_voLPFilter(power, &scm_stMotoPwrInLpf);
 
         /* Power Limit Control */
-        pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi; 
-        pwr_stPwrLimIn.swIqrefPu = asr_stSpdPIOut.swIqRefPu;                   
-        pwr_stPwrLimIn.PCBTemp = GetParam(); //adc_stUpOut.PCBTemp;
-        pwr_voPwrLim2(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
+        pwr_stPwrLimIn.swMotorPwrPu = scm_stMotoPwrInLpf.slY.sw.hi;
+        pwr_stPwrLimIn.swPCBTemp    = get<0>(GetParam());
+        pwr_stPwrLimIn.uwBatCap     = get<1>(GetParam());
+        pwr_voPwrLimPI(&pwr_stPwrLimIn, &pwr_stPwrLimCof, &pwr_stPwrLimOut2); // Q14
 
         /* Speed Control */
-        if(i % (FTBC_HZ / FTBS_HZ) == 0)
+        if (i % (FTBC_HZ / FTBS_HZ) == 0)
         {
-            asr_stSpdPIIn.swSpdRefPu = spdRefPu;            
-            asr_stSpdPIIn.swSpdFdbPu = McStatus1.Pu.swElecOmega; 
-            asr_stSpdPIIn.swIqMaxPu = ABS(pwr_stPwrLimOut2.swIqLimPu);
-            asr_stSpdPIIn.swIqMinPu = -ABS(pwr_stPwrLimOut2.swIqLimPu);
+            asr_stSpdPIIn.swSpdRefPu = spdRefPu;
+            asr_stSpdPIIn.swSpdFdbPu = McStatus1.Pu.swElecOmega;
+            asr_stSpdPIIn.swIqMaxPu  = ABS(pwr_stPwrLimOut2.swIqLimPu);
+            asr_stSpdPIIn.swIqMinPu  = -ABS(pwr_stPwrLimOut2.swIqLimPu);
             asr_voSpdPI(&asr_stSpdPIIn, &asr_stSpdPICoef, &asr_stSpdPIOut);
         }
 
-        UdpScope::Send(0, pwr_stPwrLimOut2.swMotorPwrLimitActualPu, power, pwr_stPwrLimOut2.swIqLimPu, asr_stSpdPIOut.swIqRefPu, spdRefPu,  McStatus1.Pu.swElecOmega);
+        UdpScope::Send(0, pwr_stPwrLimOut2.swPwrLimActualPu, power, pwr_stPwrLimOut2.swIqLimPu, asr_stSpdPIOut.swIqRefPu, McStatus1.Pu.swIq,
+                       McStatus1.Pu.swElecOmega);
     }
 
-    EXPECT_NEAR(pwr_stPwrLimOut2.swMotorPwrLimitActualPu, scm_stMotoPwrInLpf.slY.sw.hi, 2);
+    /* 判断单元测试是否通过 */
+    /* 判断实际功率是否在限制功率以下 */
+    EXPECT_LE(scm_stMotoPwrInLpf.slY.sw.hi, (pwr_stPwrLimOut2.swPwrLimActualPu + 2));
+
+    /* 判断限制电流与设计值是否一致 */
+    if(ABS(pwr_stPwrLimOut2.swPwrLimActualPu - scm_stMotoPwrInLpf.slY.sw.hi) > 10)
+    {
+        double Iqlim;
+        if (pwr_stPwrLimIn.swPCBTemp < pwr_stPwrLimCof.swPwrLimStartTemp)
+        {
+            Iqlim = pwr_stPwrLimCof.swIqMaxPu;
+        }
+        else if(pwr_stPwrLimIn.swPCBTemp < pwr_stPwrLimCof.swPwrLimEndTemp)
+        {
+            Iqlim = pwr_stPwrLimCof.swIqMaxPu - ((int32_t)(pwr_stPwrLimIn.swPCBTemp - pwr_stPwrLimCof.swPwrLimStartTemp) * pwr_stPwrLimCof.swIqMaxPu * 83 / (pwr_stPwrLimCof.swPwrLimEndTemp - pwr_stPwrLimCof.swPwrLimStartTemp) >> 7);
+        }
+        else
+        {
+            Iqlim = (int32_t)pwr_stPwrLimCof.swIqMaxPu * 45 >> 7;
+        }
+
+        EXPECT_NEAR(Iqlim, pwr_stPwrLimOut2.swIqLimPu, 2);
+    }
+    else 
+    {
+        EXPECT_NEAR((pwr_stPwrLimCof.swIqMaxPu + pwr_stPwrLimOut2.swIqLimDetaSum), pwr_stPwrLimOut2.swIqLimPu, 2);
+    }
 }
 
-INSTANTIATE_TEST_SUITE_P(DiffTemp, PwrLimTest2, ::testing::Values(20, 90, 150));
+INSTANTIATE_TEST_SUITE_P(DiffTemp, PwrLimTest2,::testing::Combine(::testing::Values(60, 95, 100),::testing::Values(100, 15, 10, 0)));

+ 5 - 1
xmake.lua

@@ -20,9 +20,13 @@ target("unittest")
     add_files("User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Source/macroequ.c","User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Source/glbcof.c")
     add_files("User project/3.BasicFunction/Source/Cadence.c", "User project/3.BasicFunction/Source/bikespeed.c", "User project/3.BasicFunction/Source/torquesensor.c")
     add_files("User project/3.BasicFunction/Source/InputCapture.c")
+    add_files("User project/3.BasicFunction/Source/Temp.c")
+    -- add_files("User project/3.BasicFunction/Source/AssistCurve.c")
+    -- add_files("User project/3.BasicFunction/Source/bikebrake.c")
+    -- add_files("User project/3.BasicFunction/Source/bikegearsensor.c")
+    -- add_files("User project/3.BasicFunction/Source/flash_master.c")
     add_files("User project/2.MotorDrive/Source/adc.c", "User project/2.MotorDrive/Source/pwm.c", "User project/2.MotorDrive/Source/packed/torqobs.c")
     add_files("User project/2.MotorDrive/Source/packed/pwrlim.c", "User project/2.MotorDrive/Source/packed/asr.c", "User project/2.MotorDrive/Source/packed/brake.c")
-    add_files("User project/3.BasicFunction/Source/Temp.c")
     add_files("User project/1.FrameLayer/Source/sys_ctrl.c")
 
     add_includedirs("User project/1.FrameLayer/Include")