global(6954).c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* includes ----------------------------------------------------------*/
  2. #include "global.h"
  3. /* typedef -----------------------------------------------------------*/
  4. /* define ------------------------------------------------------------*/
  5. /* macro -------------------------------------------------------------*/
  6. /* variables ---------------------------------------------------------*/
  7. global_par s_global_par;
  8. /* function prototypes -----------------------------------------------*/
  9. // 定义指令周期数,用于实现1ms的延时
  10. // 假设时钟频率为72MHz,每个指令周期为1/72MHz = 13.889ns
  11. // 因此,1ms需要72000个指令周期
  12. #define INSTRUCTION_CYCLES_PER_MS 72000
  13. // 延时函数
  14. void global_delay_ms(uint32_t ms) {
  15. uint32_t i;
  16. uint32_t cycles = ms * INSTRUCTION_CYCLES_PER_MS; // 计算需要消耗的指令周期数
  17. // 消耗指令周期
  18. for (i = 0; i < cycles; ++i) {
  19. __NOP(); // 使用NOP指令来消耗一个指令周期
  20. }
  21. }
  22. /**
  23. * @brief 中控板初始化
  24. * @param p_param_boot : 参数区指针
  25. * @param p_rs485RecDate:485接收数据区的指针
  26. * @note NONE
  27. * @retval 无
  28. */
  29. void centralCtrSys_Init(param_boot *p_param_boot, global_par *p_global_par){
  30. uint8_t recBack = 0;
  31. rs485RecDate *p_rs485RecDate = &s_rs485RecDate;
  32. // 1. 从flash中读出VIN
  33. Read_ParamArea(); // 读出参数区的所有数据
  34. // 2. 与车通信获取vin数据(读取所有寄存器的值)
  35. modbus_read_holding_registers(0x01, 0x0000, 67);
  36. global_delay_ms(1000); // 延时3s
  37. // 3. vin码比对
  38. if(s_comData.vinRecSuccess == 1){ // 成功获取到车辆数据
  39. if(memcmp(p_param_boot->vin, p_rs485RecDate->VIN, 26) != 0){ // 比对不成功
  40. // 备份新的VIN码
  41. memcpy(p_param_boot->vin, p_rs485RecDate->VIN, 26);
  42. Write_paramArea();
  43. // 置位中控板迁移事件
  44. BIT_SET(p_global_par->ctrEvent, CtrlPanelMigration);
  45. }
  46. }
  47. // 4. 初始化4g模块 4g模块在这里不好处理 放在任务中,围栏数据也一样
  48. // 5. 读取96位(或者说是3个32位的字)的唯一ID
  49. GetUniqueID(s_messageDate.devId);
  50. // 6. 陀螺仪的初始化
  51. recBack = atk_ms6050_init();
  52. if(recBack != 0){
  53. printf("ATK-MS6050 init failed!\r\n");
  54. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  55. }
  56. printf("ATK-MS6050 init\r\n");
  57. recBack = atk_ms6050_dmp_init();
  58. if(recBack != 0){
  59. printf("ATK-MS6050 DMP init failed!\r\n");
  60. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  61. }
  62. printf("ATK-MS6050 DMP init!\r\n");
  63. }
  64. // 电池故障判断
  65. void fault_bat(rs485RecDate *p_rs485RecDate, comData *p_comData){
  66. uint16_t errorLevel = 0;
  67. /* 尝试获取互斥量,等待无限长时间 */
  68. if(osMutexAcquire(mutex_rs485RecDateHandle, osWaitForever) == osOK)
  69. {
  70. errorLevel = p_rs485RecDate->alarmLevel;
  71. /* 访问完成,释放互斥量 */
  72. osMutexRelease(mutex_rs485RecDateHandle);
  73. }
  74. // 存在电池故障
  75. if(errorLevel != 0){
  76. BIT_SET(s_comData.Malfunction, batError); // 置位电池故障
  77. }else{
  78. BIT_CLEAR(s_comData.Malfunction, batError); // 清除电池故障
  79. }
  80. }
  81. /**
  82. * @brief 中控板故障检测
  83. * @param p_global_par:global_par的全局参数指针
  84. * @note 周期为1s1次 清除除电池故障的其他故障
  85. * @retval 无
  86. */
  87. void faultDetection(global_par *p_global_par){
  88. // 参数定义
  89. static uint8_t timesCnt_gps = 0; // gps记时
  90. static uint8_t errorCnt_gyro = 0; // 陀螺仪数据故障滤波次数
  91. static uint16_t timesOut_ota = 0; // OTA超时计数
  92. comData *p_comData = &s_comData;
  93. // 1. 电池故障判断
  94. fault_bat(&s_rs485RecDate, &s_comData); // 电池故障判断
  95. // 开启超级权限 不判断除电池故障的其他故障
  96. if(p_global_par->superUser == 1){
  97. BIT_CLEAR(s_comData.Malfunction, fence);
  98. BIT_CLEAR(s_comData.Malfunction, positionError);
  99. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  100. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  101. BIT_CLEAR(s_comData.Malfunction, init_4G_error);
  102. BIT_CLEAR(s_comData.Malfunction, com485);
  103. BIT_CLEAR(s_comData.Malfunction, gyroscope);
  104. BIT_CLEAR(s_comData.Malfunction, OTA_fault);
  105. return;
  106. }
  107. // 2. 超出围栏故障判断
  108. if(p_comData->fenceStatus == 1){
  109. BIT_SET(s_comData.Malfunction, fence);
  110. }else{
  111. BIT_CLEAR(s_comData.Malfunction, fence);
  112. }
  113. // 3. GPS信号丢失
  114. // 获取gps失败,但陀螺仪数据正常的情况下,一分钟之后置位gps故障
  115. // 获取gps失败,陀螺仪数据异常,直接置位gps故障
  116. if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 0)){
  117. if(timesCnt_gps < 60){
  118. timesCnt_gps++;
  119. }else{
  120. timesCnt_gps = 60;
  121. BIT_SET(s_comData.Malfunction, positionError);
  122. }
  123. }else if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 1)){
  124. timesCnt_gps = 0;
  125. BIT_SET(s_comData.Malfunction, positionError);
  126. }else{
  127. timesCnt_gps = 0;
  128. }
  129. if(p_global_par->positionErrorCnt == 0){ // 清除gps故障
  130. BIT_CLEAR(s_comData.Malfunction, positionError);
  131. }
  132. // 4. MQTT通信异常
  133. if(p_global_par->mqttTimeoutCnt >= PUBLISH_TIME_MS){
  134. BIT_SET(s_comData.Malfunction, errorMqtt); // 置位MQTT故障
  135. }
  136. if(p_global_par->mqttTimeoutCnt == 0){ // 清除MQTT故障
  137. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  138. }
  139. // 5. 4G模块初始化故障
  140. if(p_global_par->InitFaultFlag_4G == 1){
  141. BIT_SET(s_comData.Malfunction, init_4G_error); // 置位4g模块初始化故障
  142. }else{
  143. BIT_CLEAR(s_comData.Malfunction, init_4G_error); // 清除4g模块初始化故障
  144. }
  145. // 6. 485通信故障
  146. if(p_global_par->timeoutCnt_485 >= TIMEOUT_485){
  147. BIT_SET(s_comData.Malfunction, com485); // 置位485通信故障
  148. }
  149. if(p_global_par->timeoutCnt_485 == 0){
  150. BIT_CLEAR(s_comData.Malfunction, com485); // 置位485通信故障
  151. }
  152. // 7. 陀螺仪数据故障-- 获取的姿态角数据全部为0
  153. if(p_global_par->gyroDataFaultFlag == 1){
  154. if(errorCnt_gyro < FILTER_TIME_gyro){
  155. errorCnt_gyro++;
  156. }else{
  157. errorCnt_gyro = FILTER_TIME_gyro;
  158. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  159. }
  160. }else{
  161. errorCnt_gyro = 0;
  162. BIT_CLEAR(s_comData.Malfunction, gyroscope);
  163. }
  164. // 8. 升级故障--开始升级后,开始记时,2分钟还未升级成功的话,置位升级故障,
  165. // 退出升级,此次升级失败,旧版本运行,清除升级状态变量、标志、数据,重启释放
  166. if(p_global_par->otaUpgradeStartFlag == 1){
  167. if(timesOut_ota < TIME_OUT_OTA){
  168. timesOut_ota++;
  169. }else{
  170. timesOut_ota = TIME_OUT_OTA;
  171. BIT_SET(s_comData.Malfunction, OTA_fault); // 置位OTA异常故障
  172. }
  173. }
  174. }
  175. /**
  176. * @brief 控制蜂鸣器响停
  177. * @param count 当前计数
  178. * @param on_threshold 响的阈值
  179. * @param off_threshold 停的阈值
  180. * @retval 无
  181. */
  182. void beep_control(uint8_t count, uint8_t on_threshold, uint8_t off_threshold) {
  183. if (count < on_threshold) {
  184. GPIO_BEEP(GPIO_PIN_SET);
  185. } else if (count < off_threshold) {
  186. GPIO_BEEP(GPIO_PIN_RESET);
  187. }
  188. }
  189. /**
  190. * @brief 控制蜂鸣器响应
  191. * @note 根据不同条件控制蜂鸣器的响停 进入周期为100ms
  192. * @param 无
  193. * @retval 无
  194. */
  195. void control_beep_response(void) {
  196. comData *p_comData = &s_comData;
  197. global_par *p_global_par = &s_global_par;
  198. // 蜂鸣器计数器
  199. static uint8_t beep_count_fence = 0;
  200. static uint8_t beep_count_fault = 0;
  201. static uint8_t beep_count_ota = 0;
  202. // 超出围栏 500ms频率响应
  203. if (p_comData->fenceStatus == 1) {
  204. beep_control(beep_count_fence, 5, 10);
  205. beep_count_fence = (beep_count_fence >= 10) ? 0 : beep_count_fence + 1;
  206. }
  207. // 运行中限制车速的故障 1s的频率响应
  208. else if ((BIT_CHECK(s_comData.Malfunction, fence))
  209. || (BIT_CHECK(s_comData.Malfunction, batError))
  210. || (BIT_CHECK(s_comData.Malfunction, positionError))){
  211. beep_control(beep_count_fault, 10, 20);
  212. beep_count_fault = (beep_count_fault >= 20) ? 0 : beep_count_fault + 1;
  213. }
  214. // OTA升级 2s的响应频率
  215. else if (p_global_par->otaUpgradeStartFlag == 1) {
  216. beep_control(beep_count_ota, 20, 40);
  217. beep_count_ota = (beep_count_ota >= 40) ? 0 : beep_count_ota + 1;
  218. }
  219. // 不存在蜂鸣器响应,蜂鸣器应关闭
  220. else{
  221. GPIO_BEEP(GPIO_PIN_RESET);
  222. }
  223. }