navigation_main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.2 || 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::HandleNavCmd(const NavMessage::NavCmd& cmd)
  42. {
  43. if(cmd.action()==4)
  44. {
  45. if(cmd.wheelbase()<2.4 || cmd.wheelbase()>3.5)
  46. {
  47. printf("Failed to Switch MoveMode --> main,wheelbase invalid :%f\n",cmd.wheelbase());
  48. return;
  49. }
  50. SwitchMode(Monitor_emqx::eMain,cmd.wheelbase());
  51. return ;
  52. }
  53. if(cmd.action()==5)
  54. {
  55. printf(" Switch MoveMode --> single\n");
  56. SwitchMode(Monitor_emqx::eSingle,0);
  57. return ;
  58. }
  59. else
  60. {
  61. Navigation::HandleNavCmd(cmd);
  62. }
  63. }
  64. bool NavigationMain::Start(const NavMessage::NavCmd& cmd)
  65. {
  66. /*if(move_mode_!=Monitor_emqx::eMain)
  67. {
  68. printf(" navigation mode must set main,parameter:Pose2d\n");
  69. return false;
  70. }*/
  71. return Navigation::Start(cmd);
  72. }
  73. void NavigationMain::SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  74. double v,double angular)
  75. {
  76. if(monitor_)
  77. {
  78. monitor_->set_speed(mode,type,v,angular,wheelBase_);
  79. }
  80. }
  81. bool NavigationMain::CreateRobotStatuMsg(NavMessage::RobotStatu& robotStatu)
  82. {
  83. if(Navigation::CreateRobotStatuMsg(robotStatu)) {
  84. robotStatu.mutable_agvstatu()->set_clamp_other(timed_other_clamp_.Get());
  85. return true;
  86. }
  87. //std::cout<<agvStatu.DebugString()<<std::endl;
  88. return false;
  89. }
  90. void NavigationMain::ResetOtherClamp(ClampStatu statu)
  91. {
  92. timed_other_clamp_.reset(statu,1);
  93. }
  94. void NavigationMain::HandleAgvStatu(const MqttMsg& msg)
  95. {
  96. NavMessage::AgvStatu speed;
  97. if(msg.toProtoMessage(speed))
  98. {
  99. ResetStatu(speed.v(),speed.w());
  100. ResetClamp((ClampStatu)speed.clamp());
  101. ResetOtherClamp((ClampStatu)speed.clamp_other());
  102. //printf(" clamp:%d other:%d\n",speed.clamp(),speed.clamp_other());
  103. }
  104. }
  105. bool NavigationMain::clamp_close()
  106. {
  107. if(move_mode_==Monitor_emqx::eSingle)
  108. return Navigation::clamp_close();
  109. printf("双车夹持\n");
  110. if(monitor_) {
  111. monitor_->clamp_close(move_mode_);
  112. while (exit_ == false) {
  113. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  114. printf("timed clamp is timeout\n");
  115. return false;
  116. }
  117. if (timed_clamp_.Get() == eClosed && timed_other_clamp_.Get()==eClosed) {
  118. printf("双车夹持completed!!!\n");
  119. return true;
  120. }
  121. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  122. monitor_->clamp_close(move_mode_);
  123. }
  124. return false;
  125. }
  126. return false;
  127. }
  128. bool NavigationMain::clamp_open() {
  129. if(move_mode_==Monitor_emqx::eSingle)
  130. return Navigation::clamp_open();
  131. if(monitor_) {
  132. printf("双车松夹持\n");
  133. monitor_->clamp_open(move_mode_);
  134. while(exit_==false)
  135. {
  136. if(timed_clamp_.timeout()||timed_other_clamp_.timeout())
  137. {
  138. printf("timed clamp is timeout\n");
  139. return false;
  140. }
  141. if(timed_clamp_.Get()==eOpened&&timed_other_clamp_.Get()==eOpened) {
  142. printf("双车松夹持completed!!!\n");
  143. return true;
  144. }
  145. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  146. monitor_->clamp_open(move_mode_);
  147. }
  148. return false;
  149. }
  150. return false;
  151. }