navigation_main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (timedPose_.timeout()) {
  120. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  121. return eMpcFailed;
  122. }
  123. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  124. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  125. double acc_angular = 25 * M_PI / 180.0;
  126. double dt = 0.1;
  127. Pose2d rotated = Pose2d(space.x(), space.y(), space.theta());
  128. target.set_l(0.05);
  129. target.set_w(0.03);
  130. target.set_id(space.id());
  131. //后车先到,当前车进入2点,保持与后车一致的朝向
  132. if (brother.in_space() && brother.space_id() == space.id()) {
  133. printf("RotateBeforeEnterSpace | 后车先到, __LINE__ = %d\n", __LINE__);
  134. rotated.mutable_theta() = brother.odom().theta();
  135. } else { //当前车先到,正向
  136. printf("RotateBeforeEnterSpace | 该车先到, __LINE__ = %d\n", __LINE__);
  137. rotated.mutable_theta() = space.theta();
  138. }
  139. std::cout << "===============================> RotateBeforeEnterSpace ,target:" << rotated << std::endl;
  140. double x = space.x();
  141. double y = space.y();
  142. if (move_mode_ == eSingle) {
  143. x += wheelbase / 2 * cos(rotated.theta());
  144. y += wheelbase / 2 * sin(rotated.theta());
  145. printf("确定车位点:eSingle\n");
  146. }
  147. target.set_x(x);
  148. target.set_y(y);
  149. target.set_theta(rotated.theta());
  150. while (cancel_ == false) {
  151. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  152. Pose2d current = timedPose_.Get();
  153. double yawDiff = (rotated - current).theta();
  154. //一次变速
  155. std::vector<double> out;
  156. bool ret;
  157. TimerRecord::Execute([&, this]() {
  158. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  159. }, "Rotation_mpc_once");
  160. if (ret == false) {
  161. Stop();
  162. return eMpcFailed;
  163. }
  164. //下发速度
  165. if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  166. SendMoveCmd(move_mode_, eRotation, 0, out[0]);
  167. actionType_ = eRotation;
  168. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f diff:%f anyDirect:false\n",
  169. timedA_.Get(), out[0], yawDiff);
  170. } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  171. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  172. return eMpcSuccess;
  173. }
  174. continue;
  175. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  176. // Pose2d current = timedPose_.Get();
  177. // double yawDiff = (rotated - current).theta();
  178. // if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  179. // double theta = limit_gause(yawDiff, limit_rotate.min, limit_rotate.max);
  180. // double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  181. // double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  182. //
  183. // SendMoveCmd(move_mode_, Monitor_emqx::eRotation, 0, limit_angular);
  184. // actionType_ = eRotation;
  185. // printf(" RotateBeforeEnterSpace | input anguar:%f,next angular:%f,down:%f diff:%f anyDirect:false\n",
  186. // timedA_.Get(), angular, limit_angular, yawDiff);
  187. //
  188. // continue;
  189. // } else {
  190. // if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  191. // printf(" RotateBeforeEnterSpace refer target completed\n");
  192. // printf("---------------- update new target :%f %f %f \n",target.x(),target.y(),target.theta());
  193. // return true;
  194. // }
  195. // }
  196. }
  197. return eMpcFailed;
  198. }