navigation_main.cpp 4.1 KB

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