protect_check.c 11 KB

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