protect_check.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #include "protect_check.h"
  2. #include "math_tools.h"
  3. #include "gpio.h"
  4. #include "hall_sensor.h"
  5. #include "eeprom_24c02.h"
  6. #include "log_save.h"
  7. /*****************************全局变量定义*******************************/
  8. //过流保护检测参数
  9. FlagStatus MC_Protect_OverCurrentTrig_Flag = RESET;
  10. /******************************局部函数定义******************************/
  11. //低压保护检测
  12. void MC_Protect_UnderVoltage_Process(TrueOrFalse_Flag_Struct_t IsBMS_ComOK, uint8_t SOC, uint16_t BusVoltage, uint8_t DesignVoltage, uint16_t UV_TH, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  13. {
  14. static uint32_t uvTimeCnt = 0;
  15. static uint32_t uvFaultTimeCnt = 0;
  16. uint16_t UV_Voltage = 0;
  17. //根据额定电压更新保护阈值
  18. switch(DesignVoltage)
  19. {
  20. case 24:
  21. {
  22. UV_Voltage = UV_TH * 7;//低压保护时,电芯电压为3100mV
  23. break;
  24. }
  25. case 36:
  26. {
  27. UV_Voltage = UV_TH * 10;//低压保护时,电芯电压为3100mV
  28. break;
  29. }
  30. case 48:
  31. {
  32. UV_Voltage = UV_TH * 13;//低压保护时,电芯电压为3100mV
  33. break;
  34. }
  35. default:
  36. {
  37. UV_Voltage = BusVoltage;
  38. break;
  39. }
  40. }
  41. if(IsBMS_ComOK == TRUE)//电池通讯正常,根据BMS的SOC作为低压保护
  42. {
  43. if(p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage == 0)
  44. {
  45. //低压保护判断
  46. if((SOC >= 1) && (BusVoltage >= UV_Voltage))
  47. {
  48. uvTimeCnt = HAL_GetTick();
  49. }
  50. if((HAL_GetTick() - uvTimeCnt) > 5000)
  51. {
  52. p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage = 1;
  53. //记录故障日志
  54. MC_ErrorLogSaveInfo.NotesInfo1 = BusVoltage;
  55. MC_ErrorLogSaveInfo.NotesInfo2 = (uint16_t)IsBMS_ComOK;
  56. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  57. IsErrorLogSaveInfoUpdateFlag = TRUE;
  58. //存储故障次数
  59. MC_RunLog1.UV_ProtectCnt++;
  60. RunLogSaveIndex = 1;
  61. }
  62. uvFaultTimeCnt = HAL_GetTick();
  63. }
  64. else
  65. {
  66. //低压保护恢复
  67. if((SOC < 1) || (BusVoltage < (UV_Voltage + 1000)))
  68. {
  69. uvFaultTimeCnt = HAL_GetTick();
  70. }
  71. if((HAL_GetTick() - uvFaultTimeCnt) > 5000)
  72. {
  73. p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage = 0;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. if(p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage == 0)
  80. {
  81. //低压保护判断
  82. if(BusVoltage >= UV_Voltage)
  83. {
  84. uvTimeCnt = HAL_GetTick();
  85. }
  86. if((HAL_GetTick() - uvTimeCnt) > 5000)
  87. {
  88. p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage = 1;
  89. //记录故障日志
  90. MC_ErrorLogSaveInfo.NotesInfo1 = BusVoltage;
  91. MC_ErrorLogSaveInfo.NotesInfo2 = (uint16_t)IsBMS_ComOK;
  92. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  93. IsErrorLogSaveInfoUpdateFlag = TRUE;
  94. //存储故障次数
  95. MC_RunLog1.UV_ProtectCnt++;
  96. RunLogSaveIndex = 1;
  97. }
  98. uvFaultTimeCnt = HAL_GetTick();
  99. }
  100. else
  101. {
  102. //低压保护恢复
  103. if(BusVoltage < (UV_Voltage + 1000))
  104. {
  105. uvFaultTimeCnt = HAL_GetTick();
  106. }
  107. if((HAL_GetTick() - uvFaultTimeCnt) > 5000)
  108. {
  109. p_MC_ErrorCode->ERROR_Bit.Protect_UnderVoltage = 0;
  110. }
  111. }
  112. }
  113. }
  114. //过压保护检测
  115. void MC_Protect_OverVoltage_Process(uint16_t BusVoltage, uint16_t DesignVoltage, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  116. {
  117. static uint32_t ovTimeCnt = 0;
  118. static uint32_t ovFaultTimeCnt = 0;
  119. uint16_t OV_Voltage = 0;
  120. //根据额定电压更新保护阈值
  121. switch(DesignVoltage)
  122. {
  123. case 24:
  124. {
  125. OV_Voltage = 4500 * 7;//过压保护时,电芯电压为4500mV
  126. break;
  127. }
  128. case 36:
  129. {
  130. OV_Voltage = 4500 * 10;//过压保护时,电芯电压为4500mV
  131. break;
  132. }
  133. case 48:
  134. {
  135. OV_Voltage = 4500 * 13;//过压保护时,电芯电压为4500mV
  136. break;
  137. }
  138. default:
  139. {
  140. OV_Voltage = BusVoltage;
  141. break;
  142. }
  143. }
  144. if(p_MC_ErrorCode->ERROR_Bit.Protect_OverVoltage == 0)
  145. {
  146. //过压保护判断
  147. if(BusVoltage <= OV_Voltage)
  148. {
  149. ovTimeCnt = HAL_GetTick();
  150. }
  151. if((HAL_GetTick() - ovTimeCnt) > 5000)
  152. {
  153. p_MC_ErrorCode->ERROR_Bit.Protect_OverVoltage = 1;
  154. //记录故障日志
  155. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  156. IsErrorLogSaveInfoUpdateFlag = TRUE;
  157. //存储故障次数
  158. MC_RunLog1.OV_ProtectCnt++;
  159. RunLogSaveIndex = 1;
  160. }
  161. ovFaultTimeCnt = HAL_GetTick();
  162. }
  163. else
  164. {
  165. //过压保护恢复
  166. if(BusVoltage > (OV_Voltage - 500))
  167. {
  168. ovFaultTimeCnt = HAL_GetTick();
  169. }
  170. if((HAL_GetTick() - ovFaultTimeCnt) > 5000)
  171. {
  172. p_MC_ErrorCode->ERROR_Bit.Protect_OverVoltage = 0;
  173. }
  174. }
  175. }
  176. //堵转保护检测
  177. void MC_Protect_RotorLock_Process(uint16_t BusCurrent, TrueOrFalse_Flag_Struct_t IsStopFlag, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  178. {
  179. static uint32_t Protect_1st_TimeCnt = 0;
  180. static uint32_t Protect_2st_TimeCnt = 0;
  181. static uint32_t FaultTimeCnt = 0;
  182. if(p_MC_ErrorCode->ERROR_Bit.Protect_LockRotor == 0)
  183. {
  184. //堵转保护检测
  185. //一级电流堵转:电流大于2.5A,超时8s
  186. //二级电流堵转:电流大于7.5A,超时3s
  187. if(IsStopFlag == FALSE)
  188. {
  189. Protect_1st_TimeCnt = HAL_GetTick();
  190. Protect_2st_TimeCnt = HAL_GetTick();
  191. }
  192. else
  193. {
  194. if(BusCurrent < 2500)
  195. {
  196. Protect_1st_TimeCnt = HAL_GetTick();
  197. Protect_2st_TimeCnt = HAL_GetTick();
  198. }
  199. else if(BusCurrent < 7500)
  200. {
  201. Protect_2st_TimeCnt = HAL_GetTick();
  202. }
  203. }
  204. if(((HAL_GetTick() - Protect_1st_TimeCnt) > 8000) ||
  205. ((HAL_GetTick() - Protect_2st_TimeCnt) > 3000))
  206. {
  207. p_MC_ErrorCode->ERROR_Bit.Protect_LockRotor = 1;
  208. //记录故障日志
  209. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  210. IsErrorLogSaveInfoUpdateFlag = TRUE;
  211. //存储故障次数
  212. MC_RunLog1.LockRotor_ProtectCnt++;
  213. RunLogSaveIndex = 1;
  214. FaultTimeCnt = HAL_GetTick();
  215. }
  216. }
  217. else
  218. {
  219. //堵转保护恢复
  220. if((HAL_GetTick() - FaultTimeCnt) > 5000)
  221. {
  222. if((MC_ControlCode.GearSt == MC_GearSt_OFF) && //关闭助力
  223. (ADC_SensorData.TorqueSensor < 10) && //无踩踏力矩
  224. (ADC_SensorData.GasSensor < 10) && //指拨为0
  225. (MC_CadenceResult.IsStopFlag == TRUE)) //踏频停止
  226. {
  227. p_MC_ErrorCode->ERROR_Bit.Protect_LockRotor = 0;
  228. }
  229. }
  230. }
  231. }
  232. //过热保护检测
  233. void MC_Protect_OverHeat_Process(uint8_t T_MCU, uint8_t T_PCB, uint8_t T_Coil, uint8_t TH, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  234. {
  235. static uint32_t OT_Set_TimeCnt = 0;
  236. static uint32_t OT_Reset_TimeCnt = 0;
  237. if(HAL_GetTick() < 3000)
  238. {
  239. OT_Set_TimeCnt = HAL_GetTick();
  240. OT_Reset_TimeCnt = HAL_GetTick();
  241. }
  242. if(p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp == 0)
  243. {
  244. //TTKZ019A之前的电路板,单片机100度保护,绕组130度保护
  245. if( MC_HallSensorData.InverterExistFlag==TRUE )
  246. {
  247. if( (T_PCB < (TH - 25))&&(T_Coil < (TH + 5)) )
  248. {
  249. OT_Set_TimeCnt = HAL_GetTick();
  250. }
  251. }
  252. else //TTKZ019A之后的电路板,单片机90度保护,绕组130度保护,MOS管110度保护,
  253. {
  254. if( (T_PCB < (TH - 35))&&(T_Coil < (TH + 5))&&(T_MCU < (TH - 15)) )
  255. {
  256. OT_Set_TimeCnt = HAL_GetTick();
  257. }
  258. }
  259. //过热保护判断
  260. if((HAL_GetTick() - OT_Set_TimeCnt) > 5000)
  261. {
  262. OT_Reset_TimeCnt = HAL_GetTick();
  263. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 1;
  264. //记录故障日志
  265. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  266. IsErrorLogSaveInfoUpdateFlag = TRUE;
  267. //存储故障次数
  268. MC_RunLog1.OT_ProtectCnt++;
  269. RunLogSaveIndex = 1;
  270. }
  271. }
  272. else
  273. {
  274. //过热保护恢复
  275. if( MC_HallSensorData.InverterExistFlag==TRUE )
  276. {
  277. if(( (T_PCB < (TH - 25)) && (T_Coil < (TH - 20)) ) && ((HAL_GetTick() - OT_Reset_TimeCnt) > 300000))
  278. {
  279. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 0;
  280. OT_Set_TimeCnt = HAL_GetTick();
  281. }
  282. }
  283. else
  284. {
  285. if(( (T_PCB < (TH - 35)) && (T_Coil < (TH - 20))&&(T_MCU < (TH - 15)) ) && ((HAL_GetTick() - OT_Reset_TimeCnt) > 300000))
  286. {
  287. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 0;
  288. OT_Set_TimeCnt = HAL_GetTick();
  289. }
  290. }
  291. }
  292. }
  293. //电压波动异常保护检测
  294. void MC_Protect_VoltageChange_Process(uint16_t Voltage, uint16_t Current, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  295. {
  296. static uint32_t PeriodTimeCnt = 0;
  297. uint32_t DiffSqrtResult = 0;
  298. static uint8_t TrigCnt = 0;
  299. static uint16_t Array[10] = {0};
  300. uint16_t i;
  301. if((HAL_GetTick() - PeriodTimeCnt) >= 100)
  302. {
  303. for(i=0; i<(sizeof(Array) / 2 - 1); i++)
  304. {
  305. Array[i] = Array[i + 1];
  306. }
  307. Array[sizeof(Array) / 2 - 1] = Voltage;
  308. DiffSqrtResult = GetStandardDeviation(Array, sizeof(Array) >> 1);
  309. //保护判断
  310. if(p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange == 0)
  311. {
  312. if((Current < 50) && (DiffSqrtResult > 1000))
  313. {
  314. if(TrigCnt > 50) // 5s
  315. {
  316. p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange = 1;
  317. //记录故障日志
  318. MC_ErrorLogSaveInfo.NotesInfo1 = DiffSqrtResult / 256;
  319. MC_ErrorLogSaveInfo.NotesInfo2 = GetMaxData(Array, sizeof(Array) >> 1);
  320. MC_ErrorLogSaveInfo.NotesInfo3 = GetMinData(Array, sizeof(Array) >> 1);
  321. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  322. IsErrorLogSaveInfoUpdateFlag = TRUE;
  323. //存储故障次数
  324. MC_RunLog2.VoltageChange_FaultCnt++;
  325. RunLogSaveIndex = 2;
  326. }
  327. }
  328. else
  329. {
  330. TrigCnt = 0;
  331. }
  332. }
  333. else
  334. {
  335. //恢复判断
  336. if(DiffSqrtResult <= 1000)
  337. {
  338. p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange = 0;
  339. }
  340. }
  341. }
  342. }
  343. //软件过流保护检测
  344. void MC_OverCurrent_SoftProtect_Process(uint16_t BusCurrent, uint16_t MaxCurrent, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  345. {
  346. static uint32_t Protect_TimeCnt = 0;
  347. static uint32_t Fault_TimeCnt = 0;
  348. uint16_t Current_Th;
  349. Current_Th = (MaxCurrent * 3) >> 1;//1.5倍
  350. if(p_MC_ErrorCode->ERROR_Bit.Protect_OverCurrent == 0)
  351. {
  352. if(BusCurrent < Current_Th)
  353. {
  354. Protect_TimeCnt = HAL_GetTick();
  355. }
  356. if((HAL_GetTick() - Protect_TimeCnt) > 5000)
  357. {
  358. p_MC_ErrorCode->ERROR_Bit.Protect_OverCurrent = 1;
  359. //记录故障日志
  360. MC_ErrorLogSaveInfo.NotesInfo1 = 2;
  361. MC_ErrorLogSaveInfo.NotesInfo2 = Current_Th;
  362. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  363. IsErrorLogSaveInfoUpdateFlag = TRUE;
  364. //存储故障次数
  365. MC_RunLog1.OC_ProtectCnt++;
  366. RunLogSaveIndex = 1;
  367. Fault_TimeCnt = HAL_GetTick();
  368. }
  369. }
  370. else
  371. {
  372. //过流保护恢复
  373. if((HAL_GetTick() - Fault_TimeCnt) > 5000)
  374. {
  375. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  376. }
  377. }
  378. }
  379. /******************************全局函数定义******************************/
  380. #define OC_CLEARFLAG_DELAYTIME 15 //过流标志间隔清零延时,单位ms
  381. #define OC_COUNTER_TH 1000 //过流保护计数判断阈值
  382. //过流保护检测
  383. /*
  384. 检测原理:
  385. 1、过流触发后,OverCurrentTrigFlag置位,封波2个周期后再次发波;
  386. 2、在过流触发计数达到OC_COUNTER_TH 次之前,如果存在两次过流触发间隔时间超过OC_CLEARFLAG_DELAYTIME ms,清除过流触发计数;
  387. 3、如果每两次过流触发间隔时间都不超过OC_CLEARFLAG_DELAYTIME ms,当过流触发计数达到OC_COUNTER_TH 次,则进入过流保护;
  388. 4、进入过流保护后,超时5s解除。
  389. 更改处理方式20210630
  390. 1、关闭过流告警;
  391. 2、出现过流信号时,封闭PWM2个周期
  392. */
  393. void MC_Protect_OverCurrent_Process(FlagStatus* OverCurrentTrigFlag, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  394. {
  395. static uint8_t OffPwmCnt = 0; //过流信号触发后,关闭PWM延时计数
  396. static uint8_t StarPwmCnt = 0; //关闭PWM后启动PWM延时计数
  397. if(*OverCurrentTrigFlag == RESET)
  398. {
  399. //关闭PWM计数清零
  400. OffPwmCnt = 0;
  401. //开启PWM
  402. if(StarPwmCnt == 0)
  403. {
  404. StarPwmCnt++;
  405. Enable_PwmGpio_Out();
  406. }
  407. }
  408. else
  409. {
  410. //开启PWM计数清零
  411. StarPwmCnt = 0;
  412. //2个PWM周期后,过流触发标志复位
  413. if(OffPwmCnt >= 1)
  414. {
  415. *OverCurrentTrigFlag = RESET;
  416. }
  417. else
  418. {
  419. //关闭PWM
  420. Disable_PwmGpio_Out();
  421. }
  422. OffPwmCnt++;
  423. }
  424. }
  425. //保护判断
  426. void MC_Protect_Check_Process(void)
  427. {
  428. //低压保护检测
  429. MC_Protect_UnderVoltage_Process(IsComOK_BMS.IsOK_Flag, MC_RunInfo.SOC, ((IsComOK_BMS.IsOK_Flag == TRUE) ? BMS_RunInfo.Voltage : MC_RunInfo.BusVoltage),
  430. MC_MotorParam.Rate_Voltage, ((MC_ConfigParam1.UV_Protect_TH == 0) ? 3100 : MC_ConfigParam1.UV_Protect_TH),
  431. &MC_ErrorCode);
  432. //过压保护检测
  433. MC_Protect_OverVoltage_Process(((IsComOK_BMS.IsOK_Flag == TRUE) ? BMS_RunInfo.Voltage : MC_RunInfo.BusVoltage), MC_MotorParam.Rate_Voltage, &MC_ErrorCode);
  434. //堵转保护检测
  435. MC_Protect_RotorLock_Process(MC_RunInfo.BusCurrent, MC_HallSensorData.IsStopFlag, &MC_ErrorCode);
  436. //过热保护检测
  437. MC_Protect_OverHeat_Process(MC_RunInfo.T_MCU, MC_RunInfo.T_PCB, MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Protect, &MC_ErrorCode);
  438. //电压波动异常保护检测
  439. MC_Protect_VoltageChange_Process(MC_RunInfo.BusVoltage, MC_RunInfo.BusCurrent, &MC_ErrorCode);
  440. //软件过流保护检测
  441. MC_OverCurrent_SoftProtect_Process(MC_RunInfo.BusCurrent, MC_ConfigParam1.CurrentLimit * 1000, &MC_ErrorCode);
  442. }