protect_check.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. //旧板无MOS温度传感器,根据PCB温度和绕组温度
  245. if( MC_HallSensorData.InverterExistFlag==TRUE )
  246. {
  247. //单片机100度保护,绕组135度保护
  248. if( (T_PCB < (TH - 25))&&(T_Coil < (TH + 10)) )
  249. {
  250. OT_Set_TimeCnt = HAL_GetTick();
  251. }
  252. }
  253. //新板有MOS温度传感器,根据PCB温度、MOS温度、绕组温度
  254. else
  255. {
  256. //单片机100度保护,绕组135度保护,MOS管105度保护
  257. if( (T_PCB < (TH - 25))&&(T_Coil < (TH + 10))&&(T_MCU < (TH - 20)) )
  258. {
  259. OT_Set_TimeCnt = HAL_GetTick();
  260. }
  261. }
  262. //过热保护判断
  263. if((HAL_GetTick() - OT_Set_TimeCnt) > 5000)
  264. {
  265. OT_Reset_TimeCnt = HAL_GetTick();
  266. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 1;
  267. //记录故障日志
  268. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  269. IsErrorLogSaveInfoUpdateFlag = TRUE;
  270. //存储故障次数
  271. MC_RunLog1.OT_ProtectCnt++;
  272. RunLogSaveIndex = 1;
  273. }
  274. }
  275. else
  276. {
  277. if( MC_HallSensorData.InverterExistFlag==TRUE )
  278. {
  279. //过热保护恢复 PCB温度小于90度,绕组温度小于105度,且保护时间大于5分钟
  280. if(( (T_PCB < (TH - 35)) && (T_Coil < (TH - 20)) ) && ((HAL_GetTick() - OT_Reset_TimeCnt) > 300000))
  281. {
  282. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 0;
  283. OT_Set_TimeCnt = HAL_GetTick();
  284. }
  285. }
  286. else
  287. { //过热保护恢复 PCB温度小于80度,绕组温度小于105度,MOS管温度小于95度,且保护时间大于5分钟
  288. if(( (T_PCB < (TH - 45)) && (T_Coil < (TH - 20))&&(T_MCU < (TH - 30)) ) && ((HAL_GetTick() - OT_Reset_TimeCnt) > 300000))
  289. {
  290. p_MC_ErrorCode->ERROR_Bit.Protect_OverTemp = 0;
  291. OT_Set_TimeCnt = HAL_GetTick();
  292. }
  293. }
  294. }
  295. }
  296. //电压波动异常保护检测
  297. void MC_Protect_VoltageChange_Process(uint16_t Voltage, uint16_t Current, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  298. {
  299. static uint32_t PeriodTimeCnt = 0;
  300. uint32_t DiffSqrtResult = 0;
  301. static uint8_t TrigCnt = 0;
  302. static uint16_t Array[10] = {0};
  303. uint16_t i;
  304. if((HAL_GetTick() - PeriodTimeCnt) >= 100)
  305. {
  306. for(i=0; i<(sizeof(Array) / 2 - 1); i++)
  307. {
  308. Array[i] = Array[i + 1];
  309. }
  310. Array[sizeof(Array) / 2 - 1] = Voltage;
  311. DiffSqrtResult = GetStandardDeviation(Array, sizeof(Array) >> 1);
  312. //保护判断
  313. if(p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange == 0)
  314. {
  315. if((Current < 50) && (DiffSqrtResult > 1000))
  316. {
  317. if(TrigCnt > 50) // 5s
  318. {
  319. p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange = 1;
  320. //记录故障日志
  321. MC_ErrorLogSaveInfo.NotesInfo1 = DiffSqrtResult / 256;
  322. MC_ErrorLogSaveInfo.NotesInfo2 = GetMaxData(Array, sizeof(Array) >> 1);
  323. MC_ErrorLogSaveInfo.NotesInfo3 = GetMinData(Array, sizeof(Array) >> 1);
  324. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  325. IsErrorLogSaveInfoUpdateFlag = TRUE;
  326. //存储故障次数
  327. MC_RunLog2.VoltageChange_FaultCnt++;
  328. RunLogSaveIndex = 2;
  329. }
  330. }
  331. else
  332. {
  333. TrigCnt = 0;
  334. }
  335. }
  336. else
  337. {
  338. //恢复判断
  339. if(DiffSqrtResult <= 1000)
  340. {
  341. p_MC_ErrorCode->ERROR_Bit.Protect_VoltageChange = 0;
  342. }
  343. }
  344. }
  345. }
  346. //软件过流保护检测
  347. void MC_OverCurrent_SoftProtect_Process(uint16_t BusCurrent, uint16_t MaxCurrent, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  348. {
  349. static uint32_t Protect_TimeCnt = 0;
  350. static uint32_t Fault_TimeCnt = 0;
  351. uint16_t Current_Th;
  352. Current_Th = (MaxCurrent * 3) >> 1;//1.5倍
  353. if(p_MC_ErrorCode->ERROR_Bit.Protect_OverCurrent == 0)
  354. {
  355. if(BusCurrent < Current_Th)
  356. {
  357. Protect_TimeCnt = HAL_GetTick();
  358. }
  359. if((HAL_GetTick() - Protect_TimeCnt) > 5000)
  360. {
  361. p_MC_ErrorCode->ERROR_Bit.Protect_OverCurrent = 1;
  362. //记录故障日志
  363. MC_ErrorLogSaveInfo.NotesInfo1 = 2;
  364. MC_ErrorLogSaveInfo.NotesInfo2 = Current_Th;
  365. ErrorLogSave_Update(&MC_ErrorLogSaveInfo);
  366. IsErrorLogSaveInfoUpdateFlag = TRUE;
  367. //存储故障次数
  368. MC_RunLog1.OC_ProtectCnt++;
  369. RunLogSaveIndex = 1;
  370. Fault_TimeCnt = HAL_GetTick();
  371. }
  372. }
  373. else
  374. {
  375. //过流保护恢复
  376. if((HAL_GetTick() - Fault_TimeCnt) > 5000)
  377. {
  378. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  379. }
  380. }
  381. }
  382. /******************************全局函数定义******************************/
  383. #define OC_CLEARFLAG_DELAYTIME 15 //过流标志间隔清零延时,单位ms
  384. #define OC_COUNTER_TH 1000 //过流保护计数判断阈值
  385. //过流保护检测
  386. /*
  387. 检测原理:
  388. 1、过流触发后,OverCurrentTrigFlag置位,封波2个周期后再次发波;
  389. 2、在过流触发计数达到OC_COUNTER_TH 次之前,如果存在两次过流触发间隔时间超过OC_CLEARFLAG_DELAYTIME ms,清除过流触发计数;
  390. 3、如果每两次过流触发间隔时间都不超过OC_CLEARFLAG_DELAYTIME ms,当过流触发计数达到OC_COUNTER_TH 次,则进入过流保护;
  391. 4、进入过流保护后,超时5s解除。
  392. 更改处理方式20210630
  393. 1、关闭过流告警;
  394. 2、出现过流信号时,封闭PWM2个周期
  395. */
  396. void MC_Protect_OverCurrent_Process(FlagStatus* OverCurrentTrigFlag, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  397. {
  398. static uint8_t OffPwmCnt = 0; //过流信号触发后,关闭PWM延时计数
  399. static uint8_t StarPwmCnt = 0; //关闭PWM后启动PWM延时计数
  400. if(*OverCurrentTrigFlag == RESET)
  401. {
  402. //关闭PWM计数清零
  403. OffPwmCnt = 0;
  404. //开启PWM
  405. if(StarPwmCnt == 0)
  406. {
  407. StarPwmCnt++;
  408. Enable_PwmGpio_Out();
  409. }
  410. }
  411. else
  412. {
  413. //开启PWM计数清零
  414. StarPwmCnt = 0;
  415. //2个PWM周期后,过流触发标志复位
  416. if(OffPwmCnt >= 1)
  417. {
  418. *OverCurrentTrigFlag = RESET;
  419. }
  420. else
  421. {
  422. //关闭PWM
  423. Disable_PwmGpio_Out();
  424. }
  425. OffPwmCnt++;
  426. }
  427. }
  428. //保护判断
  429. void MC_Protect_Check_Process(void)
  430. {
  431. //低压保护检测
  432. MC_Protect_UnderVoltage_Process(IsComOK_BMS.IsOK_Flag, MC_RunInfo.SOC, ((IsComOK_BMS.IsOK_Flag == TRUE) ? BMS_RunInfo.Voltage : MC_RunInfo.BusVoltage),
  433. MC_MotorParam.Rate_Voltage, ((MC_ConfigParam1.UV_Protect_TH == 0) ? 3000 : MC_ConfigParam1.UV_Protect_TH),
  434. &MC_ErrorCode);
  435. //过压保护检测
  436. if(MC_RunInfo.BusCurrent < 5000)
  437. {
  438. MC_Protect_OverVoltage_Process(((IsComOK_BMS.IsOK_Flag == TRUE) ? BMS_RunInfo.Voltage : MC_RunInfo.BusVoltage), MC_MotorParam.Rate_Voltage, &MC_ErrorCode);
  439. }
  440. //堵转保护检测
  441. MC_Protect_RotorLock_Process(MC_RunInfo.BusCurrent, MC_HallSensorData.IsStopFlag, &MC_ErrorCode);
  442. //过热保护检测
  443. MC_Protect_OverHeat_Process(MC_RunInfo.T_MCU, MC_RunInfo.T_PCB, MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Protect, &MC_ErrorCode);
  444. //电压波动异常保护检测
  445. MC_Protect_VoltageChange_Process(MC_RunInfo.BusVoltage, MC_RunInfo.BusCurrent, &MC_ErrorCode);
  446. //软件过流保护检测
  447. MC_OverCurrent_SoftProtect_Process(MC_RunInfo.BusCurrent, MC_ConfigParam1.CurrentLimit * 1000, &MC_ErrorCode);
  448. }