navigation.cpp 20 KB

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