navigation.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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(brotherEmqx_)
  13. delete brotherEmqx_;
  14. if(monitor_)
  15. delete monitor_;
  16. if(thread_!= nullptr)
  17. {
  18. if(thread_->joinable())
  19. thread_->join();
  20. delete thread_;
  21. }
  22. if(pubthread_!= nullptr)
  23. {
  24. if(pubthread_->joinable())
  25. pubthread_->join();
  26. delete pubthread_;
  27. }
  28. }
  29. /*
  30. * 高斯函数压缩,x=3
  31. */
  32. double limit_gause(double x,double min,double max){
  33. double r=x>=0?1.0:-1.0;
  34. double delta=max/1.0;
  35. return (max-(max-min)*exp(-x*x/(delta*delta)))*r;
  36. }
  37. double limit(double x,double min,double max){
  38. double ret=x;
  39. if(x>=0){
  40. if(x>max) ret=max;
  41. if(x<min) ret=min;
  42. }else{
  43. if(x<-max) ret=-max;
  44. if(x>-min) ret=-min;
  45. }
  46. return ret;
  47. }
  48. double next_speed(double speed,double target_speed,double acc,double dt=0.1)
  49. {
  50. if(fabs(target_speed-speed)/dt<acc)
  51. {
  52. return target_speed;
  53. }
  54. else
  55. {
  56. double r=target_speed-speed>=0.?1.0:-1.0;
  57. return speed+r*acc*dt;
  58. }
  59. }
  60. bool Navigation::Init(const Navigation_parameter& parameter)
  61. {
  62. parameter_=parameter;
  63. AgvEmqx_parameter agv_p=parameter.agv_emqx();
  64. if(monitor_== nullptr) {
  65. monitor_ = new Monitor_emqx(agv_p.nodeid());
  66. if(monitor_->Connect(agv_p.ip(),agv_p.port())==false)
  67. {
  68. printf(" agv emqx connected failed\n");
  69. return false;
  70. }
  71. monitor_->set_speedcmd_topic(agv_p.pubspeedtopic());
  72. monitor_->AddCallback(agv_p.subposetopic(),Navigation::RobotPoseCallback,this);
  73. monitor_->AddCallback(agv_p.subspeedtopic(),Navigation::RobotSpeedCallback,this);
  74. }
  75. Emqx_parameter terminal_p=parameter.terminal_emqx();
  76. if(terminator_== nullptr) {
  77. terminator_ = new Terminator_emqx(terminal_p.nodeid());
  78. if(terminator_->Connect(terminal_p.ip(),terminal_p.port())==false)
  79. {
  80. printf(" terminator emqx connected failed\n");
  81. return false;
  82. }
  83. terminator_->AddCallback(terminal_p.subnavcmdtopic(),Navigation::NavCmdCallback,this);
  84. }
  85. if(parameter.has_brother_emqx()) {
  86. BrotherEmqx brotherEmqx = parameter.brother_emqx();
  87. if (brotherEmqx_ == nullptr) {
  88. brotherEmqx_ = new Terminator_emqx(brotherEmqx.nodeid());
  89. if (brotherEmqx_->Connect(brotherEmqx.ip(), brotherEmqx.port()) == true) {
  90. brotherEmqx_->AddCallback(brotherEmqx.subbrotherstatutopic(), Navigation::BrotherAgvStatuCallback, this);
  91. }else{
  92. printf(" brother emqx connected failed\n");
  93. // return false;
  94. }
  95. }
  96. }
  97. inited_=true;
  98. if(pubthread_!= nullptr)
  99. {
  100. exit_=true;
  101. if(pubthread_->joinable())
  102. pubthread_->join();
  103. delete pubthread_;
  104. }
  105. exit_=false;
  106. pubthread_=new std::thread(&Navigation::pubStatuThreadFuc,this);
  107. return true;
  108. }
  109. void Navigation::RobotPoseCallback(const MqttMsg& msg,void* context)
  110. {
  111. Navigation* navigator=(Navigation*)context;
  112. NavMessage::LidarOdomStatu statu;
  113. if(msg.toProtoMessage(statu))
  114. {
  115. navigator->ResetPose(Pose2d(statu.x(),statu.y(),statu.theta()));
  116. }
  117. }
  118. void Navigation::HandleAgvStatu(const MqttMsg& msg)
  119. {
  120. NavMessage::AgvStatu speed;
  121. if(msg.toProtoMessage(speed))
  122. {
  123. ResetStatu(speed.v(),speed.w());
  124. ResetClamp((ClampStatu)speed.clamp());
  125. }
  126. }
  127. void Navigation::RobotSpeedCallback(const MqttMsg& msg,void* context)
  128. {
  129. Navigation* navigator=(Navigation*)context;
  130. navigator->HandleAgvStatu(msg);
  131. }
  132. void Navigation::pubStatuThreadFuc(void* p) {
  133. Navigation *navogator = (Navigation *) p;
  134. if (navogator) {
  135. while (navogator->exit_ == false) {
  136. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  137. NavMessage::NavStatu statu;
  138. statu.set_main_agv(false);
  139. navogator->publish_statu(statu);
  140. }
  141. }
  142. }
  143. void Navigation::publish_statu(NavMessage::NavStatu& statu) {
  144. //发布nav状态
  145. statu.set_statu(actionType_); //
  146. //printf(" statu:%d\n",actionType_);
  147. statu.set_move_mode(move_mode_);
  148. NavMessage::Action *current_action = nullptr;
  149. if (running_)
  150. {
  151. statu.set_key(global_navCmd_.key());
  152. std::queue<NavMessage::Action> actios = unfinished_cations_;
  153. while (actios.empty() == false) {
  154. //保存当前动作
  155. NavMessage::Action *act = statu.add_unfinished_actions();
  156. current_action = new NavMessage::Action(*act);
  157. act->CopyFrom(actios.front());
  158. actios.pop();
  159. }
  160. }
  161. //发布MPC信息
  162. if (current_action != nullptr) {
  163. delete current_action;
  164. //发布mpc 预选点
  165. if (selected_traj_.timeout() == false) {
  166. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  167. Pose2d pt = selected_traj_.Get()[i];
  168. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  169. pose->set_x(pt.x());
  170. pose->set_y(pt.y());
  171. pose->set_theta(pt.theta());
  172. }
  173. }
  174. //发布 mpc 预测点
  175. if (predict_traj_.timeout() == false) {
  176. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  177. Pose2d pt = predict_traj_.Get()[i];
  178. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  179. pose->set_x(pt.x());
  180. pose->set_y(pt.y());
  181. pose->set_theta(pt.theta());
  182. }
  183. }
  184. }
  185. MqttMsg msg;
  186. msg.fromProtoMessage(statu);
  187. if(terminator_)
  188. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(),msg);
  189. //发布位姿 ------robot状态--------------------------
  190. NavMessage::RobotStatu robot=CreateRobotStatuMsg();
  191. if(terminator_) {
  192. MqttMsg msg;
  193. msg.fromProtoMessage(robot);
  194. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  195. }
  196. }
  197. NavMessage::RobotStatu Navigation::CreateRobotStatuMsg()
  198. {
  199. NavMessage::RobotStatu robotStatu;
  200. //printf(" %d %d %d \n",timedPose_.timeout(),timedV_.timeout(),timedA_.timeout());
  201. if (timedPose_.timeout() == false) {
  202. Pose2d pose = timedPose_.Get();
  203. robotStatu.set_x(pose.x());
  204. robotStatu.set_y(pose.y());
  205. robotStatu.set_theta(pose.theta());
  206. if((timedV_.timeout() == false && timedA_.timeout() == false)) {
  207. NavMessage::AgvStatu plc;
  208. plc.set_v(timedV_.Get());
  209. plc.set_w(timedA_.Get());
  210. plc.set_clamp(timed_clamp_.Get());
  211. //printf(" timed_clamp_:%d\n ",timed_clamp_.Get());
  212. robotStatu.mutable_agvstatu()->CopyFrom(plc);
  213. }
  214. }
  215. return robotStatu;
  216. }
  217. void Navigation::ResetStatu(double v,double a)
  218. {
  219. timedV_.reset(v,0.5);
  220. timedA_.reset(a,0.5);
  221. }
  222. void Navigation::ResetClamp(ClampStatu statu){
  223. timed_clamp_.reset(statu,1);
  224. }
  225. void Navigation::ResetPose(const Pose2d& pose)
  226. {
  227. timedPose_.reset(pose,0.5);
  228. }
  229. bool Navigation::Start(const NavMessage::NavCmd& cmd)
  230. {
  231. if(unfinished_cations_.empty()==false) //正在运行中,上一次指令未完成
  232. {
  233. if(pause_)
  234. {
  235. pause_=false;
  236. return true;
  237. }
  238. printf(" navigation is running pls cancel before\n");
  239. return false;
  240. }
  241. if(thread_!= nullptr)
  242. {
  243. if(thread_->joinable())
  244. thread_->join();
  245. delete thread_;
  246. }
  247. //add 先检查当前点与起点的距离
  248. global_navCmd_=cmd;
  249. for(int i=0;i<cmd.actions_size();++i)
  250. unfinished_cations_.push(cmd.actions(i));
  251. thread_=new std::thread(&Navigation::navigatting,this);
  252. return true;
  253. }
  254. void Navigation::Cancel()
  255. {
  256. cancel_=true;
  257. }
  258. bool Navigation::Stop() {
  259. if(monitor_)
  260. {
  261. monitor_->stop();
  262. while(cancel_==false)
  263. {
  264. if(timedV_.timeout()==false && timedA_.timeout()==false)
  265. {
  266. if(fabs(timedV_.Get())<1e-6 && fabs(timedA_.Get())<1e-6)
  267. return true;
  268. else
  269. monitor_->stop();
  270. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  271. }else{
  272. printf("Stop failed V/A timeout\n");
  273. return false;
  274. }
  275. }
  276. }
  277. return false;
  278. }
  279. void Navigation::Pause()
  280. {
  281. if(Stop())
  282. pause_=true;
  283. }
  284. Navigation::Navigation()
  285. {
  286. move_mode_=Monitor_emqx::eSingle;
  287. thread_= nullptr;
  288. monitor_= nullptr;
  289. terminator_= nullptr;
  290. timedBrotherPose_.reset(Pose2d(-100,0,0),0.1);
  291. }
  292. void Navigation::BrotherAgvStatuCallback(const MqttMsg& msg,void* context)
  293. {
  294. Navigation* navigator=(Navigation*)context;
  295. NavMessage::LidarOdomStatu brother_statu;
  296. if(msg.toProtoMessage(brother_statu)==false) {
  297. std::cout<<" msg transform to AGVStatu failed,msg:"<<msg.data()<<std::endl;
  298. return;
  299. }
  300. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  301. Pose2d pose(brother_statu.x(),brother_statu.y(),brother_statu.theta());
  302. navigator->timedBrotherPose_.reset(pose,1);
  303. navigator->timedBrotherV_.reset(brother_statu.v(),1);
  304. navigator->timedBrotherA_.reset(brother_statu.vth(),1);
  305. }
  306. void Navigation::HandleNavCmd(const NavMessage::NavCmd& cmd)
  307. {
  308. if(cmd.action()==3)
  309. {
  310. printf(" Nav cancel\n");
  311. Cancel();
  312. return ;
  313. }
  314. if(cmd.action()==2) {
  315. printf(" Nav continue\n");
  316. pause_=false;
  317. return ;
  318. }
  319. if(cmd.action()==1) {
  320. printf(" Nav pause\n");
  321. Pause();
  322. return ;
  323. }
  324. if(cmd.action()==0)
  325. {
  326. Start(cmd);
  327. }
  328. else
  329. printf(" Invalid action ,action:%d\n",cmd.action());
  330. }
  331. void Navigation::NavCmdCallback(const MqttMsg& msg,void* context)
  332. {
  333. NavMessage::NavCmd cmd;
  334. if(msg.toProtoMessage(cmd)==false) {
  335. printf(" msg transform to NavCmd failed ..!!\n");
  336. return;
  337. }
  338. Navigation* navigator=(Navigation*)context;
  339. navigator->HandleNavCmd(cmd);
  340. }
  341. bool Navigation::execute_adjust_action(const Pose2d& target,const Pose2d& target_diff,
  342. stLimit limit_v,stLimit limit_h,stLimit limit_rotate)
  343. {
  344. std::cout<<" adjust tarhet:"<<target<<" diff:"<<target_diff<<std::endl;
  345. if(inited_==false) {
  346. printf(" navigation has not inited\n");
  347. return false;
  348. }
  349. Pose2d thresh=Pose2d::abs(target_diff);
  350. int action=0; //1 原地旋转,2横移,3前进
  351. while(cancel_==false) {
  352. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  353. if (pause_ == true) {
  354. //发送暂停消息
  355. continue;
  356. }
  357. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  358. printf(" navigation Error:Pose/v/a is timeout \n");
  359. break;
  360. }
  361. //判断是否到达目标点
  362. if (IsArrived(timedPose_.Get(), timedV_.Get(), timedA_.Get(), target, target_diff)) {
  363. if(Stop()) {
  364. printf("adjust completed\n");
  365. return true;
  366. }
  367. return false;
  368. }
  369. //计算目标点在当前pose下的pose
  370. Pose2d diff = Pose2d::relativePose(target, timedPose_.Get());
  371. //停止状态
  372. if (action == 0) {
  373. //优先进入旋转
  374. if (Pose2d::abs(diff).x() > thresh.x()) {
  375. //前进
  376. action = 3;
  377. }else if (Pose2d::abs(diff).y() > thresh.y()) {
  378. //横移
  379. action = 2;
  380. }
  381. else if (Pose2d::abs(diff).theta() > thresh.theta()) {
  382. action = 1; //原地旋转
  383. }
  384. }
  385. else
  386. {
  387. double acc_v=2.0;
  388. double acc_angular=20.0*M_PI/180.0;
  389. double dt=0.1;
  390. if (action == 1) {
  391. if (Pose2d::abs(diff).theta() > thresh.theta()) {
  392. double theta = limit_gause(diff.theta(),limit_rotate.min*M_PI/180.0,limit_rotate.max*M_PI/180.0);
  393. double angular=next_speed(timedA_.Get(),theta,acc_angular,dt);
  394. double limit_angular=limit(angular,limit_rotate.min*M_PI/180.0,limit_rotate.max*M_PI/180.0);
  395. SendMoveCmd(move_mode_,Monitor_emqx::eRotate, 0, limit_angular);
  396. actionType_=eRotation;
  397. printf(" Ratate | input anguar:%f,next angular:%f,down:%f diff:%f\n",timedA_.Get(),angular,limit_angular,theta);
  398. continue;
  399. }
  400. }
  401. if (action == 2) {
  402. if (Pose2d::abs(diff).y() > thresh.y()) {
  403. double hv=limit_gause(diff.y(),limit_h.min,limit_h.max);
  404. double velocity=limit(next_speed(timedV_.Get(),hv,acc_v,dt),limit_h.min,limit_h.max);
  405. SendMoveCmd(move_mode_,Monitor_emqx::eHorizontal,velocity , 0);
  406. actionType_=eHorizon;
  407. printf(" Horizontal input:%f out:%f\n",timedV_.Get(),velocity);
  408. continue;
  409. }
  410. }
  411. if (action == 3) {
  412. if (Pose2d::abs(diff).x() > thresh.x()) {
  413. double v=limit_gause(diff.x(),limit_v.min,limit_v.max);
  414. double velocity=limit(next_speed(timedV_.Get(),v,acc_v,dt),limit_v.min,limit_v.max);
  415. SendMoveCmd(move_mode_,Monitor_emqx::eMPC, velocity, 0);
  416. actionType_=eVertical;
  417. printf(" Vrtical input:%f,out:%f\n",timedV_.Get(),velocity);
  418. continue;
  419. }
  420. }
  421. if(Stop()) {
  422. printf(" monitor type :%d completed!!! reset action type!!!\n", action);
  423. action = 0;
  424. }
  425. }
  426. }
  427. return false;
  428. }
  429. bool Navigation::mpc_once(const Trajectory& traj,stLimit limit_v,std::vector<double>& out)
  430. {
  431. if(timedPose_.timeout()==true && timedV_.timeout()==false && timedA_.timeout()==false)
  432. {
  433. printf(" MPC once Error:Pose/V/A is timeout \n");
  434. return false;
  435. }
  436. if(traj.size()==0)
  437. {
  438. printf("traj size ==0\n");
  439. return false;
  440. }
  441. Pose2d pose=timedPose_.Get();
  442. double velocity=timedV_.Get(); //从plc获取状态
  443. double angular=timedA_.Get();
  444. Pose2d brother=timedBrotherPose_.Get();
  445. Pose2d relative=Pose2d::relativePose(brother,pose);//计算另一节在当前小车坐标系下的坐标
  446. if(move_mode_==Monitor_emqx::ActionMode::eMain)
  447. relative=Pose2d(-1e8,-1e8,0);
  448. LoadedMPC MPC(relative,limit_v.min, limit_v.max);
  449. Eigen::VectorXd statu=Eigen::VectorXd::Zero(5);//agv状态
  450. statu[0]=pose.x();
  451. statu[1]=pose.y();
  452. statu[2]=pose.theta();
  453. statu[3]=velocity;
  454. statu[4]=angular;
  455. Trajectory optimize_trajectory;
  456. Trajectory selected_trajectory;
  457. bool ret= MPC.solve(traj,traj[traj.size()-1],statu,out,selected_trajectory,optimize_trajectory);
  458. predict_traj_.reset(optimize_trajectory,1);
  459. selected_traj_.reset(selected_trajectory,1);
  460. return ret;
  461. }
  462. bool Navigation::IsArrived(const Pose2d& cur,double velocity,double angular,
  463. const Pose2d& target,const Pose2d& diff)
  464. {
  465. Pose2d distance=Pose2d::relativePose(target,cur);
  466. if(Pose2d::abs(distance)<diff)
  467. {
  468. if(fabs(velocity)<0.05 && fabs(angular)< 10*M_PI/180.0)
  469. return true;
  470. }
  471. //std::cout<<"distance:"<<distance<<" diff:"<<diff<<" v:"<<velocity<<" a:"<<angular<<std::endl;
  472. return false;
  473. }
  474. void Navigation::SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  475. double v,double angular){
  476. if(monitor_) {
  477. monitor_->set_speed(mode, type, v, angular);
  478. }
  479. }
  480. bool Navigation::clamp_close()
  481. {
  482. if(monitor_) {
  483. monitor_->clamp_close(move_mode_);
  484. actionType_=eClampClose;
  485. while (cancel_ == false) {
  486. if (timed_clamp_.timeout()) {
  487. printf("timed clamp is timeout\n");
  488. return false;
  489. }
  490. if (timed_clamp_.Get() == eClosed)
  491. return true;
  492. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  493. monitor_->clamp_close(move_mode_);
  494. actionType_=eClampClose;
  495. }
  496. return false;
  497. }
  498. return false;
  499. }
  500. bool Navigation::clamp_open() {
  501. if(monitor_) {
  502. monitor_->clamp_open(move_mode_);
  503. actionType_=eClampOpen;
  504. while(cancel_==false)
  505. {
  506. if(timed_clamp_.timeout())
  507. {
  508. printf("timed clamp is timeout\n");
  509. return false;
  510. }
  511. if(timed_clamp_.Get()==eOpened)
  512. return true;
  513. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  514. monitor_->clamp_open(move_mode_);
  515. actionType_=eClampOpen;
  516. }
  517. return false;
  518. }
  519. return false;
  520. }
  521. bool Navigation::execute_along_action(const Pose2d& begin,const Pose2d& target,const Pose2d& target_diff,stLimit limit_v) {
  522. if (inited_ == false) {
  523. printf(" navigation has not inited\n");
  524. return false;
  525. }
  526. if (timedPose_.timeout()) {
  527. printf(" navigation Error:Pose is timeout \n");
  528. return false;
  529. }
  530. //生成轨迹
  531. Trajectory traj = Trajectory::create_line_trajectory(begin, target);
  532. if (traj.size() == 0)
  533. return true;
  534. while (cancel_ == false) {
  535. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  536. if (pause_ == true) {
  537. //发送暂停消息
  538. continue;
  539. }
  540. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  541. printf(" navigation Error:Pose/v/a is timeout \n");
  542. return false;
  543. }
  544. //判断是否到达终点
  545. if (IsArrived(timedPose_.Get(), timedV_.Get(), timedA_.Get(), target, target_diff)) {
  546. if (Stop()) {
  547. printf(" exec along completed !!!\n");
  548. return true;
  549. }
  550. }
  551. /*
  552. * 判断周边是否有障碍物,半径r,当障碍物进入r内,停下等待,当障碍物远离 1.2*r,恢复
  553. * */
  554. if(move_mode_==Monitor_emqx::eSingle) {
  555. double r = 2.8;
  556. static bool obs_stoped = false;
  557. Pose2d obs_relative = Pose2d::relativePose(timedBrotherPose_.Get(), timedPose_.Get());
  558. double distance = sqrt(obs_relative.x() * obs_relative.x() + obs_relative.y() * obs_relative.y());
  559. if (obs_stoped == false) {
  560. if (distance < r && fabs(obs_relative.x()) > 0.2) {
  561. std::cout << "find obs stop to wait...." << std::endl;
  562. Stop();
  563. obs_stoped = true;
  564. continue;
  565. }
  566. } else {
  567. //障碍解除
  568. if (distance > 1.2 * r) {
  569. std::cout << "obs release continue MPC" << std::endl;
  570. obs_stoped = false;
  571. } else {
  572. continue;
  573. }
  574. }
  575. }
  576. //一次变速
  577. std::vector<double> out;
  578. bool ret;
  579. TimerRecord::Execute([&, this]() {
  580. ret = mpc_once(traj, limit_v, out);
  581. }, "mpc_once");
  582. if (ret == false) {
  583. Stop();
  584. return false;
  585. }
  586. /*
  587. * 判断车辆是否在终点附近徘徊,无法到达终点
  588. */
  589. if (mpc_possible_to_end(target, target_diff) == false) {
  590. printf(" --------------MPC imposible to target -----------------------\n");
  591. Stop();
  592. //调整到目标点
  593. stLimit limitv, limitR;
  594. limitv.min = 0.05;
  595. limitv.max = 0.1;
  596. limitR.min = 2 * M_PI / 180.0;
  597. limitR.max = 10 * M_PI / 180.0;
  598. if (execute_adjust_action(target, target_diff, limitv, limitv, limitR))
  599. return true;
  600. else
  601. return false;
  602. }
  603. SendMoveCmd(move_mode_, Monitor_emqx::eMPC, out[0], out[1]);
  604. actionType_=eMPC;
  605. //std::this_thread::sleep_for(std::chrono::milliseconds (400));
  606. }
  607. return false;
  608. }
  609. bool Navigation::mpc_possible_to_end(const Pose2d& target,const Pose2d& target_diff)
  610. {
  611. if(timedPose_.timeout()==false) {
  612. Pose2d diff = Pose2d::relativePose(target, timedPose_.Get());
  613. if (fabs(diff.y()) < target_diff.y() && fabs(diff.x()) < target_diff.x()) { //xy在精度内
  614. if (timedV_.timeout() == true)
  615. return false;
  616. if(fabs(timedV_.Get())<0.05 && fabs(diff.theta())>target_diff.theta())
  617. return false;
  618. return true;
  619. } else { //xy不在精度内
  620. if (fabs(diff.x()) < 1e-3)
  621. return false; //x已到达,Y未到达,则不可能到达目标点
  622. double theta = fabs(atan(diff.y() / diff.x()));
  623. if (theta > 5 * M_PI / 180.0) {
  624. if (timedV_.timeout() == true)
  625. return false;
  626. if (fabs(timedV_.Get()) < 0.05)
  627. return false;
  628. else
  629. return true;
  630. } else {
  631. return true;
  632. }
  633. }
  634. }
  635. return false;
  636. }
  637. void Navigation::navigatting()
  638. {
  639. if(inited_==false) {
  640. printf(" navigation has not inited\n");
  641. return ;
  642. }
  643. pause_=false;
  644. cancel_=false;
  645. running_=true;
  646. actionType_=eReady;
  647. printf(" navigation beg...\n");
  648. while(unfinished_cations_.empty()==false && cancel_==false)
  649. {
  650. NavMessage::Action act=unfinished_cations_.front();
  651. Pose2d target(act.target().x(),act.target().y(),act.target().theta());
  652. Pose2d target_diff(act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  653. if(act.type()==1)
  654. {
  655. printf(" adjust to (%.3f %.3f %.3f) diff:(%.3f,%.3f,%.3f)\n",
  656. act.target().x(),act.target().y(),act.target().theta(),
  657. act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  658. stLimit limit_angular,limit_velocity,limint_horize;
  659. limit_velocity.min=act.velocity_limit().min();
  660. limit_velocity.max=act.velocity_limit().max();
  661. limit_angular.min=act.angular_limit().min();
  662. limit_angular.max=act.angular_limit().max();
  663. limint_horize.min=act.horize_limit().min();
  664. limint_horize.max=act.horize_limit().max();
  665. if(execute_adjust_action(target,target_diff,limit_velocity,limint_horize,limit_angular)==false)
  666. {
  667. printf("execute adjust action failed\n");
  668. break;
  669. }
  670. }
  671. else if(act.type()==2)
  672. {
  673. Pose2d begin(act.begin().x(),act.begin().y(),act.begin().theta());
  674. stLimit limit_velocity;
  675. limit_velocity.min=act.velocity_limit().min();
  676. limit_velocity.max=act.velocity_limit().max();
  677. printf(" MPC to (%.3f %.3f %.3f) diff:(%.3f,%.3f,%.3f)\n",
  678. act.target().x(),act.target().y(),act.target().theta(),
  679. act.target_diff().x(),act.target_diff().y(),act.target_diff().theta());
  680. if(execute_along_action(begin,target,target_diff,limit_velocity)==false) {
  681. printf("execute along action failed\n");
  682. break;
  683. }
  684. }
  685. else if(act.type()==3) //夹持
  686. {
  687. if(this->clamp_close()==false)
  688. {
  689. printf("夹持failed ...\n");
  690. break;
  691. }
  692. }
  693. else if(act.type()==4)
  694. {
  695. if(this->clamp_open()==false)
  696. {
  697. printf("打开夹持 failed...\n");
  698. break;
  699. }
  700. }
  701. else
  702. {
  703. printf(" unknow action type:%d\n",act.type());
  704. break;
  705. }
  706. unfinished_cations_.pop();
  707. }
  708. actionType_=eReady;
  709. Stop();
  710. if(cancel_==true) {
  711. printf(" navigation canceled\n");
  712. while(unfinished_cations_.empty()==false)
  713. unfinished_cations_.pop();
  714. }else {
  715. if (unfinished_cations_.empty())
  716. printf("navigation completed!!!\n");
  717. else{
  718. printf(" navigation Failed\n");
  719. while(unfinished_cations_.empty()==false)
  720. unfinished_cations_.pop();
  721. }
  722. }
  723. running_=false;
  724. }