test_diff_ctrl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. ******************************************************************************
  3. * @file : test_diff_ctrl.cpp
  4. * @author : wangyingjie
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2025/5/23
  8. ******************************************************************************
  9. */
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. #include "nzzn/diff_ctrl_c/heading_diff_ctrl_c.h"
  15. #include "control/pid/studio_pid_c.h"
  16. #include "geography/studio_proj_c.h"
  17. #include "geometry/studio_geo_algo_c.h"
  18. #ifdef __cplusplus
  19. }
  20. #endif
  21. // 测试使用
  22. #include "geometry/studio_geo_utils.h"
  23. #include "geometry/studio_geo_algo.h"
  24. // 测试使用结束
  25. int main()
  26. {
  27. double central_1 = floor(120.07158158 / 3.0) * 3.0;
  28. double central_2 = static_cast<int>(120.07158158 / 3) * 3;
  29. double central_3 = floor((120.07158158 + 1.5) / 3.0) * 3.0;
  30. printf("===================== %s =====================\n", __FILE__);
  31. silly::geo::utils::init_gdal_env();
  32. std::string path;
  33. #ifdef IS_LINUX
  34. path = "/home/wyj/myself/2_data/2_geojson/multi_point/route_1.geojson";
  35. #else
  36. path = "D:/9_data/2_readfile/geojson/multi_point/route_3.geojson";
  37. #endif
  38. std::cout << "path: " << path << std::endl;
  39. std::vector<studio_geo_coll> res_collections;
  40. //studio_geo_coll temp_300;
  41. //temp_300.m_type = enum_geometry_type::egtLineString;
  42. //studio_geo_coll temp_100;
  43. //temp_100.m_type = enum_geometry_type::egtLineString;
  44. // 差速控制测试开始 ==================
  45. studio_point_c beg_p = studio_point_init(120.07158158, 36.15932277);
  46. studio_point_c end_p = studio_point_init(120.07193922, 36.15990152);
  47. studio_point_c change_1_p = studio_point_init(120.07232455, 36.15964342);
  48. studio_point_c change_2_p = studio_point_init(120.07215543, 36.15944710);
  49. // 测试记录 实际删去 --------beg--------
  50. studio_geo_coll beg_end_points;
  51. beg_end_points.m_type = enum_geometry_type::egtMultiPoint;
  52. beg_end_points.m_points.push_back(studio_point(beg_p.x, beg_p.y));
  53. beg_end_points.m_points.push_back(studio_point(end_p.x, end_p.y));
  54. beg_end_points.m_points.push_back(studio_point(change_1_p.x, change_1_p.y));
  55. beg_end_points.m_points.push_back(studio_point(change_2_p.x, change_2_p.y));
  56. //temp_300.m_line.push_back(studio_point(beg_p.x, beg_p.y));
  57. //temp_100.m_line.push_back(studio_point(beg_p.x, beg_p.y));
  58. // 测试记录 实际删去 --------end--------
  59. int count = 800; // 最大迭代次数
  60. const int heads = 2;
  61. double current_heading[heads] = {300.0, 100.0};
  62. for (int i = 0; i < heads; ++i)
  63. {
  64. studio_geo_coll temp_line;
  65. temp_line.m_type = enum_geometry_type::egtLineString;
  66. temp_line.m_points.push_back(studio_point(beg_p.x, beg_p.y));
  67. diff_ctrl ctrl;
  68. init_diff_ctrl(&ctrl, &end_p, 0.5, 0.8, 0.01, 0.2);
  69. studio_point_c current_pos = beg_p;
  70. double current_heading_deg = current_heading[i];
  71. for (int j = 0; j < count; ++j)
  72. {
  73. double dist_sq = update_boat(&ctrl, &current_pos, current_heading_deg);
  74. printf("Step %02d: 航向=%.1f° L=%.2f R=%.2f 距离²=%.1fm²\n", j + 1, current_heading_deg, ctrl.left_throttle, ctrl.right_throttle, dist_sq);
  75. if (dist_sq < 4.0)
  76. {
  77. printf("到达目标!\n");
  78. break;
  79. }
  80. simulate_movement(&current_pos, &current_heading_deg, ctrl.left_throttle, ctrl.right_throttle);
  81. temp_line.m_line.push_back(studio_point(current_pos.x, current_pos.y));
  82. }
  83. // 前往先一个点
  84. reset_diff_ctrl_goal(&ctrl, &change_1_p);
  85. for (int j = 0; j < count; ++j)
  86. {
  87. double dist_sq = update_boat(&ctrl, &current_pos, current_heading_deg);
  88. printf("Step %02d: 航向=%.1f° L=%.2f R=%.2f 距离²=%.1fm²\n", j + 1, current_heading_deg, ctrl.left_throttle, ctrl.right_throttle, dist_sq);
  89. if (dist_sq < 4.0)
  90. {
  91. printf("到达目标!\n");
  92. break;
  93. }
  94. simulate_movement(&current_pos, &current_heading_deg, ctrl.left_throttle, ctrl.right_throttle);
  95. temp_line.m_line.push_back(studio_point(current_pos.x, current_pos.y));
  96. }
  97. // 前往第二个点
  98. reset_diff_ctrl_goal(&ctrl, &change_2_p);
  99. for (int j = 0; j < count; ++j)
  100. {
  101. double dist_sq = update_boat(&ctrl, &current_pos, current_heading_deg);
  102. printf("Step %02d: 航向=%.1f° L=%.2f R=%.2f 距离²=%.1fm²\n", j + 1, current_heading_deg, ctrl.left_throttle, ctrl.right_throttle, dist_sq);
  103. if (dist_sq < 4.0)
  104. {
  105. printf("到达目标!\n");
  106. break;
  107. }
  108. simulate_movement(&current_pos, &current_heading_deg, ctrl.left_throttle, ctrl.right_throttle);
  109. temp_line.m_line.push_back(studio_point(current_pos.x, current_pos.y));
  110. }
  111. res_collections.push_back(temp_line);
  112. }
  113. res_collections.push_back(beg_end_points);
  114. silly::geo::utils::write_geo_coll(path, res_collections);
  115. silly::geo::utils::destroy_gdal_env();
  116. return 0;
  117. }