task_communication(3432).c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "task_communication.h"
  2. /* typedef -----------------------------------------------------------*/
  3. comData s_comData = {
  4. .maxFeed = 60,
  5. .maxRunFeed = 0,
  6. };
  7. // 下发的车时速控制
  8. void Speed_Control(comData *p_comData, ec800Date *p_ec800Date){
  9. if (p_comData == NULL || p_ec800Date == NULL) {
  10. // 错误处理:输入参数无效
  11. return;
  12. }
  13. // 获取后台允许的最高车速
  14. p_comData->maxFeed = s_param_boot.speed_limit;
  15. // 检查是否处于硬件更新状态
  16. if(p_ec800Date->hardwareUpdate == 0x01){
  17. p_comData->maxRunFeed = 0;
  18. return;
  19. }
  20. // 后台锁车
  21. if(s_param_boot.car_lock_flag){
  22. p_comData->maxRunFeed = (p_comData->maxRunFeed > 0) ? p_comData->maxRunFeed - 1 : 0;
  23. return;
  24. }
  25. // 检查围栏状态和驾驶状态
  26. if(p_ec800Date->fenceRecSuccess == 0 || (p_comData->driveStatus == 1 && p_comData->fenceStatus == 1)){
  27. p_comData->maxRunFeed = p_comData->maxFeed;
  28. return;
  29. }
  30. // 检查是否超出围栏且处于运行状态
  31. if(p_comData->driveStatus == 3 && (BIT_CHECK(s_comData.Malfunction, fence))){
  32. p_comData->maxRunFeed = (p_comData->maxRunFeed > p_comData->maxFeed) ? p_comData->maxRunFeed - 1 : p_comData->maxFeed;
  33. return;
  34. }
  35. // 如果在围栏内,设置最大速度为后台允许的最高车速
  36. if(p_comData->fenceStatus == 0){
  37. p_comData->maxRunFeed = p_comData->maxFeed;
  38. }
  39. }
  40. // 车运行状态切换
  41. void vehicle_runState(rs485RecDate *p_rs485RecDate, comData *p_comData){
  42. uint16_t vehSpeed; // 车速
  43. static uint8_t status2_cnt, status3_cnt = 0;
  44. // 初始化还未完成,不进行行车状态判断
  45. if(p_comData->driveStatus == 0){
  46. return;
  47. }
  48. /* 尝试获取互斥量,等待无限长时间 */
  49. if(osMutexAcquire(mutex_rs485RecDateHandle, 1000) == osOK)
  50. {
  51. vehSpeed = p_rs485RecDate->vehicleSpeed;
  52. /* 访问完成,释放互斥量 */
  53. osMutexRelease(mutex_rs485RecDateHandle);
  54. }
  55. // 车速判断 判断车的运行状态
  56. if(vehSpeed == 0){
  57. status3_cnt = 0;
  58. if(status2_cnt < times_60s){
  59. status2_cnt++;
  60. }else{
  61. status2_cnt = times_60s;
  62. p_comData->driveStatus = 2;
  63. }
  64. }
  65. if(vehSpeed > 0){
  66. status2_cnt = 0;
  67. if(status3_cnt < times_5s){
  68. status3_cnt++;
  69. }else{
  70. status3_cnt = times_5s;
  71. p_comData->driveStatus = 3;
  72. }
  73. }
  74. }
  75. // 电子围栏判断
  76. void evaluateGeofenceBoundary(comData *p_comData, double PolygonLat[], double PolygonLng[]) {
  77. double point1Lat = 34.155896;
  78. double point1Lng = 108.978638;
  79. /* 尝试获取互斥量,等待无限长时间 */
  80. if(osMutexAcquire(s_messageDate_locationHandle, 100) == osOK)
  81. {
  82. /* 安全地访问s_rs485RecDate结构体 */
  83. point1Lat = (double)s_messageDate.latitude / 100000;
  84. point1Lng = (double)s_messageDate.longitude / 100000;
  85. // point1Lat = 34.15792;
  86. // point1Lng = 108.97439;
  87. /* 访问完成,释放互斥量 */
  88. osMutexRelease(s_messageDate_locationHandle);
  89. }
  90. // 测试点在多边形内部的情况
  91. bool result1 = isPointInPolygon(point1Lat, point1Lng, PolygonLat, PolygonLng, REC_COORDINATE_DEPTH);
  92. // printf("Test case 1: Point (%f, %f) is%s inside the polygon.\n", point1Lat, point1Lng, result1 ? "" : " not");
  93. if(result1){ // 处于围栏内
  94. p_comData->fenceStatus = 0;
  95. }else{ // 处于围栏外
  96. p_comData->fenceStatus = 1;
  97. }
  98. }
  99. // 测试用
  100. void readFaultRecord(void){
  101. char storeFaultData[100] = {0};
  102. // 读数据
  103. norflash_read((uint8_t *)storeFaultData, 0, 56);
  104. HAL_UART_Transmit(&huart1, (uint8_t*)&storeFaultData ,56,1000);
  105. }
  106. /* USER CODE BEGIN Header_Task_communication */
  107. /**
  108. * @brief Function implementing the communication thread.
  109. * @param argument: Not used
  110. * @retval None
  111. */
  112. /* USER CODE END Header_Task_communication */
  113. void task_communication_content(void)
  114. {
  115. // unsigned long times = 0;
  116. comData *p_comData = &s_comData;
  117. LED0_TOGGLE(); // 灯翻转
  118. faultDetection(&s_global_par); // 故障监测
  119. handleOTAUpgradeFault(); // OTA升级故障处理
  120. EC800_FTP_OTA_Upgrade(); // OTA升级
  121. // 固件升级时不做其他操作,保证固件更新的完成,发送限制车速为0,锁定车辆
  122. if(s_ec800Date.hardwareUpdate != 0){
  123. // HAL_NVIC_DisableIRQ(USART3_IRQn); // 避免被中斷
  124. // HAL_NVIC_DisableIRQ(USART1_IRQn);
  125. // 写限制车速为0
  126. modbus_write_holding_registers(0x01, 0x0001, 1, 0);
  127. osDelay(200);
  128. }else{
  129. if(s_ec800Date.fenceRecSuccess == 1){ // 围栏数据接收完成 执行围栏判断
  130. evaluateGeofenceBoundary(&s_comData, polygonLat, polygonLng); // 电子围栏
  131. // 从无状态转为初始态
  132. if(p_comData->driveStatus == 0){
  133. p_comData->driveStatus = 1;
  134. }
  135. }
  136. refreshSavedTime(); // 更新保存的时间信息
  137. storeFaultRecord(); // 历史故障存储
  138. Speed_Control(&s_comData, &s_ec800Date); // 速度控制
  139. vehicle_runState(&s_rs485RecDate, &s_comData); // 行车状态判断
  140. rs485_poll_sendReceive(s_comData.maxRunFeed); // 与车进行通信
  141. osDelay(1000);
  142. }
  143. // times = getRunTimeCounterValue();
  144. // printf("test_isPointInPolygon:%ld\r\n", times);
  145. }