navigation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //
  2. // Created by zx on 22-12-2.
  3. //
  4. #include "navigation.h"
  5. #include "loaded_mpc.h"
  6. #include "../define/TimerRecord.h"
  7. Navigation::~Navigation()
  8. {
  9. exit_=true;
  10. if(terminator_)
  11. delete terminator_;
  12. if(monitor_)
  13. delete monitor_;
  14. if(thread_!= nullptr)
  15. {
  16. if(thread_->joinable())
  17. thread_->join();
  18. delete thread_;
  19. }
  20. if(pubthread_!= nullptr)
  21. {
  22. if(pubthread_->joinable())
  23. pubthread_->join();
  24. delete pubthread_;
  25. }
  26. }
  27. /*
  28. * 高斯函数压缩,x=3
  29. */
  30. double limit(double x,double min,double max){
  31. double r=x>=0?1.0:-1.0;
  32. double delta=max/3.0;
  33. return (max-(max-min)*exp(-x*x/(delta*delta)))*r;
  34. }
  35. bool Navigation::Init(const Navigation_parameter& parameter)
  36. {
  37. parameter_=parameter;
  38. AgvEmqx_parameter agv_p=parameter.agv_emqx();
  39. if(monitor_== nullptr) {
  40. monitor_ = new Monitor_emqx(agv_p.nodeid(), agv_p.pubtopic(), agv_p.subtopic());
  41. monitor_->set_statu_arrived_callback(Navigation::RobotStatuCallback, this);
  42. if(monitor_->Connect(agv_p.ip(),agv_p.port())==false)
  43. {
  44. printf(" agv emqx connected failed\n");
  45. return false;
  46. }
  47. }
  48. Emqx_parameter terminal_p=parameter.terminal_emqx();
  49. if(terminator_== nullptr) {
  50. terminator_ = new Terminator_emqx(terminal_p.nodeid());
  51. if(terminator_->Connect(terminal_p.ip(),terminal_p.port())==false)
  52. {
  53. printf(" terminator emqx connected failed\n");
  54. return false;
  55. }
  56. terminator_->AddCallback(terminal_p.subnavcmdtopic(),Navigation::NavCmdCallback,this);
  57. terminator_->AddCallback(terminal_p.subbrotherstatutopic(),Navigation::BrotherAgvStatuCallback,this);
  58. }
  59. inited_=true;
  60. if(pubthread_!= nullptr)
  61. {
  62. exit_=true;
  63. if(pubthread_->joinable())
  64. pubthread_->join();
  65. delete pubthread_;
  66. }
  67. exit_=false;
  68. pubthread_=new std::thread(&Navigation::publish_statu,this);
  69. return true;
  70. }
  71. void Navigation::publish_statu() {
  72. while (exit_ == false)
  73. {
  74. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  75. //发布nav状态
  76. NavMessage::NavStatu statu;
  77. statu.set_statu(eReady); //默认ready
  78. NavMessage::Action *current_action = nullptr;
  79. if (running_)
  80. {
  81. statu.set_statu(eRunning);
  82. statu.set_key(global_navCmd_.key());
  83. std::queue<NavMessage::Action> actios = unfinished_cations_;
  84. while (actios.empty() == false) {
  85. //保存当前动作
  86. NavMessage::Action *act = statu.add_unfinished_actions();
  87. current_action = new NavMessage::Action(*act);
  88. act->CopyFrom(actios.front());
  89. actios.pop();
  90. }
  91. }
  92. //发布MPC信息
  93. if (current_action != nullptr) {
  94. delete current_action;
  95. //发布mpc 预选点
  96. if (selected_traj_.timeout() == false) {
  97. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  98. Pose2d pt = selected_traj_.Get()[i];
  99. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  100. pose->set_x(pt.x());
  101. pose->set_y(pt.y());
  102. pose->set_theta(pt.theta());
  103. }
  104. }
  105. //发布 mpc 预测点
  106. if (predict_traj_.timeout() == false) {
  107. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  108. Pose2d pt = predict_traj_.Get()[i];
  109. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  110. pose->set_x(pt.x());
  111. pose->set_y(pt.y());
  112. pose->set_theta(pt.theta());
  113. }
  114. }
  115. }
  116. MqttMsg msg;
  117. msg.fromProtoMessage(statu);
  118. if(terminator_)
  119. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(),msg);
  120. //发布位姿 --------------------------------
  121. if (timedPose_.timeout() == false&&
  122. timedV_.timeout() == false && timedA_.timeout() == false) {
  123. NavMessage::AGVStatu agvStatu;
  124. Pose2d pose = timedPose_.Get();
  125. agvStatu.set_x(pose.x());
  126. agvStatu.set_y(pose.y());
  127. agvStatu.set_theta(pose.theta());
  128. agvStatu.set_v(timedV_.Get());
  129. agvStatu.set_vth(timedA_.Get());
  130. if(terminator_) {
  131. MqttMsg msg;
  132. msg.fromProtoMessage(agvStatu);
  133. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  134. }
  135. }
  136. }
  137. }
  138. void Navigation::ResetStatu(double v,double a)
  139. {
  140. timedV_.reset(v,0.5);
  141. timedA_.reset(a,0.5);
  142. }
  143. void Navigation::ResetPose(const Pose2d& pose)
  144. {
  145. timedPose_.reset(pose,0.5);
  146. }
  147. bool Navigation::Start(const NavMessage::NavCmd& cmd)
  148. {
  149. if(unfinished_cations_.empty()==false) //正在运行中,上一次指令未完成
  150. {
  151. if(pause_)
  152. {
  153. pause_=false;
  154. return true;
  155. }
  156. printf(" navigation is running pls cancel before\n");
  157. return false;
  158. }
  159. if(thread_!= nullptr)
  160. {
  161. if(thread_->joinable())
  162. thread_->join();
  163. delete thread_;
  164. }
  165. //add 先检查当前点与起点的距离
  166. global_navCmd_=cmd;
  167. for(int i=0;i<cmd.actions_size();++i)
  168. unfinished_cations_.push(cmd.actions(i));
  169. thread_=new std::thread(&Navigation::navigatting,this);
  170. return true;
  171. }
  172. void Navigation::Cancel()
  173. {
  174. cancel_=true;
  175. }
  176. void Navigation::Pause()
  177. {
  178. monitor_->stop();
  179. pause_=true;
  180. }
  181. Navigation::Navigation()
  182. {
  183. thread_= nullptr;
  184. monitor_= nullptr;
  185. terminator_= nullptr;
  186. }
  187. void Navigation::BrotherAgvStatuCallback(const MqttMsg& msg,void* context)
  188. {
  189. Navigation* navigator=(Navigation*)context;
  190. NavMessage::AGVStatu brother_statu;
  191. if(msg.toProtoMessage(brother_statu)==false) {
  192. printf(" msg transform to AGVStatu failed ..!!\n");
  193. return;
  194. }
  195. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  196. Pose2d pose(brother_statu.x(),brother_statu.y(),brother_statu.theta());
  197. navigator->timedBrotherPose_.reset(pose,1);
  198. navigator->timedBrotherV_.reset(brother_statu.v(),1);
  199. navigator->timedBrotherA_.reset(brother_statu.vth(),1);
  200. }
  201. void Navigation::NavCmdCallback(const MqttMsg& msg,void* context)
  202. {
  203. Navigation* navigator=(Navigation*)context;
  204. NavMessage::NavCmd cmd;
  205. if(msg.toProtoMessage(cmd)==false) {
  206. printf(" msg transform to NavCmd failed ..!!\n");
  207. return;
  208. }
  209. if(cmd.action()==3)
  210. {
  211. navigator->Cancel();
  212. return ;
  213. }
  214. if(cmd.action()==2) {
  215. navigator->pause_=false;
  216. return ;
  217. }
  218. if(cmd.action()==1) {
  219. navigator->Pause();
  220. return ;
  221. }
  222. navigator->Start(cmd);
  223. }
  224. void Navigation::RobotStatuCallback(double x,double y,double theta,double v,double vth,void* context)
  225. {
  226. Navigation* navigator=(Navigation*)context;
  227. navigator->ResetPose(Pose2d(x,y,theta));
  228. navigator->ResetStatu(v,vth);
  229. }
  230. bool Navigation::exec_adjust_action(const Pose2d& target,const Pose2d& target_diff)
  231. {
  232. if(inited_==false) {
  233. printf(" navigation has not inited\n");
  234. return false;
  235. }
  236. Pose2d thresh=Pose2d::abs(target_diff);
  237. int action=0; //1 原地旋转,2横移,3前进
  238. while(cancel_==false) {
  239. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  240. if (pause_ == true) {
  241. //发送暂停消息
  242. continue;
  243. }
  244. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  245. printf(" navigation Error:Pose/v/a is timeout \n");
  246. break;
  247. }
  248. //判断是否到达目标点
  249. if (IsArrived(timedPose_.Get(), timedV_.Get(), timedA_.Get(), target, target_diff)) {
  250. monitor_->stop();
  251. printf("adjust completed\n");
  252. return true;
  253. }
  254. //计算目标点在当前pose下的pose
  255. Pose2d diff = Pose2d::TargetByPose(target, timedPose_.Get());
  256. //停止状态
  257. if (action == 0) {
  258. //优先进入旋转
  259. if (Pose2d::abs(diff).theta() > thresh.theta()) {
  260. action = 1; //原地旋转
  261. } else if (Pose2d::abs(diff).x() > thresh.x()) {
  262. //前进
  263. action = 3;
  264. }else if (Pose2d::abs(diff).y() > thresh.y()) {
  265. //横移
  266. action = 2;
  267. }
  268. }
  269. else
  270. {
  271. if (action == 1) {
  272. if (Pose2d::abs(diff).theta() > thresh.theta()) {
  273. double theta = limit(diff.theta(),5*M_PI/180.0,40*M_PI/180.0);
  274. monitor_->set_speed(Monitor_emqx::eRotate, 0, theta);
  275. printf(" Ratate :%f\n",theta);
  276. continue;
  277. }
  278. }
  279. if (action == 2) {
  280. if (Pose2d::abs(diff).y() > thresh.y()) {
  281. monitor_->set_speed(Monitor_emqx::eHorizontal, limit(diff.y(),0.05,0.2), 0);
  282. printf(" Horizontal :%f\n",limit(diff.y(),0.05,0.2));
  283. continue;
  284. }
  285. }
  286. if (action == 3) {
  287. if (Pose2d::abs(diff).x() > thresh.x()) {
  288. monitor_->set_speed(Monitor_emqx::eMPC, limit(diff.x(),0.05,0.5), 0);
  289. printf(" Vrtical :%f\n",limit(diff.x(),0.05,0.5));
  290. continue;
  291. }
  292. }
  293. monitor_->stop();
  294. printf(" monitor type :%d completed!!! reset action type!!!\n", action);
  295. action = 0;
  296. }
  297. }
  298. return false;
  299. }
  300. bool Navigation::mpc_once(const Trajectory& traj,std::vector<double>& out)
  301. {
  302. if(timedPose_.timeout()==true && timedV_.timeout()==false && timedA_.timeout()==false)
  303. {
  304. printf(" MPC once Error:Pose/V/A is timeout \n");
  305. return false;
  306. }
  307. if(traj.size()==0)
  308. {
  309. printf("traj size ==0\n");
  310. return false;
  311. }
  312. Pose2d pose=timedPose_.Get();
  313. double velocity=timedV_.Get(); //从plc获取状态
  314. double angular=timedA_.Get();
  315. LoadedMPC MPC(2.7);
  316. Eigen::VectorXd statu=Eigen::VectorXd::Zero(5);//agv状态
  317. statu[0]=pose.x();
  318. statu[1]=pose.y();
  319. statu[2]=pose.theta();
  320. statu[3]=velocity;
  321. statu[4]=angular;
  322. Trajectory optimize_trajectory;
  323. Trajectory selected_trajectory;
  324. bool ret= MPC.solve(traj,traj[traj.size()-1],statu,out,selected_trajectory,optimize_trajectory);
  325. predict_traj_.reset(optimize_trajectory,1);
  326. selected_traj_.reset(selected_trajectory,1);
  327. return ret;
  328. }
  329. bool Navigation::IsArrived(const Pose2d& cur,double velocity,double angular,
  330. const Pose2d& target,const Pose2d& diff)
  331. {
  332. Pose2d distance=Pose2d::TargetByPose(target,cur);
  333. if(Pose2d::abs(distance)<diff)
  334. {
  335. if(fabs(velocity)<0.1 && fabs(angular)< 10*M_PI/180.0)
  336. return true;
  337. }
  338. return false;
  339. }
  340. bool Navigation::execute_along_action(const Pose2d& target,const Pose2d& target_diff)
  341. {
  342. if(inited_==false) {
  343. printf(" navigation has not inited\n");
  344. return false;
  345. }
  346. if (timedPose_.timeout() ) {
  347. printf(" navigation Error:Pose is timeout \n");
  348. return false;
  349. }
  350. //生成轨迹
  351. Trajectory traj=Trajectory::create_line_trajectory(timedPose_.Get(),target);
  352. if(traj.size()==0)
  353. return true;
  354. while(cancel_==false)
  355. {
  356. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  357. if(pause_==true)
  358. {
  359. //发送暂停消息
  360. continue;
  361. }
  362. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  363. printf(" navigation Error:Pose/v/a is timeout \n");
  364. return false;
  365. }
  366. //判断是否到达终点
  367. if(IsArrived(timedPose_.Get(),timedV_.Get(),timedA_.Get(),target,target_diff))
  368. {
  369. monitor_->stop();
  370. printf(" exec along completed !!!\n");
  371. return true;
  372. }
  373. //一次变速
  374. std::vector<double> out;
  375. bool ret;
  376. TimerRecord::Execute([&,this](){
  377. ret=mpc_once(traj,out);
  378. },"mpc_once");
  379. if(ret==false)
  380. {
  381. monitor_->stop();
  382. return false;
  383. }
  384. monitor_->set_speed(Monitor_emqx::eMPC,out[0],out[1]);
  385. }
  386. return false;
  387. }
  388. void Navigation::navigatting()
  389. {
  390. if(inited_==false) {
  391. printf(" navigation has not inited\n");
  392. return ;
  393. }
  394. pause_=false;
  395. cancel_=false;
  396. running_=true;
  397. printf(" navigation beg...\n");
  398. while(unfinished_cations_.empty()==false && cancel_==false)
  399. {
  400. NavMessage::Action act=unfinished_cations_.front();
  401. Pose2d target(act.target().x(),act.target().y(),act.target().theta());
  402. Pose2d target_diff(act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  403. if(act.type()==1)
  404. {
  405. printf(" adjust to (%.3f %.3f %.3f) diff:(%.3f,%.3f,%.3f)\n",
  406. act.target().x(),act.target().y(),act.target().theta(),
  407. act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  408. if(exec_adjust_action(target,target_diff)==false)
  409. {
  410. printf("execute adjust action failed\n");
  411. break;
  412. }
  413. }
  414. else if(act.type()==2)
  415. {
  416. printf(" MPC to (%.3f %.3f %.3f) diff:(%.3f,%.3f,%.3f)\n",
  417. act.target().x(),act.target().y(),act.target().theta(),
  418. act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  419. if(execute_along_action(target,target_diff)==false) {
  420. printf("execute along action failed\n");
  421. break;
  422. }
  423. }
  424. else
  425. {
  426. printf(" unknow action type:%d\n",act.type());
  427. break;
  428. }
  429. unfinished_cations_.pop();
  430. }
  431. monitor_->stop();
  432. if(cancel_==true) {
  433. printf(" navigation canceled\n");
  434. while(unfinished_cations_.empty()==false)
  435. unfinished_cations_.pop();
  436. }else {
  437. if (unfinished_cations_.empty())
  438. printf("navigation completed!!!\n");
  439. else{
  440. printf(" navigation Failed\n");
  441. while(unfinished_cations_.empty()==false)
  442. unfinished_cations_.pop();
  443. }
  444. }
  445. running_=false;
  446. }