protect_check.c 10 KB

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