navigation_main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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::HandleNavCmd(const NavMessage::NavCmd& cmd)
  42. {
  43. if(cmd.action()==4)
  44. {
  45. if(cmd.newactions_size()==0)
  46. {
  47. printf(" Error main \n");
  48. return;
  49. }
  50. NavMessage::NewAction action=cmd.newactions(0);
  51. if(action.wheelbase()<2.4 || action.wheelbase()>3.5)
  52. {
  53. printf("Failed to Switch MoveMode --> main,wheelbase invalid :%f\n",action.wheelbase());
  54. return;
  55. }
  56. SwitchMode(Monitor_emqx::eMain,action.wheelbase());
  57. return ;
  58. }
  59. if(cmd.action()==5)
  60. {
  61. printf(" Switch MoveMode --> single\n");
  62. SwitchMode(Monitor_emqx::eSingle,0);
  63. return ;
  64. }
  65. else
  66. {
  67. Navigation::HandleNavCmd(cmd);
  68. }
  69. }
  70. bool NavigationMain::Start(const NavMessage::NavCmd& cmd)
  71. {
  72. /*if(move_mode_!=Monitor_emqx::eMain)
  73. {
  74. printf(" navigation mode must set main,parameter:Pose2d\n");
  75. return false;
  76. }*/
  77. return Navigation::Start(cmd);
  78. }
  79. void NavigationMain::SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  80. double v,double angular)
  81. {
  82. if(monitor_)
  83. {
  84. monitor_->set_speed(mode,type,v,angular,wheelBase_);
  85. }
  86. }
  87. bool NavigationMain::CreateRobotStatuMsg(NavMessage::RobotStatu& robotStatu)
  88. {
  89. if(Navigation::CreateRobotStatuMsg(robotStatu)) {
  90. robotStatu.mutable_agvstatu()->set_clamp_other(timed_other_clamp_.Get());
  91. return true;
  92. }
  93. //std::cout<<agvStatu.DebugString()<<std::endl;
  94. return false;
  95. }
  96. void NavigationMain::ResetOtherClamp(ClampStatu statu)
  97. {
  98. timed_other_clamp_.reset(statu,1);
  99. }
  100. void NavigationMain::HandleAgvStatu(const MqttMsg& msg)
  101. {
  102. NavMessage::AgvStatu speed;
  103. if(msg.toProtoMessage(speed))
  104. {
  105. ResetStatu(speed.v(),speed.w());
  106. ResetClamp((ClampStatu)speed.clamp());
  107. ResetOtherClamp((ClampStatu)speed.clamp_other());
  108. //printf(" clamp:%d other:%d\n",speed.clamp(),speed.clamp_other());
  109. }
  110. }
  111. bool NavigationMain::clamp_close()
  112. {
  113. if(move_mode_==Monitor_emqx::eSingle)
  114. return Navigation::clamp_close();
  115. printf("双车夹持\n");
  116. if(monitor_) {
  117. monitor_->clamp_close(move_mode_);
  118. while (exit_ == false) {
  119. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  120. printf("timed clamp is timeout\n");
  121. return false;
  122. }
  123. if (timed_clamp_.Get() == eClosed && timed_other_clamp_.Get()==eClosed) {
  124. printf("双车夹持completed!!!\n");
  125. return true;
  126. }
  127. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  128. monitor_->clamp_close(move_mode_);
  129. }
  130. return false;
  131. }
  132. return false;
  133. }
  134. bool NavigationMain::clamp_open() {
  135. if(move_mode_==Monitor_emqx::eSingle)
  136. return Navigation::clamp_open();
  137. if(monitor_) {
  138. printf("双车松夹持\n");
  139. monitor_->clamp_open(move_mode_);
  140. while(exit_==false)
  141. {
  142. if(timed_clamp_.timeout()||timed_other_clamp_.timeout())
  143. {
  144. printf("timed clamp is timeout\n");
  145. return false;
  146. }
  147. if(timed_clamp_.Get()==eOpened&&timed_other_clamp_.Get()==eOpened) {
  148. printf("双车松夹持completed!!!\n");
  149. return true;
  150. }
  151. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  152. monitor_->clamp_open(move_mode_);
  153. }
  154. return false;
  155. }
  156. return false;
  157. }
  158. bool NavigationMain::RotateBeforeEnterSpace(NavMessage::PathNode space,double wheelbase,NavMessage::PathNode& target) {
  159. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  160. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  161. return false;
  162. }
  163. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  164. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  165. double acc_angular = 25 * M_PI / 180.0;
  166. double dt = 0.1;
  167. Pose2d rotated = Pose2d(space.x(),space.y(),space.theta());
  168. target.set_l(0.05);
  169. target.set_w(0.03);
  170. target.set_id(space.id());
  171. //后车先到,当前车进入2点,保持与后车一致的朝向
  172. if (brother.in_space() && brother.space_id() == space.id()) {
  173. rotated.mutable_theta() = brother.odom().theta();
  174. } else { //当前车先到,正向
  175. rotated.mutable_theta() = space.theta();
  176. }
  177. std::cout<<"===============================> RotateBeforeEnterSpace ,target:"<<rotated<<std::endl;
  178. double x = space.x() + wheelbase/2.0 * cos(rotated.theta());
  179. double y = space.y() + wheelbase/2.0 * sin(rotated.theta());
  180. target.set_x(x);
  181. target.set_y(y);
  182. target.set_theta(rotated.theta());
  183. while (cancel_ == false) {
  184. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  185. Pose2d current = timedPose_.Get();
  186. double yawDiff = (rotated - current).theta();
  187. if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  188. double theta = limit_gause(yawDiff, limit_rotate.min, limit_rotate.max);
  189. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  190. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  191. SendMoveCmd(move_mode_, Monitor_emqx::eRotate, 0, limit_angular);
  192. actionType_ = eRotation;
  193. printf(" RotateBeforeEnterSpace | input anguar:%f,next angular:%f,down:%f diff:%f anyDirect:false\n",
  194. timedA_.Get(), angular, limit_angular, yawDiff);
  195. continue;
  196. } else {
  197. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  198. printf(" RotateBeforeEnterSpace refer target completed\n");
  199. printf("---------------- update new target :%f %f %f \n",target.x(),target.y(),target.theta());
  200. return true;
  201. }
  202. }
  203. }
  204. return false;
  205. }