task_communication(5062).c 4.1 KB

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