navigation_main.cpp 5.9 KB

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