navigation.cpp 20 KB

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