task_communication(7354).c 4.1 KB

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