navigation_main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // Created by zx on 23-5-8.
  3. //
  4. #include "navigation_main.h"
  5. #include "TimerRecord.h"
  6. NavigationMain::NavigationMain() {
  7. move_mode_ = eSingle;
  8. wheelBase_ = 0;
  9. }
  10. NavigationMain::~NavigationMain() {
  11. }
  12. void NavigationMain::ResetPose(const Pose2d &pose) {
  13. if (move_mode_ == eDouble) {
  14. if (timedBrotherPose_.timeout() == true) {
  15. std::cout << "Brother pose is timeout can not set MainAGV pose" << std::endl;
  16. return;
  17. }
  18. //Pose2d transform(-wheelBase_/2.0,0,0);
  19. //Navigation::ResetPose(pose * transform);
  20. Pose2d brother = timedBrotherPose_.Get();
  21. Pose2d diff = Pose2d::abs(Pose2d::relativePose(brother, pose));
  22. if (diff.x() > 3.6 || diff.x() < 2.2 || diff.y() > 0.3 || diff.theta() > 5 * M_PI / 180.0) {
  23. std::cout << " distance with two agv is too far diff: " << diff << std::endl;
  24. return;
  25. }
  26. Pose2d abs_diff = pose - brother;
  27. //计算两车朝向的方向
  28. float theta = Pose2d::vector2yaw(abs_diff.x(), abs_diff.y());
  29. Pose2d agv = Pose2d((pose.x() + brother.x()) / 2.0, (pose.y() + brother.y()) / 2.0, theta);
  30. Navigation::ResetPose(agv);
  31. } else
  32. Navigation::ResetPose(pose);
  33. }
  34. void NavigationMain::publish_statu(NavMessage::NavStatu &statu) {
  35. statu.set_main_agv(true);
  36. Navigation::publish_statu(statu);
  37. }
  38. void NavigationMain::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  39. /*if(move_mode_!=Monitor_emqx::eDouble)
  40. {
  41. printf(" navigation mode must set main,parameter:Pose2d\n");
  42. return false;
  43. }*/
  44. Navigation::Start(cmd, response);
  45. }
  46. void NavigationMain::SendMoveCmd(int mode, ActionType type,
  47. double v, double angular) {
  48. if (monitor_) {
  49. monitor_->set_speed(mode, type, v, angular, wheelBase_);
  50. }
  51. }
  52. bool NavigationMain::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  53. if (Navigation::CreateRobotStatuMsg(robotStatu)) {
  54. robotStatu.mutable_agvstatu()->set_clamp_other(timed_other_clamp_.Get());
  55. return true;
  56. }
  57. //std::cout<<agvStatu.DebugString()<<std::endl;
  58. return false;
  59. }
  60. void NavigationMain::ResetOtherClamp(ClampStatu statu) {
  61. timed_other_clamp_.reset(statu, 1);
  62. }
  63. void NavigationMain::HandleAgvStatu(const MqttMsg &msg) {
  64. NavMessage::AgvStatu speed;
  65. if (msg.toProtoMessage(speed)) {
  66. ResetStatu(speed.v(), speed.w());
  67. ResetClamp((ClampStatu) speed.clamp());
  68. ResetOtherClamp((ClampStatu) speed.clamp_other());
  69. //printf(" clamp:%d other:%d\n",speed.clamp(),speed.clamp_other());
  70. }
  71. }
  72. bool NavigationMain::clamp_close() {
  73. if (move_mode_ == eSingle)
  74. return Navigation::clamp_close();
  75. printf("双车夹持\n");
  76. if (monitor_) {
  77. monitor_->clamp_close(move_mode_);
  78. while (exit_ == false) {
  79. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  80. printf("timed clamp is timeout\n");
  81. return false;
  82. }
  83. if (timed_clamp_.Get() == eClosed && timed_other_clamp_.Get() == eClosed) {
  84. printf("双车夹持completed!!!\n");
  85. return true;
  86. }
  87. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  88. monitor_->clamp_close(move_mode_);
  89. }
  90. return false;
  91. }
  92. return false;
  93. }
  94. bool NavigationMain::clamp_open() {
  95. if (move_mode_ == eSingle)
  96. return Navigation::clamp_open();
  97. if (monitor_) {
  98. printf("双车松夹持\n");
  99. monitor_->clamp_open(move_mode_);
  100. while (exit_ == false) {
  101. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  102. printf("timed clamp is timeout\n");
  103. return false;
  104. }
  105. if (timed_clamp_.Get() == eOpened && timed_other_clamp_.Get() == eOpened) {
  106. printf("双车松夹持completed!!!\n");
  107. return true;
  108. }
  109. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  110. monitor_->clamp_open(move_mode_);
  111. }
  112. return false;
  113. }
  114. return false;
  115. }
  116. Navigation::MpcResult
  117. NavigationMain::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target) {
  118. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  119. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  120. return eMpcFailed;
  121. }
  122. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  123. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  124. double acc_angular = 25 * M_PI / 180.0;
  125. double dt = 0.1;
  126. Pose2d rotated = Pose2d(space.x(), space.y(), space.theta());
  127. target.set_l(0.05);
  128. target.set_w(0.03);
  129. target.set_id(space.id());
  130. //后车先到,当前车进入2点,保持与后车一致的朝向
  131. if (brother.in_space() && brother.space_id() == space.id()) {
  132. rotated.mutable_theta() = brother.odom().theta();
  133. } else { //当前车先到,正向
  134. rotated.mutable_theta() = space.theta();
  135. }
  136. std::cout << "===============================> RotateBeforeEnterSpace ,target:" << rotated << std::endl;
  137. double x = space.x() + wheelbase / 2.0 * cos(rotated.theta());
  138. double y = space.y() + wheelbase / 2.0 * sin(rotated.theta());
  139. target.set_x(x);
  140. target.set_y(y);
  141. target.set_theta(rotated.theta());
  142. while (cancel_ == false) {
  143. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  144. Pose2d current = timedPose_.Get();
  145. double yawDiff = (rotated - current).theta();
  146. //一次变速
  147. std::vector<double> out;
  148. bool ret;
  149. TimerRecord::Execute([&, this]() {
  150. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  151. }, "Rotation_mpc_once");
  152. if (ret == false) {
  153. Stop();
  154. return eMpcFailed;
  155. }
  156. //下发速度
  157. if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  158. SendMoveCmd(move_mode_, eRotation, 0, out[0]);
  159. actionType_ = eRotation;
  160. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f diff:%f anyDirect:false\n",
  161. timedA_.Get(), out[0], yawDiff);
  162. } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  163. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  164. return eMpcSuccess;
  165. }
  166. continue;
  167. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  168. // Pose2d current = timedPose_.Get();
  169. // double yawDiff = (rotated - current).theta();
  170. // if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  171. // double theta = limit_gause(yawDiff, limit_rotate.min, limit_rotate.max);
  172. // double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  173. // double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  174. //
  175. // SendMoveCmd(move_mode_, Monitor_emqx::eRotation, 0, limit_angular);
  176. // actionType_ = eRotation;
  177. // printf(" RotateBeforeEnterSpace | input anguar:%f,next angular:%f,down:%f diff:%f anyDirect:false\n",
  178. // timedA_.Get(), angular, limit_angular, yawDiff);
  179. //
  180. // continue;
  181. // } else {
  182. // if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  183. // printf(" RotateBeforeEnterSpace refer target completed\n");
  184. // printf("---------------- update new target :%f %f %f \n",target.x(),target.y(),target.theta());
  185. // return true;
  186. // }
  187. // }
  188. }
  189. return eMpcFailed;
  190. }