task_communication(3388).c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if(p_ec800Date->vehSpeed > 0){ // 服务器下发的限制车速大于0
  42. p_comData->maxRunFeed = p_ec800Date->vehSpeed;
  43. }else{
  44. p_comData->maxRunFeed = 0;
  45. }
  46. return;
  47. }
  48. // 如果运行态超出围栏或者出现故障
  49. if(((p_comData->driveStatus == 3) && (p_comData->fenceStatus == 1))
  50. || (BIT_CHECK(s_comData.Malfunction, fence))
  51. || (BIT_CHECK(s_comData.Malfunction, batError))
  52. || (BIT_CHECK(s_comData.Malfunction, positionError))
  53. ){
  54. if(p_comData->maxRunFeed > 0){
  55. p_comData->maxRunFeed = p_comData->maxRunFeed - 1;
  56. }else{
  57. p_comData->maxRunFeed = 0;
  58. }
  59. return;
  60. }
  61. // 围栏内行驶
  62. if(p_comData->fenceStatus == 0){
  63. p_comData->maxRunFeed = p_comData->maxFeed;
  64. }
  65. }
  66. }
  67. // 车运行状态切换
  68. void vehicle_runState(rs485RecDate *p_rs485RecDate, comData *p_comData){
  69. uint16_t vehSpeed; // 车速
  70. static uint8_t status2_cnt, status3_cnt = 0;
  71. // 初始化还未完成,不进行行车状态判断
  72. if(p_comData->driveStatus == 0){
  73. return;
  74. }
  75. /* 尝试获取互斥量,等待无限长时间 */
  76. if(osMutexAcquire(mutex_rs485RecDateHandle, osWaitForever) == osOK)
  77. {
  78. vehSpeed = p_rs485RecDate->vehicleSpeed;
  79. /* 访问完成,释放互斥量 */
  80. osMutexRelease(mutex_rs485RecDateHandle);
  81. }
  82. // 车速判断 判断车的运行状态
  83. if(vehSpeed == 0){
  84. status3_cnt = 0;
  85. if(status2_cnt < times_60s){
  86. status2_cnt++;
  87. }else{
  88. status2_cnt = times_60s;
  89. p_comData->driveStatus = 2;
  90. }
  91. }
  92. if(vehSpeed > 0){
  93. status2_cnt = 0;
  94. if(status3_cnt < times_5s){
  95. status3_cnt++;
  96. }else{
  97. status3_cnt = times_5s;
  98. p_comData->driveStatus = 3;
  99. }
  100. }
  101. }
  102. /* USER CODE BEGIN Header_Task_communication */
  103. /**
  104. * @brief Function implementing the communication thread.
  105. * @param argument: Not used
  106. * @retval None
  107. */
  108. /* USER CODE END Header_Task_communication */
  109. void task_communication_content(void)
  110. {
  111. // unsigned long times = 0;
  112. comData *p_comData = &s_comData;
  113. LED0_TOGGLE(); // 灯翻转
  114. faultDetection(&s_global_par); // 故障监测
  115. EC800_FTP_OTA_Upgrade(); // OTA升级
  116. // 固件升级时不做其他操作,保证固件更新的完成
  117. if(s_ec800Date.hardwareUpdate != 0){
  118. HAL_NVIC_DisableIRQ(USART3_IRQn); // 避免被中斷
  119. // HAL_NVIC_DisableIRQ(USART1_IRQn);
  120. osDelay(200);
  121. }else{
  122. if(s_ec800Date.fenceRecSuccess == 1){ // 围栏数据接收完成 执行围栏判断
  123. test_isPointInPolygon(&s_comData, polygonLat, polygonLng); // 电子围栏
  124. // 从无状态转为初始态
  125. if(p_comData->driveStatus == 0){
  126. p_comData->driveStatus = 1;
  127. }
  128. }
  129. vehicle_runState(&s_rs485RecDate, &s_comData); // 行车状态判断
  130. Speed_Control(&s_comData, &s_ec800Date); // 速度控制
  131. rs485_poll_sendReceive(s_comData.maxRunFeed); // 与车进行通信
  132. osDelay(1000);
  133. }
  134. // times = getRunTimeCounterValue();
  135. // printf("test_isPointInPolygon:%ld\r\n", times);
  136. }