navigation.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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. if (timedPose_.timeout() == false) {
  52. return false;
  53. } else {
  54. if (timedPose_.timeout()) {
  55. printf(" current pose is timeout \n");
  56. }
  57. // if (timedBrotherPose_.timeout()) {
  58. // printf(" brother pose is timeout \n");
  59. // }
  60. return true;
  61. }
  62. }
  63. bool Navigation::Init(const NavParameter::Navigation_parameter &parameter) {
  64. parameter_ = parameter;
  65. NavParameter::AgvEmqx_parameter agv_p = parameter.agv_emqx();
  66. if (monitor_ == nullptr) {
  67. monitor_ = new Monitor_emqx(agv_p.nodeid());
  68. if (monitor_->Connect(agv_p.ip(), agv_p.port()) == false) {
  69. printf(" agv emqx connected failed\n");
  70. return false;
  71. }
  72. monitor_->set_speedcmd_topic(agv_p.pubspeedtopic());
  73. monitor_->AddCallback(agv_p.subposetopic(), Navigation::RobotPoseCallback, this);
  74. monitor_->AddCallback(agv_p.subspeedtopic(), Navigation::RobotSpeedCallback, this);
  75. }
  76. NavParameter::Emqx_parameter terminal_p = parameter.terminal_emqx();
  77. if (terminator_ == nullptr) {
  78. terminator_ = new Terminator_emqx(terminal_p.nodeid());
  79. if (terminator_->Connect(terminal_p.ip(), terminal_p.port()) == false) {
  80. printf(" terminator emqx connected failed\n");
  81. return false;
  82. }
  83. }
  84. if (parameter.has_brother_emqx()) {
  85. NavParameter::BrotherEmqx brotherEmqx = parameter.brother_emqx();
  86. if (brotherEmqx_ == nullptr) {
  87. brotherEmqx_ = new Terminator_emqx(brotherEmqx.nodeid());
  88. if (brotherEmqx_->Connect(brotherEmqx.ip(), brotherEmqx.port()) == true) {
  89. brotherEmqx_->AddCallback(brotherEmqx.subbrotherstatutopic(), Navigation::BrotherAgvStatuCallback,
  90. this);
  91. } else {
  92. printf(" brother emqx connected failed\n");
  93. // return false;
  94. }
  95. //
  96. }
  97. }
  98. inited_ = true;
  99. if (pubthread_ != nullptr) {
  100. exit_ = true;
  101. if (pubthread_->joinable())
  102. pubthread_->join();
  103. delete pubthread_;
  104. }
  105. exit_ = false;
  106. pubthread_ = new std::thread(&Navigation::pubStatuThreadFuc, this);
  107. return true;
  108. }
  109. void Navigation::RobotPoseCallback(const MqttMsg &msg, void *context) {
  110. Navigation *navigator = (Navigation *) context;
  111. NavMessage::LidarOdomStatu statu;
  112. if (msg.toProtoMessage(statu)) {
  113. navigator->ResetPose(Pose2d(statu.x(), statu.y(), statu.theta()));
  114. }
  115. }
  116. void Navigation::HandleAgvStatu(const MqttMsg &msg) {
  117. NavMessage::AgvStatu speed;
  118. if (msg.toProtoMessage(speed)) {
  119. ResetStatu(speed.v(), speed.w());
  120. ResetClamp((ClampStatu) speed.clamp());
  121. ResetLifter((LifterStatus) speed.lifter());
  122. }
  123. }
  124. void Navigation::RobotSpeedCallback(const MqttMsg &msg, void *context) {
  125. Navigation *navigator = (Navigation *) context;
  126. navigator->HandleAgvStatu(msg);
  127. }
  128. void Navigation::pubStatuThreadFuc(void *p) {
  129. Navigation *navogator = (Navigation *) p;
  130. if (navogator) {
  131. while (navogator->exit_ == false) {
  132. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  133. NavMessage::NavStatu statu;
  134. statu.set_main_agv(false);
  135. navogator->publish_statu(statu);
  136. }
  137. }
  138. }
  139. void Navigation::publish_statu(NavMessage::NavStatu &statu) {
  140. if (timedPose_.timeout() == false) {
  141. NavMessage::LidarOdomStatu odom;
  142. odom.set_x(timedPose_.Get().x());
  143. odom.set_y(timedPose_.Get().y());
  144. odom.set_theta(timedPose_.Get().theta());
  145. if (timedV_.timeout() == false)
  146. odom.set_v(timedV_.Get());
  147. if (timedA_.timeout() == false)
  148. odom.set_vth(timedA_.Get());
  149. statu.mutable_odom()->CopyFrom(odom);
  150. }
  151. statu.set_in_space(isInSpace_);
  152. if (isInSpace_) statu.set_space_id(space_id_);
  153. //发布nav状态
  154. statu.set_statu(actionType_); //
  155. statu.set_move_mode(move_mode_);
  156. if (running_) {
  157. statu.set_key(global_navCmd_.key());
  158. }
  159. //发布MPC信息
  160. //发布mpc 预选点
  161. if (selected_traj_.timeout() == false) {
  162. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  163. Pose2d pt = selected_traj_.Get()[i];
  164. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  165. pose->set_x(pt.x());
  166. pose->set_y(pt.y());
  167. pose->set_theta(pt.theta());
  168. }
  169. }
  170. //发布 mpc 预测点
  171. if (predict_traj_.timeout() == false) {
  172. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  173. Pose2d pt = predict_traj_.Get()[i];
  174. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  175. pose->set_x(pt.x());
  176. pose->set_y(pt.y());
  177. pose->set_theta(pt.theta());
  178. }
  179. }
  180. // std::cout<<"nav statu:"<<statu.DebugString()<<std::endl;
  181. MqttMsg msg;
  182. msg.fromProtoMessage(statu);
  183. if (terminator_)
  184. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(), msg);
  185. //发布位姿 ------robot状态--------------------------
  186. NavMessage::RobotStatu robot;
  187. if (CreateRobotStatuMsg(robot)) {
  188. if (terminator_) {
  189. MqttMsg msg;
  190. msg.fromProtoMessage(robot);
  191. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  192. }
  193. }
  194. }
  195. bool Navigation::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  196. //printf(" %d %d %d \n",timedPose_.timeout(),timedV_.timeout(),timedA_.timeout());
  197. if (timedPose_.timeout() == false) {
  198. Pose2d pose = timedPose_.Get();
  199. robotStatu.set_x(pose.x());
  200. robotStatu.set_y(pose.y());
  201. robotStatu.set_theta(pose.theta());
  202. if ((timedV_.timeout() == false && timedA_.timeout() == false)) {
  203. NavMessage::AgvStatu plc;
  204. plc.set_v(timedV_.Get());
  205. plc.set_w(timedA_.Get());
  206. plc.set_clamp(timed_clamp_.Get());
  207. plc.set_lifter(timed_lifter_.Get());
  208. //printf(" timed_clamp_:%d\n ",timed_clamp_.Get());
  209. robotStatu.mutable_agvstatu()->CopyFrom(plc);
  210. }
  211. return true;
  212. }
  213. return false;
  214. }
  215. void Navigation::ResetStatu(double v, double a) {
  216. timedV_.reset(v, 0.5);
  217. timedA_.reset(a, 0.5);
  218. }
  219. void Navigation::ResetClamp(ClampStatu statu) {
  220. timed_clamp_.reset(statu, 1);
  221. }
  222. void Navigation::ResetLifter(LifterStatus status) {
  223. timed_lifter_.reset(status, 1);
  224. }
  225. void Navigation::ResetPose(const Pose2d &pose) {
  226. timedPose_.reset(pose, 1.0);
  227. }
  228. void Navigation::Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  229. cancel_ = true;
  230. response.set_ret(0);
  231. }
  232. bool Navigation::Stop() {
  233. if (monitor_) {
  234. monitor_->stop();
  235. while (cancel_ == false) {
  236. if (timedV_.timeout() == false && timedA_.timeout() == false) {
  237. if (fabs(timedV_.Get()) < 1e-2 && fabs(timedA_.Get()) < 1e-3)
  238. return true;
  239. else
  240. monitor_->stop();
  241. printf("Stoped wait V/A ==0(1e-4,1e-4),V:%f,A:%f\n", fabs(timedV_.Get()), fabs(timedA_.Get()));
  242. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  243. } else {
  244. printf("Stop failed V/A timeout\n");
  245. return false;
  246. }
  247. }
  248. }
  249. printf("stoped ret false.\n");
  250. return false;
  251. }
  252. bool Navigation::SlowlyStop() {
  253. double acc = 3.0;
  254. double dt = 0.1;
  255. while (cancel_ == false) {
  256. if (timedV_.timeout() == false) {
  257. double v = timedV_.Get();
  258. if (fabs(v < 1e-2 && fabs(timedA_.Get()) < 1e-3))
  259. return true;
  260. if (v > 0) {
  261. double new_v = v - acc * dt;
  262. new_v = new_v > 0 ? new_v : 0;
  263. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  264. } else {
  265. double new_v = v + acc * dt;
  266. new_v = new_v < 0 ? new_v : 0;
  267. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  268. }
  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. printf("stoped ret false.\n");
  276. return false;
  277. }
  278. Navigation::Navigation() {
  279. isInSpace_ = false; //是否在车位或者正在进入车位
  280. RWheel_position_ = eUnknow;
  281. move_mode_ = eSingle;
  282. monitor_ = nullptr;
  283. terminator_ = nullptr;
  284. timedBrotherPose_.reset(Pose2d(-100, 0, 0), 0.5);
  285. }
  286. void Navigation::BrotherAgvStatuCallback(const MqttMsg &msg, void *context) {
  287. Navigation *navigator = (Navigation *) context;
  288. NavMessage::NavStatu brother_statu;
  289. if (msg.toProtoMessage(brother_statu) == false) {
  290. std::cout << " msg transform to AGVStatu failed,msg:" << msg.data() << std::endl;
  291. return;
  292. }
  293. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  294. if (brother_statu.has_odom()) {
  295. NavMessage::LidarOdomStatu odom = brother_statu.odom();
  296. Pose2d pose(odom.x(), odom.y(), odom.theta());
  297. navigator->timedBrotherPose_.reset(pose, 1);
  298. navigator->timedBrotherV_.reset(odom.v(), 1);
  299. navigator->timedBrotherA_.reset(odom.vth(), 1);
  300. navigator->timedBrotherNavStatu_.reset(brother_statu, 0.1);
  301. }
  302. }
  303. //未使用
  304. bool Navigation::RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect) {
  305. while (cancel_ == false) {
  306. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  307. if (timedPose_.timeout()) {
  308. printf(" RotateBytarget failed pose timeout\n");
  309. return false;
  310. }
  311. if (timedA_.timeout()) {
  312. printf(" RotateBytarget failed wmg timeout\n");
  313. return false;
  314. }
  315. Pose2d targetInPose = Pose2d::relativePose(target, timedPose_.Get());
  316. float yawDiff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  317. float minYawdiff = yawDiff;
  318. if (anyDirect) {
  319. Pose2d p0 = timedPose_.Get();
  320. float yawdiff[4] = {0};
  321. yawdiff[0] = yawDiff;
  322. Pose2d relativePose;
  323. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI / 2));
  324. yawdiff[1] = Pose2d::vector2yaw(relativePose.x(), relativePose.y()); ////使用减法,保证角度在【-pi pi】
  325. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI));
  326. yawdiff[2] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  327. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, 3 * M_PI / 2));
  328. yawdiff[3] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  329. for (int i = 1; i < 4; ++i) {
  330. if (fabs(yawdiff[i]) < fabs(minYawdiff)) {
  331. minYawdiff = yawdiff[i];
  332. targetInPose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, i * M_PI / 2.0));
  333. }
  334. }
  335. }
  336. //std::cout<<" Rotate refer to target:"<<target<<",targetInPose:"
  337. //<<targetInPose<<",anyDirect:"<<anyDirect<<std::endl;
  338. //以当前点为原点,想方向经过目标点的二次曲线曲率>0.25,则需要调整 y=a x*x
  339. float cuv = compute_cuv(targetInPose);
  340. float dt = 0.1;
  341. float acc_angular = 15 * M_PI / 180.0;
  342. if (cuv > 2 * M_PI / 180.0) {
  343. double theta = limit_gause(minYawdiff, limit_rotate.min, limit_rotate.max);
  344. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  345. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  346. SendMoveCmd(move_mode_, eRotation, 0, limit_angular);
  347. actionType_ = eRotation;
  348. printf(" Rotate | input angular:%f,next angular:%f,down:%f diff:%f anyDirect:%d\n",
  349. timedA_.Get(), angular, limit_angular, minYawdiff, anyDirect);
  350. continue;
  351. } else {
  352. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  353. printf(" Rotate refer target completed,cuv:%f\n", cuv);
  354. return true;
  355. }
  356. continue;
  357. }
  358. }
  359. return false;
  360. }
  361. Navigation::MpcResult Navigation::RotateReferToTarget(NavMessage::PathNode node, stLimit limit_rotate, int directions) {
  362. if (inited_ == false) {
  363. printf(" MPC_rotate has not inited\n");
  364. return eMpcFailed;
  365. }
  366. if (PoseTimeout()) {
  367. printf(" MPC_rotate Error:Pose is timeout \n");
  368. return eMpcFailed;
  369. }
  370. printf(" exec MPC_rotate autoDirect:%d\n", directions);
  371. while (cancel_ == false) {
  372. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  373. if (pause_ == true) {
  374. //发送暂停消息
  375. continue;
  376. }
  377. if (PoseTimeout()) {
  378. printf(" MPC_rotate Error:Pose is timeout \n");
  379. return eMpcFailed;
  380. }
  381. if (timedA_.timeout()) {
  382. printf(" MPC_rotate Error:v/a is timeout \n");
  383. return eMpcFailed;
  384. }
  385. Pose2d current = timedPose_.Get();
  386. Pose2d target(node.x(), node.y(), node.theta());
  387. Pose2d targetInPose = Pose2d::relativePose(target, current);
  388. // 需要旋转的角度
  389. float target_yaw_diff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  390. // 自由方向
  391. if (directions) {
  392. std::vector<double> yaw_diffs;
  393. if (directions & Direction::eForward) {
  394. yaw_diffs.push_back(target_yaw_diff);
  395. }
  396. if (directions & Direction::eBackward) {
  397. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, M_PI));
  398. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  399. }
  400. if (directions & Direction::eLeft) {
  401. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 1.0 / 2 * M_PI));
  402. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  403. }
  404. if (directions & Direction::eRight) {
  405. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 3.0 / 2 * M_PI));
  406. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  407. }
  408. for (auto i = 0; i < yaw_diffs.size(); ++i) {
  409. if (fabs(yaw_diffs[i]) < fabs(target_yaw_diff))
  410. target_yaw_diff = yaw_diffs[i];
  411. }
  412. // std::cout << std::endl;
  413. // std::cout << std::endl << " min_diff: " << current_to_target.theta() << std::endl;
  414. }
  415. //一次变速
  416. std::vector<double> out;
  417. bool ret;
  418. TimerRecord::Execute([&, this]() {
  419. ret = Rotation_mpc_once(Pose2d(0, 0, target_yaw_diff), limit_rotate, out);
  420. }, "Rotation_mpc_once");
  421. if (ret == false) {
  422. Stop();
  423. return eMpcFailed;
  424. }
  425. //下发速度
  426. if (fabs(target_yaw_diff) < 0.2 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  427. printf(" MPC_rotate | refer target completed\\n,cuv:%f\n", target_yaw_diff);
  428. Stop();
  429. return eMpcSuccess;
  430. } else{
  431. const int down_count = 3;
  432. double v[down_count] = {0,0,0};
  433. double w[down_count] = {out[0], out[1], out[2]};
  434. // if(out[0]>=0){
  435. // w[0]=1.*M_PI/180.0;
  436. // w[1]=1.*M_PI/180.0;
  437. // w[2]=1.*M_PI/180.0;
  438. // }else{
  439. // w[0]=-1.*M_PI/180.0;
  440. // w[1]=-1.*M_PI/180.0;
  441. // w[2]=-1.*M_PI/180.0;
  442. // }
  443. SendMoveCmd(move_mode_, eRotation, v, w);
  444. actionType_ = eRotation;
  445. double input_w = timedA_.Get();
  446. for (int i = 0; i < down_count; ++i) {
  447. printf(" MPC_rotate |P[%d], input anguar:%f, down:%f(%f), diff:%f anyDirect:%d\n",
  448. i, input_w, w[i], w[i]/M_PI*180, target_yaw_diff, directions);
  449. }
  450. }
  451. continue;
  452. // //下发速度
  453. // if (fabs(target_yaw_diff) > 2 * M_PI / 180.0) {
  454. // SendMoveCmd(move_mode_, eRotation, 0, out[0]);
  455. // actionType_ = eRotation;
  456. // printf(" MPC_rotate | input angular_v:%f, down:%f, diff:%f autoDirect:%d\n",
  457. // timedA_.Get(), out[0], target_yaw_diff, directions);
  458. // } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  459. // printf(" MPC_rotate refer target completed,cuv:%f\n", target_yaw_diff);
  460. // return eMpcSuccess;
  461. // }
  462. // continue;
  463. }
  464. return eMpcFailed;
  465. }
  466. bool Navigation::MoveToTarget(NavMessage::PathNode node, Navigation::stLimit limit_v, Navigation::stLimit limit_w,
  467. bool anyDirect, bool enable_rotate) {
  468. // LogWriter("MoveToTarget beg");
  469. if (IsArrived(node)) {
  470. return true;
  471. }
  472. bool already_in_mpc = false;
  473. while (cancel_ == false) {
  474. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  475. if (PoseTimeout()) {
  476. printf(" navigation Error:Pose is timeout \n");
  477. break;
  478. }
  479. Pose2d current = timedPose_.Get();
  480. if (IsArrived(node)) {
  481. break;
  482. }
  483. //两个方向都无法到达目标点,旋转 朝向目标点
  484. bool x_enable = PossibleToTarget(node, false);
  485. bool y_enable = PossibleToTarget(node, true);
  486. bool enableTotarget = x_enable || y_enable;
  487. if (anyDirect == false) {
  488. enableTotarget = x_enable;
  489. }
  490. //巡线无法到达目标点
  491. if (enableTotarget == false) {
  492. if (already_in_mpc && enable_rotate == false) {
  493. printf(" Move to node failed ,impossible to target and rotate is not enabled\n");
  494. return false;
  495. }
  496. Pose2d target(node.x(), node.y(), 0);
  497. // if (RotateReferToTarget(target, limit_w, anyDirect) == false) {
  498. int directions = Direction::eForward;
  499. if (anyDirect)
  500. directions = Direction::eForward | Direction::eBackward |
  501. Direction::eLeft | Direction::eRight;
  502. if (RotateReferToTarget(node, limit_w, directions) != eMpcSuccess) {
  503. std::cout << " Rotate refer to target failed,target: " << target <<
  504. " directions: " << std::hex << directions << " line_: " << __LINE__ << std::endl;
  505. return false;
  506. }
  507. continue;
  508. }
  509. already_in_mpc = true;
  510. MpcResult ret = MpcToTarget(node, limit_v, anyDirect);
  511. if (ret == eMpcSuccess) {
  512. printf(" MPC to target:%f %f l:%f,w:%f theta:%f completed\n",
  513. node.x(), node.y(), node.l(), node.w(), node.theta());
  514. break;
  515. } else if (ret == eImpossibleToTarget) {
  516. printf(" MPC to target:%f %f l:%f,w:%f theta:%f impossible ,retry ...\n",
  517. node.x(), node.y(), node.l(), node.w(), node.theta());
  518. } else {
  519. return false;
  520. }
  521. }
  522. if (cancel_) {
  523. return false;
  524. }
  525. return true;
  526. }
  527. bool Navigation::execute_nodes(NavMessage::NewAction action) {
  528. if (action.type() == 3 || action.type() == 4) //最优动作导航 or 导航中保证朝向严格一致
  529. {
  530. if (!parameter_.has_nodeangularlimit() || !parameter_.has_nodevelocitylimit() || action.pathnodes_size() == 0) {
  531. std::cout << "execute pathNodes failed ,Action invalid:" << action.DebugString() << std::endl;
  532. return false;
  533. }
  534. bool anyDirect = (action.type() == 3);
  535. //速度限制
  536. stLimit wmg_limit = {parameter_.nodeangularlimit().min(), parameter_.nodeangularlimit().max()};
  537. stLimit mpc_velocity = {parameter_.nodevelocitylimit().min(), parameter_.nodevelocitylimit().max()};
  538. for (int i = 0; i < action.pathnodes_size(); ++i) {
  539. NavMessage::PathNode node = action.pathnodes(i);
  540. if (i + 1 < action.pathnodes_size()) {
  541. NavMessage::PathNode next = action.pathnodes(i + 1);
  542. Pose2d vec(next.x() - node.x(), next.y() - node.y(), 0);
  543. node.set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  544. node.set_l(0.05);
  545. node.set_w(0.10);
  546. } else {
  547. node.set_theta(0);
  548. node.set_l(0.03);
  549. node.set_w(0.10);
  550. }
  551. int directions = Direction::eForward;
  552. if (anyDirect)
  553. directions = Direction::eForward | Direction::eBackward |
  554. Direction::eLeft | Direction::eRight;
  555. if (i > 0){
  556. if (RotateReferToTarget(node, wmg_limit, directions) != eMpcSuccess){
  557. printf(" before move node RotateReferToNone Failed!!\n");
  558. return false;
  559. }
  560. }
  561. if (MoveToTarget(node, mpc_velocity, wmg_limit, anyDirect, true) == false)
  562. return false;
  563. else
  564. isInSpace_ = false;
  565. }
  566. return true;
  567. }
  568. printf("execute PathNode failed ,action type is not 3/4 :%d\n", action.type());
  569. return false;
  570. }
  571. Navigation::MpcResult Navigation::RotateAfterOutSpace(){
  572. if(move_mode_!=eDouble)
  573. return eMpcSuccess;
  574. Pose2d init = timedPose_.Get();
  575. double diff1=fabs(M_PI/2.0-init.theta());
  576. double diff2=fabs(-M_PI/2.0-init.theta());
  577. double target= diff1<diff2?M_PI/2.0:-M_PI/2.0;
  578. stLimit limit_rotate = {3 * M_PI / 180.0, 15 * M_PI / 180.0};
  579. while (cancel_ == false) {
  580. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  581. Pose2d current = timedPose_.Get();
  582. double yawDiff = (target - current.theta());
  583. //一次变速
  584. std::vector<double> out;
  585. bool ret;
  586. TimerRecord::Execute([&, this]() {
  587. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  588. }, "Rotation_mpc_once");
  589. if (ret == false) {
  590. Stop();
  591. return eMpcFailed;
  592. }
  593. //下发速度
  594. if (fabs(yawDiff) < 0.3 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  595. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  596. Stop();
  597. return eMpcSuccess;
  598. } else{
  599. const int down_count = 3;
  600. double v[down_count] = {0,0,0};
  601. double w[down_count] = {out[0], out[1], out[2]};
  602. SendMoveCmd(move_mode_, eRotation, v, w);
  603. actionType_ = eRotation;
  604. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  605. timedA_.Get(), out[0], out[0]/M_PI*180, yawDiff);
  606. }
  607. continue;
  608. }
  609. return eMpcFailed;
  610. }
  611. Navigation::MpcResult
  612. Navigation::RotateBeforeEnterSpace(NavMessage::PathNode space,
  613. double wheelbase, NavMessage::PathNode &target) {
  614. // if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  615. if (timedPose_.timeout()) {
  616. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  617. return eMpcFailed;
  618. }
  619. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  620. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  621. double acc_angular = 20 * M_PI / 180.0;
  622. double dt = 0.1;
  623. Pose2d rotated = timedPose_.Get();
  624. double x = space.x();
  625. double y = space.y();
  626. //前车先到,后车进入2点,保持与前车一致的朝向
  627. if (brother.in_space() && brother.space_id() == space.id()) {
  628. printf("RotateBeforeEnterSpace | 前车先到, __LINE__ = %d\n", __LINE__);
  629. // rotated.mutable_theta() = brother.odom().theta();
  630. Pose2d vec(x - rotated.x(), y - rotated.y(), 0);
  631. rotated.mutable_theta() = Pose2d::vector2yaw(vec.x(), vec.y());
  632. if (move_mode_ == eSingle) {
  633. x -= wheelbase * cos(rotated.theta());
  634. y -= wheelbase * sin(rotated.theta());
  635. printf("确定车位点:eSingle\n");
  636. }
  637. } else { //当后车先到,倒车入库
  638. printf("RotateBeforeEnterSpace | 后车先到, __LINE__ = %d\n", __LINE__);
  639. rotated.mutable_theta() = space.theta();
  640. rotated = rotated.rotate(rotated.x(), rotated.y(), M_PI);
  641. }
  642. target.set_x(x);
  643. target.set_y(y);
  644. target.set_theta(rotated.theta());
  645. target.set_l(0.02);
  646. target.set_w(0.05);
  647. target.set_id(space.id());
  648. printf("RotateBeforeEnterSpace | target:[x:%f,y:%f,theta:%f]\n",x,y,target.theta());
  649. // //整车协调的时候,保持前后与车位一致即可
  650. // if (move_mode_ == eDouble) {
  651. // double yawDiff1 = (rotated - timedPose_.Get()).theta();
  652. // double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  653. // if (fabs(yawDiff1) < fabs(yawDiff2)) {
  654. // rotated = rotated + Pose2d(0, 0, M_PI);
  655. // }
  656. // }
  657. while (cancel_ == false) {
  658. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  659. Pose2d current = timedPose_.Get();
  660. double yawDiff = (rotated - current).theta();
  661. //一次变速
  662. std::vector<double> out;
  663. bool ret;
  664. TimerRecord::Execute([&, this]() {
  665. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  666. }, "Rotation_mpc_once");
  667. if (ret == false) {
  668. Stop();
  669. return eMpcFailed;
  670. }
  671. //下发速度
  672. if (fabs(yawDiff) < 0.3 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  673. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  674. Stop();
  675. return eMpcSuccess;
  676. } else{
  677. const int down_count = 3;
  678. double v[down_count] = {0,0,0};
  679. double w[down_count] = {out[0], out[1], out[2]};
  680. SendMoveCmd(move_mode_, eRotation, v, w);
  681. actionType_ = eRotation;
  682. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  683. timedA_.Get(), out[0], out[0]/M_PI*180, yawDiff);
  684. }
  685. continue;
  686. }
  687. return eMpcFailed;
  688. }
  689. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  690. // LogWriter("execute_InOut_space beg");
  691. if (action.type() != 1 && action.type() != 2) {
  692. printf(" Inout_space failed : msg action type must ==1\n ");
  693. return false;
  694. }
  695. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  696. !action.has_streetnode()) {
  697. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  698. return false;
  699. }
  700. if (PoseTimeout() == true) {
  701. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  702. return false;
  703. }
  704. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  705. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  706. //入库,起点为streetNode
  707. if (action.type() == 1) {
  708. // LogWriter("In space beg-");
  709. Pose2d vec(action.spacenode().x() - action.streetnode().x(), action.spacenode().y() - action.streetnode().y(),
  710. 0);
  711. action.mutable_streetnode()->set_l(0.05);
  712. action.mutable_streetnode()->set_w(0.05);
  713. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  714. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true) == false) {
  715. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  716. return false;
  717. }
  718. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  719. printf("inout ----------------------------------------\n");
  720. //计算车位朝向
  721. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  722. //计算当前车是进入车位1点还是2点
  723. NavMessage::PathNode new_target;
  724. if (RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(), new_target) == eMpcFailed) {
  725. return false;
  726. }
  727. std::string id = action.spacenode().id().substr(1,action.spacenode().id().length()-1);
  728. int space_id = stoi(id);
  729. if (space_id>99 && space_id< 1000){
  730. //进载车板库前提升机构上升
  731. if (lifter_rise() == false){
  732. printf(" Enter space | lifter rise failed. line:%d\n",__LINE__);
  733. }
  734. }
  735. else{
  736. //否则下降
  737. if (lifter_down() == false){
  738. printf(" Enter space | lifter down failed. line:%d\n",__LINE__);
  739. }
  740. }
  741. if (space_id == 1101){
  742. //单车进入口搬车,夹持杆打开到全开位
  743. }
  744. //
  745. // //移动到预入库点位,较高精度
  746. // NavMessage::PathNode pre_target;
  747. // bool jump_preenter = false;
  748. // if (move_mode_ == eSingle){
  749. // double x = action.spacenode().x();
  750. // double y = action.spacenode().y();
  751. // double theta = action.spacenode().theta();
  752. //
  753. // double back_distance = 3.3 + 0.2 + 1;//距载车板靠墙侧车轮挡板距离(距离车位点距离)
  754. // Pose2d current = timedPose_.Get();
  755. // double diff = Pose2d::distance(current, Pose2d(x,y,theta));
  756. // if (diff < back_distance) jump_preenter = true;
  757. // x -= back_distance* cos(theta);
  758. // y -= back_distance* sin(theta);
  759. //
  760. // pre_target.set_x(x);
  761. // pre_target.set_y(y);
  762. // pre_target.set_theta(theta);
  763. // pre_target.set_l(0.10);
  764. // pre_target.set_w(0.05);
  765. // pre_target.set_id("R_0");
  766. // }
  767. // else if (move_mode_ == eDouble){
  768. // double x = action.spacenode().x();
  769. // double y = action.spacenode().y();
  770. // double theta = action.spacenode().theta();
  771. //
  772. // double back_distance = 3.3 + 0.2 + 1 + action.wheelbase()/2;//距载车板靠墙侧车轮挡板距离(距离车位点距离)
  773. // Pose2d current = timedPose_.Get();
  774. // double diff = Pose2d::distance(current, Pose2d(x,y,theta));
  775. // if (diff < back_distance) jump_preenter = true;
  776. // x -= back_distance* cos(theta);
  777. // y -= back_distance* sin(theta);
  778. //
  779. // pre_target.set_x(x);
  780. // pre_target.set_y(y);
  781. // pre_target.set_theta(theta);
  782. // pre_target.set_l(0.05);
  783. // pre_target.set_w(0.03);
  784. // pre_target.set_id("R_0");
  785. // }
  786. // else{ return printf("PreEnter space failed. move_mode_ error!. line: %d\n", __LINE__);}
  787. // if (!jump_preenter) {
  788. // printf("PreEnter space beg...\npre_target: x:%f, y:%f, theta:%f\n",pre_target.x(),pre_target.y(),pre_target.theta());
  789. // if (MoveToTarget(pre_target, limit_v, limit_w, true, true) == false) {
  790. // printf(" PreEnter pre_space:%s failed. type:%d. line:%d\n", pre_target.id().data(), action.type(),
  791. // __LINE__);
  792. // return false;
  793. // }
  794. // }
  795. //入库
  796. printf("Enter space beg...\n");
  797. if (MoveToTarget(new_target, limit_v, limit_w, true, false) == false) {
  798. printf(" Enter space:%s failed. type:%d\n",action.spacenode().id().data(), action.type());
  799. return false;
  800. }
  801. } else if (action.type() == 2) {
  802. // LogWriter("Out space beg-");
  803. // 计算当前位置,在车位点到马路点的连线方向上,与车位点的相对位姿
  804. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  805. action.streetnode().y() - action.spacenode().y(), 0);
  806. float theta = Pose2d::vector2yaw(vec.x(), vec.y());
  807. Pose2d space(action.spacenode().x(), action.spacenode().y(), theta);
  808. Pose2d current = timedPose_.Get();
  809. Pose2d diff = Pose2d::abs(Pose2d::relativePose(Pose2d(current.x(),current.y(),0),space));
  810. if (diff.x() < 0.05 || diff.y() < 0.10) {
  811. //出库,移动到马路点,允许自由方向,不允许中途旋转
  812. action.mutable_streetnode()->set_theta(theta);
  813. action.mutable_streetnode()->set_l(0.04);
  814. action.mutable_streetnode()->set_w(0.06);
  815. std::cout << " Out space target street node:" << space << std::endl;
  816. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  817. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  818. return false;
  819. }
  820. //出库后提升机构下降
  821. if (lifter_down() == false){
  822. printf(" Out space | lifter down failed. line:%d\n",__LINE__);
  823. }
  824. // if(RotateAfterOutSpace()!=eMpcSuccess) {
  825. // printf(" after out space rotate Failed!!!\n");
  826. // return false;
  827. // }
  828. // LogWriter("Out space end-");
  829. } else {
  830. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  831. << std::endl;
  832. return false;
  833. }
  834. }
  835. return true;
  836. }
  837. /* current_to_target: current_to_target.theta()为所需旋转的角度
  838. * */
  839. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  840. bool all_direct) {
  841. if (PoseTimeout() == true) {
  842. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  843. return false;
  844. }
  845. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  846. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  847. return false;
  848. }
  849. Pose2d pose = timedPose_.Get();
  850. double line_velocity = timedV_.Get(); //从plc获取状态
  851. double angular_velocity = timedA_.Get();
  852. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  853. statu[0] = pose.x();
  854. statu[1] = pose.y();
  855. statu[2] = pose.theta();
  856. statu[3] = line_velocity;
  857. statu[4] = angular_velocity;
  858. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  859. double obs_w = 0.95;//0.85;
  860. double obs_h = 1.55;//1.45;
  861. MpcError ret = failed;
  862. Pose2d brother = timedBrotherPose_.Get();
  863. Pose2d brother_in_self = Pose2d::relativePose(brother, pose);
  864. // std::cout<<" brother_in_self: "<<brother_in_self<<std::endl;
  865. if (move_mode_ == eDouble) {
  866. brother_in_self = Pose2d(pose.x() + 100, pose.y() + 100, 0);
  867. }
  868. RotateMPC MPC(brother_in_self, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  869. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  870. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  871. ret = MPC.solve(current_to_target, statu, parameter, out);
  872. if (ret == no_solution) {
  873. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  874. out.clear();
  875. out.push_back(0);
  876. out.push_back(0);
  877. out.push_back(0);
  878. } else {
  879. // double min_angular_velocity = 1.0 / 180 * M_PI;
  880. double min_angular_velocity = 0.01;
  881. const int down_count = 3;
  882. for (int i = 0; i < down_count; ++i) {
  883. if (fabs(out[i]) < min_angular_velocity) {
  884. if (out[i] > 0)
  885. out[i] = min_angular_velocity;
  886. else if (out[i] < 0)
  887. out[i] = -min_angular_velocity;
  888. else {}
  889. }
  890. }
  891. }
  892. return ret == success || ret == no_solution;
  893. }
  894. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  895. if (PoseTimeout() == true) {
  896. printf(" MPC once Error:Pose is timeout \n");
  897. return false;
  898. }
  899. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  900. printf(" MPC once Error:V/A is timeout \n");
  901. return false;
  902. }
  903. if (traj.size() == 0) {
  904. printf("traj size ==0\n");
  905. return false;
  906. }
  907. Pose2d pose = timedPose_.Get();
  908. double velocity = timedV_.Get(); //从plc获取状态
  909. double angular = timedA_.Get();
  910. // float diffYaw1=timedPose_.Get().m_diffYaw1;
  911. // float diffYaw2=timedPose_.Get().m_diffYaw2;
  912. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  913. statu[0] = pose.x();
  914. statu[1] = pose.y();
  915. statu[2] = pose.theta();
  916. statu[3] = velocity;
  917. statu[4] = angular;
  918. Trajectory optimize_trajectory;
  919. Trajectory selected_trajectory;
  920. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  921. double obs_w =1.5;// 0.95;//0.85;
  922. double obs_h =3.0;// 1.55;//1.45;
  923. if (directY == true) {
  924. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  925. statu[2] += M_PI / 2;
  926. if (statu[2] > M_PI)
  927. statu[2] -= 2 * M_PI;
  928. mpc_parameter = parameter_.y_mpc_parameter();
  929. obs_w = 3.0;// 0.95;//0.85;
  930. obs_h = 1.5;//1.55;//1.45;
  931. }
  932. Pose2d brother = timedBrotherPose_.Get();
  933. Pose2d relative = Pose2d::relativePose(brother, pose);
  934. if (directY)
  935. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  936. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  937. std::cout<<"pose:"<<pose<<", brother:"<<brother<<", relative:"<<relative<<std::endl;
  938. if (move_mode_ == eDouble)
  939. relative = Pose2d(-1e8, -1e8, 0);
  940. MpcError ret = failed;
  941. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  942. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  943. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  944. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  945. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  946. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  947. out, selected_trajectory, optimize_trajectory,directY);
  948. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  949. if (ret == no_solution) {
  950. printf(" mpc solve no solution set v/w = 0\n");
  951. out.clear();
  952. out.push_back(0);
  953. out.push_back(0);
  954. out.push_back(0);
  955. out.push_back(0);
  956. out.push_back(0);
  957. out.push_back(0);
  958. }
  959. //
  960. // diff_yaw.push_back(0);
  961. // diff_yaw.push_back(0);
  962. predict_traj_.reset(optimize_trajectory, 1);
  963. selected_traj_.reset(selected_trajectory, 1);
  964. return ret == success || ret == no_solution;
  965. }
  966. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  967. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  968. printf(" pose / v / w timeout,IsArrived return false\n");
  969. return false;
  970. }
  971. Pose2d current = timedPose_.Get();
  972. double x = current.x();
  973. double y = current.y();
  974. double tx = targetNode.x();
  975. double ty = targetNode.y();
  976. double l = fabs(targetNode.l());
  977. double w = fabs(targetNode.w());
  978. double theta = -targetNode.theta();
  979. if (newUnfinished_cations_.front().type() == 1 && targetNode.id().find('S')!=-1){//入库只判断前后方向上是否到达
  980. tx = x;
  981. }
  982. if (l < 1e-10 || w < 1e-10) {
  983. printf("IsArrived: Error target l or w == 0\n");
  984. return false;
  985. }
  986. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  987. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  988. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  989. if (fabs(timedV_.Get()) < 0.06 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  990. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  991. return true;
  992. }
  993. }
  994. return false;
  995. }
  996. void Navigation::SendMoveCmd(int mode, ActionType type,
  997. double v, double angular) {
  998. if (monitor_) {
  999. monitor_->set_speed(mode, type, v, angular);
  1000. if (type == eRotation)
  1001. RWheel_position_ = eR;
  1002. if (type == eVertical)
  1003. RWheel_position_ = eX;
  1004. if (type == eHorizontal)
  1005. RWheel_position_ = eY;
  1006. }
  1007. }
  1008. void Navigation::SendMoveCmd(int mode, ActionType type,
  1009. double v[], double angular[]) {
  1010. if (monitor_) {
  1011. monitor_->set_ToAgvCmd(mode, type, v, angular);
  1012. if (type == eRotation)
  1013. RWheel_position_ = eR;
  1014. if (type == eVertical)
  1015. RWheel_position_ = eX;
  1016. if (type == eHorizontal)
  1017. RWheel_position_ = eY;
  1018. }
  1019. }
  1020. void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[], int space_id, double distance) {
  1021. if (monitor_) {
  1022. monitor_->set_ToAgvCmd(mode, type, v, angular, 0, space_id, distance);
  1023. if (type == eRotation)
  1024. RWheel_position_ = eR;
  1025. if (type == eVertical)
  1026. RWheel_position_ = eX;
  1027. if (type == eHorizontal)
  1028. RWheel_position_ = eY;
  1029. }
  1030. }
  1031. //void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[], double yaw_diff[] ,int space_id, double distance) {
  1032. // if (monitor_) {
  1033. // monitor_->set_ToAgvCmd(mode, type, v, angular, yaw_diff ,0,space_id, distance);
  1034. // if (type == eRotation)
  1035. // RWheel_position_ = eR;
  1036. // if (type == eVertical)
  1037. // RWheel_position_ = eX;
  1038. // if (type == eHorizontal)
  1039. // RWheel_position_ = eY;
  1040. // }
  1041. //}
  1042. bool Navigation::clamp_close() {
  1043. if (monitor_) {
  1044. printf("Clamp closing...\n");
  1045. monitor_->clamp_close(move_mode_);
  1046. actionType_ = eClampClose;
  1047. while (cancel_ == false) {
  1048. if (timed_clamp_.timeout()) {
  1049. printf("timed clamp is timeout\n");
  1050. return false;
  1051. }
  1052. if (timed_clamp_.Get() == eClosed)
  1053. return true;
  1054. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1055. monitor_->clamp_close(move_mode_);
  1056. actionType_ = eClampClose;
  1057. }
  1058. return false;
  1059. }
  1060. return false;
  1061. }
  1062. bool Navigation::clamp_open() {
  1063. if (monitor_) {
  1064. printf("Clamp openning...\n");
  1065. monitor_->clamp_open(move_mode_);
  1066. actionType_ = eClampOpen;
  1067. while (cancel_ == false) {
  1068. if (timed_clamp_.timeout()) {
  1069. printf("timed clamp is timeout\n");
  1070. return false;
  1071. }
  1072. if (timed_clamp_.Get() == eOpened)
  1073. return true;
  1074. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1075. monitor_->clamp_open(move_mode_);
  1076. actionType_ = eClampOpen;
  1077. }
  1078. return false;
  1079. }
  1080. return false;
  1081. }
  1082. bool Navigation::clamp_fully_open(){
  1083. if (monitor_){
  1084. printf("Clamp fully openning...\n");
  1085. monitor_->clamp_fully_open(move_mode_);
  1086. actionType_ = eClampFullyOpen;
  1087. while (cancel_ == false) {
  1088. if (timed_clamp_.timeout()) {
  1089. printf("timed clamp is timeout\n");
  1090. return false;
  1091. }
  1092. if (timed_clamp_.Get() == eFullyOpened)
  1093. return true;
  1094. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1095. monitor_->clamp_fully_open(move_mode_);
  1096. actionType_ = eClampFullyOpen;
  1097. }
  1098. return false;
  1099. }
  1100. return false;
  1101. }
  1102. bool Navigation::lifter_rise() {
  1103. if (monitor_) {
  1104. printf("Lifter upping...\n");
  1105. monitor_->lifter_rise(move_mode_);
  1106. actionType_ = eLifterRise;
  1107. while (cancel_ == false) {
  1108. if (timed_lifter_.timeout()) {
  1109. printf("timed lifter is timeout\n");
  1110. return false;
  1111. }
  1112. if (timed_lifter_.Get() == eRose)
  1113. return true;
  1114. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1115. monitor_->lifter_rise(move_mode_);
  1116. actionType_ = eLifterRise;
  1117. }
  1118. return false;
  1119. }
  1120. return false;
  1121. }
  1122. bool Navigation::lifter_down() {
  1123. if (monitor_) {
  1124. printf("Lifter downing...\n");
  1125. monitor_->lifter_down(move_mode_);
  1126. actionType_ = eLifterDown;
  1127. while (cancel_ == false) {
  1128. if (timed_lifter_.timeout()) {
  1129. printf("timed lifter is timeout\n");
  1130. return false;
  1131. }
  1132. if (timed_lifter_.Get() == eDowned)
  1133. return true;
  1134. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1135. monitor_->lifter_down(move_mode_);
  1136. actionType_ = eLifterDown;
  1137. }
  1138. return false;
  1139. }
  1140. return false;
  1141. }
  1142. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect) {
  1143. // LogWriter("MpcToTarget beg");
  1144. if (IsArrived(node)) {
  1145. printf(" current already in target completed !!!\n");
  1146. return eMpcSuccess;
  1147. }
  1148. if (inited_ == false) {
  1149. printf(" navigation has not inited\n");
  1150. return eMpcFailed;
  1151. }
  1152. if (PoseTimeout()) {
  1153. printf(" navigation Error:Pose is timeout \n");
  1154. return eMpcFailed;
  1155. }
  1156. Pose2d current = timedPose_.Get();
  1157. Pose2d target(node.x(), node.y(), 0);
  1158. //生成轨迹
  1159. Trajectory traj = Trajectory::create_line_trajectory(current, target, 0.1);
  1160. if (traj.size() == 0)
  1161. return eMpcSuccess;
  1162. //判断 巡线方向
  1163. bool directY = false;
  1164. if (autoDirect) {
  1165. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  1166. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  1167. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  1168. directY = true;
  1169. } else {
  1170. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  1171. }
  1172. }
  1173. printf(" exec along autoDirect:%d\n", autoDirect);
  1174. //文件log
  1175. std::ofstream ofs; //创建流对象
  1176. std::string log_file_dir = "../data/";
  1177. time_t t = time(0);
  1178. char tmp[32]={NULL};
  1179. strftime(tmp, sizeof(tmp), "%Y_%m_%d_%H_%M_%S.txt",localtime(&t));
  1180. std::string log_file_name = tmp;
  1181. ofs.open(log_file_dir+log_file_name,std::ios::app); //打开的地址和方式
  1182. auto beg_time = std::chrono::steady_clock::now();
  1183. while (cancel_ == false) {
  1184. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  1185. if (pause_ == true) {
  1186. //发送暂停消息
  1187. continue;
  1188. }
  1189. if (PoseTimeout()) {
  1190. printf(" navigation Error:Pose is timeout \n");
  1191. ofs.close();//关闭文件
  1192. return eMpcFailed;
  1193. }
  1194. if (timedV_.timeout() || timedA_.timeout()) {
  1195. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  1196. ofs.close();//关闭文件
  1197. return eMpcFailed;
  1198. }
  1199. //判断是否到达终点
  1200. if (IsArrived(node)) {
  1201. if (Stop()) {
  1202. printf(" exec along completed !!!\n");
  1203. ofs.close();//关闭文件
  1204. return eMpcSuccess;
  1205. }
  1206. }
  1207. //一次变速
  1208. std::vector<double> out;
  1209. // std::vector<double> diff_yaw; //两车分别与整车的相对角度
  1210. bool ret;
  1211. TimerRecord::Execute([&, this]() {
  1212. ret = mpc_once(traj, limit_v, out, directY);
  1213. }, "mpc_once");
  1214. if (ret == false) {
  1215. Stop();
  1216. ofs.close();//关闭文件
  1217. return eMpcFailed;
  1218. }
  1219. const int down_count = 3;//下发前多少步
  1220. double down_v[down_count] ={out[0], out[2], out[4]};//下发线速度
  1221. double down_w[down_count] ={out[1], out[3], out[5]};//下发角速度
  1222. /*
  1223. * AGV 在终点附近低速巡航时,设置最小速度0.05
  1224. */
  1225. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  1226. if (directY) {
  1227. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  1228. }
  1229. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  1230. for (int i = 0; i < down_count; ++i) {
  1231. if (down_v[i] >= 0 && down_v[i] < limit_v.min)
  1232. down_v[i] = limit_v.min;
  1233. if (down_v[i] < 0 && down_v[i] > -limit_v.min)
  1234. down_v[i] = -limit_v.min;
  1235. }
  1236. }
  1237. //放大角速度
  1238. // out[1] *= 10;
  1239. //入库,行进方向上最后一米不纠偏
  1240. if (newUnfinished_cations_.front().type() == 1 && node.id().find('S')!=-1){//入库
  1241. if(fabs(target_in_agv.x()) < 0.5){
  1242. printf("target_in_agv: %f", target_in_agv.x());
  1243. for (double & i : down_w) {
  1244. i = 0.0;
  1245. }
  1246. }
  1247. }else if (newUnfinished_cations_.front().type() == 2){//出库,行进方向上开始一米不纠偏
  1248. Pose2d agv_in_space =
  1249. Pose2d::relativePose(
  1250. Pose2d(newUnfinished_cations_.front().spacenode().x(), newUnfinished_cations_.front().spacenode().y(), 0),
  1251. timedPose_.Get());
  1252. if(fabs(agv_in_space.x()) < 0.3){
  1253. printf("agv_in_space: %f", agv_in_space.x());
  1254. for (double & i : down_w) {
  1255. i = 0.0;
  1256. }
  1257. }
  1258. }
  1259. //下发速度
  1260. //printf(" nav input :%f out:%f\n",timedV_.Get(),out[0]);
  1261. //添加车位号,距目标点距离信息
  1262. double distance = Pose2d::distance(target_in_agv, Pose2d(0,0,0));
  1263. int space_id = 0;
  1264. if(node.id().find('S') != -1){
  1265. std::string id = node.id().substr(1,node.id().length()-1);
  1266. space_id = stoi(id);
  1267. }
  1268. // double diff_yaw_[2] = {diff_yaw[0], diff_yaw[1]};
  1269. if (directY == false)
  1270. SendMoveCmd(move_mode_, eVertical, down_v, down_w, space_id, distance);
  1271. else
  1272. SendMoveCmd(move_mode_, eHorizontal, down_v, down_w, space_id, distance);
  1273. actionType_ = directY ? eHorizontal : eVertical;
  1274. //日志打印
  1275. std::string log_inf_line = std::to_string(move_mode_);
  1276. log_inf_line += " ";
  1277. log_inf_line += std::to_string(parameter_.x_mpc_parameter().shortest_radius())+" "+std::to_string(parameter_.x_mpc_parameter().acc_angular());
  1278. log_inf_line += " ";
  1279. Pose2d cur_pose = timedPose_.Get();//自身x,y,theta
  1280. log_inf_line += std::to_string(cur_pose.x()) + " " + std::to_string(cur_pose.y()) + " " + std::to_string(cur_pose.theta());
  1281. log_inf_line += " ";
  1282. Pose2d cur_Bro_pose = timedBrotherPose_.Get();//另一节x,y,theta
  1283. log_inf_line += std::to_string(cur_Bro_pose.x()) + " " + std::to_string(cur_Bro_pose.y()) + " " + std::to_string(cur_Bro_pose.theta());
  1284. log_inf_line += " ";
  1285. log_inf_line += std::to_string(out[0]) + " " + std::to_string(out[1]);//下发速度
  1286. log_inf_line += " ";
  1287. log_inf_line += std::to_string(out[2]) + " " + std::to_string(out[3]);//下发速度
  1288. log_inf_line += " ";
  1289. log_inf_line += std::to_string(out[4]) + " " + std::to_string(out[5]);//下发速度
  1290. log_inf_line += " ";
  1291. auto cur_time = std::chrono::steady_clock::now();
  1292. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(cur_time - beg_time);
  1293. double time =
  1294. double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  1295. log_inf_line += std::to_string(time);
  1296. ofs << log_inf_line << std::endl; //一行日志的内容
  1297. /*
  1298. * 判断车辆是否在终点附近徘徊,无法到达终点
  1299. */
  1300. if (PossibleToTarget(node, directY) == false) {
  1301. printf(" --------------MPC imposible to target -------------------------------------------\n");
  1302. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  1303. SlowlyStop();
  1304. else
  1305. Stop();
  1306. ofs.close();//关闭文件
  1307. return eImpossibleToTarget;
  1308. }
  1309. }
  1310. ofs.close();//关闭文件
  1311. return eMpcFailed;
  1312. }
  1313. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  1314. if (timedPose_.timeout()) {
  1315. return false;
  1316. }
  1317. Pose2d current = timedPose_.Get();
  1318. if(targetNode.id().find('S') != -1){
  1319. //车位点强制可到达
  1320. return true;
  1321. }
  1322. double tx = targetNode.x();
  1323. double ty = targetNode.y();
  1324. double l = fabs(targetNode.l());
  1325. double w = fabs(targetNode.w());
  1326. double theta = -targetNode.theta();
  1327. // double W = 2.45;
  1328. // double L = 1.3;
  1329. // double minYaw = 3 * M_PI / 180.0;
  1330. // if (move_mode_ == eDouble) {
  1331. // L = 1.3 + 2.7;
  1332. //
  1333. // }
  1334. double W = 2.5;
  1335. double L = 1.565;
  1336. double minYaw = 5 * M_PI / 180.0;
  1337. if (move_mode_ == eDouble) {
  1338. L = 1.565 + 2.78;
  1339. }
  1340. if (directY) {
  1341. current = current.rotate(current.x(), current.y(), M_PI / 2);
  1342. double t = W;
  1343. W = L;
  1344. L = t;
  1345. }
  1346. double minR = W / 2 + L / tan(minYaw);
  1347. //printf("l:%f,w:%f\n",l,w);
  1348. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  1349. bool nagY = false;
  1350. bool posiY = false;
  1351. for (int i = 0; i < poses.size(); ++i) {
  1352. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  1353. Pose2d relative = Pose2d::relativePose(rpos, current);
  1354. double ax = fabs(relative.x());
  1355. double ay = fabs(relative.y());
  1356. if (relative.y() > 0) posiY = true;
  1357. if (relative.y() < 0) nagY = true;
  1358. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  1359. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  1360. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  1361. minR,ax,ay);*/
  1362. return true;
  1363. } else if (nagY && posiY) {
  1364. // std::cout << "nagY && posiY" << std::endl;
  1365. return true;
  1366. }
  1367. // printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  1368. // relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  1369. // minR, ax, ay);
  1370. }
  1371. printf(" impossible to target:%f %f l:%f,w:%f theta:%f, ",
  1372. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  1373. printf(" current:%f, %f, %f\n",
  1374. current.x(), current.y(),current.theta());
  1375. return false;
  1376. }
  1377. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  1378. if (inited_ == false) {
  1379. response.set_ret(-1);
  1380. response.set_info("navigation has not inited");
  1381. printf(" navigation has not inited\n");
  1382. return;
  1383. }
  1384. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  1385. {
  1386. response.set_ret(-2);
  1387. response.set_info("navigation is running pls cancel before");
  1388. printf(" navigation is running pls cancel before\n");
  1389. return;
  1390. }
  1391. //add 先检查当前点与起点的距离
  1392. global_navCmd_ = cmd;
  1393. for (int i = 0; i < cmd.newactions_size(); ++i)
  1394. newUnfinished_cations_.push(cmd.newactions(i));
  1395. pause_ = false;
  1396. cancel_ = false;
  1397. running_ = true;
  1398. actionType_ = eReady;
  1399. printf("Navigation beg...\n");
  1400. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  1401. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  1402. NavMessage::NewAction act = newUnfinished_cations_.front();
  1403. if (act.type() == 1) { //入库
  1404. space_id_ = act.spacenode().id();
  1405. isInSpace_ = true;
  1406. if (!execute_InOut_space(act)) {
  1407. printf(" In space failed\n");
  1408. isInSpace_ = false;
  1409. break;
  1410. }
  1411. } else if (act.type() == 2) { //出库
  1412. if (!execute_InOut_space(act)) {
  1413. printf(" out space failed\n");
  1414. break;
  1415. } else {
  1416. isInSpace_ = false;
  1417. }
  1418. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1419. if (!execute_nodes(act)){
  1420. printf(" street nav failed\n");
  1421. break;
  1422. }
  1423. } else if (act.type() == 5) {
  1424. printf(" 汽车模型导航....\n");
  1425. } else if (act.type() == 6) {//夹持
  1426. if (this->clamp_close() == false) {
  1427. printf("夹持failed ...\n");
  1428. break;
  1429. }
  1430. } else if (act.type() == 7) {
  1431. if (this->clamp_open() == false) {
  1432. printf("打开夹持 failed...\n");
  1433. break;
  1434. }
  1435. } else if (act.type() == 8) { //切换模式
  1436. SwitchMode(act.changedmode(), act.wheelbase());
  1437. } else if (act.type() == 9) { //提升机构提升
  1438. if (this->lifter_rise() == false) {
  1439. printf("提升failed ...\n");
  1440. break;
  1441. }
  1442. } else if (act.type() == 10) { //提升机构下降
  1443. if (this->lifter_down() == false) {
  1444. printf("下降failed ...\n");
  1445. break;
  1446. }
  1447. }else {
  1448. printf(" action type invalid not handled !!\n");break;
  1449. }
  1450. newUnfinished_cations_.pop();
  1451. }
  1452. actionType_ = eReady;
  1453. Stop();
  1454. if (cancel_ == true) {
  1455. response.set_ret(-3);
  1456. response.set_info("navigation canceled");
  1457. printf(" navigation canceled\n");
  1458. while (newUnfinished_cations_.empty() == false)
  1459. newUnfinished_cations_.pop();
  1460. } else {
  1461. if (newUnfinished_cations_.empty()) {
  1462. response.set_ret(0);
  1463. response.set_info("navigation completed!!!");
  1464. printf("navigation completed!!!\n");
  1465. } else {
  1466. response.set_ret(-4);
  1467. response.set_info("navigation Failed!!!!");
  1468. printf(" navigation Failed\n");
  1469. while (newUnfinished_cations_.empty() == false)
  1470. newUnfinished_cations_.pop();
  1471. }
  1472. }
  1473. running_ = false;
  1474. }
  1475. void Navigation::SwitchMode(int mode, float wheelBase) {
  1476. printf("Child AGV can not Switch Mode\n");
  1477. }
  1478. float Navigation::compute_cuv(const Pose2d &target) {
  1479. float x = fabs(target.x());
  1480. float y = fabs(target.y());
  1481. float cuv = atan(y / (x + 1e-8));
  1482. //printf(" ---------------------- cuv:%f\n",cuv);
  1483. return cuv;
  1484. }
  1485. void Navigation::ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response) {
  1486. return;
  1487. // pause_ = false;
  1488. // cancel_ = false;
  1489. // running_ = true;
  1490. // actionType_ = eReady;
  1491. // printf("ManualOperation: %d | start...\n", cmd.operation_type());
  1492. //
  1493. // float symbol = cmd.velocity() < 0 ? -1 : 1; // 判断为正方向or负方向
  1494. // double velocity;
  1495. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1496. // switch (cmd.operation_type()) {
  1497. // case 1: // 旋转
  1498. // velocity = 2.0 * M_PI / 180.0 * symbol;
  1499. // SendMoveCmd(move_mode_, eRotation, 0, velocity);
  1500. // actionType_ = eRotation;
  1501. // printf(" ManualOperation: %d | input angular_v: %f, down: %f\n",
  1502. // cmd.operation_type(), timedA_.Get(), velocity);
  1503. // break;
  1504. // case 2: // X平移
  1505. // velocity = 1.0 * symbol;
  1506. // SendMoveCmd(move_mode_, eVertical, velocity, 0);
  1507. // actionType_ = eVertical;
  1508. // printf(" ManualOperation: %d | input line_v : %f, down: %f\n",
  1509. // cmd.operation_type(), timedV_.Get(), velocity);
  1510. // break;
  1511. // case 3: // Y平移
  1512. // velocity = 1.0 * symbol;
  1513. // SendMoveCmd(move_mode_, eHorizontal, velocity, 0);
  1514. // actionType_ = eHorizontal;
  1515. // printf(" ManualOperation: %d | input line_v: %f, down: %f\n",
  1516. // cmd.operation_type(), timedV_.Get(), velocity);
  1517. // break;
  1518. // default:
  1519. // break;
  1520. // }
  1521. //
  1522. // actionType_ = eReady;
  1523. // response.set_ret(-3);
  1524. // response.set_info("ManualOperation: %d, canceled!!!", cmd.operation_type());
  1525. // printf("ManualOperation: %d | canceled!!!\n", cmd.operation_type());
  1526. // running_ = false;
  1527. }