global(546).c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* includes ----------------------------------------------------------*/
  2. #include "global.h"
  3. /* typedef -----------------------------------------------------------*/
  4. /* define ------------------------------------------------------------*/
  5. /* macro -------------------------------------------------------------*/
  6. /* variables ---------------------------------------------------------*/
  7. global_par s_global_par;
  8. /* function prototypes -----------------------------------------------*/
  9. /**
  10. * @brief 中控板初始化
  11. * @param p_param_boot : 参数区指针
  12. * @param p_rs485RecDate:485接收数据区的指针
  13. * @note NONE
  14. * @retval 无
  15. */
  16. void centralCtrSys_Init(param_boot *p_param_boot, global_par *p_global_par){
  17. uint8_t recBack = 0;
  18. rs485RecDate *p_rs485RecDate = &s_rs485RecDate;
  19. // 1. 从flash中读出VIN
  20. Read_ParamArea(); // 读出参数区的所有数据
  21. // 2. 与车通信获取vin数据(读取所有寄存器的值)
  22. modbus_read_holding_registers(0x01, 0x0000, 67);
  23. HAL_Delay(5000); // 延时5s
  24. // 3. vin码比对
  25. if(s_comData.vinRecSuccess == 1){ // 成功获取到车辆数据
  26. if(memcmp(p_param_boot->vin, p_rs485RecDate->VIN, 26) != 0){ // 比对不成功
  27. // 备份新的VIN码
  28. memcpy(p_param_boot->vin, p_rs485RecDate->VIN, 26);
  29. Write_paramArea();
  30. // 置位中控板迁移事件
  31. BIT_SET(p_global_par->ctrEvent, CtrlPanelMigration);
  32. }
  33. }
  34. // 4. 初始化4g模块 4g模块在这里不好处理 放在任务中,围栏数据也一样
  35. // 5. 读取96位(或者说是3个32位的字)的唯一ID
  36. GetUniqueID(s_messageDate.devId);
  37. // 6. 陀螺仪的初始化
  38. recBack = atk_ms6050_init();
  39. if(recBack != 0){
  40. printf("ATK-MS6050 init failed!\r\n");
  41. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  42. }
  43. printf("ATK-MS6050 init\r\n");
  44. recBack = atk_ms6050_dmp_init();
  45. if(recBack != 0){
  46. printf("ATK-MS6050 DMP init failed!\r\n");
  47. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  48. }
  49. printf("ATK-MS6050 DMP init!\r\n");
  50. }
  51. // 电池故障判断
  52. void fault_bat(rs485RecDate *p_rs485RecDate, comData *p_comData){
  53. uint16_t errorLevel = 0;
  54. /* 尝试获取互斥量,等待无限长时间 */
  55. if(osMutexAcquire(mutex_rs485RecDateHandle, osWaitForever) == osOK)
  56. {
  57. errorLevel = p_rs485RecDate->alarmLevel;
  58. /* 访问完成,释放互斥量 */
  59. osMutexRelease(mutex_rs485RecDateHandle);
  60. }
  61. // 存在电池故障
  62. if(errorLevel != 0){
  63. BIT_SET(s_comData.Malfunction, batError); // 置位电池故障
  64. }else{
  65. BIT_CLEAR(s_comData.Malfunction, batError); // 清除电池故障
  66. }
  67. }
  68. /**
  69. * @brief 中控板故障检测
  70. * @param p_param_boot : 参数区指针
  71. * @param p_rs485RecDate:485接收数据区的指针
  72. * @note NONE
  73. * @retval 无
  74. */
  75. void faultDetection(comData *p_comData){
  76. uint8_t timesCnt_gps = 0; // gps记时
  77. // 1. 超出围栏故障判断
  78. if(p_comData->fenceStatus == 1){
  79. BIT_SET(s_comData.Malfunction, fence);
  80. }else{
  81. BIT_CLEAR(s_comData.Malfunction, fence);
  82. }
  83. // 2. 电池故障判断
  84. fault_bat(&s_rs485RecDate, &s_comData); // 电池故障判断
  85. // 3. GPS信号丢失
  86. // 获取gps失败,但陀螺仪数据正常的情况下,一分钟之后置位gps故障
  87. // 获取gps失败,陀螺仪数据异常,直接置位gps故障
  88. if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 0)){
  89. if(timesCnt_gps < 60){
  90. timesCnt_gps++;
  91. }else{
  92. timesCnt_gps = 60;
  93. BIT_SET(s_comData.Malfunction, positionError);
  94. }
  95. }else if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 1)){
  96. timesCnt_gps = 0;
  97. BIT_SET(s_comData.Malfunction, positionError);
  98. }else{
  99. timesCnt_gps = 0;
  100. }
  101. if(p_global_par->positionErrorCnt == 0){ // 清除gps故障
  102. BIT_CLEAR(s_comData.Malfunction, positionError);
  103. }
  104. // 4. MQTT通信异常
  105. if(p_global_par->mqttTimeoutCnt >= PUBLISH_TIME_MS){
  106. BIT_SET(s_comData.Malfunction, errorMqtt); // 置位MQTT故障
  107. }
  108. if(p_global_par->mqttTimeoutCnt == 0){ // 清除MQTT故障
  109. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  110. }
  111. // 5. 4G模块初始化故障
  112. if(p_global_par->InitFaultFlag_4G == 1){
  113. BIT_SET(s_comData.Malfunction, init_4G_error); // 置位4g模块初始化故障
  114. }else{
  115. BIT_CLEAR(s_comData.Malfunction, init_4G_error); // 清除4g模块初始化故障
  116. }
  117. // 6. 485通信故障
  118. if(p_global_par->timeoutCnt_485 >= TIMEOUT_485){
  119. BIT_SET(s_comData.Malfunction, com485); // 置位485通信故障
  120. }
  121. if(p_global_par->timeoutCnt_48 == 0){
  122. BIT_CLEAR(s_comData.Malfunction, com485); // 置位485通信故障
  123. }
  124. }