global.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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(500); // 延时
  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. // 备份新的车辆数据
  43. p_param_boot->carTotalMileage = (s_rs485RecDate.accTotalMileage_h << 16) | s_rs485RecDate.accTotalMileage_l;
  44. Write_paramArea();
  45. // 置位中控板迁移事件
  46. BIT_SET(p_global_par->ctrEvent, CtrlPanelMigration);
  47. }
  48. }
  49. // 4. 初始化4g模块 4g模块在这里不好处理 放在任务中,围栏数据也一样
  50. // 5. 读取96位(或者说是3个32位的字)的唯一ID
  51. GetUniqueID(s_messageDate.devId);
  52. // 6. 陀螺仪的初始化
  53. recBack = atk_ms6050_init();
  54. if(recBack != 0){
  55. // printf("ATK-MS6050 init failed!\r\n");
  56. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  57. }
  58. // printf("ATK-MS6050 init\r\n");
  59. recBack = atk_ms6050_dmp_init();
  60. if(recBack != 0){
  61. // printf("ATK-MS6050 DMP init failed!\r\n");
  62. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  63. }
  64. // printf("ATK-MS6050 DMP init!\r\n");
  65. // 7. 外部flash初始化
  66. norflash_init();
  67. }
  68. // 车本体故障判断
  69. void fault_car(rs485RecDate *p_rs485RecDate, comData *p_comData){
  70. uint16_t errorLevel = 0;
  71. /* 尝试获取互斥量,等待无限长时间 */
  72. if(osMutexAcquire(mutex_rs485RecDateHandle, 1000) == osOK)
  73. {
  74. errorLevel = p_rs485RecDate->alarmLevel;
  75. /* 访问完成,释放互斥量 */
  76. osMutexRelease(mutex_rs485RecDateHandle);
  77. }
  78. // 存在车故障
  79. if(errorLevel != 0){
  80. BIT_SET(s_comData.Malfunction, carError); // 置位车故障
  81. }else{
  82. BIT_CLEAR(s_comData.Malfunction, carError); // 清除车故障
  83. }
  84. }
  85. /**
  86. * @brief 中控板故障检测
  87. * @param p_global_par:global_par的全局参数指针
  88. * @note 周期为1s1次 清除除电池故障的其他故障
  89. * @retval 无
  90. */
  91. void faultDetection(global_par *p_global_par){
  92. // 参数定义
  93. static uint16_t timesCnt_gps = 0; // gps记时
  94. static uint8_t errorCnt_gyro = 0; // 陀螺仪数据故障滤波次数
  95. static uint16_t timesOut_ota = 0; // OTA超时计数
  96. static uint16_t timesCnt_runFence; // 超运行围栏计数
  97. static uint16_t timesCnt_outSpeed; // 超速计时器
  98. comData *p_comData = &s_comData;
  99. param_boot *p_param_boot = &s_param_boot;
  100. // 开启超级权限 不判断所有故障
  101. if(p_global_par->superUser == 1){
  102. p_comData->Malfunction = 0; // 清除所有故障
  103. return;
  104. }
  105. // 1. 车本体故障判断
  106. fault_car(&s_rs485RecDate, &s_comData); // 车故障判断
  107. // 2. 超出围栏故障判断
  108. if(p_comData->fenceStatus == 1){
  109. if(timesCnt_runFence < p_param_boot->fenceBreach_Timeout){
  110. timesCnt_runFence++;
  111. }else{
  112. timesCnt_runFence = p_param_boot->fenceBreach_Timeout;
  113. BIT_SET(s_comData.Malfunction, fence);
  114. }
  115. }else{
  116. timesCnt_runFence = 0;
  117. BIT_CLEAR(s_comData.Malfunction, fence);
  118. }
  119. // 3. GPS信号丢失
  120. // 获取gps失败,但陀螺仪数据正常的情况下,一分钟之后置位gps故障
  121. // 获取gps失败,陀螺仪数据异常,直接置位gps故障
  122. if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 0)){
  123. if(timesCnt_gps < FILTER_TIME_GPS){ // 4分钟滤波时间
  124. timesCnt_gps++;
  125. }else{
  126. timesCnt_gps = FILTER_TIME_GPS;
  127. BIT_SET(s_comData.Malfunction, positionError);
  128. }
  129. }else if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 1)){
  130. timesCnt_gps = 0;
  131. BIT_SET(s_comData.Malfunction, positionError);
  132. }else{
  133. timesCnt_gps = 0;
  134. }
  135. if(p_global_par->positionErrorCnt == 0){ // 清除gps故障
  136. BIT_CLEAR(s_comData.Malfunction, positionError);
  137. }
  138. // 4. MQTT通信异常
  139. if(p_global_par->mqttTimeoutCnt >= PUBLISH_TIME_MS){
  140. BIT_SET(s_comData.Malfunction, errorMqtt); // 置位MQTT故障
  141. }
  142. if(p_global_par->mqttTimeoutCnt == 0){ // 清除MQTT故障
  143. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  144. }
  145. // 5. 4G模块初始化故障
  146. if(p_global_par->InitFaultFlag_4G == 1){
  147. BIT_SET(s_comData.Malfunction, init_4G_error); // 置位4g模块初始化故障
  148. }else{
  149. BIT_CLEAR(s_comData.Malfunction, init_4G_error); // 清除4g模块初始化故障
  150. }
  151. // 6. 485通信故障
  152. if(p_global_par->timeoutCnt_485 >= TIMEOUT_485){
  153. BIT_SET(s_comData.Malfunction, com485); // 置位485通信故障
  154. }
  155. if(p_global_par->timeoutCnt_485 == 0){
  156. BIT_CLEAR(s_comData.Malfunction, com485); // 置位485通信故障
  157. }
  158. // 7. 陀螺仪数据故障-- 获取的姿态角数据全部为0
  159. if(p_global_par->gyroDataFaultFlag == 1){
  160. if(errorCnt_gyro < FILTER_TIME_gyro){
  161. errorCnt_gyro++;
  162. }else{
  163. errorCnt_gyro = FILTER_TIME_gyro;
  164. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  165. }
  166. }else{
  167. errorCnt_gyro = 0;
  168. BIT_CLEAR(s_comData.Malfunction, gyroscope);
  169. }
  170. // 8. 超速故障
  171. if(s_rs485RecDate.vehicleSpeed > s_param_boot.speed_limit){
  172. if(timesCnt_outSpeed < s_param_boot.overspeed_Timeout){
  173. timesCnt_outSpeed++;
  174. }else{
  175. timesCnt_outSpeed = s_param_boot.overspeed_Timeout;
  176. BIT_SET(s_comData.Malfunction, outSpeed); // 置位超速故障
  177. }
  178. }else{
  179. timesCnt_outSpeed = 0;
  180. BIT_CLEAR(s_comData.Malfunction, outSpeed); // 清零超速故障
  181. }
  182. // 9. 升级故障--开始升级后,开始记时,2分钟还未升级成功的话,置位升级故障,
  183. // 退出升级,此次升级失败,旧版本运行,清除升级状态变量、标志、数据,重启释放
  184. // OTA开始后任务周期为200ms
  185. if(p_global_par->otaUpgradeStartFlag == 1){
  186. if(timesOut_ota < TIME_OUT_OTA){
  187. timesOut_ota++;
  188. }else{
  189. timesOut_ota = TIME_OUT_OTA;
  190. BIT_SET(s_comData.Malfunction, OTA_fault); // 置位OTA异常故障
  191. }
  192. }
  193. // 10. 订阅主题失败
  194. if(p_global_par->subscribe_fail == 1){
  195. BIT_SET(s_comData.Malfunction, subscribeFail); // 置位订阅主题失败故障
  196. }else{
  197. BIT_CLEAR(s_comData.Malfunction, subscribeFail); // 清除订阅主题失败故障
  198. }
  199. // 11. 发布消息失败
  200. if((p_global_par->publish_fail == 1) || (s_global_par.reconnect_server_flag == 1)){
  201. BIT_SET(s_comData.Malfunction, publishFail); // 置位发布消息失败故障
  202. }else{
  203. BIT_CLEAR(s_comData.Malfunction, publishFail); // 清除发布消息失败故障
  204. }
  205. // 12. 获取定位信息失败
  206. if(p_global_par->get_location_error == 1){
  207. BIT_SET(s_comData.Malfunction, getLocationError); // 置位获取定位信息失败故障
  208. }else{
  209. BIT_CLEAR(s_comData.Malfunction, getLocationError); // 清除获取定位信息失败故障
  210. }
  211. }
  212. /**
  213. * @brief 控制蜂鸣器响停
  214. * @param count 当前计数
  215. * @param on_threshold 响的阈值
  216. * @param off_threshold 停的阈值
  217. * @retval 无
  218. */
  219. void beep_control(uint8_t count, uint8_t on_threshold, uint8_t off_threshold) {
  220. if (count < on_threshold) {
  221. GPIO_BEEP(GPIO_PIN_SET);
  222. } else if (count < off_threshold) {
  223. GPIO_BEEP(GPIO_PIN_RESET);
  224. }
  225. }
  226. /**
  227. * @brief 控制蜂鸣器响应
  228. * @note 根据不同条件控制蜂鸣器的响停 进入周期为100ms
  229. * @param 无
  230. * @retval 无
  231. */
  232. void control_beep_response(void) {
  233. global_par *p_global_par = &s_global_par;
  234. // 蜂鸣器计数器
  235. static uint8_t beep_count_fence = 0;
  236. // static uint8_t beep_count_fault = 0;
  237. static uint8_t beep_count_ota = 0;
  238. // 超出围栏 500ms频率响应
  239. if (BIT_CHECK(s_comData.Malfunction, fence)) {
  240. beep_control(beep_count_fence, 5, 10);
  241. beep_count_fence = (beep_count_fence >= 10) ? 0 : beep_count_fence + 1;
  242. }
  243. // OTA升级 2s的响应频率
  244. else if (p_global_par->otaUpgradeStartFlag == 1) {
  245. beep_control(beep_count_ota, 20, 40);
  246. beep_count_ota = (beep_count_ota >= 40) ? 0 : beep_count_ota + 1;
  247. }
  248. // 不存在蜂鸣器响应,蜂鸣器应关闭
  249. else{
  250. GPIO_BEEP(GPIO_PIN_RESET);
  251. }
  252. }
  253. // 格式转换函数
  254. void formatDateTimeAndMalfunction(uint16_t year, uint8_t month, uint8_t day,
  255. uint8_t hour, uint8_t minute, uint8_t sec,
  256. uint32_t malfunction, char *output) {
  257. // 确保提供的output数组足够大
  258. // 格式化字符串为 YYYYMMDD-HHMM:0xXXXXXXXX 格式
  259. sprintf(output, "%04u%02u%02u-%02u%02u%02u:0x%08X,",
  260. year, month, day, hour, minute, sec, malfunction);
  261. output[29] = '\0'; // 确保字符串以空字符结尾
  262. }
  263. /**
  264. * @brief 历史故障存储
  265. * @note 此函数1s1次
  266. * @param 无
  267. * @retval 无
  268. */
  269. void storeFaultRecord(void) {
  270. uint16_t id = 0;
  271. char storeFaultData[30] = {0};
  272. static uint32_t fault_log = 0; // 用来同当前故障数据对比,看是否产生故障变化
  273. static uint16_t timeCnt_delay = 0; // 存在故障
  274. id = norflash_read_id(); /* 读取FLASH ID */
  275. if((id == 0) || (id == 0XFFFF)) { // 检测不到FLASH芯片
  276. return;
  277. }
  278. // 检查是否存在故障
  279. if (s_comData.Malfunction == 0) {
  280. return; // 如果没有故障,直接返回
  281. }
  282. // 检查是否产生故障变化或者到达计时时间
  283. if((fault_log == s_comData.Malfunction) && (timeCnt_delay < FAULT_RECORD_TIME)){
  284. timeCnt_delay++;
  285. return; // 如果没有故障变化或者未到达计时时间,直接返回
  286. }else{
  287. // 更新计数值和故障log
  288. timeCnt_delay = 0;
  289. fault_log = s_comData.Malfunction;
  290. }
  291. // 存储格式转换成 这样的20240513-172203:0x0000000f,字符串数据
  292. formatDateTimeAndMalfunction(s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,
  293. s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec,s_comData.Malfunction, storeFaultData);
  294. // 写入数据
  295. norflash_write((uint8_t *)storeFaultData, s_param_boot.nextFaultAddr, strlen(storeFaultData));
  296. // 更新索引
  297. s_param_boot.faultRecordIndex = (s_param_boot.faultRecordIndex < MAX_RECORDS) ? (s_param_boot.faultRecordIndex + 1) : 0 ;
  298. // 更新地址
  299. s_param_boot.nextFaultAddr += strlen(storeFaultData);
  300. if(s_param_boot.faultRecordIndex == 0){
  301. s_param_boot.nextFaultAddr = 0;
  302. }
  303. // 保存最新的索引和地址数据
  304. Write_paramArea();
  305. }
  306. /**
  307. * @brief 更新保存的时间信息
  308. * @note 此函数1s1次
  309. * @param 无
  310. * @retval 无
  311. */
  312. void refreshSavedTime(void){
  313. static uint8_t timesCnt = 0;
  314. // 未获取到时间数据,则不保存
  315. if(s_global_par.time_stamp_flag != 1){
  316. return;
  317. }
  318. s_param_boot.year = s_nmea_utc_time.year;
  319. s_param_boot.month = s_nmea_utc_time.month;
  320. s_param_boot.date = s_nmea_utc_time.date;
  321. s_param_boot.hour = s_nmea_utc_time.hour;
  322. s_param_boot.min = s_nmea_utc_time.min;
  323. s_param_boot.sec = s_nmea_utc_time.sec;
  324. // 10s保存一次时间数据,发生故障时保存一次,固件更新时保存一次
  325. if(timesCnt < times_10s){
  326. timesCnt++;
  327. }else{
  328. timesCnt = 0;
  329. Write_paramArea();
  330. }
  331. }
  332. // 车辆行驶里程计算
  333. // 参数:day
  334. // 参数2:累积行驶总里程 485下发的总里程
  335. void calculate_driving_distance(uint32_t totalMileage){
  336. param_boot *p_param_boot = &s_param_boot;
  337. // 问询的车辆总里程没有备份的大,则不做操作
  338. if(totalMileage <= p_param_boot->carTotalMileage){
  339. return;
  340. }
  341. // 1. 计算当日行驶里程
  342. p_param_boot->dailyMileage += totalMileage - p_param_boot->carTotalMileage;
  343. // 2. 备份总里程信息
  344. p_param_boot->carTotalMileage = totalMileage;
  345. }
  346. // 车辆行驶时长计算
  347. // 参数1 :单次行驶时长
  348. // 参数2 :当日行驶时长
  349. // 参数3 :累积行驶时长
  350. // 周期为1s1次
  351. void calculateDriveTime(void){
  352. static uint16_t run_sec = 0;
  353. param_boot *p_param_boot = &s_param_boot;
  354. // 数据累加 按s累加
  355. run_sec++;
  356. p_param_boot->dailyDriveTime++;
  357. p_param_boot->totalDriveTime++;
  358. // 计算本次行驶时长
  359. int hours = (run_sec % 86400) / 3600; // 计算剩余秒数对应的小时数
  360. int minutes = (run_sec % 3600) / 60; // 计算剩余秒数对应的分钟数
  361. s_rs485RecDate.runTime = (uint16_t)hours << 8 | minutes;
  362. }
  363. // 车辆状态参数数据迁移vehicle state data migration
  364. void vehStateDataMig(uint8_t timeDate){
  365. param_boot *p_param_boot = &s_param_boot;
  366. rs485RecDate *p_rs485RecDate = &s_rs485RecDate;
  367. // 判断没有在当日 跨天则当日行驶里程清零
  368. if(timeDate != p_param_boot->dailyDate){
  369. p_param_boot->dailyDriveTime = 0;
  370. p_param_boot->dailyMileage = 0;
  371. p_param_boot->dailyDate = timeDate; // 更新时间信息
  372. }
  373. p_rs485RecDate->dailyDrivMileage = p_param_boot->dailyMileage * 10; // 单位转换
  374. int hours = (p_param_boot->dailyDriveTime % 86400) / 3600; // 计算剩余秒数对应的小时数
  375. int minutes = (p_param_boot->dailyDriveTime % 3600) / 60; // 计算剩余秒数对应的分钟数
  376. p_rs485RecDate->dailyDrivTime = (uint16_t)hours << 8 | minutes;
  377. int days = p_param_boot->totalDriveTime / 86400; // 计算天数
  378. hours = (p_param_boot->totalDriveTime % 86400) / 3600; // 计算剩余秒数对应的小时数
  379. minutes = (p_param_boot->totalDriveTime % 3600) / 60; // 计算剩余秒数对应的分钟数
  380. p_rs485RecDate->accTotalDrivTime_day = days;
  381. p_rs485RecDate->accTotalDrivTime_h_min = (uint16_t)hours << 8 | minutes;
  382. }
  383. // 测试使用
  384. void fun(void){
  385. static uint8_t cnt = 0;
  386. uint32_t accTotalMileage = 0;
  387. accTotalMileage = (uint32_t)s_rs485RecDate.accTotalMileage_h << 16 | s_rs485RecDate.accTotalMileage_l;
  388. if(cnt < times_5s){
  389. cnt++;
  390. return;
  391. }else{
  392. cnt = 0;
  393. accTotalMileage++;
  394. }
  395. s_rs485RecDate.accTotalMileage_h = accTotalMileage >> 16;
  396. s_rs485RecDate.accTotalMileage_l = accTotalMileage & 0x0000FFFF; // 使用位与操作符
  397. }