navigation.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Created by zx on 22-12-2.
  3. //
  4. #include "navigation.h"
  5. #include "loaded_mpc.h"
  6. Navigation* Navigation::navigation_=nullptr;
  7. Navigation* Navigation::GetInstance()
  8. {
  9. if(navigation_== nullptr)
  10. {
  11. navigation_=new Navigation();
  12. }
  13. return navigation_;
  14. }
  15. Navigation::~Navigation()
  16. {
  17. }
  18. void Navigation::ResetPose(const Pose2d& pose)
  19. {
  20. timedPose_.reset(pose,0.1);
  21. }
  22. bool Navigation::Start(std::queue<Trajectory> global_trajectorys)
  23. {
  24. if(running_==true)
  25. {
  26. if(pause_)
  27. {
  28. pause_=false;
  29. return true;
  30. }
  31. printf(" navigation is running pls cancel before\n");
  32. return false;
  33. }
  34. if(thread_!= nullptr)
  35. {
  36. if(thread_->joinable())
  37. thread_->join();
  38. delete thread_;
  39. }
  40. //add 先检查当前点与起点的距离
  41. global_trajectorys_=global_trajectorys;
  42. thread_=new std::thread(&Navigation::navigatting,this);
  43. return true;
  44. }
  45. void Navigation::Cancel()
  46. {
  47. cancel_=true;
  48. if(thread_!= nullptr)
  49. {
  50. if(thread_->joinable())
  51. {
  52. thread_->join();
  53. delete thread_;
  54. thread_= nullptr;
  55. }
  56. }
  57. }
  58. void Navigation::Pause()
  59. {
  60. pause_=true;
  61. }
  62. Navigation::Navigation()
  63. {
  64. }
  65. // 待完成
  66. bool Navigation::moveToPoint(const Pose2d& pose,const Pose2d& distance)
  67. {
  68. Pose2d thresh=Pose2d::abs(distance);
  69. while(cancel_==false)
  70. {
  71. if (pause_ == true)
  72. {
  73. //发送暂停消息
  74. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  75. continue;
  76. }
  77. if (timedPose_.timeout() == true)
  78. {
  79. printf(" navigation Error:Pose is timeout \n");
  80. break;
  81. }
  82. //计算目标pose在当前pose下的pose
  83. Pose2d diff = Pose2d::PoseByPose(pose,timedPose_.Get());
  84. if (Pose2d::abs(diff) < thresh)
  85. {
  86. printf(" Move to pt complete\n");
  87. break;
  88. }
  89. //先旋转使得Y=0摆直,再移动x,再X最后旋转
  90. if(Pose2d::abs(diff).y()>thresh.y())
  91. {
  92. printf("旋转 diff.y:%.5f\n",diff.y());
  93. }else if(Pose2d::abs(diff).x()>thresh.x()){
  94. printf("y移动 diff.x():%.5f\n",diff.x());
  95. }
  96. else if(Pose2d::abs(diff).theta()>thresh.theta()){
  97. printf("旋转 diff.theta():%.5f\n",diff.theta());
  98. }
  99. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  100. }
  101. if(cancel_==true)
  102. return false;
  103. return true;
  104. }
  105. //待完成
  106. bool Navigation::mpc_once(const Trajectory& traj)
  107. {
  108. if(timedPose_.timeout()==true)
  109. {
  110. printf(" MPC once Error:Pose is timeout \n");
  111. return false;
  112. }
  113. if(traj.size()==0)
  114. {
  115. return false;
  116. }
  117. Pose2d pose=timedPose_.Get();
  118. double velocity=0; //从plc获取状态
  119. double angular=0;
  120. LoadedMPC MPC;
  121. Eigen::VectorXd statu;//agv状态
  122. statu<<pose.x(),pose.y(),pose.theta(),velocity,angular;
  123. std::vector<double> out;
  124. Trajectory optimize_trajectory;
  125. return MPC.solve(traj,traj[traj.size()-1],statu,out,optimize_trajectory);
  126. }
  127. bool Navigation::execute_child_trajectory(const Trajectory& child)
  128. {
  129. if(child.size()==0)
  130. return true;
  131. Pose2d head,tail;
  132. Pose2d normal_diff(0.2,0.2,10.*M_PI/180.0);
  133. Pose2d mpc_diff(0.2,0.2,0.5*M_PI/180.0);
  134. Trajectory traj=child;
  135. while(cancel_==false)
  136. {
  137. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  138. if(pause_==true)
  139. {
  140. //发送暂停消息
  141. continue;
  142. }
  143. if(timedPose_.timeout()==true)
  144. {
  145. printf(" navigation Error:Pose is timeout \n");
  146. break;
  147. }
  148. if(Pose2d::abs(timedPose_.Get()-tail)<mpc_diff)
  149. {
  150. printf(" navigation completed!!!\n");
  151. return true;
  152. }
  153. head=child[0];
  154. tail=child[child.size()-1];
  155. //如果与起点差距过大先运动到起点
  156. if(false==Pose2d::abs(timedPose_.Get()-traj[0])<normal_diff)
  157. {
  158. if (false == moveToPoint(head, normal_diff))
  159. return false;
  160. else
  161. continue;
  162. }
  163. else{
  164. //一次变速
  165. if(mpc_once(traj)==false)
  166. {
  167. return false;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. void Navigation::navigatting()
  174. {
  175. pause_=false;
  176. cancel_=false;
  177. running_=true;
  178. printf(" navigation beg...\n");
  179. while(global_trajectorys_.empty()==false)
  180. {
  181. Trajectory traj=global_trajectorys_.front();
  182. if(execute_child_trajectory(traj))
  183. {
  184. global_trajectorys_.pop();
  185. }
  186. else
  187. {
  188. break;
  189. }
  190. }
  191. if(cancel_==true)
  192. printf(" navigation sia canceled\n");
  193. printf(" navigation end...\n");
  194. if(global_trajectorys_.size()==0)
  195. printf("navigation completed!!!\n");
  196. running_=false;
  197. }