navigation.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //
  2. // Created by zx on 22-12-2.
  3. //
  4. #include "navigation.h"
  5. #include "loaded_mpc.h"
  6. #include "rotate_mpc.h"
  7. #include "../define/TimerRecord.h"
  8. Navigation::~Navigation() {
  9. exit_ = true;
  10. if (terminator_)
  11. delete terminator_;
  12. if (brotherEmqx_)
  13. delete brotherEmqx_;
  14. if (monitor_)
  15. delete monitor_;
  16. if (pubthread_ != nullptr) {
  17. if (pubthread_->joinable())
  18. pubthread_->join();
  19. delete pubthread_;
  20. }
  21. }
  22. /*
  23. * 高斯函数压缩,x=3
  24. */
  25. double limit_gause(double x, double min, double max) {
  26. double r = x >= 0 ? 1.0 : -1.0;
  27. double delta = max / 1.0;
  28. return (max - (max - min) * exp(-x * x / (delta * delta))) * r;
  29. }
  30. double limit(double x, double min, double max) {
  31. double ret = x;
  32. if (x >= 0) {
  33. if (x > max) ret = max;
  34. if (x < min) ret = min;
  35. } else {
  36. if (x < -max) ret = -max;
  37. if (x > -min) ret = -min;
  38. }
  39. return ret;
  40. }
  41. double next_speed(double speed, double target_speed, double acc, double dt) {
  42. if (fabs(target_speed - speed) / dt < acc) {
  43. return target_speed;
  44. } else {
  45. double r = target_speed - speed >= 0. ? 1.0 : -1.0;
  46. return speed + r * acc * dt;
  47. }
  48. }
  49. bool Navigation::PoseTimeout() {
  50. if (timedPose_.timeout() == false && timedBrotherPose_.timeout() == false) {
  51. return false;
  52. } else {
  53. if (timedPose_.timeout()) {
  54. printf(" current pose is timeout \n");
  55. }
  56. if (timedBrotherPose_.timeout()) {
  57. printf(" brother pose is timeout \n");
  58. }
  59. return true;
  60. }
  61. }
  62. bool Navigation::Init(const NavParameter::Navigation_parameter &parameter) {
  63. parameter_ = parameter;
  64. NavParameter::AgvEmqx_parameter agv_p = parameter.agv_emqx();
  65. if (monitor_ == nullptr) {
  66. monitor_ = new Monitor_emqx(agv_p.nodeid());
  67. if (monitor_->Connect(agv_p.ip(), agv_p.port()) == false) {
  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. NavParameter::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. printf(" terminator emqx connected failed\n");
  80. return false;
  81. }
  82. }
  83. if (parameter.has_brother_emqx()) {
  84. NavParameter::BrotherEmqx brotherEmqx = parameter.brother_emqx();
  85. if (brotherEmqx_ == nullptr) {
  86. brotherEmqx_ = new Terminator_emqx(brotherEmqx.nodeid());
  87. if (brotherEmqx_->Connect(brotherEmqx.ip(), brotherEmqx.port()) == true) {
  88. brotherEmqx_->AddCallback(brotherEmqx.subbrotherstatutopic(), Navigation::BrotherAgvStatuCallback,
  89. this);
  90. } else {
  91. printf(" brother emqx connected failed\n");
  92. // return false;
  93. }
  94. }
  95. }
  96. inited_ = true;
  97. if (pubthread_ != nullptr) {
  98. exit_ = true;
  99. if (pubthread_->joinable())
  100. pubthread_->join();
  101. delete pubthread_;
  102. }
  103. exit_ = false;
  104. pubthread_ = new std::thread(&Navigation::pubStatuThreadFuc, this);
  105. return true;
  106. }
  107. void Navigation::RobotPoseCallback(const MqttMsg &msg, void *context) {
  108. Navigation *navigator = (Navigation *) context;
  109. NavMessage::LidarOdomStatu statu;
  110. if (msg.toProtoMessage(statu)) {
  111. navigator->ResetPose(Pose2d(statu.x(), statu.y(), statu.theta()));
  112. }
  113. }
  114. void Navigation::HandleAgvStatu(const MqttMsg &msg) {
  115. NavMessage::AgvStatu speed;
  116. if (msg.toProtoMessage(speed)) {
  117. ResetStatu(speed.v(), speed.w());
  118. ResetClamp((ClampStatu) speed.clamp());
  119. }
  120. }
  121. void Navigation::RobotSpeedCallback(const MqttMsg &msg, void *context) {
  122. Navigation *navigator = (Navigation *) context;
  123. navigator->HandleAgvStatu(msg);
  124. }
  125. void Navigation::pubStatuThreadFuc(void *p) {
  126. Navigation *navogator = (Navigation *) p;
  127. if (navogator) {
  128. while (navogator->exit_ == false) {
  129. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  130. NavMessage::NavStatu statu;
  131. statu.set_main_agv(false);
  132. navogator->publish_statu(statu);
  133. }
  134. }
  135. }
  136. void Navigation::publish_statu(NavMessage::NavStatu &statu) {
  137. if (timedPose_.timeout() == false) {
  138. NavMessage::LidarOdomStatu odom;
  139. odom.set_x(timedPose_.Get().x());
  140. odom.set_y(timedPose_.Get().y());
  141. odom.set_theta(timedPose_.Get().theta());
  142. if (timedV_.timeout() == false)
  143. odom.set_v(timedV_.Get());
  144. if (timedA_.timeout() == false)
  145. odom.set_vth(timedA_.Get());
  146. statu.mutable_odom()->CopyFrom(odom);
  147. }
  148. statu.set_in_space(isInSpace_);
  149. if (isInSpace_) statu.set_space_id(space_id_);
  150. //发布nav状态
  151. statu.set_statu(actionType_); //
  152. statu.set_move_mode(move_mode_);
  153. if (running_) {
  154. statu.set_key(global_navCmd_.key());
  155. }
  156. //发布MPC信息
  157. //发布mpc 预选点
  158. if (selected_traj_.timeout() == false) {
  159. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  160. Pose2d pt = selected_traj_.Get()[i];
  161. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  162. pose->set_x(pt.x());
  163. pose->set_y(pt.y());
  164. pose->set_theta(pt.theta());
  165. }
  166. }
  167. //发布 mpc 预测点
  168. if (predict_traj_.timeout() == false) {
  169. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  170. Pose2d pt = predict_traj_.Get()[i];
  171. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  172. pose->set_x(pt.x());
  173. pose->set_y(pt.y());
  174. pose->set_theta(pt.theta());
  175. }
  176. }
  177. //std::cout<<"nav statu:"<<statu.DebugString()<<std::endl;
  178. MqttMsg msg;
  179. msg.fromProtoMessage(statu);
  180. if (terminator_)
  181. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(), msg);
  182. //发布位姿 ------robot状态--------------------------
  183. NavMessage::RobotStatu robot;
  184. if (CreateRobotStatuMsg(robot)) {
  185. if (terminator_) {
  186. MqttMsg msg;
  187. msg.fromProtoMessage(robot);
  188. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  189. }
  190. }
  191. }
  192. bool Navigation::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  193. //printf(" %d %d %d \n",timedPose_.timeout(),timedV_.timeout(),timedA_.timeout());
  194. if (timedPose_.timeout() == false) {
  195. Pose2d pose = timedPose_.Get();
  196. robotStatu.set_x(pose.x());
  197. robotStatu.set_y(pose.y());
  198. robotStatu.set_theta(pose.theta());
  199. if ((timedV_.timeout() == false && timedA_.timeout() == false)) {
  200. NavMessage::AgvStatu plc;
  201. plc.set_v(timedV_.Get());
  202. plc.set_w(timedA_.Get());
  203. plc.set_clamp(timed_clamp_.Get());
  204. //printf(" timed_clamp_:%d\n ",timed_clamp_.Get());
  205. robotStatu.mutable_agvstatu()->CopyFrom(plc);
  206. }
  207. return true;
  208. }
  209. return false;
  210. }
  211. void Navigation::ResetStatu(double v, double a) {
  212. timedV_.reset(v, 0.5);
  213. timedA_.reset(a, 0.5);
  214. }
  215. void Navigation::ResetClamp(ClampStatu statu) {
  216. timed_clamp_.reset(statu, 1);
  217. }
  218. void Navigation::ResetPose(const Pose2d &pose) {
  219. timedPose_.reset(pose, 1.0);
  220. }
  221. void Navigation::Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  222. cancel_ = true;
  223. response.set_ret(0);
  224. }
  225. bool Navigation::Stop() {
  226. if (monitor_) {
  227. monitor_->stop();
  228. while (cancel_ == false) {
  229. if (timedV_.timeout() == false && timedA_.timeout() == false) {
  230. if (fabs(timedV_.Get()) < 1e-2 && fabs(timedA_.Get()) < 1e-3)
  231. return true;
  232. else
  233. monitor_->stop();
  234. printf("Stoped wait V/A ==0(1e-4,1e-4),V:%f,A:%f\n", fabs(timedV_.Get()), fabs(timedA_.Get()));
  235. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  236. } else {
  237. printf("Stop failed V/A timeout\n");
  238. return false;
  239. }
  240. }
  241. }
  242. printf("stoped ret false.\n");
  243. return false;
  244. }
  245. bool Navigation::SlowlyStop() {
  246. double acc = 3.0;
  247. double dt = 0.1;
  248. while (cancel_ == false) {
  249. if (timedV_.timeout() == false) {
  250. double v = timedV_.Get();
  251. if (fabs(v < 1e-2 && fabs(timedA_.Get()) < 1e-3))
  252. return true;
  253. if (v > 0) {
  254. double new_v = v - acc * dt;
  255. new_v = new_v > 0 ? new_v : 0;
  256. SendMoveCmd(move_mode_, Monitor_emqx::ActionType(actionType_), new_v, 0);
  257. } else {
  258. double new_v = v + acc * dt;
  259. new_v = new_v < 0 ? new_v : 0;
  260. SendMoveCmd(move_mode_, Monitor_emqx::ActionType(actionType_), new_v, 0);
  261. }
  262. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  263. } else {
  264. printf("Stop failed V/A timeout\n");
  265. return false;
  266. }
  267. }
  268. printf("stoped ret false.\n");
  269. return false;
  270. }
  271. Navigation::Navigation() {
  272. isInSpace_ = false; //是否在车位或者正在进入车位
  273. RWheel_position_ = eUnknow;
  274. move_mode_ = Monitor_emqx::eSingle;
  275. monitor_ = nullptr;
  276. terminator_ = nullptr;
  277. timedBrotherPose_.reset(Pose2d(-100, 0, 0), 0.5);
  278. }
  279. void Navigation::BrotherAgvStatuCallback(const MqttMsg &msg, void *context) {
  280. Navigation *navigator = (Navigation *) context;
  281. NavMessage::NavStatu brother_statu;
  282. if (msg.toProtoMessage(brother_statu) == false) {
  283. std::cout << " msg transform to AGVStatu failed,msg:" << msg.data() << std::endl;
  284. return;
  285. }
  286. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  287. if (brother_statu.has_odom()) {
  288. NavMessage::LidarOdomStatu odom = brother_statu.odom();
  289. Pose2d pose(odom.x(), odom.y(), odom.theta());
  290. navigator->timedBrotherPose_.reset(pose, 1);
  291. navigator->timedBrotherV_.reset(odom.v(), 1);
  292. navigator->timedBrotherA_.reset(odom.vth(), 1);
  293. navigator->timedBrotherNavStatu_.reset(brother_statu, 0.1);
  294. }
  295. }
  296. bool Navigation::RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect) {
  297. while (cancel_ == false) {
  298. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  299. if (timedPose_.timeout()) {
  300. printf(" RotateBytarget failed pose timeout\n");
  301. return false;
  302. }
  303. if (timedA_.timeout()) {
  304. printf(" RotateBytarget failed wmg timeout\n");
  305. return false;
  306. }
  307. Pose2d targetInPose = Pose2d::relativePose(target, timedPose_.Get());
  308. float yawDiff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  309. float minYawdiff = yawDiff;
  310. if (anyDirect) {
  311. Pose2d p0 = timedPose_.Get();
  312. float yawdiff[4] = {0};
  313. yawdiff[0] = yawDiff;
  314. Pose2d relativePose;
  315. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI / 2));
  316. yawdiff[1] = Pose2d::vector2yaw(relativePose.x(), relativePose.y()); ////使用减法,保证角度在【-pi pi】
  317. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI));
  318. yawdiff[2] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  319. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, 3 * M_PI / 2));
  320. yawdiff[3] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  321. for (int i = 1; i < 4; ++i) {
  322. if (fabs(yawdiff[i]) < fabs(minYawdiff)) {
  323. minYawdiff = yawdiff[i];
  324. targetInPose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, i * M_PI / 2.0));
  325. }
  326. }
  327. }
  328. //std::cout<<" Rotate refer to target:"<<target<<",targetInPose:"
  329. //<<targetInPose<<",anyDirect:"<<anyDirect<<std::endl;
  330. //以当前点为原点,想方向经过目标点的二次曲线曲率>0.25,则需要调整 y=a x*x
  331. float cuv = compute_cuv(targetInPose);
  332. float dt = 0.1;
  333. float acc_angular = 15 * M_PI / 180.0;
  334. if (cuv > 2 * M_PI / 180.0) {
  335. double theta = limit_gause(minYawdiff, limit_rotate.min, limit_rotate.max);
  336. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  337. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  338. SendMoveCmd(move_mode_, Monitor_emqx::eRotate, 0, limit_angular);
  339. actionType_ = eRotation;
  340. printf(" Rotate | input angular:%f,next angular:%f,down:%f diff:%f anyDirect:%d\n",
  341. timedA_.Get(), angular, limit_angular, minYawdiff, anyDirect);
  342. continue;
  343. } else {
  344. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  345. printf(" Rotate refer target completed,cuv:%f\n", cuv);
  346. return true;
  347. }
  348. continue;
  349. }
  350. }
  351. return false;
  352. }
  353. Navigation::MpcResult Navigation::RotateReferToTarget(NavMessage::PathNode node, stLimit limit_rotate, int directions) {
  354. if (inited_ == false) {
  355. printf(" MPC_rotate has not inited\n");
  356. return eMpcFailed;
  357. }
  358. if (PoseTimeout()) {
  359. printf(" MPC_rotate Error:Pose is timeout \n");
  360. return eMpcFailed;
  361. }
  362. printf(" exec MPC_rotate autoDirect:%d\n", directions);
  363. while (cancel_ == false) {
  364. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  365. if (pause_ == true) {
  366. //发送暂停消息
  367. continue;
  368. }
  369. // if (PoseTimeout()) {
  370. // printf(" MPC_rotate Error:Pose is timeout \n");
  371. // return eMpcFailed;
  372. // }
  373. // if (timedA_.timeout()) {
  374. // printf(" MPC_rotate Error:v/a is timeout \n");
  375. // return eMpcFailed;
  376. // }
  377. Pose2d current = timedPose_.Get();
  378. Pose2d target(node.x(), node.y(), node.theta());
  379. Pose2d targetInPose = Pose2d::relativePose(target, current);
  380. // 需要旋转的角度
  381. float target_yaw_diff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  382. // 自由方向
  383. if (directions) {
  384. std::vector<double> yaw_diffs;
  385. if (directions & MpcRotateDirection::eForward){
  386. yaw_diffs.push_back(target_yaw_diff);
  387. }
  388. if (directions & MpcRotateDirection::eBackward){
  389. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, M_PI));
  390. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  391. }
  392. if (directions & MpcRotateDirection::eLeft) {
  393. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 1.0/2*M_PI));
  394. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  395. }
  396. if (directions & MpcRotateDirection::eRight){
  397. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 3.0/2*M_PI));
  398. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  399. }
  400. std::cout << " yaw_diffs[i] = ";
  401. for (auto i = 0; i < yaw_diffs.size(); ++i) {
  402. std::cout << yaw_diffs[i] << " ";
  403. if (fabs(yaw_diffs[i]) < fabs(target_yaw_diff))
  404. target_yaw_diff = yaw_diffs[i];
  405. }
  406. std::cout<< std::endl;
  407. // std::cout << std::endl << " min_diff: " << current_to_target.theta() << std::endl;
  408. }
  409. //一次变速
  410. std::vector<double> out;
  411. bool ret;
  412. TimerRecord::Execute([&, this]() {
  413. ret = Rotation_mpc_once(Pose2d(0,0,target_yaw_diff), limit_rotate, out);
  414. }, "Rotation_mpc_once");
  415. if (ret == false) {
  416. Stop();
  417. return eMpcFailed;
  418. }
  419. //下发速度
  420. if (fabs(target_yaw_diff) > 2 * M_PI / 180.0) {
  421. SendMoveCmd(move_mode_, Monitor_emqx::eRotate, 0, out[0]);
  422. actionType_ = eRotation;
  423. printf(" MPC_rotate | input angular_v:%f, down:%f, diff:%f autoDirect:%d\n",
  424. timedA_.Get(), out[0], target_yaw_diff, directions);
  425. } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  426. printf(" MPC_rotate refer target completed,cuv:%f\n", target_yaw_diff);
  427. return eMpcSuccess;
  428. }
  429. continue;
  430. }
  431. return eMpcFailed;
  432. }
  433. bool Navigation::MoveToTarget(NavMessage::PathNode node, Navigation::stLimit limit_v, Navigation::stLimit limit_w,
  434. bool anyDirect, bool enable_rotate) {
  435. if (IsArrived(node)) {
  436. return true;
  437. }
  438. bool already_in_mpc = false;
  439. while (cancel_ == false) {
  440. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  441. if (PoseTimeout()) {
  442. printf(" navigation Error:Pose is timeout \n");
  443. break;
  444. }
  445. Pose2d current = timedPose_.Get();
  446. if (IsArrived(node)) {
  447. break;
  448. }
  449. //两个方向都无法到达目标点,旋转 朝向目标点
  450. bool x_enable = PossibleToTarget(node, false);
  451. bool y_enable = PossibleToTarget(node, true);
  452. bool enableTotarget = x_enable || y_enable;
  453. if (anyDirect == false) {
  454. enableTotarget = x_enable;
  455. }
  456. //巡线无法到达目标点
  457. if (enableTotarget == false) {
  458. if (already_in_mpc && enable_rotate == false) {
  459. printf(" Move to node failed ,impossible to target and rotate is not enabled\n");
  460. return false;
  461. }
  462. Pose2d target(node.x(), node.y(), 0);
  463. // if (RotateReferToTarget(target, limit_w, anyDirect) == false) {
  464. int direction = MpcRotateDirection::eForward | MpcRotateDirection::eBackward |
  465. MpcRotateDirection::eLeft | MpcRotateDirection::eRight;
  466. if (RotateReferToTarget(node, limit_w, direction) != eMpcSuccess) {
  467. std::cout << " Rotate refer to target failed,target: " << target <<
  468. " direction: " << direction << " line_: " << __LINE__ << std::endl;
  469. return false;
  470. }
  471. continue;
  472. }
  473. already_in_mpc = true;
  474. MpcResult ret = MpcToTarget(node, limit_v, anyDirect);
  475. if (ret == eMpcSuccess) {
  476. printf(" MPC to target:%f %f l:%f,w:%f theta:%f completed\n",
  477. node.x(), node.y(), node.l(), node.w(), node.theta());
  478. break;
  479. } else if (ret == eImpossibleToTarget) {
  480. printf(" MPC to target:%f %f l:%f,w:%f theta:%f impossible ,retry ..............................\n",
  481. node.x(), node.y(), node.l(), node.w(), node.theta());
  482. } else {
  483. return false;
  484. }
  485. }
  486. if (cancel_) {
  487. return false;
  488. }
  489. return true;
  490. }
  491. bool Navigation::execute_nodes(NavMessage::NewAction action) {
  492. if (action.type() == 3 || action.type() == 4) //最优动作导航 or 导航中保证朝向严格一致
  493. {
  494. if (!parameter_.has_nodeangularlimit() || !parameter_.has_nodevelocitylimit() || action.pathnodes_size() == 0) {
  495. std::cout << "execute pathNodes failed ,Action invalid:" << action.DebugString() << std::endl;
  496. return false;
  497. }
  498. bool anyDirect = (action.type() == 3);
  499. //速度限制
  500. stLimit adjust_angular = {parameter_.nodeangularlimit().min(), parameter_.nodeangularlimit().max()};
  501. stLimit mpc_velocity = {parameter_.nodevelocitylimit().min(), parameter_.nodevelocitylimit().max()};
  502. for (int i = 0; i < action.pathnodes_size(); ++i) {
  503. NavMessage::PathNode node = action.pathnodes(i);
  504. if (i + 1 < action.pathnodes_size()) {
  505. NavMessage::PathNode next = action.pathnodes(i + 1);
  506. Pose2d vec(next.x() - node.x(), next.y() - node.y(), 0);
  507. node.set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  508. node.set_l(0.3);
  509. node.set_w(0.1);
  510. } else {
  511. node.set_theta(0);
  512. node.set_w(0.2);
  513. node.set_l(0.2);
  514. }
  515. if (MoveToTarget(node, mpc_velocity, adjust_angular, anyDirect, true) == false)
  516. return false;
  517. else
  518. isInSpace_ = false;
  519. }
  520. return true;
  521. }
  522. printf("execute PathNode failed ,action type is not 3/4 :%d\n", action.type());
  523. return false;
  524. }
  525. bool Navigation::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target) {
  526. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  527. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  528. return false;
  529. }
  530. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  531. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  532. double acc_angular = 20 * M_PI / 180.0;
  533. double dt = 0.1;
  534. Pose2d rotated = timedPose_.Get();
  535. //前车先到,当前车进入2点,保持与前车一致的朝向
  536. if (brother.in_space() && brother.space_id() == space.id()) {
  537. rotated.mutable_theta() = brother.odom().theta();
  538. } else { //当前车先到(后车),倒车入库
  539. rotated.mutable_theta() = space.theta();
  540. rotated = rotated.rotate(rotated.x(), rotated.y(), M_PI);
  541. }
  542. double x = space.x() - wheelbase / 2 * cos(rotated.theta());
  543. double y = space.y() - wheelbase / 2 * sin(rotated.theta());
  544. target.set_x(x);
  545. target.set_y(y);
  546. target.set_theta(rotated.theta());
  547. target.set_l(0.05);
  548. target.set_w(0.03);
  549. target.set_id(space.id());
  550. //整车协调的时候,保持前后与车位一致即可
  551. if (move_mode_ == Monitor_emqx::eMain) {
  552. double yawDiff1 = (rotated - timedPose_.Get()).theta();
  553. double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  554. if (fabs(yawDiff1) < fabs(yawDiff2)) {
  555. rotated = rotated + Pose2d(0, 0, M_PI);
  556. }
  557. }
  558. while (cancel_ == false) {
  559. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  560. Pose2d current = timedPose_.Get();
  561. double yawDiff = (rotated - current).theta();
  562. if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  563. double theta = limit_gause(yawDiff, limit_rotate.min, limit_rotate.max);
  564. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  565. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  566. SendMoveCmd(move_mode_, Monitor_emqx::eRotate, 0, limit_angular);
  567. actionType_ = eRotation;
  568. printf(" RotateBeforeEnterSpace | input anguar:%f,next angular:%f,down:%f diff:%f anyDirect:false\n",
  569. timedA_.Get(), angular, limit_angular, yawDiff);
  570. continue;
  571. } else {
  572. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  573. printf(" RotateBeforeEnterSpace refer target completed\n");
  574. printf("---------------- update new target :%f %f %f \n", target.x(), target.y(), target.theta());
  575. return true;
  576. }
  577. }
  578. }
  579. return false;
  580. }
  581. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  582. if (action.type() != 1 && action.type() != 2) {
  583. printf(" Inout_space failed : msg action type must ==1\n ");
  584. return false;
  585. }
  586. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  587. !action.has_streetnode()) {
  588. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  589. return false;
  590. }
  591. if (PoseTimeout() == true) {
  592. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  593. return false;
  594. }
  595. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  596. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  597. //入库,起点为streetNode
  598. if (action.type() == 1) {
  599. Pose2d vec(action.spacenode().x() - action.streetnode().x(), action.spacenode().y() - action.streetnode().y(),
  600. 0);
  601. action.mutable_streetnode()->set_l(0.2);
  602. action.mutable_streetnode()->set_w(0.05);
  603. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  604. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true) == false) {
  605. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  606. return false;
  607. }
  608. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  609. printf("inout ----------------------------------------\n");
  610. //计算车位朝向
  611. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  612. //计算当前车是进入车位1点还是2点
  613. NavMessage::PathNode new_atrget;
  614. if (RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(), new_atrget) == false) {
  615. return false;
  616. }
  617. if (MoveToTarget(new_atrget, limit_v, limit_w, true, false) == false) {
  618. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  619. return false;
  620. }
  621. } else if (action.type() == 2) {
  622. Pose2d space(action.spacenode().x(), action.spacenode().y(), 0);
  623. Pose2d diff = Pose2d::abs(Pose2d::relativePose(space, timedPose_.Get()));
  624. if (diff.y() < 0.05) {
  625. //出库,移动到马路点,允许自由方向,不允许中途旋转
  626. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  627. action.streetnode().y() - action.spacenode().y(), 0);
  628. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  629. action.mutable_streetnode()->set_l(0.2);
  630. action.mutable_streetnode()->set_w(0.1);
  631. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  632. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  633. return false;
  634. }
  635. } else {
  636. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  637. << std::endl;
  638. return false;
  639. }
  640. }
  641. return true;
  642. }
  643. /* current_to_target: current_to_target.theta()为所需旋转的角度
  644. * */
  645. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  646. bool all_direct) {
  647. if (PoseTimeout() == true) {
  648. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  649. return false;
  650. }
  651. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  652. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  653. return false;
  654. }
  655. Pose2d pose = timedPose_.Get();
  656. double line_velocity = timedV_.Get(); //从plc获取状态
  657. double angular_velocity = timedA_.Get();
  658. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  659. statu[0] = pose.x();
  660. statu[1] = pose.y();
  661. statu[2] = pose.theta();
  662. statu[3] = line_velocity;
  663. statu[4] = angular_velocity;
  664. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  665. double obs_w = 2;//0.85;
  666. double obs_h = 3;//1.45;
  667. MpcError ret = failed;
  668. Pose2d brother = timedBrotherPose_.Get();
  669. Pose2d relative2brother = Pose2d::relativePose(brother, pose);
  670. // std::cout<<" relative2brother: "<<relative2brother<<std::endl;
  671. RotateMPC MPC(relative2brother, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  672. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  673. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  674. ret = MPC.solve(current_to_target, statu, parameter, out);
  675. if (ret == no_solution) {
  676. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  677. out.clear();
  678. out.push_back(0);
  679. }
  680. return ret == success || ret == no_solution;
  681. }
  682. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  683. if (PoseTimeout() == true) {
  684. printf(" MPC once Error:Pose is timeout \n");
  685. return false;
  686. }
  687. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  688. printf(" MPC once Error:V/A is timeout \n");
  689. return false;
  690. }
  691. if (traj.size() == 0) {
  692. printf("traj size ==0\n");
  693. return false;
  694. }
  695. Pose2d pose = timedPose_.Get();
  696. double velocity = timedV_.Get(); //从plc获取状态
  697. double angular = timedA_.Get();
  698. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  699. statu[0] = pose.x();
  700. statu[1] = pose.y();
  701. statu[2] = pose.theta();
  702. statu[3] = velocity;
  703. statu[4] = angular;
  704. Trajectory optimize_trajectory;
  705. Trajectory selected_trajectory;
  706. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  707. double obs_w = 0.85;
  708. double obs_h = 1.45;
  709. if (directY == true) {
  710. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  711. statu[2] += M_PI / 2;
  712. if (statu[2] > M_PI)
  713. statu[2] -= 2 * M_PI;
  714. mpc_parameter = parameter_.y_mpc_parameter();
  715. obs_w = 1.45;
  716. obs_h = 0.85;
  717. }
  718. Pose2d brother = timedBrotherPose_.Get();
  719. Pose2d relative = Pose2d::relativePose(brother, pose);
  720. if (directY)
  721. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  722. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  723. //std::cout<<"pose:"<<pose<<", brother:"<<brother<<", relative:"<<relative<<std::endl;
  724. if (move_mode_ == Monitor_emqx::ActionMode::eMain)
  725. relative = Pose2d(-1e8, -1e8, 0);
  726. MpcError ret = failed;
  727. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  728. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  729. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  730. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  731. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  732. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  733. out, selected_trajectory, optimize_trajectory);
  734. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  735. if (ret == no_solution) {
  736. printf(" mpc solve no solution set v/w = 0\n");
  737. out.clear();
  738. out.push_back(0);
  739. out.push_back(0);
  740. }
  741. predict_traj_.reset(optimize_trajectory, 1);
  742. selected_traj_.reset(selected_trajectory, 1);
  743. return ret == success || ret == no_solution;
  744. }
  745. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  746. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  747. printf(" pose / v / w timeout,IsArrived return false\n");
  748. return false;
  749. }
  750. Pose2d current = timedPose_.Get();
  751. double x = current.x();
  752. double y = current.y();
  753. double tx = targetNode.x();
  754. double ty = targetNode.y();
  755. double l = fabs(targetNode.l());
  756. double w = fabs(targetNode.w());
  757. double theta = -targetNode.theta();
  758. if (l < 1e-10 || w < 1e-10) {
  759. printf("IsArrived: Error target l or w == 0\n");
  760. return false;
  761. }
  762. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  763. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  764. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  765. if (fabs(timedV_.Get()) < 0.06 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  766. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  767. return true;
  768. }
  769. }
  770. return false;
  771. }
  772. void Navigation::SendMoveCmd(Monitor_emqx::ActionMode mode, Monitor_emqx::ActionType type,
  773. double v, double angular) {
  774. if (monitor_) {
  775. monitor_->set_speed(mode, type, v, angular);
  776. if (mode == ActionType::eRotation)
  777. RWheel_position_ = eR;
  778. if (mode == ActionType::eVertical)
  779. RWheel_position_ = eX;
  780. if (mode == ActionType::eHorizon)
  781. RWheel_position_ = eY;
  782. }
  783. }
  784. bool Navigation::clamp_close() {
  785. if (monitor_) {
  786. printf("Clamp closing...\n");
  787. monitor_->clamp_close(move_mode_);
  788. actionType_ = eClampClose;
  789. while (cancel_ == false) {
  790. if (timed_clamp_.timeout()) {
  791. printf("timed clamp is timeout\n");
  792. return false;
  793. }
  794. if (timed_clamp_.Get() == eClosed)
  795. return true;
  796. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  797. monitor_->clamp_close(move_mode_);
  798. actionType_ = eClampClose;
  799. }
  800. return false;
  801. }
  802. return false;
  803. }
  804. bool Navigation::clamp_open() {
  805. if (monitor_) {
  806. printf("Clamp openning...\n");
  807. monitor_->clamp_open(move_mode_);
  808. actionType_ = eClampOpen;
  809. while (cancel_ == false) {
  810. if (timed_clamp_.timeout()) {
  811. printf("timed clamp is timeout\n");
  812. return false;
  813. }
  814. if (timed_clamp_.Get() == eOpened)
  815. return true;
  816. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  817. monitor_->clamp_open(move_mode_);
  818. actionType_ = eClampOpen;
  819. }
  820. return false;
  821. }
  822. return false;
  823. }
  824. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect) {
  825. if (IsArrived(node)) {
  826. printf(" current already in target completed !!!\n");
  827. return eMpcSuccess;
  828. }
  829. if (inited_ == false) {
  830. printf(" navigation has not inited\n");
  831. return eMpcFailed;
  832. }
  833. if (PoseTimeout()) {
  834. printf(" navigation Error:Pose is timeout \n");
  835. return eMpcFailed;
  836. }
  837. Pose2d current = timedPose_.Get();
  838. Pose2d target(node.x(), node.y(), 0);
  839. //生成轨迹
  840. Trajectory traj = Trajectory::create_line_trajectory(current, target);
  841. if (traj.size() == 0)
  842. return eMpcSuccess;
  843. //判断 巡线方向
  844. bool directY = false;
  845. if (autoDirect) {
  846. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  847. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  848. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  849. directY = true;
  850. } else {
  851. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  852. }
  853. }
  854. printf(" exec along autoDirect:%d\n", autoDirect);
  855. while (cancel_ == false) {
  856. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  857. if (pause_ == true) {
  858. //发送暂停消息
  859. continue;
  860. }
  861. if (PoseTimeout()) {
  862. printf(" navigation Error:Pose is timeout \n");
  863. return eMpcFailed;
  864. }
  865. if (timedV_.timeout() || timedA_.timeout()) {
  866. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  867. return eMpcFailed;
  868. }
  869. //判断是否到达终点
  870. if (IsArrived(node)) {
  871. if (Stop()) {
  872. printf(" exec along completed !!!\n");
  873. return eMpcSuccess;
  874. }
  875. }
  876. //一次变速
  877. std::vector<double> out;
  878. bool ret;
  879. TimerRecord::Execute([&, this]() {
  880. ret = mpc_once(traj, limit_v, out, directY);
  881. }, "mpc_once");
  882. if (ret == false) {
  883. Stop();
  884. return eMpcFailed;
  885. }
  886. /*
  887. * AGV 在终点附近低速巡航时,设置最小速度0.05
  888. */
  889. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  890. if (directY) {
  891. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  892. }
  893. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  894. if (out[0] >= 0 && out[0] < limit_v.min)
  895. out[0] = limit_v.min;
  896. if (out[0] < 0 && out[0] > -limit_v.min)
  897. out[0] = -limit_v.min;
  898. }
  899. //下发速度
  900. if (directY == false)
  901. SendMoveCmd(move_mode_, Monitor_emqx::eVertical, out[0], out[1]);
  902. else
  903. SendMoveCmd(move_mode_, Monitor_emqx::eHorizontal, out[0], out[1]);
  904. actionType_ = directY ? eHorizon : eVertical;
  905. /*
  906. * 判断车辆是否在终点附近徘徊,无法到达终点
  907. */
  908. if (PossibleToTarget(node, directY) == false) {
  909. printf(" --------------MPC imposible to target -------------------------------------------\n");
  910. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  911. SlowlyStop();
  912. else
  913. Stop();
  914. return eImpossibleToTarget;
  915. }
  916. }
  917. return eMpcFailed;
  918. }
  919. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  920. if (timedPose_.timeout()) {
  921. return false;
  922. }
  923. Pose2d current = timedPose_.Get();
  924. double tx = targetNode.x();
  925. double ty = targetNode.y();
  926. double l = fabs(targetNode.l());
  927. double w = fabs(targetNode.w());
  928. double theta = -targetNode.theta();
  929. double W = 2.45;
  930. double L = 1.3;
  931. double minYaw = 5 * M_PI / 180.0;
  932. if (move_mode_ == Monitor_emqx::eMain) {
  933. L = 1.3 + 2.7;
  934. }
  935. if (directY) {
  936. current = current.rotate(current.x(), current.y(), M_PI / 2);
  937. double t = W;
  938. W = L;
  939. L = t;
  940. }
  941. double minR = W / 2 + L / tan(minYaw);
  942. //printf("l:%f,w:%f\n",l,w);
  943. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  944. bool nagY = false;
  945. bool posiY = false;
  946. for (int i = 0; i < poses.size(); ++i) {
  947. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  948. Pose2d relative = Pose2d::relativePose(rpos, current);
  949. double ax = fabs(relative.x());
  950. double ay = fabs(relative.y());
  951. if (relative.y() > 0) posiY = true;
  952. if (relative.y() < 0) nagY = true;
  953. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  954. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  955. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  956. minR,ax,ay);*/
  957. return true;
  958. } else if (nagY && posiY) {
  959. return true;
  960. }
  961. printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  962. relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  963. minR, ax, ay);
  964. }
  965. printf(" impossible to target:%f %f l:%f,w:%f theta:%f\n",
  966. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  967. return false;
  968. }
  969. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  970. if (inited_ == false) {
  971. response.set_ret(-1);
  972. response.set_info("navigation has not inited");
  973. printf(" navigation has not inited\n");
  974. return;
  975. }
  976. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  977. {
  978. response.set_ret(-2);
  979. response.set_info("navigation is running pls cancel before");
  980. printf(" navigation is running pls cancel before\n");
  981. return;
  982. }
  983. //add 先检查当前点与起点的距离
  984. global_navCmd_ = cmd;
  985. for (int i = 0; i < cmd.newactions_size(); ++i)
  986. newUnfinished_cations_.push(cmd.newactions(i));
  987. pause_ = false;
  988. cancel_ = false;
  989. running_ = true;
  990. actionType_ = eReady;
  991. printf(" navigation beg...\n");
  992. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  993. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  994. NavMessage::NewAction act = newUnfinished_cations_.front();
  995. if (act.type() == 1) { //入库
  996. space_id_ = act.spacenode().id();
  997. isInSpace_ = true;
  998. if (!execute_InOut_space(act)) {
  999. printf(" In space failed\n");
  1000. break;
  1001. }
  1002. } else if (act.type() == 2) { //出库
  1003. if (!execute_InOut_space(act)) {
  1004. printf(" out space failed\n");
  1005. break;
  1006. } else {
  1007. isInSpace_ = false;
  1008. }
  1009. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1010. if (!execute_nodes(act))
  1011. break;
  1012. } else if (act.type() == 5) {
  1013. printf(" 汽车模型导航....\n");
  1014. } else if (act.type() == 6) {//夹持
  1015. if (this->clamp_close() == false) {
  1016. printf("夹持failed ...\n");
  1017. break;
  1018. }
  1019. } else if (act.type() == 7) {
  1020. if (this->clamp_open() == false) {
  1021. printf("打开夹持 failed...\n");
  1022. break;
  1023. }
  1024. } else if (act.type() == 8) { //切换模式
  1025. SwitchMode(Monitor_emqx::ActionMode(act.changedmode()), act.wheelbase());
  1026. } else {
  1027. printf(" action type invalid not handled !!\n");
  1028. break;
  1029. }
  1030. newUnfinished_cations_.pop();
  1031. }
  1032. actionType_ = eReady;
  1033. Stop();
  1034. if (cancel_ == true) {
  1035. response.set_ret(-3);
  1036. response.set_info("navigation canceled");
  1037. printf(" navigation canceled\n");
  1038. while (newUnfinished_cations_.empty() == false)
  1039. newUnfinished_cations_.pop();
  1040. } else {
  1041. if (newUnfinished_cations_.empty()) {
  1042. response.set_ret(0);
  1043. response.set_info("navigation completed!!!");
  1044. printf("navigation completed!!!\n");
  1045. } else {
  1046. response.set_ret(-4);
  1047. response.set_info("navigation Failed!!!!");
  1048. printf(" navigation Failed\n");
  1049. while (newUnfinished_cations_.empty() == false)
  1050. newUnfinished_cations_.pop();
  1051. }
  1052. }
  1053. running_ = false;
  1054. }
  1055. void Navigation::SwitchMode(Monitor_emqx::ActionMode mode, float wheelBase) {
  1056. printf("Child AGV can not Switch Mode\n");
  1057. }
  1058. float Navigation::compute_cuv(const Pose2d &target) {
  1059. float x = fabs(target.x());
  1060. float y = fabs(target.y());
  1061. float cuv = atan(y / (x + 1e-8));
  1062. //printf(" ---------------------- cuv:%f\n",cuv);
  1063. return cuv;
  1064. }