navigation.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. if (move_mode_ == eSingle) {
  631. x -= wheelbase * cos(rotated.theta());
  632. y -= wheelbase * sin(rotated.theta());
  633. printf("确定车位点:eSingle\n");
  634. }
  635. } else { //当后车先到,倒车入库
  636. printf("RotateBeforeEnterSpace | 后车先到, __LINE__ = %d\n", __LINE__);
  637. rotated.mutable_theta() = space.theta();
  638. rotated = rotated.rotate(rotated.x(), rotated.y(), M_PI);
  639. }
  640. target.set_x(x);
  641. target.set_y(y);
  642. target.set_theta(rotated.theta());
  643. target.set_l(0.02);
  644. target.set_w(0.05);
  645. target.set_id(space.id());
  646. printf("RotateBeforeEnterSpace | target:[x:%f,y:%f,theta:%f]\n",x,y,target.theta());
  647. // //整车协调的时候,保持前后与车位一致即可
  648. // if (move_mode_ == eDouble) {
  649. // double yawDiff1 = (rotated - timedPose_.Get()).theta();
  650. // double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  651. // if (fabs(yawDiff1) < fabs(yawDiff2)) {
  652. // rotated = rotated + Pose2d(0, 0, M_PI);
  653. // }
  654. // }
  655. while (cancel_ == false) {
  656. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  657. Pose2d current = timedPose_.Get();
  658. double yawDiff = (rotated - current).theta();
  659. //一次变速
  660. std::vector<double> out;
  661. bool ret;
  662. TimerRecord::Execute([&, this]() {
  663. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  664. }, "Rotation_mpc_once");
  665. if (ret == false) {
  666. Stop();
  667. return eMpcFailed;
  668. }
  669. //下发速度
  670. if (fabs(yawDiff) < 0.2 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  671. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  672. Stop();
  673. return eMpcSuccess;
  674. } else{
  675. const int down_count = 3;
  676. double v[down_count] = {0,0,0};
  677. double w[down_count] = {out[0], out[1], out[2]};
  678. SendMoveCmd(move_mode_, eRotation, v, w);
  679. actionType_ = eRotation;
  680. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  681. timedA_.Get(), out[0], out[0]/M_PI*180, yawDiff);
  682. }
  683. continue;
  684. }
  685. return eMpcFailed;
  686. }
  687. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  688. // LogWriter("execute_InOut_space beg");
  689. if (action.type() != 1 && action.type() != 2) {
  690. printf(" Inout_space failed : msg action type must ==1\n ");
  691. return false;
  692. }
  693. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  694. !action.has_streetnode()) {
  695. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  696. return false;
  697. }
  698. if (PoseTimeout() == true) {
  699. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  700. return false;
  701. }
  702. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  703. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  704. //入库,起点为streetNode
  705. if (action.type() == 1) {
  706. // LogWriter("In space beg-");
  707. Pose2d vec(action.spacenode().x() - action.streetnode().x(), action.spacenode().y() - action.streetnode().y(),
  708. 0);
  709. action.mutable_streetnode()->set_l(0.05);
  710. action.mutable_streetnode()->set_w(0.05);
  711. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  712. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true) == false) {
  713. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  714. return false;
  715. }
  716. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  717. printf("inout ----------------------------------------\n");
  718. //计算车位朝向
  719. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  720. //计算当前车是进入车位1点还是2点
  721. NavMessage::PathNode new_target;
  722. if (RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(), new_target) == eMpcFailed) {
  723. return false;
  724. }
  725. std::string id = action.spacenode().id().substr(1,action.spacenode().id().length()-1);
  726. int space_id = stoi(id);
  727. if (space_id>99 && space_id< 1000){
  728. //进载车板库前提升机构上升
  729. if (lifter_rise() == false){
  730. printf(" Enter space | lifter rise failed. line:%d\n",__LINE__);
  731. }
  732. }
  733. else{
  734. //否则下降
  735. if (lifter_down() == false){
  736. printf(" Enter space | lifter down failed. line:%d\n",__LINE__);
  737. }
  738. }
  739. //
  740. // //移动到预入库点位,较高精度
  741. // NavMessage::PathNode pre_target;
  742. // bool jump_preenter = false;
  743. // if (move_mode_ == eSingle){
  744. // double x = action.spacenode().x();
  745. // double y = action.spacenode().y();
  746. // double theta = action.spacenode().theta();
  747. //
  748. // double back_distance = 3.3 + 0.2 + 1;//距载车板靠墙侧车轮挡板距离(距离车位点距离)
  749. // Pose2d current = timedPose_.Get();
  750. // double diff = Pose2d::distance(current, Pose2d(x,y,theta));
  751. // if (diff < back_distance) jump_preenter = true;
  752. // x -= back_distance* cos(theta);
  753. // y -= back_distance* sin(theta);
  754. //
  755. // pre_target.set_x(x);
  756. // pre_target.set_y(y);
  757. // pre_target.set_theta(theta);
  758. // pre_target.set_l(0.10);
  759. // pre_target.set_w(0.05);
  760. // pre_target.set_id("R_0");
  761. // }
  762. // else if (move_mode_ == eDouble){
  763. // double x = action.spacenode().x();
  764. // double y = action.spacenode().y();
  765. // double theta = action.spacenode().theta();
  766. //
  767. // double back_distance = 3.3 + 0.2 + 1 + action.wheelbase()/2;//距载车板靠墙侧车轮挡板距离(距离车位点距离)
  768. // Pose2d current = timedPose_.Get();
  769. // double diff = Pose2d::distance(current, Pose2d(x,y,theta));
  770. // if (diff < back_distance) jump_preenter = true;
  771. // x -= back_distance* cos(theta);
  772. // y -= back_distance* sin(theta);
  773. //
  774. // pre_target.set_x(x);
  775. // pre_target.set_y(y);
  776. // pre_target.set_theta(theta);
  777. // pre_target.set_l(0.05);
  778. // pre_target.set_w(0.03);
  779. // pre_target.set_id("R_0");
  780. // }
  781. // else{ return printf("PreEnter space failed. move_mode_ error!. line: %d\n", __LINE__);}
  782. // if (!jump_preenter) {
  783. // printf("PreEnter space beg...\npre_target: x:%f, y:%f, theta:%f\n",pre_target.x(),pre_target.y(),pre_target.theta());
  784. // if (MoveToTarget(pre_target, limit_v, limit_w, true, true) == false) {
  785. // printf(" PreEnter pre_space:%s failed. type:%d. line:%d\n", pre_target.id().data(), action.type(),
  786. // __LINE__);
  787. // return false;
  788. // }
  789. // }
  790. //入库
  791. printf("Enter space beg...\n");
  792. if (MoveToTarget(new_target, limit_v, limit_w, true, false) == false) {
  793. printf(" Enter space:%s failed. type:%d\n",action.spacenode().id().data(), action.type());
  794. return false;
  795. }
  796. } else if (action.type() == 2) {
  797. // LogWriter("Out space beg-");
  798. // 计算当前位置,在车位点到马路点的连线方向上,与车位点的相对位姿
  799. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  800. action.streetnode().y() - action.spacenode().y(), 0);
  801. float theta = Pose2d::vector2yaw(vec.x(), vec.y());
  802. Pose2d space(action.spacenode().x(), action.spacenode().y(), theta);
  803. Pose2d current = timedPose_.Get();
  804. Pose2d diff = Pose2d::abs(Pose2d::relativePose(Pose2d(current.x(),current.y(),0),space));
  805. if (diff.x() < 0.05 || diff.y() < 0.10) {
  806. //出库,移动到马路点,允许自由方向,不允许中途旋转
  807. action.mutable_streetnode()->set_theta(theta);
  808. action.mutable_streetnode()->set_l(0.04);
  809. action.mutable_streetnode()->set_w(0.06);
  810. std::cout << " Out space target street node:" << space << std::endl;
  811. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  812. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  813. return false;
  814. }
  815. //出库后提升机构下降
  816. if (lifter_down() == false){
  817. printf(" Out space | lifter down failed. line:%d\n",__LINE__);
  818. }
  819. // if(RotateAfterOutSpace()!=eMpcSuccess) {
  820. // printf(" after out space rotate Failed!!!\n");
  821. // return false;
  822. // }
  823. // LogWriter("Out space end-");
  824. } else {
  825. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  826. << std::endl;
  827. return false;
  828. }
  829. }
  830. return true;
  831. }
  832. /* current_to_target: current_to_target.theta()为所需旋转的角度
  833. * */
  834. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  835. bool all_direct) {
  836. if (PoseTimeout() == true) {
  837. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  838. return false;
  839. }
  840. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  841. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  842. return false;
  843. }
  844. Pose2d pose = timedPose_.Get();
  845. double line_velocity = timedV_.Get(); //从plc获取状态
  846. double angular_velocity = timedA_.Get();
  847. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  848. statu[0] = pose.x();
  849. statu[1] = pose.y();
  850. statu[2] = pose.theta();
  851. statu[3] = line_velocity;
  852. statu[4] = angular_velocity;
  853. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  854. double obs_w = 0.95;//0.85;
  855. double obs_h = 1.55;//1.45;
  856. MpcError ret = failed;
  857. Pose2d brother = timedBrotherPose_.Get();
  858. Pose2d brother_in_self = Pose2d::relativePose(brother, pose);
  859. // std::cout<<" brother_in_self: "<<brother_in_self<<std::endl;
  860. if (move_mode_ == eDouble) {
  861. brother_in_self = Pose2d(pose.x() + 100, pose.y() + 100, 0);
  862. }
  863. RotateMPC MPC(brother_in_self, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  864. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  865. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  866. ret = MPC.solve(current_to_target, statu, parameter, out);
  867. if (ret == no_solution) {
  868. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  869. out.clear();
  870. out.push_back(0);
  871. out.push_back(0);
  872. out.push_back(0);
  873. } else {
  874. // double min_angular_velocity = 1.0 / 180 * M_PI;
  875. double min_angular_velocity = 0.00001;
  876. const int down_count = 3;
  877. for (int i = 0; i < down_count; ++i) {
  878. if (fabs(out[i]) < min_angular_velocity) {
  879. if (out[i] > 0)
  880. out[i] = min_angular_velocity;
  881. else if (out[i] < 0)
  882. out[i] = -min_angular_velocity;
  883. else {}
  884. }
  885. }
  886. }
  887. return ret == success || ret == no_solution;
  888. }
  889. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  890. if (PoseTimeout() == true) {
  891. printf(" MPC once Error:Pose is timeout \n");
  892. return false;
  893. }
  894. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  895. printf(" MPC once Error:V/A is timeout \n");
  896. return false;
  897. }
  898. if (traj.size() == 0) {
  899. printf("traj size ==0\n");
  900. return false;
  901. }
  902. Pose2d pose = timedPose_.Get();
  903. double velocity = timedV_.Get(); //从plc获取状态
  904. double angular = timedA_.Get();
  905. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  906. statu[0] = pose.x();
  907. statu[1] = pose.y();
  908. statu[2] = pose.theta();
  909. statu[3] = velocity;
  910. statu[4] = angular;
  911. Trajectory optimize_trajectory;
  912. Trajectory selected_trajectory;
  913. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  914. double obs_w = 0.95;//0.85;
  915. double obs_h = 1.55;//1.45;
  916. if (directY == true) {
  917. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  918. statu[2] += M_PI / 2;
  919. if (statu[2] > M_PI)
  920. statu[2] -= 2 * M_PI;
  921. mpc_parameter = parameter_.y_mpc_parameter();
  922. obs_w = 0.95;//0.85;
  923. obs_h = 1.55;//1.45;
  924. }
  925. Pose2d brother = timedBrotherPose_.Get();
  926. Pose2d relative = Pose2d::relativePose(brother, pose);
  927. if (directY)
  928. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  929. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  930. //std::cout<<"pose:"<<pose<<", brother:"<<brother<<", relative:"<<relative<<std::endl;
  931. if (move_mode_ == eDouble)
  932. relative = Pose2d(-1e8, -1e8, 0);
  933. MpcError ret = failed;
  934. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  935. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  936. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  937. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  938. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  939. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  940. out, selected_trajectory, optimize_trajectory);
  941. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  942. if (ret == no_solution) {
  943. printf(" mpc solve no solution set v/w = 0\n");
  944. out.clear();
  945. out.push_back(0);
  946. out.push_back(0);
  947. out.push_back(0);
  948. out.push_back(0);
  949. out.push_back(0);
  950. out.push_back(0);
  951. }
  952. predict_traj_.reset(optimize_trajectory, 1);
  953. selected_traj_.reset(selected_trajectory, 1);
  954. return ret == success || ret == no_solution;
  955. }
  956. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  957. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  958. printf(" pose / v / w timeout,IsArrived return false\n");
  959. return false;
  960. }
  961. Pose2d current = timedPose_.Get();
  962. double x = current.x();
  963. double y = current.y();
  964. double tx = targetNode.x();
  965. double ty = targetNode.y();
  966. double l = fabs(targetNode.l());
  967. double w = fabs(targetNode.w());
  968. double theta = -targetNode.theta();
  969. if (newUnfinished_cations_.front().type() == 1 && targetNode.id().find('S')!=-1){//入库只判断前后方向上是否到达
  970. tx = x;
  971. }
  972. if (l < 1e-10 || w < 1e-10) {
  973. printf("IsArrived: Error target l or w == 0\n");
  974. return false;
  975. }
  976. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  977. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  978. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  979. if (fabs(timedV_.Get()) < 0.06 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  980. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  981. return true;
  982. }
  983. }
  984. return false;
  985. }
  986. void Navigation::SendMoveCmd(int mode, ActionType type,
  987. double v, double angular) {
  988. if (monitor_) {
  989. monitor_->set_speed(mode, type, v, angular);
  990. if (type == eRotation)
  991. RWheel_position_ = eR;
  992. if (type == eVertical)
  993. RWheel_position_ = eX;
  994. if (type == eHorizontal)
  995. RWheel_position_ = eY;
  996. }
  997. }
  998. void Navigation::SendMoveCmd(int mode, ActionType type,
  999. double v[], double angular[]) {
  1000. if (monitor_) {
  1001. monitor_->set_ToAgvCmd(mode, type, v, angular);
  1002. if (type == eRotation)
  1003. RWheel_position_ = eR;
  1004. if (type == eVertical)
  1005. RWheel_position_ = eX;
  1006. if (type == eHorizontal)
  1007. RWheel_position_ = eY;
  1008. }
  1009. }
  1010. void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[], int space_id, double distance) {
  1011. if (monitor_) {
  1012. monitor_->set_ToAgvCmd(mode, type, v, angular, 0,space_id, distance);
  1013. if (type == eRotation)
  1014. RWheel_position_ = eR;
  1015. if (type == eVertical)
  1016. RWheel_position_ = eX;
  1017. if (type == eHorizontal)
  1018. RWheel_position_ = eY;
  1019. }
  1020. }
  1021. bool Navigation::clamp_close() {
  1022. if (monitor_) {
  1023. printf("Clamp closing...\n");
  1024. monitor_->clamp_close(move_mode_);
  1025. actionType_ = eClampClose;
  1026. while (cancel_ == false) {
  1027. if (timed_clamp_.timeout()) {
  1028. printf("timed clamp is timeout\n");
  1029. return false;
  1030. }
  1031. if (timed_clamp_.Get() == eClosed)
  1032. return true;
  1033. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1034. monitor_->clamp_close(move_mode_);
  1035. actionType_ = eClampClose;
  1036. }
  1037. return false;
  1038. }
  1039. return false;
  1040. }
  1041. bool Navigation::clamp_open() {
  1042. if (monitor_) {
  1043. printf("Clamp openning...\n");
  1044. monitor_->clamp_open(move_mode_);
  1045. actionType_ = eClampOpen;
  1046. while (cancel_ == false) {
  1047. if (timed_clamp_.timeout()) {
  1048. printf("timed clamp is timeout\n");
  1049. return false;
  1050. }
  1051. if (timed_clamp_.Get() == eOpened)
  1052. return true;
  1053. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1054. monitor_->clamp_open(move_mode_);
  1055. actionType_ = eClampOpen;
  1056. }
  1057. return false;
  1058. }
  1059. return false;
  1060. }
  1061. bool Navigation::lifter_rise() {
  1062. if (monitor_) {
  1063. printf("Lifter upping...\n");
  1064. monitor_->lifter_rise(move_mode_);
  1065. actionType_ = eLifterRise;
  1066. while (cancel_ == false) {
  1067. if (timed_lifter_.timeout()) {
  1068. printf("timed lifter is timeout\n");
  1069. return false;
  1070. }
  1071. if (timed_lifter_.Get() == eRose)
  1072. return true;
  1073. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1074. monitor_->lifter_rise(move_mode_);
  1075. actionType_ = eLifterRise;
  1076. }
  1077. return false;
  1078. }
  1079. return false;
  1080. }
  1081. bool Navigation::lifter_down() {
  1082. if (monitor_) {
  1083. printf("Lifter downing...\n");
  1084. monitor_->lifter_down(move_mode_);
  1085. actionType_ = eLifterDown;
  1086. while (cancel_ == false) {
  1087. if (timed_lifter_.timeout()) {
  1088. printf("timed lifter is timeout\n");
  1089. return false;
  1090. }
  1091. if (timed_lifter_.Get() == eDowned)
  1092. return true;
  1093. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1094. monitor_->lifter_down(move_mode_);
  1095. actionType_ = eLifterDown;
  1096. }
  1097. return false;
  1098. }
  1099. return false;
  1100. }
  1101. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect) {
  1102. // LogWriter("MpcToTarget beg");
  1103. if (IsArrived(node)) {
  1104. printf(" current already in target completed !!!\n");
  1105. return eMpcSuccess;
  1106. }
  1107. if (inited_ == false) {
  1108. printf(" navigation has not inited\n");
  1109. return eMpcFailed;
  1110. }
  1111. if (PoseTimeout()) {
  1112. printf(" navigation Error:Pose is timeout \n");
  1113. return eMpcFailed;
  1114. }
  1115. Pose2d current = timedPose_.Get();
  1116. Pose2d target(node.x(), node.y(), 0);
  1117. //生成轨迹
  1118. Trajectory traj = Trajectory::create_line_trajectory(current, target, 0.1);
  1119. if (traj.size() == 0)
  1120. return eMpcSuccess;
  1121. //判断 巡线方向
  1122. bool directY = false;
  1123. if (autoDirect) {
  1124. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  1125. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  1126. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  1127. directY = true;
  1128. } else {
  1129. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  1130. }
  1131. }
  1132. printf(" exec along autoDirect:%d\n", autoDirect);
  1133. //文件log
  1134. std::ofstream ofs; //创建流对象
  1135. std::string log_file_dir = "../data/";
  1136. time_t t = time(0);
  1137. char tmp[32]={NULL};
  1138. strftime(tmp, sizeof(tmp), "%Y_%m_%d_%H_%M_%S.txt",localtime(&t));
  1139. std::string log_file_name = tmp;
  1140. ofs.open(log_file_dir+log_file_name,std::ios::app); //打开的地址和方式
  1141. auto beg_time = std::chrono::steady_clock::now();
  1142. while (cancel_ == false) {
  1143. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  1144. if (pause_ == true) {
  1145. //发送暂停消息
  1146. continue;
  1147. }
  1148. if (PoseTimeout()) {
  1149. printf(" navigation Error:Pose is timeout \n");
  1150. ofs.close();//关闭文件
  1151. return eMpcFailed;
  1152. }
  1153. if (timedV_.timeout() || timedA_.timeout()) {
  1154. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  1155. ofs.close();//关闭文件
  1156. return eMpcFailed;
  1157. }
  1158. //判断是否到达终点
  1159. if (IsArrived(node)) {
  1160. if (Stop()) {
  1161. printf(" exec along completed !!!\n");
  1162. ofs.close();//关闭文件
  1163. return eMpcSuccess;
  1164. }
  1165. }
  1166. //一次变速
  1167. std::vector<double> out;
  1168. bool ret;
  1169. TimerRecord::Execute([&, this]() {
  1170. ret = mpc_once(traj, limit_v, out, directY);
  1171. }, "mpc_once");
  1172. if (ret == false) {
  1173. Stop();
  1174. ofs.close();//关闭文件
  1175. return eMpcFailed;
  1176. }
  1177. const int down_count = 3;//下发前多少步
  1178. double down_v[down_count] ={out[0], out[2], out[4]};//下发线速度
  1179. double down_w[down_count] ={out[1], out[3], out[5]};//下发角速度
  1180. /*
  1181. * AGV 在终点附近低速巡航时,设置最小速度0.05
  1182. */
  1183. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  1184. if (directY) {
  1185. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  1186. }
  1187. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  1188. for (int i = 0; i < down_count; ++i) {
  1189. if (down_v[i] >= 0 && down_v[i] < limit_v.min)
  1190. down_v[i] = limit_v.min;
  1191. if (down_v[i] < 0 && down_v[i] > -limit_v.min)
  1192. down_v[i] = -limit_v.min;
  1193. }
  1194. }
  1195. //放大角速度
  1196. // out[1] *= 10;
  1197. //入库,行进方向上最后一米不纠偏
  1198. if (newUnfinished_cations_.front().type() == 1 && node.id().find('S')!=-1){//入库
  1199. // if(fabs(target_in_agv.x()) < 0.4){
  1200. // printf("target_in_agv: %f", target_in_agv.x());
  1201. // for (double & i : down_w) {
  1202. // i = 0.0;
  1203. // }
  1204. // }
  1205. }else if (newUnfinished_cations_.front().type() == 2){//出库,行进方向上开始一米不纠偏
  1206. Pose2d agv_in_space =
  1207. Pose2d::relativePose(
  1208. Pose2d(newUnfinished_cations_.front().spacenode().x(), newUnfinished_cations_.front().spacenode().y(), 0),
  1209. timedPose_.Get());
  1210. if(fabs(agv_in_space.x()) < 0.3){
  1211. printf("agv_in_space: %f", agv_in_space.x());
  1212. for (double & i : down_w) {
  1213. i = 0.0;
  1214. }
  1215. }
  1216. }
  1217. //下发速度
  1218. //printf(" nav input :%f out:%f\n",timedV_.Get(),out[0]);
  1219. //添加车位号,距目标点距离信息
  1220. double distance = Pose2d::distance(target_in_agv, Pose2d(0,0,0));
  1221. int space_id = 0;
  1222. if(node.id().find('S') != -1){
  1223. std::string id = node.id().substr(1,node.id().length()-1);
  1224. space_id = stoi(id);
  1225. }
  1226. if (directY == false)
  1227. SendMoveCmd(move_mode_, eVertical, down_v, down_w, space_id, distance);
  1228. else
  1229. SendMoveCmd(move_mode_, eHorizontal, down_v, down_w, space_id, distance);
  1230. actionType_ = directY ? eHorizontal : eVertical;
  1231. //日志打印
  1232. std::string log_inf_line = std::to_string(move_mode_);
  1233. log_inf_line += " ";
  1234. log_inf_line += std::to_string(parameter_.x_mpc_parameter().shortest_radius())+" "+std::to_string(parameter_.x_mpc_parameter().acc_angular());
  1235. log_inf_line += " ";
  1236. Pose2d cur_pose = timedPose_.Get();//自身x,y,theta
  1237. log_inf_line += std::to_string(cur_pose.x()) + " " + std::to_string(cur_pose.y()) + " " + std::to_string(cur_pose.theta());
  1238. log_inf_line += " ";
  1239. Pose2d cur_Bro_pose = timedBrotherPose_.Get();//另一节x,y,theta
  1240. log_inf_line += std::to_string(cur_Bro_pose.x()) + " " + std::to_string(cur_Bro_pose.y()) + " " + std::to_string(cur_Bro_pose.theta());
  1241. log_inf_line += " ";
  1242. log_inf_line += std::to_string(out[0]) + " " + std::to_string(out[1]);//下发速度
  1243. log_inf_line += " ";
  1244. log_inf_line += std::to_string(out[2]) + " " + std::to_string(out[3]);//下发速度
  1245. log_inf_line += " ";
  1246. log_inf_line += std::to_string(out[4]) + " " + std::to_string(out[5]);//下发速度
  1247. log_inf_line += " ";
  1248. auto cur_time = std::chrono::steady_clock::now();
  1249. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(cur_time - beg_time);
  1250. double time =
  1251. double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  1252. log_inf_line += std::to_string(time);
  1253. ofs << log_inf_line << std::endl; //一行日志的内容
  1254. /*
  1255. * 判断车辆是否在终点附近徘徊,无法到达终点
  1256. */
  1257. if (PossibleToTarget(node, directY) == false) {
  1258. printf(" --------------MPC imposible to target -------------------------------------------\n");
  1259. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  1260. SlowlyStop();
  1261. else
  1262. Stop();
  1263. ofs.close();//关闭文件
  1264. return eImpossibleToTarget;
  1265. }
  1266. }
  1267. ofs.close();//关闭文件
  1268. return eMpcFailed;
  1269. }
  1270. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  1271. if (timedPose_.timeout()) {
  1272. return false;
  1273. }
  1274. Pose2d current = timedPose_.Get();
  1275. if(targetNode.id().find('S') != -1){
  1276. //车位点强制可到达
  1277. return true;
  1278. }
  1279. double tx = targetNode.x();
  1280. double ty = targetNode.y();
  1281. double l = fabs(targetNode.l());
  1282. double w = fabs(targetNode.w());
  1283. double theta = -targetNode.theta();
  1284. // double W = 2.45;
  1285. // double L = 1.3;
  1286. // double minYaw = 3 * M_PI / 180.0;
  1287. // if (move_mode_ == eDouble) {
  1288. // L = 1.3 + 2.7;
  1289. //
  1290. // }
  1291. double W = 2.5;
  1292. double L = 1.565;
  1293. double minYaw = 5 * M_PI / 180.0;
  1294. if (move_mode_ == eDouble) {
  1295. L = 1.565 + 2.78;
  1296. }
  1297. if (directY) {
  1298. current = current.rotate(current.x(), current.y(), M_PI / 2);
  1299. double t = W;
  1300. W = L;
  1301. L = t;
  1302. }
  1303. double minR = W / 2 + L / tan(minYaw);
  1304. //printf("l:%f,w:%f\n",l,w);
  1305. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  1306. bool nagY = false;
  1307. bool posiY = false;
  1308. for (int i = 0; i < poses.size(); ++i) {
  1309. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  1310. Pose2d relative = Pose2d::relativePose(rpos, current);
  1311. double ax = fabs(relative.x());
  1312. double ay = fabs(relative.y());
  1313. if (relative.y() > 0) posiY = true;
  1314. if (relative.y() < 0) nagY = true;
  1315. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  1316. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  1317. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  1318. minR,ax,ay);*/
  1319. return true;
  1320. } else if (nagY && posiY) {
  1321. // std::cout << "nagY && posiY" << std::endl;
  1322. return true;
  1323. }
  1324. // printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  1325. // relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  1326. // minR, ax, ay);
  1327. }
  1328. printf(" impossible to target:%f %f l:%f,w:%f theta:%f, ",
  1329. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  1330. printf(" current:%f, %f, %f\n",
  1331. current.x(), current.y(),current.theta());
  1332. return false;
  1333. }
  1334. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  1335. if (inited_ == false) {
  1336. response.set_ret(-1);
  1337. response.set_info("navigation has not inited");
  1338. printf(" navigation has not inited\n");
  1339. return;
  1340. }
  1341. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  1342. {
  1343. response.set_ret(-2);
  1344. response.set_info("navigation is running pls cancel before");
  1345. printf(" navigation is running pls cancel before\n");
  1346. return;
  1347. }
  1348. //add 先检查当前点与起点的距离
  1349. global_navCmd_ = cmd;
  1350. for (int i = 0; i < cmd.newactions_size(); ++i)
  1351. newUnfinished_cations_.push(cmd.newactions(i));
  1352. pause_ = false;
  1353. cancel_ = false;
  1354. running_ = true;
  1355. actionType_ = eReady;
  1356. printf("Navigation beg...\n");
  1357. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  1358. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  1359. NavMessage::NewAction act = newUnfinished_cations_.front();
  1360. if (act.type() == 1) { //入库
  1361. space_id_ = act.spacenode().id();
  1362. isInSpace_ = true;
  1363. if (!execute_InOut_space(act)) {
  1364. printf(" In space failed\n");
  1365. break;
  1366. }
  1367. } else if (act.type() == 2) { //出库
  1368. if (!execute_InOut_space(act)) {
  1369. printf(" out space failed\n");
  1370. break;
  1371. } else {
  1372. isInSpace_ = false;
  1373. }
  1374. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1375. if (!execute_nodes(act)){
  1376. printf(" street nav failed\n");
  1377. break;
  1378. }
  1379. } else if (act.type() == 5) {
  1380. printf(" 汽车模型导航....\n");
  1381. } else if (act.type() == 6) {//夹持
  1382. if (this->clamp_close() == false) {
  1383. printf("夹持failed ...\n");
  1384. break;
  1385. }
  1386. } else if (act.type() == 7) {
  1387. if (this->clamp_open() == false) {
  1388. printf("打开夹持 failed...\n");
  1389. break;
  1390. }
  1391. } else if (act.type() == 8) { //切换模式
  1392. SwitchMode(act.changedmode(), act.wheelbase());
  1393. } else if (act.type() == 9) { //提升机构提升
  1394. if (this->lifter_rise() == false) {
  1395. printf("提升failed ...\n");
  1396. break;
  1397. }
  1398. } else if (act.type() == 10) { //提升机构下降
  1399. if (this->lifter_down() == false) {
  1400. printf("下降failed ...\n");
  1401. break;
  1402. }
  1403. }else {
  1404. printf(" action type invalid not handled !!\n");break;
  1405. }
  1406. newUnfinished_cations_.pop();
  1407. }
  1408. actionType_ = eReady;
  1409. Stop();
  1410. if (cancel_ == true) {
  1411. response.set_ret(-3);
  1412. response.set_info("navigation canceled");
  1413. printf(" navigation canceled\n");
  1414. while (newUnfinished_cations_.empty() == false)
  1415. newUnfinished_cations_.pop();
  1416. } else {
  1417. if (newUnfinished_cations_.empty()) {
  1418. response.set_ret(0);
  1419. response.set_info("navigation completed!!!");
  1420. printf("navigation completed!!!\n");
  1421. } else {
  1422. response.set_ret(-4);
  1423. response.set_info("navigation Failed!!!!");
  1424. printf(" navigation Failed\n");
  1425. while (newUnfinished_cations_.empty() == false)
  1426. newUnfinished_cations_.pop();
  1427. }
  1428. }
  1429. running_ = false;
  1430. }
  1431. void Navigation::SwitchMode(int mode, float wheelBase) {
  1432. printf("Child AGV can not Switch Mode\n");
  1433. }
  1434. float Navigation::compute_cuv(const Pose2d &target) {
  1435. float x = fabs(target.x());
  1436. float y = fabs(target.y());
  1437. float cuv = atan(y / (x + 1e-8));
  1438. //printf(" ---------------------- cuv:%f\n",cuv);
  1439. return cuv;
  1440. }
  1441. void Navigation::ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response) {
  1442. return;
  1443. // pause_ = false;
  1444. // cancel_ = false;
  1445. // running_ = true;
  1446. // actionType_ = eReady;
  1447. // printf("ManualOperation: %d | start...\n", cmd.operation_type());
  1448. //
  1449. // float symbol = cmd.velocity() < 0 ? -1 : 1; // 判断为正方向or负方向
  1450. // double velocity;
  1451. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1452. // switch (cmd.operation_type()) {
  1453. // case 1: // 旋转
  1454. // velocity = 2.0 * M_PI / 180.0 * symbol;
  1455. // SendMoveCmd(move_mode_, eRotation, 0, velocity);
  1456. // actionType_ = eRotation;
  1457. // printf(" ManualOperation: %d | input angular_v: %f, down: %f\n",
  1458. // cmd.operation_type(), timedA_.Get(), velocity);
  1459. // break;
  1460. // case 2: // X平移
  1461. // velocity = 1.0 * symbol;
  1462. // SendMoveCmd(move_mode_, eVertical, velocity, 0);
  1463. // actionType_ = eVertical;
  1464. // printf(" ManualOperation: %d | input line_v : %f, down: %f\n",
  1465. // cmd.operation_type(), timedV_.Get(), velocity);
  1466. // break;
  1467. // case 3: // Y平移
  1468. // velocity = 1.0 * symbol;
  1469. // SendMoveCmd(move_mode_, eHorizontal, velocity, 0);
  1470. // actionType_ = eHorizontal;
  1471. // printf(" ManualOperation: %d | input line_v: %f, down: %f\n",
  1472. // cmd.operation_type(), timedV_.Get(), velocity);
  1473. // break;
  1474. // default:
  1475. // break;
  1476. // }
  1477. //
  1478. // actionType_ = eReady;
  1479. // response.set_ret(-3);
  1480. // response.set_info("ManualOperation: %d, canceled!!!", cmd.operation_type());
  1481. // printf("ManualOperation: %d | canceled!!!\n", cmd.operation_type());
  1482. // running_ = false;
  1483. }