main.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <csignal>
  2. #include "pangolinViewer.h"
  3. #include "dijkstra/dijkstra.h"
  4. #include "define/timedlockdata.hpp"
  5. #include "define/TimerRecord.h"
  6. #include <glog/logging.h>
  7. #include "dijkstra/trajectory.h"
  8. #include "emqx/terminator_emqx.h"
  9. #include "tool/pathcreator.h"
  10. #include <queue>
  11. Terminator_emqx* g_terminator= nullptr;
  12. //路径相关
  13. int node_beg=1,node_end=6;
  14. std::mutex pathMutex;
  15. std::vector<int> shortest_path;
  16. PathMap DijkstraMap;
  17. //控制相关
  18. TimedLockData<NavMessage::NavStatu> timed_navStatu_;
  19. bool Create_trajectoris(PathMap& map,std::vector<int> nodes_id,std::queue<Trajectory>& trajs);
  20. bool InitMap(std::string config_file);
  21. bool InitDijkstraMap();
  22. void pangolinSpinOnce(PangolinViewer* viewer) {
  23. if (timed_navStatu_.timeout() == false) {
  24. NavMessage::NavStatu statu = timed_navStatu_.Get();
  25. if (statu.pose_timeout() == false && statu.twist_timeout() == false) {
  26. Pose2d pose2d(statu.current_pose().x(), statu.current_pose().y(), statu.current_pose().theta());
  27. Eigen::Matrix3d rotation_matrix3;
  28. rotation_matrix3 = Eigen::AngleAxisd(pose2d.theta(), Eigen::Vector3d::UnitZ()) *
  29. Eigen::AngleAxisd(0, Eigen::Vector3d::UnitY()) *
  30. Eigen::AngleAxisd(0, Eigen::Vector3d::UnitX());
  31. Eigen::Matrix4d pose;
  32. pose.topLeftCorner(3, 3) = rotation_matrix3;
  33. pose.topRightCorner(3, 1) = Eigen::Vector3d(pose2d.x(), pose2d.y(), 0);
  34. viewer->ShowRealtimeStatu(pose, statu.current_pose().v(), statu.current_pose().vth());
  35. }
  36. Trajectory select_traj;
  37. Trajectory predict_traj;
  38. for (int i = 0; i < statu.selected_traj().poses_size(); ++i) {
  39. select_traj.push_point(Pose2d(statu.selected_traj().poses(i).x(),
  40. statu.selected_traj().poses(i).y(),
  41. statu.selected_traj().poses(i).theta()));
  42. }
  43. for (int i = 0; i < statu.predict_traj().poses_size(); ++i) {
  44. predict_traj.push_point(Pose2d(statu.predict_traj().poses(i).x(),
  45. statu.predict_traj().poses(i).y(),
  46. statu.predict_traj().poses(i).theta()));
  47. }
  48. viewer->DrawTrajectory(select_traj, 1, 0, 0, 12);
  49. viewer->DrawTrajectory(predict_traj, 0, 1, 1, 15);
  50. }
  51. //绘制dijkstra地图及最短路径
  52. pathMutex.lock();
  53. std::vector<int> solve_path = shortest_path;
  54. pathMutex.unlock();
  55. std::reverse(solve_path.begin(), solve_path.end());
  56. solve_path.push_back(node_end);
  57. viewer->DrawDijkastraMap(DijkstraMap, solve_path);
  58. }
  59. bool OnDijkstraBtn(int beg,int end,float& distance,
  60. std::vector<int>& shortestPath)
  61. {
  62. bool ret= DijkstraMap.GetShortestPath(beg, end, shortestPath, distance);
  63. if(ret)
  64. {
  65. node_beg=beg;
  66. node_end=end;
  67. pathMutex.lock();
  68. shortest_path = shortestPath;
  69. pathMutex.unlock();
  70. }
  71. return ret;
  72. }
  73. bool CreateNavCmd(std::vector<int> nodes,NavMessage::NavCmd& cmd);
  74. void navigation_start()
  75. {
  76. pathMutex.lock();
  77. std::vector<int> solve_path=shortest_path;
  78. pathMutex.unlock();
  79. std::reverse(solve_path.begin(),solve_path.end());
  80. solve_path.push_back(node_end);
  81. //std::queue<Trajectory> global_trajectorys;
  82. //Create_trajectoris(DijkstraMap,solve_path,global_trajectorys);
  83. //发布指令
  84. NavMessage::NavCmd cmd;
  85. if(false==CreateNavCmd(solve_path,cmd))
  86. return;
  87. std::cout<<" ------------------------------------new NavCmd ---------------------------"<<std::endl;
  88. std::cout<<cmd.DebugString()<<std::endl<<std::endl;
  89. if(g_terminator)
  90. g_terminator->SendNavCmd(cmd);
  91. }
  92. void navigation_pause()
  93. {
  94. NavMessage::NavCmd cmd;
  95. cmd.set_action(1); //暂停
  96. if(g_terminator)
  97. g_terminator->SendNavCmd(cmd);
  98. }
  99. void navigation_stop()
  100. {
  101. NavMessage::NavCmd cmd;
  102. cmd.set_action(3); //取消导航
  103. if(g_terminator)
  104. g_terminator->SendNavCmd(cmd);
  105. }
  106. void NavStatuArrived(const NavMessage::NavStatu& statu,void* context)
  107. {
  108. timed_navStatu_.reset(statu,0.5);
  109. }
  110. int main(int argc, char** argv)
  111. {
  112. //reg参数
  113. std::string config_file="../config/horizon_config.yaml";
  114. if(InitMap(config_file)==false)
  115. return -1;
  116. if(!InitDijkstraMap())
  117. {
  118. printf("Dijkstra Map load failed\n");
  119. return -2;
  120. }
  121. g_terminator=new Terminator_emqx("UI_nav","agv1/nav_cmd","agv1/nav_statu");
  122. g_terminator->set_statu_arrived(NavStatuArrived, nullptr);
  123. if(g_terminator->Connect("192.168.2.100",1883)==false)
  124. {
  125. printf(" AGV emqx connect failed\n");
  126. return -1;
  127. }
  128. PangolinViewer* viewer=PangolinViewer::CreateViewer();
  129. viewer->SetCallbacks(pangolinSpinOnce,OnDijkstraBtn);
  130. viewer->SetNavigationCallbacks(navigation_start,navigation_pause,navigation_stop);
  131. viewer->Join();
  132. TimerRecord::PrintAll();
  133. delete viewer;
  134. delete g_terminator;
  135. return 0;
  136. }
  137. bool InitMap(std::string config_file)
  138. {
  139. return true;
  140. }
  141. bool InitDijkstraMap()
  142. {
  143. DijkstraMap.AddVertex(1, 0, 0);
  144. DijkstraMap.AddVertex(2, 20, 0);
  145. DijkstraMap.AddVertex(3, 60, 0);
  146. DijkstraMap.AddVertex(4, 100, 0);
  147. DijkstraMap.AddVertex(5, 100, -16);
  148. DijkstraMap.AddVertex(6, 60, -16);
  149. DijkstraMap.AddVertex(7, 20, 20);
  150. bool ret=true;
  151. ret=ret&&DijkstraMap.AddEdge(1, 2, false);
  152. ret=ret&&DijkstraMap.AddEdge(1, 3, false);
  153. ret=ret&&DijkstraMap.AddEdge(1, 4, false);
  154. ret=ret&&DijkstraMap.AddEdge(2, 3, false);
  155. ret=ret&&DijkstraMap.AddEdge(2, 4, false);
  156. ret=ret&&DijkstraMap.AddEdge(2, 7, false);
  157. ret=ret&&DijkstraMap.AddEdge(3, 4, false);
  158. ret=ret&&DijkstraMap.AddEdge(3, 6, false);
  159. ret=ret&&DijkstraMap.AddEdge(4, 5, false);
  160. ret=ret&&DijkstraMap.AddEdge(5, 6, false);
  161. return ret;
  162. }
  163. bool Create_trajectoris(PathMap& map,std::vector<int> nodes_id,std::queue<Trajectory>& trajs)
  164. {
  165. if(nodes_id.size()<2)
  166. return false;
  167. for(int i=0;i<nodes_id.size()-1;++i)
  168. {
  169. float x1,y1,x2,y2;
  170. map.GetVertexPos(nodes_id[i],x1,y1);
  171. map.GetVertexPos(nodes_id[i+1],x2,y2);
  172. Pose2d start(x1,y1,0);
  173. Pose2d end(x2,y2,0);
  174. Trajectory traj=Trajectory::create_line_trajectory(start,end,0.05);
  175. if(traj.size()>1)
  176. trajs.push(traj);
  177. else
  178. return false;
  179. }
  180. return true;
  181. }
  182. bool CreateNavCmd(std::vector<int> nodes,NavMessage::NavCmd& cmd)
  183. {
  184. if(nodes.size()==0)
  185. return false;
  186. cmd.set_action(0); //开始新导航
  187. char key[255]={0};
  188. static int N=1111;
  189. sprintf(key,"abcd-efg-%d",N++);
  190. cmd.set_key(key);
  191. NavMessage::Pose2d headdiff,enddiff;
  192. headdiff.set_x(0.1);
  193. headdiff.set_y(0.1);
  194. headdiff.set_theta(2*M_PI/180.0);
  195. enddiff.set_x(0.05);
  196. enddiff.set_y(0.03);
  197. enddiff.set_theta(0.5*M_PI/180.0);
  198. //调整到起点,朝向与当前朝向一致
  199. if(timed_navStatu_.timeout()|| timed_navStatu_.Get().pose_timeout())
  200. {
  201. printf(" agv statu is timeout can not get current direction\n");
  202. return false;
  203. }
  204. float current_theta=timed_navStatu_.Get().current_pose().theta();
  205. float x,y;
  206. DijkstraMap.GetVertexPos(nodes[0],x,y);
  207. NavMessage::Action act_head;
  208. act_head.set_type(1);
  209. act_head.mutable_target()->set_x(x);
  210. act_head.mutable_target()->set_y(y);
  211. act_head.mutable_target()->set_theta(current_theta);
  212. act_head.mutable_target_diff()->CopyFrom(headdiff);
  213. cmd.add_actions()->CopyFrom(act_head);
  214. for(int i=1;i<nodes.size();++i)
  215. {
  216. //调整到上一点位姿,朝向当前点
  217. float lx,ly;
  218. DijkstraMap.GetVertexPos(nodes[i-1],lx,ly);
  219. DijkstraMap.GetVertexPos(nodes[i],x,y);
  220. float theta=Trajectory::gen_axis_angle(Pose2d(lx,ly,0),Pose2d(x,y,0));
  221. NavMessage::Action act_adjust;
  222. act_adjust.set_type(1);
  223. act_adjust.mutable_target()->set_x(lx);
  224. act_adjust.mutable_target()->set_y(ly);
  225. act_adjust.mutable_target()->set_theta(theta);
  226. act_adjust.mutable_target_diff()->CopyFrom(headdiff);
  227. cmd.add_actions()->CopyFrom(act_adjust);
  228. //MPC 控制
  229. NavMessage::Action act_along;
  230. act_along.set_type(2);
  231. act_along.mutable_target()->set_x(x);
  232. act_along.mutable_target()->set_y(y);
  233. act_along.mutable_target()->set_theta(theta);
  234. if(i==nodes.size()-1) //最后一个节点
  235. act_along.mutable_target_diff()->CopyFrom(enddiff);
  236. else
  237. act_along.mutable_target_diff()->CopyFrom(headdiff);
  238. cmd.add_actions()->CopyFrom(act_along);
  239. }
  240. return true;
  241. }