navigation.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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_->set_clampLifterCmd_topic(agv_p.pubclampliftertopic());
  74. monitor_->AddCallback(agv_p.subposetopic(), Navigation::RobotPoseCallback, this);
  75. monitor_->AddCallback(agv_p.subspeedtopic(), Navigation::RobotSpeedCallback, this);
  76. }
  77. NavParameter::Emqx_parameter terminal_p = parameter.terminal_emqx();
  78. if (terminator_ == nullptr) {
  79. terminator_ = new Terminator_emqx(terminal_p.nodeid());
  80. if (terminator_->Connect(terminal_p.ip(), terminal_p.port()) == false) {
  81. printf(" terminator emqx connected failed\n");
  82. return false;
  83. }
  84. }
  85. if (parameter.has_brother_emqx()) {
  86. NavParameter::BrotherEmqx brotherEmqx = parameter.brother_emqx();
  87. if (brotherEmqx_ == nullptr) {
  88. brotherEmqx_ = new Terminator_emqx(brotherEmqx.nodeid());
  89. if (brotherEmqx_->Connect(brotherEmqx.ip(), brotherEmqx.port()) == true) {
  90. brotherEmqx_->AddCallback(brotherEmqx.subbrotherstatutopic(), Navigation::BrotherAgvStatuCallback,
  91. this);
  92. } else {
  93. printf(" brother emqx connected failed\n");
  94. // return false;
  95. }
  96. //
  97. }
  98. }
  99. inited_ = true;
  100. if (pubthread_ != nullptr) {
  101. exit_ = true;
  102. if (pubthread_->joinable())
  103. pubthread_->join();
  104. delete pubthread_;
  105. }
  106. exit_ = false;
  107. pubthread_ = new std::thread(&Navigation::pubStatuThreadFuc, this);
  108. return true;
  109. }
  110. void Navigation::RobotPoseCallback(const MqttMsg &msg, void *context) {
  111. Navigation *navigator = (Navigation *) context;
  112. NavMessage::LidarOdomStatu statu;
  113. if (msg.toProtoMessage(statu)) {
  114. navigator->ResetPose(Pose2d(statu.x(), statu.y(), statu.theta()));
  115. }
  116. }
  117. void Navigation::HandleAgvStatu(const MqttMsg &msg) {
  118. NavMessage::AgvStatu speed;
  119. if (IsMasterAGV()) {
  120. if (msg.toProtoMessage(speed)) {
  121. ResetStatu(speed.v(), speed.w());
  122. ResetClamp((ClampStatu) speed.clamp());
  123. // ResetOtherClamp((ClampStatu) speed.clamp_other());
  124. ResetLifter((LifterStatus) speed.lifter());
  125. // ResetOtherLifter((LifterStatus) speed.lifter_other());
  126. //printf(" clamp:%d other:%d\n",speed.clamp(),speed.clamp_other());
  127. }
  128. } else {
  129. if (msg.toProtoMessage(speed)) {
  130. ResetStatu(speed.v(), speed.w());
  131. ResetClamp((ClampStatu) speed.clamp());
  132. ResetLifter((LifterStatus) speed.lifter());
  133. }
  134. }
  135. ResetDoor(speed.door());
  136. std::vector<int32_t> vecStatus;
  137. for(int i=0;i<speed.zcb_size();++i){
  138. vecStatus.push_back(speed.zcb(i));
  139. }
  140. ResetCarrier(vecStatus);
  141. }
  142. void Navigation::RobotSpeedCallback(const MqttMsg &msg, void *context) {
  143. Navigation *navigator = (Navigation *) context;
  144. navigator->HandleAgvStatu(msg);
  145. }
  146. void Navigation::pubStatuThreadFuc(void *p) {
  147. Navigation *navogator = (Navigation *) p;
  148. if (navogator) {
  149. while (navogator->exit_ == false) {
  150. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  151. NavMessage::NavStatu statu;
  152. statu.set_main_agv(false);
  153. navogator->publish_statu(statu);
  154. }
  155. }
  156. }
  157. void Navigation::publish_statu(NavMessage::NavStatu &statu) {
  158. if (timedPose_.timeout() == false) {
  159. NavMessage::LidarOdomStatu odom;
  160. odom.set_x(timedPose_.Get().x());
  161. odom.set_y(timedPose_.Get().y());
  162. odom.set_theta(timedPose_.Get().theta());
  163. if (timedV_.timeout() == false)
  164. odom.set_v(timedV_.Get());
  165. if (timedA_.timeout() == false)
  166. odom.set_vth(timedA_.Get());
  167. statu.mutable_odom()->CopyFrom(odom);
  168. }
  169. statu.set_in_space(isInSpace_);
  170. if (isInSpace_) statu.set_space_id(space_id_);
  171. //发布nav状态
  172. statu.set_statu(actionType_); //
  173. statu.set_move_mode(move_mode_);
  174. if (running_) {
  175. statu.set_key(global_navCmd_.key());
  176. }
  177. //发布MPC信息
  178. //发布mpc 预选点
  179. if (selected_traj_.timeout() == false) {
  180. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  181. Pose2d pt = selected_traj_.Get()[i];
  182. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  183. pose->set_x(pt.x());
  184. pose->set_y(pt.y());
  185. pose->set_theta(pt.theta());
  186. }
  187. }
  188. //发布 mpc 预测点
  189. if (predict_traj_.timeout() == false) {
  190. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  191. Pose2d pt = predict_traj_.Get()[i];
  192. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  193. pose->set_x(pt.x());
  194. pose->set_y(pt.y());
  195. pose->set_theta(pt.theta());
  196. }
  197. }
  198. // std::cout<<"nav statu:"<<statu.DebugString()<<std::endl;
  199. MqttMsg msg;
  200. msg.fromProtoMessage(statu);
  201. if (terminator_)
  202. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(), msg);
  203. //发布位姿 ------robot状态--------------------------
  204. NavMessage::RobotStatu robot;
  205. if (CreateRobotStatuMsg(robot)) {
  206. if (terminator_) {
  207. MqttMsg msg;
  208. msg.fromProtoMessage(robot);
  209. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  210. }
  211. }
  212. }
  213. bool Navigation::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  214. //printf(" %d %d %d \n",timedPose_.timeout(),timedV_.timeout(),timedA_.timeout());
  215. if (timedPose_.timeout() == false) {
  216. Pose2d pose = timedPose_.Get();
  217. robotStatu.set_x(pose.x());
  218. robotStatu.set_y(pose.y());
  219. robotStatu.set_theta(pose.theta());
  220. if ((timedV_.timeout() == false && timedA_.timeout() == false)) {
  221. NavMessage::AgvStatu plc;
  222. plc.set_v(timedV_.Get());
  223. plc.set_w(timedA_.Get());
  224. plc.set_clamp(timed_clamp_.Get());
  225. plc.set_lifter(timed_lifter_.Get());
  226. robotStatu.mutable_agvstatu()->CopyFrom(plc);
  227. }
  228. return true;
  229. }
  230. return false;
  231. }
  232. void Navigation::ResetStatu(double v, double a) {
  233. timedV_.reset(v, 0.5);
  234. timedA_.reset(a, 0.5);
  235. }
  236. void Navigation::ResetClamp(ClampStatu statu) {
  237. timed_clamp_.reset(statu, 1);
  238. }
  239. void Navigation::ResetLifter(LifterStatus status) {
  240. timed_lifter_.reset(status, 1);
  241. }
  242. void Navigation::ResetPose(const Pose2d &pose) {
  243. timedPose_.reset(pose, 1.0);
  244. if (pose.theta() > 0) {
  245. if (parameter_.main_agv()) {
  246. isFront_.reset(true, 1.0);
  247. } else {
  248. isFront_.reset(false, 1.0);
  249. }
  250. } else {
  251. if (parameter_.main_agv()) {
  252. isFront_.reset(false, 1.0);
  253. } else {
  254. isFront_.reset(true, 1.0);
  255. }
  256. }
  257. }
  258. void Navigation::Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  259. cancel_ = true;
  260. response.set_ret(0);
  261. }
  262. bool Navigation::Stop() {
  263. if (monitor_) {
  264. monitor_->stop();
  265. while (cancel_ == false) {
  266. if (timedV_.timeout() == false && timedA_.timeout() == false) {
  267. if (fabs(timedV_.Get()) < 1e-2 && fabs(timedA_.Get()) < 1e-3)
  268. return true;
  269. else
  270. monitor_->stop();
  271. printf("Stoped wait V/A ==0(1e-4,1e-4),V:%f,A:%f\n", fabs(timedV_.Get()), fabs(timedA_.Get()));
  272. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  273. } else {
  274. printf("Stop failed V/A timeout\n");
  275. return false;
  276. }
  277. }
  278. }
  279. printf("stoped ret false.\n");
  280. return false;
  281. }
  282. bool Navigation::SlowlyStop() {
  283. double acc = 3.0;
  284. double dt = 0.1;
  285. while (cancel_ == false) {
  286. if (timedV_.timeout() == false) {
  287. double v = timedV_.Get();
  288. if (fabs(v < 1e-2 && fabs(timedA_.Get()) < 1e-3))
  289. return true;
  290. if (v > 0) {
  291. double new_v = v - acc * dt;
  292. new_v = new_v > 0 ? new_v : 0;
  293. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  294. } else {
  295. double new_v = v + acc * dt;
  296. new_v = new_v < 0 ? new_v : 0;
  297. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  298. }
  299. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  300. } else {
  301. printf("Stop failed V/A timeout\n");
  302. return false;
  303. }
  304. }
  305. printf("stoped ret false.\n");
  306. return false;
  307. }
  308. Navigation::Navigation() {
  309. is_master_AGV=false;
  310. wheelBase_ = 0;
  311. isInSpace_ = false; //是否在车位或者正在进入车位
  312. RWheel_position_ = eUnknow;
  313. move_mode_ = eSingle;
  314. monitor_ = nullptr;
  315. terminator_ = nullptr;
  316. obs_h_ = 4.0;
  317. obs_w_ = 2.0;
  318. timedBrotherPose_.reset(Pose2d(-100, 0, 0), 0.5);
  319. LoadSpaceNo2CarrierNo();
  320. }
  321. void Navigation::BrotherAgvStatuCallback(const MqttMsg &msg, void *context) {
  322. Navigation *navigator = (Navigation *) context;
  323. NavMessage::NavStatu brother_statu;
  324. if (msg.toProtoMessage(brother_statu) == false) {
  325. std::cout << " msg transform to AGVStatu failed,msg:" << msg.data() << std::endl;
  326. return;
  327. }
  328. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  329. if (brother_statu.has_odom()) {
  330. NavMessage::LidarOdomStatu odom = brother_statu.odom();
  331. Pose2d pose(odom.x(), odom.y(), odom.theta());
  332. navigator->timedBrotherPose_.reset(pose, 1);
  333. navigator->timedBrotherV_.reset(odom.v(), 1);
  334. navigator->timedBrotherA_.reset(odom.vth(), 1);
  335. navigator->timedBrotherNavStatu_.reset(brother_statu, 0.1);
  336. }
  337. }
  338. //未使用
  339. /*
  340. bool Navigation::RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect) {
  341. while (cancel_ == false) {
  342. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  343. if (timedPose_.timeout()) {
  344. printf(" RotateBytarget failed pose timeout\n");
  345. return false;
  346. }
  347. if (timedA_.timeout()) {
  348. printf(" RotateBytarget failed wmg timeout\n");
  349. return false;
  350. }
  351. Pose2d targetInPose = Pose2d::relativePose(target, timedPose_.Get());
  352. float yawDiff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  353. float minYawdiff = yawDiff;
  354. if (anyDirect) {
  355. Pose2d p0 = timedPose_.Get();
  356. float yawdiff[4] = {0};
  357. yawdiff[0] = yawDiff;
  358. Pose2d relativePose;
  359. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI / 2));
  360. yawdiff[1] = Pose2d::vector2yaw(relativePose.x(), relativePose.y()); ////使用减法,保证角度在【-pi pi】
  361. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI));
  362. yawdiff[2] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  363. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, 3 * M_PI / 2));
  364. yawdiff[3] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  365. for (int i = 1; i < 4; ++i) {
  366. if (fabs(yawdiff[i]) < fabs(minYawdiff)) {
  367. minYawdiff = yawdiff[i];
  368. targetInPose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, i * M_PI / 2.0));
  369. }
  370. }
  371. }
  372. //std::cout<<" Rotate refer to target:"<<target<<",targetInPose:"
  373. //<<targetInPose<<",anyDirect:"<<anyDirect<<std::endl;
  374. //以当前点为原点,想方向经过目标点的二次曲线曲率>0.25,则需要调整 y=a x*x
  375. float cuv = compute_cuv(targetInPose);
  376. float dt = 0.1;
  377. float acc_angular = 15 * M_PI / 180.0;
  378. if (cuv > 2 * M_PI / 180.0) {
  379. double theta = limit_gause(minYawdiff, limit_rotate.min, limit_rotate.max);
  380. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  381. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  382. SendMoveCmd(move_mode_, eRotation, 0, limit_angular);
  383. actionType_ = eRotation;
  384. printf(" Rotate | input angular:%f,next angular:%f,down:%f diff:%f anyDirect:%d\n",
  385. timedA_.Get(), angular, limit_angular, minYawdiff, anyDirect);
  386. continue;
  387. } else {
  388. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  389. printf(" Rotate refer target completed,cuv:%f\n", cuv);
  390. return true;
  391. }
  392. continue;
  393. }
  394. }
  395. return false;
  396. }
  397. */
  398. bool Navigation::AdjustReferToTarget(const Pose2d &target, stLimit limit_rotate,int directions) {
  399. std::cout << "Adjust to : " << target << std::endl;
  400. if (inited_ == false) {
  401. printf(" MPC_rotate has not inited\n");
  402. return false;
  403. }
  404. if (PoseTimeout()) {
  405. printf(" MPC_rotate Error:Pose is timeout \n");
  406. return false;
  407. }
  408. Pose2d current = timedPose_.Get();
  409. NavMessage::PathNode yawTarget;
  410. yawTarget.set_x(current.x() + 100.);
  411. yawTarget.set_y(current.y());
  412. std::cout << "Adjust RotateRefer : " << Pose2d(yawTarget.x(), yawTarget.y(), 0) << std::endl;
  413. RotateReferToTarget(yawTarget, limit_rotate, directions);
  414. current = timedPose_.Get();
  415. Pose2d diff = target - current;
  416. Pose2d newTarget;
  417. NavMessage::PathNode node;
  418. if (fabs(diff.x()) > fabs(diff.y())) {
  419. newTarget = Pose2d(current.x(), target.y(), current.theta());
  420. node.set_theta(M_PI / 2.0);
  421. } else {
  422. newTarget = Pose2d(target.x(), current.y(), current.theta());
  423. node.set_theta(0);
  424. }
  425. node.set_x(newTarget.x());
  426. node.set_y(newTarget.y());
  427. node.set_l(0.03);
  428. node.set_w(0.2);
  429. stLimit limitV = {0.03, 0.5};
  430. stLimit limitW = {0.03, 0.1};
  431. std::cout << " Adjust MoveTarget:" << Pose2d(node.x(), node.y(), node.theta()) << std::endl;
  432. return MoveToTarget(node, limitV, limitW, true, true);
  433. }
  434. Navigation::MpcResult Navigation::RotateReferToTarget(NavMessage::PathNode node, stLimit limit_rotate, int directions) {
  435. if (inited_ == false) {
  436. printf(" MPC_rotate has not inited\n");
  437. return eMpcFailed;
  438. }
  439. if (PoseTimeout()) {
  440. printf(" MPC_rotate Error:Pose is timeout \n");
  441. return eMpcFailed;
  442. }
  443. printf(" exec MPC_rotate autoDirect:%d\n", directions);
  444. while (cancel_ == false) {
  445. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  446. if (pause_ == true) {
  447. //发送暂停消息
  448. continue;
  449. }
  450. if (PoseTimeout()) {
  451. printf(" MPC_rotate Error:Pose is timeout \n");
  452. return eMpcFailed;
  453. }
  454. if (timedA_.timeout()) {
  455. printf(" MPC_rotate Error:v/a is timeout \n");
  456. return eMpcFailed;
  457. }
  458. Pose2d current = timedPose_.Get();
  459. Pose2d target(node.x(), node.y(), node.theta());
  460. Pose2d targetInPose = Pose2d::relativePose(target, current);
  461. // 需要旋转的角度
  462. float target_yaw_diff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  463. // 自由方向
  464. if (directions) {
  465. std::vector<double> yaw_diffs;
  466. if (directions & Direction::eForward) {
  467. yaw_diffs.push_back(target_yaw_diff);
  468. }
  469. if (directions & Direction::eBackward) {
  470. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, M_PI));
  471. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  472. }
  473. if (directions & Direction::eLeft) {
  474. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 1.0 / 2 * M_PI));
  475. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  476. }
  477. if (directions & Direction::eRight) {
  478. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 3.0 / 2 * M_PI));
  479. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  480. }
  481. for (auto i = 0; i < yaw_diffs.size(); ++i) {
  482. if (fabs(yaw_diffs[i]) < fabs(target_yaw_diff))
  483. target_yaw_diff = yaw_diffs[i];
  484. }
  485. // std::cout << std::endl;
  486. // std::cout << std::endl << " min_diff: " << current_to_target.theta() << std::endl;
  487. }
  488. //一次变速
  489. std::vector<double> out;
  490. bool ret;
  491. TimerRecord::Execute([&, this]() {
  492. ret = Rotation_mpc_once(Pose2d(0, 0, target_yaw_diff), limit_rotate, out);
  493. }, "Rotation_mpc_once");
  494. if (ret == false) {
  495. Stop();
  496. return eMpcFailed;
  497. }
  498. //下发速度
  499. if (fabs(target_yaw_diff) < 0.2 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  500. printf(" MPC_rotate | refer target completed\\n,cuv:%f\n", target_yaw_diff);
  501. Stop();
  502. return eMpcSuccess;
  503. } else {
  504. const int down_count = 3;
  505. double v[down_count] = {0, 0, 0};
  506. double w[down_count] = {out[0], out[1], out[2]};
  507. SendMoveCmd(0, move_mode_, eRotation, v, w);
  508. actionType_ = eRotation;
  509. double input_w = timedA_.Get();
  510. /*
  511. for (int i = 0; i < down_count; ++i) {
  512. printf(" MPC_rotate |P[%d], input anguar:%f, down:%f(%f), diff:%f anyDirect:%d\n",
  513. i, input_w, w[i], w[i] / M_PI * 180, target_yaw_diff, directions);
  514. }*/
  515. }
  516. continue;
  517. }
  518. return eMpcFailed;
  519. }
  520. bool Navigation::MoveToTarget(NavMessage::PathNode node, Navigation::stLimit limit_v, Navigation::stLimit limit_w,
  521. bool anyDirect, bool enable_rotate, int clampLifterAction) {
  522. if (IsArrived(node)) {
  523. return true;
  524. }
  525. bool already_in_mpc = false;
  526. while (cancel_ == false) {
  527. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  528. if (PoseTimeout()) {
  529. printf(" navigation Error:Pose is timeout \n");
  530. break;
  531. }
  532. Pose2d current = timedPose_.Get();
  533. if (IsArrived(node)) {
  534. break;
  535. }
  536. //两个方向都无法到达目标点,旋转 朝向目标点
  537. bool x_enable = PossibleToTarget(node, false);
  538. bool y_enable = PossibleToTarget(node, true);
  539. bool enableTotarget = x_enable || y_enable;
  540. if (anyDirect == false) {
  541. enableTotarget = x_enable;
  542. }
  543. //巡线无法到达目标点
  544. Pose2d target(node.x(), node.y(), 0);
  545. Pose2d targetInCurrent = Pose2d::relativePose(target, current);
  546. if (enableTotarget == false) {
  547. int directions = Direction::eForward;
  548. if(anyDirect)
  549. directions = Direction::eForward | Direction::eRight | Direction::eLeft | Direction::eBackward;
  550. bool adjustRet = AdjustReferToTarget(target, limit_w,directions);
  551. if (adjustRet == false)
  552. return false;
  553. /*if (already_in_mpc && enable_rotate == false) {
  554. printf(" Move to node failed ,impossible to target and rotate is not enabled\n");
  555. return false;
  556. }
  557. int directions = Direction::eForward;
  558. if (anyDirect)
  559. directions = Direction::eForward | Direction::eBackward |
  560. Direction::eLeft | Direction::eRight;
  561. if (RotateReferToTarget(node, limit_w, directions) != eMpcSuccess) {
  562. std::cout << " Rotate refer to target failed,target: " << target <<
  563. " directions: " << std::hex << directions << " line_: " << __LINE__ << std::endl;
  564. return false;
  565. }
  566. continue;*/
  567. }
  568. already_in_mpc = true;
  569. MpcResult ret = MpcToTarget(node, limit_v, anyDirect, clampLifterAction);
  570. if (ret == eMpcSuccess) {
  571. printf(" MPC to target:%f %f l:%f,w:%f theta:%f completed\n",
  572. node.x(), node.y(), node.l(), node.w(), node.theta());
  573. break;
  574. } else if (ret == eImpossibleToTarget) {
  575. printf(" MPC to target:%f %f l:%f,w:%f theta:%f impossible ,retry ...\n",
  576. node.x(), node.y(), node.l(), node.w(), node.theta());
  577. } else {
  578. return false;
  579. }
  580. }
  581. if (cancel_) {
  582. return false;
  583. }
  584. return true;
  585. }
  586. bool Navigation::execute_nodes(NavMessage::NewAction action, int space_id) {
  587. if (action.type() == 3 || action.type() == 4) //最优动作导航 or 导航中保证朝向严格一致
  588. {
  589. if (!parameter_.has_nodeangularlimit() || !parameter_.has_nodevelocitylimit() || action.pathnodes_size() == 0) {
  590. std::cout << "execute pathNodes failed ,Action invalid:" << action.DebugString() << std::endl;
  591. return false;
  592. }
  593. bool anyDirect = (action.type() == 3);
  594. //速度限制
  595. stLimit wmg_limit = {parameter_.nodeangularlimit().min(), parameter_.nodeangularlimit().max()};
  596. stLimit mpc_velocity = {parameter_.nodevelocitylimit().min(), parameter_.nodevelocitylimit().max()};
  597. /////////////////////////////////////////
  598. int clampLifterAction = 0;
  599. if (move_mode_ == eSingle) {
  600. clampLifterAction |= eByteClampHalfOpen;
  601. }else{
  602. clampLifterAction |= eByteLifterDown;
  603. }
  604. ////////////////////////////////////////////////////////////
  605. for (int i = 0; i < action.pathnodes_size(); ++i) {
  606. NavMessage::PathNode node = action.pathnodes(i);
  607. if (i + 1 < action.pathnodes_size()) {
  608. NavMessage::PathNode next = action.pathnodes(i + 1);
  609. Pose2d vec(next.x() - node.x(), next.y() - node.y(), 0);
  610. node.set_theta(Pose2d::vector2yaw(vec.x(), vec.y()) + M_PI / 2.0);
  611. node.set_l(0.03);
  612. node.set_w(0.20);
  613. } else {
  614. NavMessage::PathNode befor = action.pathnodes(i - 1);
  615. Pose2d vec(node.x() - befor.x(), node.y() - befor.y(), 0);
  616. node.set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  617. node.set_l(0.02);
  618. node.set_w(0.2);
  619. }
  620. int directions = Direction::eForward;
  621. if (anyDirect)
  622. directions = Direction::eForward | Direction::eBackward |
  623. Direction::eLeft | Direction::eRight;
  624. /*if (i > 0) {
  625. if (RotateReferToTarget(node, wmg_limit, directions) != eMpcSuccess) {
  626. printf(" before move node RotateReferToNone Failed!!\n");
  627. return false;
  628. }
  629. }*/
  630. if (MoveToTarget(node, mpc_velocity, wmg_limit, anyDirect, true, clampLifterAction) == false)
  631. return false;
  632. else
  633. isInSpace_ = false;
  634. }
  635. if (cancel_ == true) {
  636. return false;
  637. }
  638. return true;
  639. }
  640. printf("execute PathNode failed ,action type is not 3/4 :%d\n", action.type());
  641. return false;
  642. }
  643. Navigation::MpcResult Navigation::DoubleOutSpaceCorrectTheta() {
  644. if (move_mode_ != eDouble)
  645. return eMpcSuccess;
  646. int clampLifterAction=0;
  647. if(move_mode_==eDouble){
  648. clampLifterAction |= eLifterDown;
  649. }
  650. Pose2d init = timedPose_.Get();
  651. double diff1 = fabs(M_PI / 2.0 - init.theta());
  652. double diff2 = fabs(-M_PI / 2.0 - init.theta());
  653. double target = diff1 < diff2 ? M_PI / 2.0 : -M_PI / 2.0;
  654. stLimit limit_rotate = {3 * M_PI / 180.0, 15 * M_PI / 180.0};
  655. while (cancel_ == false) {
  656. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  657. Pose2d current = timedPose_.Get();
  658. double yawDiff = (target - 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(" DoubleOutSpaceCorrectTheta refer target completed\\n,cuv:%f\n", yawDiff);
  672. Stop();
  673. if(WaitClampLifterStatu(clampLifterAction)==false){
  674. return eMpcFailed;
  675. }
  676. return eMpcSuccess;
  677. } else {
  678. const int down_count = 3;
  679. double v[down_count] = {0, 0, 0};
  680. double w[down_count] = {out[0], out[1], out[2]};
  681. SendMoveCmd(clampLifterAction, move_mode_, eRotation, v, w);
  682. actionType_ = eRotation;
  683. printf(" DoubleOutSpaceCorrectTheta | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  684. timedA_.Get(), out[0], out[0] / M_PI * 180, yawDiff);
  685. }
  686. continue;
  687. }
  688. return eMpcFailed;
  689. }
  690. Navigation::MpcResult Navigation::RotateBeforeIntoExport() {
  691. if (move_mode_ != eDouble)
  692. return eMpcSuccess;
  693. Pose2d init = timedPose_.Get();
  694. double diff1 = fabs(M_PI / 2.0 - init.theta());
  695. double diff2 = fabs(-M_PI / 2.0 - init.theta());
  696. double target = diff1 > diff2 ? M_PI / 2.0 : -M_PI / 2.0;
  697. stLimit limit_rotate = {3 * M_PI / 180.0, 15 * M_PI / 180.0};
  698. while (cancel_ == false) {
  699. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  700. Pose2d current = timedPose_.Get();
  701. double yawDiff = (target - current.theta());
  702. //一次变速
  703. std::vector<double> out;
  704. bool ret;
  705. TimerRecord::Execute([&, this]() {
  706. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  707. }, "Rotation_mpc_once");
  708. if (ret == false) {
  709. Stop();
  710. return eMpcFailed;
  711. }
  712. //下发速度
  713. if (fabs(yawDiff) < 0.2 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  714. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  715. Stop();
  716. return eMpcSuccess;
  717. } else {
  718. const int down_count = 3;
  719. double v[down_count] = {0, 0, 0};
  720. double w[down_count] = {out[0], out[1], out[2]};
  721. SendMoveCmd(0, move_mode_, eRotation, v, w);
  722. actionType_ = eRotation;
  723. // printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  724. // timedA_.Get(), out[0], out[0] / M_PI * 180, yawDiff);
  725. }
  726. continue;
  727. }
  728. return eMpcFailed;
  729. }
  730. Navigation::MpcResult
  731. Navigation::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase,
  732. NavMessage::PathNode &target, int clampLifterAction) {
  733. // if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  734. if (timedPose_.timeout()) {
  735. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  736. return eMpcFailed;
  737. }
  738. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  739. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  740. double acc_angular = 20 * M_PI / 180.0;
  741. double dt = 0.1;
  742. Pose2d current = timedPose_.Get();
  743. double x = space.x();
  744. double y = space.y();
  745. Pose2d vec(x - current.x(), y - current.y(), 0);
  746. double vecTheta = Pose2d::vector2yaw(vec.x(), vec.y());
  747. bool isFront = isFront_.Get();
  748. printf(" front__:%d theta:%f\n", isFront, vecTheta);
  749. if (vecTheta < 0.)
  750. isFront = !isFront;
  751. //如果是后车,计算目标点(车位点-轴距)
  752. if (isFront == false) {
  753. printf("后车 RotateBeforeEnterSpace | , __LINE__ = %d\n", __LINE__);
  754. if (move_mode_ == eSingle) {
  755. x -= wheelbase * cos(vecTheta);
  756. y -= wheelbase * sin(vecTheta);
  757. printf("确定车位点:eSingle\n");
  758. }
  759. } else { //当后车先到,倒车入库
  760. printf("前车RotateBeforeEnterSpace | __LINE__ = %d\n", __LINE__);
  761. //rotated.mutable_theta() = space.theta();
  762. //rotated = rotated.rotate(rotated.x(), rotated.y(), M_PI);
  763. }
  764. Pose2d spaceInCurrent = Pose2d::relativePose(Pose2d(space.x(), space.y(), 0), current);
  765. target.set_x(x);
  766. target.set_y(y);
  767. target.set_theta(current.theta());
  768. target.set_l(0.02);
  769. target.set_w(0.05);
  770. target.set_id(space.id());
  771. printf("RotateBeforeEnterSpace | target:[x:%f,y:%f,theta:%f]\n", x, y, target.theta());
  772. // //整车协调的时候,保持前后与车位一致即可
  773. // if (move_mode_ == eDouble) {
  774. // double yawDiff1 = (rotated - timedPose_.Get()).theta();
  775. // double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  776. // if (fabs(yawDiff1) < fabs(yawDiff2)) {
  777. // rotated = rotated + Pose2d(0, 0, M_PI);
  778. // }
  779. // }
  780. while (cancel_ == false) {
  781. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  782. Pose2d current_now = timedPose_.Get();
  783. if (spaceInCurrent.x() < 0.)
  784. current_now = current_now.rotate(current_now.x(), current_now.y(), M_PI);
  785. double yawDiff = vecTheta - current_now.theta();
  786. //一次变速
  787. std::vector<double> out;
  788. bool ret;
  789. TimerRecord::Execute([&, this]() {
  790. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  791. }, "Rotation_mpc_once");
  792. if (ret == false) {
  793. Stop();
  794. return eMpcFailed;
  795. }
  796. //下发速度
  797. if (fabs(yawDiff) < 0.3 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  798. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  799. Stop();
  800. return eMpcSuccess;
  801. } else {
  802. const int down_count = 3;
  803. double v[down_count] = {0, 0, 0};
  804. double w[down_count] = {out[0], out[1], out[2]};
  805. SendMoveCmd(clampLifterAction, move_mode_, eRotation, v, w);
  806. actionType_ = eRotation;
  807. // printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  808. // timedA_.Get(), out[0], out[0] / M_PI * 180, yawDiff);
  809. }
  810. continue;
  811. }
  812. return eMpcFailed;
  813. }
  814. bool Navigation::IsUperSpace(NavMessage::PathNode spaceNode) {
  815. if (spaceNode.id().find("S") < 0)
  816. return false;
  817. std::string id = spaceNode.id().substr(1, spaceNode.id().length() - 1);
  818. int space_id = stoi(id);
  819. if (space_id > 99 && space_id < 1000) {
  820. return true;
  821. }
  822. return false;
  823. }
  824. int Navigation::GetSpaceId(NavMessage::PathNode spaceNode) {
  825. if (spaceNode.id().find("S") >= 0) {
  826. std::string id = spaceNode.id().substr(1, spaceNode.id().length() - 1);
  827. int space_id = stoi(id);
  828. return space_id;
  829. } else {
  830. return -1;
  831. }
  832. }
  833. bool Navigation::TargetIsEntrance(NavMessage::PathNode node) {
  834. return node.id().find("S1101") != -1;
  835. }
  836. bool Navigation::TargetIsExport(NavMessage::PathNode node) {
  837. return node.id().find("S1101") != -1;
  838. }
  839. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  840. if (action.type() != 1 && action.type() != 2) {
  841. printf(" Inout_space failed : msg action type must ==1\n ");
  842. return false;
  843. }
  844. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  845. !action.has_streetnode()) {
  846. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  847. return false;
  848. }
  849. if (PoseTimeout() == true) {
  850. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  851. return false;
  852. }
  853. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  854. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  855. //入库,起点为streetNode
  856. if (action.type() == 1) {
  857. Pose2d vec(action.spacenode().x() - action.streetnode().x(),
  858. action.spacenode().y() - action.streetnode().y(),
  859. 0);
  860. action.mutable_streetnode()->set_l(0.02);
  861. action.mutable_streetnode()->set_w(0.2);
  862. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()) + M_PI / 2.0);
  863. /*
  864. * 是否需要添加判断,距离较远才先运动到马路点=============================
  865. */
  866. int clampLifterAction = 0;
  867. if (IsUperSpace(action.spacenode())) { //进载车板库前提升机构上升
  868. clampLifterAction |= eByteLifterUp;
  869. } else { //否则下降
  870. clampLifterAction |= eByteLifterDown;
  871. }
  872. if (move_mode_ == eSingle)
  873. clampLifterAction |= eByteClampHalfOpen;
  874. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true, clampLifterAction) == false) {
  875. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  876. return false;
  877. }
  878. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  879. printf("inout ----------------------------------------\n");
  880. //取车流程,送车到出口前旋转180
  881. if (move_mode_ == eDouble && TargetIsExport(action.spacenode())) {
  882. Pose2d current_pose = timedPose_.Get();
  883. double current_theta = current_pose.theta();
  884. if (RotateBeforeIntoExport() != eMpcSuccess) {
  885. printf("Rotate before in entrance failed\n");
  886. return false;
  887. }
  888. }
  889. //计算车位朝向
  890. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  891. //计算当前车是进入车位1点还是2点,
  892. NavMessage::PathNode new_target;
  893. bool retRotate = RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(),
  894. new_target, clampLifterAction) == eMpcSuccess;
  895. if (retRotate == false) {
  896. return false;
  897. }
  898. //去出入口,等内门打开
  899. if (TargetIsExport(action.spacenode()) || TargetIsEntrance(action.spacenode())) {
  900. printf(" Wait Inside door opened.......\n");
  901. int port_id = GetPortIDBySpace(action.spacenode());
  902. if (WaitInsideDoorCompleted(port_id, eDoorOpened) == false ) {
  903. return false;
  904. }
  905. printf(" Wait Inside door opened completed!!!\n");
  906. } else {
  907. //去车位点
  908. if (IsUperSpace(action.spacenode())) {
  909. printf(" Wait Carrier down.......\n");
  910. if (WaitCarrierCompleted(GetSpaceId(action.spacenode()), eCarrierDown) == false) {
  911. return false;
  912. }
  913. printf(" Wait Carrier down completed!!!\n");
  914. } else {
  915. if(!TargetIsExport(action.spacenode()) && !TargetIsEntrance(action.spacenode())) {
  916. printf(" Wait Carrier up.......\n");
  917. if (WaitCarrierCompleted(GetSpaceId(action.spacenode()), eCarrierUp) == false) {
  918. return false;
  919. }
  920. printf(" Wait Carrier up completed!!!\n");
  921. }else{
  922. printf(" Wait inside door open.......\n");
  923. if(WaitInsideDoorCompleted(GetSpaceId(action.spacenode()),eDoorOpened)==false)
  924. return false;
  925. printf("inside door opened\n");
  926. }
  927. }
  928. }
  929. if (IsUperSpace(action.spacenode())) {
  930. if(lifter_rise()== false){
  931. return false;
  932. }
  933. }
  934. // 单车进库必须全打开
  935. if (move_mode_ == eSingle){
  936. if (clamp_fully_open() == false) {
  937. return false;
  938. }
  939. }
  940. //入库
  941. Pose2d rotated = timedPose_.Get();
  942. double x = new_target.x();
  943. double y = new_target.y();
  944. Pose2d vecSpace(x - rotated.x(), y - rotated.y(), 0);
  945. rotated.mutable_theta() = Pose2d::vector2yaw(vecSpace.x(), vecSpace.y());
  946. bool isFront = isFront_.Get();
  947. if (rotated.theta() < 0.)
  948. isFront = !isFront;
  949. // 后进车,且单车,且先进车已过后进车的目标点
  950. Pose2d target(new_target.x(), new_target.y(), 0);
  951. if (isFront == false && move_mode_ == eSingle) {
  952. while (true) {
  953. if (cancel_) {
  954. return false;
  955. }if(move_mode_==eDouble){
  956. clampLifterAction |= eLifterDown;
  957. }
  958. Pose2d current = timedPose_.Get();
  959. Pose2d brother_pose = timedBrotherPose_.Get();
  960. Pose2d vecCurent2T = current - target;
  961. Pose2d vecBrother2T = brother_pose - target;
  962. double dot = vecCurent2T.x() * vecBrother2T.x() + vecCurent2T.y() * vecBrother2T.y();
  963. if (timedBrotherNavStatu_.Get().in_space() == true && dot < 0.) {
  964. printf("先进车已经过后进车目标点,后进车停止等待 dot=%f\n", dot);
  965. break;
  966. }
  967. printf(" 后车 等待 先车先入库。。。\n");
  968. usleep(1000 * 500);
  969. continue;
  970. }
  971. }
  972. isInSpace_ = true;
  973. printf("Enter space beg...\n");
  974. clampLifterAction = 0;
  975. if (IsUperSpace(action.spacenode())) { //进载车板库前提升机构上升
  976. clampLifterAction |= eByteLifterUp;
  977. } else { //否则下降
  978. clampLifterAction |= eByteLifterDown;
  979. }
  980. printf(" clampLifter_statu1 :%d\n", clampLifterAction);
  981. if (move_mode_ == eSingle)
  982. clampLifterAction |= eByteClampFullyOpen; //////// 后续再改为全打开位
  983. else
  984. clampLifterAction |= eByteCLampClose;
  985. printf(" clampLifter_statu2 :%d\n", clampLifterAction);
  986. bool retMove = MoveToTarget(new_target, limit_v, limit_w, true, false, clampLifterAction);
  987. if (retMove == false) {
  988. printf(" Enter space:%s failed. type:%d\n", action.spacenode().id().data(), action.type());
  989. return false;
  990. }
  991. } else if (action.type() == 2) {
  992. // LogWriter("Out space beg-");
  993. // 计算当前位置,在车位点到马路点的连线方向上,与车位点的相对位姿
  994. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  995. action.streetnode().y() - action.spacenode().y(), 0);
  996. float theta = Pose2d::vector2yaw(vec.x(), vec.y());
  997. Pose2d space(action.spacenode().x(), action.spacenode().y(), theta);
  998. Pose2d current = timedPose_.Get();
  999. Pose2d diff = Pose2d::abs(Pose2d::relativePose(Pose2d(current.x(), current.y(), 0), space));
  1000. if (diff.x() < 0.05 || diff.y() < 0.10) {
  1001. //出库,移动到马路点,允许自由方向,不允许中途旋转
  1002. action.mutable_streetnode()->set_theta(theta);
  1003. action.mutable_streetnode()->set_l(0.03);
  1004. action.mutable_streetnode()->set_w(0.2);
  1005. std::cout << " Out space target street node:" << space << std::endl;
  1006. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  1007. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  1008. return false;
  1009. }
  1010. } else {
  1011. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  1012. << std::endl;
  1013. return false;
  1014. }
  1015. isInSpace_ = false;
  1016. //摆正
  1017. if (DoubleOutSpaceCorrectTheta() != eMpcSuccess)
  1018. return false;
  1019. }
  1020. return true;
  1021. }
  1022. /* current_to_target: current_to_target.theta()为所需旋转的角度
  1023. * */
  1024. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  1025. bool all_direct) {
  1026. if (PoseTimeout() == true) {
  1027. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  1028. return false;
  1029. }
  1030. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  1031. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  1032. return false;
  1033. }
  1034. Pose2d pose = timedPose_.Get();
  1035. double line_velocity = timedV_.Get(); //从plc获取状态
  1036. double angular_velocity = timedA_.Get();
  1037. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  1038. statu[0] = pose.x();
  1039. statu[1] = pose.y();
  1040. statu[2] = pose.theta();
  1041. statu[3] = line_velocity;
  1042. statu[4] = angular_velocity;
  1043. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  1044. double obs_w = 0.95;//0.85;
  1045. double obs_h = 1.55;//1.45;
  1046. MpcError ret = failed;
  1047. Pose2d brother = timedBrotherPose_.Get();
  1048. Pose2d brother_in_self = Pose2d::relativePose(brother, pose);
  1049. // std::cout<<" brother_in_self: "<<brother_in_self<<std::endl;
  1050. if (move_mode_ == eDouble) {
  1051. brother_in_self = Pose2d(pose.x() + 100, pose.y() + 100, 0);
  1052. }
  1053. RotateMPC MPC(brother_in_self, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  1054. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  1055. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  1056. ret = MPC.solve(current_to_target, statu, parameter, out);
  1057. if (ret == no_solution) {
  1058. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  1059. out.clear();
  1060. out.push_back(0);
  1061. out.push_back(0);
  1062. out.push_back(0);
  1063. } else {
  1064. // double min_angular_velocity = 1.0 / 180 * M_PI;
  1065. double min_angular_velocity = 0.01;
  1066. const int down_count = 3;
  1067. for (int i = 0; i < down_count; ++i) {
  1068. if (fabs(out[i]) < min_angular_velocity) {
  1069. if (out[i] > 0)
  1070. out[i] = min_angular_velocity;
  1071. else if (out[i] < 0)
  1072. out[i] = -min_angular_velocity;
  1073. else {}
  1074. }
  1075. }
  1076. }
  1077. return ret == success || ret == no_solution;
  1078. }
  1079. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  1080. if (PoseTimeout() == true) {
  1081. printf(" MPC once Error:Pose is timeout \n");
  1082. return false;
  1083. }
  1084. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  1085. printf(" MPC once Error:V/A is timeout \n");
  1086. return false;
  1087. }
  1088. if (traj.size() == 0) {
  1089. printf("traj size ==0\n");
  1090. return false;
  1091. }
  1092. Pose2d pose = timedPose_.Get();
  1093. double velocity = timedV_.Get(); //从plc获取状态
  1094. double angular = timedA_.Get();
  1095. // float diffYaw1=timedPose_.Get().m_diffYaw1;
  1096. // float diffYaw2=timedPose_.Get().m_diffYaw2;
  1097. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  1098. statu[0] = pose.x();
  1099. statu[1] = pose.y();
  1100. statu[2] = pose.theta();
  1101. statu[3] = velocity;
  1102. statu[4] = angular;
  1103. Trajectory optimize_trajectory;
  1104. Trajectory selected_trajectory;
  1105. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  1106. double obs_w = obs_w_;// 0.95;//0.85;
  1107. double obs_h = obs_h_;// 1.55;//1.45;
  1108. if (directY == true) {
  1109. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  1110. statu[2] += M_PI / 2;
  1111. if (statu[2] > M_PI)
  1112. statu[2] -= 2 * M_PI;
  1113. mpc_parameter = parameter_.y_mpc_parameter();
  1114. obs_w = obs_h_;// 0.95;//0.85;
  1115. obs_h = obs_w_;//1.55;//1.45;
  1116. }
  1117. Pose2d brother = timedBrotherPose_.Get();
  1118. Pose2d relative = Pose2d::relativePose(brother, pose);
  1119. if (directY)
  1120. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  1121. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  1122. std::cout << "pose:" << pose << ", brother:" << brother << ", relative:" << relative << std::endl;
  1123. if (move_mode_ == eDouble)
  1124. relative = Pose2d(-1e8, -1e8, 0);
  1125. MpcError ret = failed;
  1126. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  1127. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  1128. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  1129. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  1130. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  1131. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  1132. out, selected_trajectory, optimize_trajectory, directY);
  1133. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  1134. if (ret == no_solution) {
  1135. printf(" mpc solve no solution set v/w = 0\n");
  1136. out.clear();
  1137. out.push_back(0);
  1138. out.push_back(0);
  1139. out.push_back(0);
  1140. out.push_back(0);
  1141. out.push_back(0);
  1142. out.push_back(0);
  1143. }
  1144. //
  1145. // diff_yaw.push_back(0);
  1146. // diff_yaw.push_back(0);
  1147. predict_traj_.reset(optimize_trajectory, 1);
  1148. selected_traj_.reset(selected_trajectory, 1);
  1149. return ret == success || ret == no_solution;
  1150. }
  1151. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  1152. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  1153. printf(" pose / v / w timeout,IsArrived return false\n");
  1154. return false;
  1155. }
  1156. Pose2d current = timedPose_.Get();
  1157. double x = current.x();
  1158. double y = current.y();
  1159. double tx = targetNode.x();
  1160. double ty = targetNode.y();
  1161. double l = fabs(targetNode.l());
  1162. double w = fabs(targetNode.w());
  1163. double theta = -targetNode.theta();
  1164. if (newUnfinished_cations_.front().type() == 1 && targetNode.id().find('S') != -1) {//入库只判断前后方向上是否到达
  1165. tx = x;
  1166. }
  1167. if (l < 1e-10 || w < 1e-10) {
  1168. printf("IsArrived: Error target l or w == 0\n");
  1169. return false;
  1170. }
  1171. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  1172. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  1173. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  1174. if (fabs(timedV_.Get()) < 0.05 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  1175. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  1176. return true;
  1177. }
  1178. }
  1179. return false;
  1180. }
  1181. void Navigation::SendMoveCmd(int mode, ActionType type,
  1182. double v, double angular) {
  1183. if (monitor_) {
  1184. monitor_->set_speed(mode, type, v, angular);
  1185. if (type == eRotation)
  1186. RWheel_position_ = eR;
  1187. if (type == eVertical)
  1188. RWheel_position_ = eX;
  1189. if (type == eHorizontal)
  1190. RWheel_position_ = eY;
  1191. }
  1192. }
  1193. void Navigation::SendMoveCmd(int clampLifterAction, int mode, ActionType type,
  1194. double v[], double angular[]) {
  1195. if (monitor_) {
  1196. monitor_->set_ToAgvCmd(clampLifterAction, mode, type, v, angular);
  1197. if (type == eRotation)
  1198. RWheel_position_ = eR;
  1199. if (type == eVertical)
  1200. RWheel_position_ = eX;
  1201. if (type == eHorizontal)
  1202. RWheel_position_ = eY;
  1203. }
  1204. }
  1205. /*void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[],
  1206. int space_id, double distance,double Y1,double Y2) {
  1207. if (monitor_) {
  1208. monitor_->set_ToAgvCmd(mode, type, v, angular, 0, space_id, distance,Y1,Y2);
  1209. if (type == eRotation)
  1210. RWheel_position_ = eR;
  1211. if (type == eVertical)
  1212. RWheel_position_ = eX;
  1213. if (type == eHorizontal)
  1214. RWheel_position_ = eY;
  1215. }
  1216. }*/
  1217. void Navigation::SendMoveCmd(int clampLifterAction, int mode, ActionType type, double v[], double angular[],
  1218. int space_id, double distance, double Y1, double Y2) {
  1219. if (monitor_) {
  1220. monitor_->set_ToAgvCmd(clampLifterAction, mode, type, v, angular, 0, space_id, distance, Y1, Y2);
  1221. if (type == eRotation)
  1222. RWheel_position_ = eR;
  1223. if (type == eVertical)
  1224. RWheel_position_ = eX;
  1225. if (type == eHorizontal)
  1226. RWheel_position_ = eY;
  1227. }
  1228. }
  1229. //void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[], double yaw_diff[] ,int space_id, double distance) {
  1230. // if (monitor_) {
  1231. // monitor_->set_ToAgvCmd(mode, type, v, angular, yaw_diff ,0,space_id, distance);
  1232. // if (type == eRotation)
  1233. // RWheel_position_ = eR;
  1234. // if (type == eVertical)
  1235. // RWheel_position_ = eX;
  1236. // if (type == eHorizontal)
  1237. // RWheel_position_ = eY;
  1238. // }
  1239. //}
  1240. bool Navigation::clamp_close() {
  1241. if (monitor_) {
  1242. printf("Clamp closing...\n");
  1243. monitor_->clamp_close(move_mode_);
  1244. actionType_ = eClampClose;
  1245. while (cancel_ == false) {
  1246. if (timed_clamp_.timeout()) {
  1247. printf("timed clamp is timeout\n");
  1248. return false;
  1249. }
  1250. if (timed_clamp_.Get() == eClosed)
  1251. return true;
  1252. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1253. monitor_->clamp_close(move_mode_);
  1254. actionType_ = eClampClose;
  1255. }
  1256. return false;
  1257. }
  1258. return false;
  1259. }
  1260. bool Navigation::clamp_half_open() {
  1261. if (monitor_) {
  1262. printf("Clamp openning...\n");
  1263. monitor_->clamp_half_open(move_mode_);
  1264. actionType_ = eClampHalfOpen;
  1265. while (cancel_ == false) {
  1266. if (timed_clamp_.timeout()) {
  1267. printf("timed clamp is timeout\n");
  1268. return false;
  1269. }
  1270. if (timed_clamp_.Get() == eHalfOpened)
  1271. return true;
  1272. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1273. monitor_->clamp_half_open(move_mode_);
  1274. actionType_ = eClampHalfOpen;
  1275. }
  1276. return false;
  1277. }
  1278. return false;
  1279. }
  1280. bool Navigation::singleClamp_fullyOpen_until_inspaceBegin() {
  1281. if (move_mode_ == eDouble) {
  1282. return true;
  1283. }
  1284. while (cancel_ == false) {
  1285. if (timedV_.timeout()) {
  1286. return false;
  1287. }
  1288. if (fabs(timedV_.Get()) < 0.02) {
  1289. usleep(1000 * 300);
  1290. continue;
  1291. }
  1292. break;
  1293. }
  1294. return clamp_fully_open();
  1295. };
  1296. bool Navigation::clamp_fully_open() {
  1297. if (monitor_) {
  1298. if (timed_clamp_.Get() == eFullyOpened)
  1299. return true;
  1300. printf("Clamp fully openning...\n");
  1301. monitor_->clamp_fully_open(move_mode_);
  1302. actionType_ = eClampFullyOpen;
  1303. while (cancel_ == false) {
  1304. if (timed_clamp_.timeout()) {
  1305. printf("timed clamp is timeout\n");
  1306. return false;
  1307. }
  1308. if (timed_clamp_.Get() == eFullyOpened)
  1309. return true;
  1310. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1311. monitor_->clamp_fully_open(move_mode_);
  1312. actionType_ = eClampFullyOpen;
  1313. }
  1314. return false;
  1315. }
  1316. return false;
  1317. }
  1318. bool Navigation::lifter_rise() {
  1319. if (monitor_) {
  1320. printf("Lifter upping...\n");
  1321. actionType_ = eLifterRise;
  1322. if (timed_lifter_.Get() == eRose)
  1323. return true;
  1324. if (timed_lifter_.Get() != eRose) {
  1325. if (timed_clamp_.Get() != eHalfOpened && timed_clamp_.Get() != eClosed) {
  1326. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  1327. if (!clamp_half_open()) {
  1328. printf(" clamp half open failed\n");
  1329. return false;
  1330. }
  1331. }
  1332. }
  1333. while (cancel_ == false) {
  1334. if (timed_lifter_.timeout()) {
  1335. printf("timed lifter is timeout\n");
  1336. return false;
  1337. }
  1338. if (timed_lifter_.timeout()) {
  1339. return false;
  1340. }
  1341. if (timed_lifter_.Get() == eRose)
  1342. return true;
  1343. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1344. monitor_->lifter_rise(move_mode_);
  1345. actionType_ = eLifterRise;
  1346. }
  1347. return false;
  1348. }
  1349. return false;
  1350. }
  1351. bool Navigation::lifter_down() {
  1352. if (monitor_) {
  1353. printf("Lifter downing...\n");
  1354. if (timed_lifter_.Get() == eDowned)
  1355. return true;
  1356. actionType_ = eLifterDown;
  1357. if (timed_lifter_.Get() != eDowned) {
  1358. if (timed_clamp_.Get() != eHalfOpened && timed_clamp_.Get() != eClosed) {
  1359. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  1360. if (!clamp_half_open()) {
  1361. printf(" clamp half open failed\n");
  1362. return false;
  1363. }
  1364. }
  1365. }
  1366. while (cancel_ == false) {
  1367. if (timed_lifter_.timeout()) {
  1368. printf("timed lifter is timeout\n");
  1369. return false;
  1370. }
  1371. if (timed_lifter_.timeout()) {
  1372. return false;
  1373. }
  1374. if (timed_lifter_.Get() == eDowned)
  1375. return true;
  1376. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1377. monitor_->lifter_down(move_mode_);
  1378. actionType_ = eLifterDown;
  1379. }
  1380. return false;
  1381. }
  1382. return false;
  1383. }
  1384. bool Navigation::WaitInsideDoorCompleted(int doorID, DoorStatu status) {
  1385. while (cancel_ == false) {
  1386. if (timed_Door_.timeout()) {
  1387. return false;
  1388. }
  1389. if (timed_Door_.Get()[doorID].second == status) {
  1390. return true;
  1391. }
  1392. usleep(1000 * 100);
  1393. printf("waiting door_id(ins):%d, to:%d...\n",doorID, status);
  1394. }
  1395. if (cancel_ == true) {
  1396. return false;
  1397. }
  1398. return false;
  1399. }
  1400. bool Navigation::WaitCarrierCompleted(int spaceID, CarrierStatu statu) {
  1401. while (cancel_ == false) {
  1402. int region_id=-1, carrier_no=-1;
  1403. SpaceNo2CarrierNo(spaceID, region_id, carrier_no);
  1404. if (timed_Carrier_[region_id].timeout()) {
  1405. return false;
  1406. }
  1407. CarrierStatu cur_ = GetCarrierStatusBySpaceID(spaceID);
  1408. if ( cur_ == statu){
  1409. return true;
  1410. }
  1411. usleep(1000 * 100);
  1412. printf("waiting space_id:%d, carrier_id[%d,%d] to:%d, cur:%d...\n",spaceID, region_id, carrier_no, statu, cur_);
  1413. }
  1414. if (cancel_ == true) {
  1415. return false;
  1416. }
  1417. return false;
  1418. }
  1419. bool Navigation::WaitClampLifterStatu(int clampLifterAction) {
  1420. int currentStatu = 0;
  1421. while (cancel_ == false) {
  1422. if (timed_clamp_.timeout() || timed_lifter_.timeout()) {
  1423. return false;
  1424. }
  1425. if (timed_clamp_.Get() == eClosed) {
  1426. currentStatu |= eByteCLampClose;
  1427. }
  1428. if (timed_clamp_.Get() == eHalfOpened) {
  1429. currentStatu |= eByteClampHalfOpen;
  1430. }
  1431. if (timed_clamp_.Get() == eFullyOpened) {
  1432. currentStatu |= eByteClampFullyOpen;
  1433. }
  1434. if (timed_lifter_.Get() == eRose) {
  1435. currentStatu |= eByteLifterUp;
  1436. }
  1437. if (timed_lifter_.Get() == eDowned) {
  1438. currentStatu |= eByteLifterDown;
  1439. }
  1440. printf(" current statu:%d action status:%d last:%d\n",
  1441. currentStatu,clampLifterAction,currentStatu&clampLifterAction);
  1442. if((currentStatu&clampLifterAction) == clampLifterAction)
  1443. return true;
  1444. double down_v[3] = {0, 0, 0};//下发线速度0
  1445. double down_w[3] = {0, 0, 0};//下发角速度0
  1446. SendMoveCmd(clampLifterAction, move_mode_, actionType_, down_v, down_w);
  1447. usleep(1000 * 100);
  1448. }
  1449. if (cancel_ == true) {
  1450. return false;
  1451. }
  1452. return false;
  1453. }
  1454. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v,
  1455. bool autoDirect, int clampLifterAction) {
  1456. if (IsArrived(node)) {
  1457. printf(" current already in target completed !!!\n");
  1458. return eMpcSuccess;
  1459. }
  1460. if (inited_ == false) {
  1461. printf(" navigation has not inited\n");
  1462. return eMpcFailed;
  1463. }
  1464. if (PoseTimeout()) {
  1465. printf(" navigation Error:Pose is timeout \n");
  1466. return eMpcFailed;
  1467. }
  1468. Pose2d current = timedPose_.Get();
  1469. Pose2d target(node.x(), node.y(), 0);
  1470. //生成轨迹
  1471. Trajectory traj = Trajectory::create_line_trajectory(current, target, 0.1);
  1472. if (traj.size() == 0)
  1473. return eMpcSuccess;
  1474. //判断 巡线方向
  1475. bool directY = false;
  1476. if (autoDirect) {
  1477. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  1478. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  1479. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  1480. directY = true;
  1481. } else {
  1482. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  1483. }
  1484. if (!PossibleToTarget(node, directY)) {
  1485. directY = !directY;
  1486. }
  1487. }
  1488. printf(" exec along autoDirect:%d\n", autoDirect);
  1489. //文件log
  1490. std::ofstream ofs; //创建流对象
  1491. std::string log_file_dir = "../data/";
  1492. time_t t = time(0);
  1493. char tmp[32] = {NULL};
  1494. strftime(tmp, sizeof(tmp), "%Y_%m_%d_%H_%M_%S.txt", localtime(&t));
  1495. std::string log_file_name = tmp;
  1496. ofs.open(log_file_dir + log_file_name, std::ios::app); //打开的地址和方式
  1497. auto beg_time = std::chrono::steady_clock::now();
  1498. while (cancel_ == false) {
  1499. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  1500. if (pause_ == true) {
  1501. //发送暂停消息
  1502. continue;
  1503. }
  1504. if (PoseTimeout()) {
  1505. printf(" navigation Error:Pose is timeout \n");
  1506. ofs.close();//关闭文件
  1507. return eMpcFailed;
  1508. }
  1509. if (timedV_.timeout() || timedA_.timeout()) {
  1510. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  1511. ofs.close();//关闭文件
  1512. return eMpcFailed;
  1513. }
  1514. //判断是否到达终点
  1515. if (IsArrived(node)) {
  1516. if (Stop()) {
  1517. printf(" exec along completed !!!\n");
  1518. ofs.close();//关闭文件
  1519. bool waited = WaitClampLifterStatu(clampLifterAction);
  1520. if (waited) return eMpcSuccess;
  1521. else return eMpcFailed;
  1522. }
  1523. }
  1524. //一次变速
  1525. std::vector<double> out;
  1526. // std::vector<double> diff_yaw; //两车分别与整车的相对角度
  1527. bool ret;
  1528. TimerRecord::Execute([&, this]() {
  1529. ret = mpc_once(traj, limit_v, out, directY);
  1530. }, "mpc_once");
  1531. if (ret == false) {
  1532. SlowlyStop();
  1533. ofs.close();//关闭文件
  1534. return eMpcFailed;
  1535. }
  1536. const int down_count = 3;//下发前多少步
  1537. double down_v[down_count] = {out[0], out[2], out[4]};//下发线速度
  1538. double down_w[down_count] = {out[1], out[3], out[5]};//下发角速度
  1539. /*
  1540. * AGV 在终点附近低速巡航时,设置最小速度0.05
  1541. */
  1542. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  1543. if (directY) {
  1544. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  1545. }
  1546. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  1547. for (int i = 0; i < down_count; ++i) {
  1548. if (down_v[i] >= 0 && down_v[i] < limit_v.min)
  1549. down_v[i] = limit_v.min;
  1550. if (down_v[i] < 0 && down_v[i] > -limit_v.min)
  1551. down_v[i] = -limit_v.min;
  1552. }
  1553. }
  1554. //放大角速度
  1555. // out[1] *= 10;
  1556. //入库,行进方向上最后一米不纠偏
  1557. if (newUnfinished_cations_.front().type() == 1 && node.id().find('S') != -1) {//入库
  1558. if (fabs(target_in_agv.x()) < 0.5) {
  1559. printf("target_in_agv: %f", target_in_agv.x());
  1560. for (double &i: down_w) {
  1561. i = 0.0;
  1562. }
  1563. }
  1564. } else if (newUnfinished_cations_.front().type() == 2) {//出库,行进方向上开始一米不纠偏
  1565. Pose2d agv_in_space =
  1566. Pose2d::relativePose(
  1567. Pose2d(newUnfinished_cations_.front().spacenode().x(),
  1568. newUnfinished_cations_.front().spacenode().y(), 0),
  1569. timedPose_.Get());
  1570. if (fabs(agv_in_space.x()) < 0.3) {
  1571. printf("agv_in_space: %f", agv_in_space.x());
  1572. for (double &i: down_w) {
  1573. i = 0.0;
  1574. }
  1575. }
  1576. }
  1577. //下发速度
  1578. //printf(" nav input :%f out:%f\n",timedV_.Get(),out[0]);
  1579. //添加车位号,距目标点距离信息
  1580. double distance = Pose2d::distance(target_in_agv, Pose2d(0, 0, 0));
  1581. int space_id = 0;
  1582. if (node.id().find('S') != -1) {
  1583. std::string id = node.id().substr(1, node.id().length() - 1);
  1584. space_id = stoi(id);
  1585. }
  1586. double Y1 = 0.0, Y2 = 0.0;
  1587. if (move_mode_ == eDouble) {
  1588. double dy = -timeYawDiff1_.Get();
  1589. Y1 = 0.2 * (1.0 / (1 + exp(-16. * dy)) - 0.5) * 1.0;
  1590. Y2 = timeYawDiff2_.Get();
  1591. if (directY == false) {
  1592. Y1 = 0;
  1593. }
  1594. }
  1595. if (directY == false)
  1596. SendMoveCmd(clampLifterAction, move_mode_, eVertical, down_v, down_w, space_id, distance, Y1, Y2);
  1597. else
  1598. SendMoveCmd(clampLifterAction, move_mode_, eHorizontal, down_v, down_w, space_id, distance, Y1, Y2);
  1599. actionType_ = directY ? eHorizontal : eVertical;
  1600. //日志打印
  1601. std::string log_inf_line = std::to_string(move_mode_);
  1602. log_inf_line += " ";
  1603. log_inf_line += std::to_string(parameter_.x_mpc_parameter().shortest_radius()) + " " +
  1604. std::to_string(parameter_.x_mpc_parameter().acc_angular());
  1605. log_inf_line += " ";
  1606. Pose2d cur_pose = timedPose_.Get();//自身x,y,theta
  1607. log_inf_line += std::to_string(cur_pose.x()) + " " + std::to_string(cur_pose.y()) + " " +
  1608. std::to_string(cur_pose.theta());
  1609. log_inf_line += " ";
  1610. Pose2d cur_Bro_pose = timedBrotherPose_.Get();//另一节x,y,theta
  1611. log_inf_line += std::to_string(cur_Bro_pose.x()) + " " + std::to_string(cur_Bro_pose.y()) + " " +
  1612. std::to_string(cur_Bro_pose.theta());
  1613. log_inf_line += " ";
  1614. log_inf_line += std::to_string(out[0]) + " " + std::to_string(out[1]);//下发速度
  1615. log_inf_line += " ";
  1616. log_inf_line += std::to_string(out[2]) + " " + std::to_string(out[3]);//下发速度
  1617. log_inf_line += " ";
  1618. log_inf_line += std::to_string(out[4]) + " " + std::to_string(out[5]);//下发速度
  1619. log_inf_line += " ";
  1620. log_inf_line += std::to_string(Y1) + " " + std::to_string(Y2);//下发速度
  1621. log_inf_line += " ";
  1622. auto cur_time = std::chrono::steady_clock::now();
  1623. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(cur_time - beg_time);
  1624. double time =
  1625. double(duration.count()) * std::chrono::microseconds::period::num /
  1626. std::chrono::microseconds::period::den;
  1627. log_inf_line += std::to_string(time);
  1628. ofs << log_inf_line << std::endl; //一行日志的内容
  1629. /*
  1630. * 判断车辆是否在终点附近徘徊,无法到达终点
  1631. */
  1632. if (PossibleToTarget(node, directY) == false) {
  1633. printf(" --------------MPC imposible to target -------------------------------------------\n");
  1634. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  1635. SlowlyStop();
  1636. else
  1637. Stop();
  1638. ofs.close();//关闭文件
  1639. return eImpossibleToTarget;
  1640. }
  1641. }
  1642. ofs.close();//关闭文件
  1643. return eMpcFailed;
  1644. }
  1645. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  1646. if (timedPose_.timeout()) {
  1647. return false;
  1648. }
  1649. Pose2d current = timedPose_.Get();
  1650. if (targetNode.id().find('S') != -1) {
  1651. //车位点强制可到达
  1652. return true;
  1653. }
  1654. double tx = targetNode.x();
  1655. double ty = targetNode.y();
  1656. double l = fabs(targetNode.l());
  1657. double w = fabs(targetNode.w());
  1658. double theta = -targetNode.theta();
  1659. // double W = 2.45;
  1660. // double L = 1.3;
  1661. // double minYaw = 3 * M_PI / 180.0;
  1662. // if (move_mode_ == eDouble) {
  1663. // L = 1.3 + 2.7;
  1664. //
  1665. // }
  1666. double W = 2.5;
  1667. double L = 1.565;
  1668. double minYaw = 5 * M_PI / 180.0;
  1669. if (move_mode_ == eDouble) {
  1670. L = 1.565 + 2.78;
  1671. }
  1672. if (directY) {
  1673. current = current.rotate(current.x(), current.y(), M_PI / 2);
  1674. double t = W;
  1675. W = L;
  1676. L = t;
  1677. }
  1678. double minR = W / 2 + L / tan(minYaw);
  1679. //printf("l:%f,w:%f\n",l,w);
  1680. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  1681. bool nagY = false;
  1682. bool posiY = false;
  1683. for (int i = 0; i < poses.size(); ++i) {
  1684. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  1685. Pose2d relative = Pose2d::relativePose(rpos, current);
  1686. double ax = fabs(relative.x());
  1687. double ay = fabs(relative.y());
  1688. if (relative.y() > 0) posiY = true;
  1689. if (relative.y() < 0) nagY = true;
  1690. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  1691. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  1692. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  1693. minR,ax,ay);*/
  1694. return true;
  1695. } else if (nagY && posiY) {
  1696. // std::cout << "nagY && posiY" << std::endl;
  1697. return true;
  1698. }
  1699. // printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  1700. // relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  1701. // minR, ax, ay);
  1702. }
  1703. printf(" impossible to target:%f %f l:%f,w:%f theta:%f, ",
  1704. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  1705. printf(" current:%f, %f, %f\n",
  1706. current.x(), current.y(), current.theta());
  1707. return false;
  1708. }
  1709. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  1710. if (inited_ == false) {
  1711. response.set_ret(-1);
  1712. response.set_info("navigation has not inited");
  1713. printf(" navigation has not inited\n");
  1714. return;
  1715. }
  1716. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  1717. {
  1718. response.set_ret(-2);
  1719. response.set_info("navigation is running pls cancel before");
  1720. printf(" navigation is running pls cancel before\n");
  1721. return;
  1722. }
  1723. //add 先检查当前点与起点的距离
  1724. global_navCmd_ = cmd;
  1725. for (int i = 0; i < cmd.newactions_size(); ++i)
  1726. newUnfinished_cations_.push(cmd.newactions(i));
  1727. pause_ = false;
  1728. cancel_ = false;
  1729. running_ = true;
  1730. actionType_ = eReady;
  1731. printf("Navigation beg...\n");
  1732. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  1733. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  1734. NavMessage::NewAction act = newUnfinished_cations_.front();
  1735. NavMessage::NewAction last_act = newUnfinished_cations_.back();
  1736. if (act.type() == 1) { //入库
  1737. space_id_ = act.spacenode().id();
  1738. obs_w_ = 1.1;
  1739. obs_h_ = 1.87;
  1740. if (!execute_InOut_space(act)) {
  1741. printf(" In space failed\n");
  1742. isInSpace_ = false;
  1743. break;
  1744. }
  1745. } else if (act.type() == 2) { //出库
  1746. obs_w_ = 1.25;
  1747. obs_h_ = 1.87;
  1748. if (!execute_InOut_space(act)) {
  1749. printf(" out space failed\n");
  1750. break;
  1751. } else {
  1752. isInSpace_ = false;
  1753. }
  1754. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1755. obs_w_ = 1.1;
  1756. obs_h_ = 1.87;
  1757. int space_id = -1;
  1758. if (last_act.type() == 1) {
  1759. std::string id = last_act.spacenode().id().substr(1, last_act.spacenode().id().length() - 1);
  1760. space_id = stoi(id);
  1761. }
  1762. if (!execute_nodes(act, space_id)) {
  1763. printf(" street nav failed\n");
  1764. break;
  1765. }
  1766. } else if (act.type() == 5) {
  1767. printf(" 汽车模型导航....\n");
  1768. } else if (act.type() == 6) {//夹持
  1769. if (this->clamp_close() == false) {
  1770. printf("夹持failed ...\n");
  1771. break;
  1772. }
  1773. } else if (act.type() == 7) {
  1774. if (this->clamp_fully_open() == false) {
  1775. printf("打开夹持 failed...\n");
  1776. break;
  1777. }
  1778. } else if (act.type() == 8) { //切换模式
  1779. SwitchMode(act.changedmode(), act.wheelbase());
  1780. } else if (act.type() == 9) { //提升机构提升
  1781. if (this->lifter_rise() == false) {
  1782. printf("提升failed ...\n");
  1783. break;
  1784. }
  1785. } else if (act.type() == 10) { //提升机构下降
  1786. if (this->lifter_down() == false) {
  1787. printf("下降failed ...\n");
  1788. break;
  1789. }
  1790. } else {
  1791. printf(" action type invalid not handled !!\n");
  1792. break;
  1793. }
  1794. newUnfinished_cations_.pop();
  1795. }
  1796. actionType_ = eReady;
  1797. Stop();
  1798. if (cancel_ == true) {
  1799. response.set_ret(-3);
  1800. response.set_info("navigation canceled");
  1801. printf(" navigation canceled\n");
  1802. while (newUnfinished_cations_.empty() == false)
  1803. newUnfinished_cations_.pop();
  1804. } else {
  1805. if (newUnfinished_cations_.empty()) {
  1806. response.set_ret(0);
  1807. response.set_info("navigation completed!!!");
  1808. printf("navigation completed!!!\n");
  1809. } else {
  1810. response.set_ret(-4);
  1811. response.set_info("navigation Failed!!!!");
  1812. printf(" navigation Failed\n");
  1813. while (newUnfinished_cations_.empty() == false)
  1814. newUnfinished_cations_.pop();
  1815. }
  1816. }
  1817. running_ = false;
  1818. }
  1819. void Navigation::SwitchMode(int mode, float wheelBase) {
  1820. wheelBase_ = wheelBase;
  1821. printf("Child AGV can not Switch Mode\n");
  1822. }
  1823. float Navigation::compute_cuv(const Pose2d &target) {
  1824. float x = fabs(target.x());
  1825. float y = fabs(target.y());
  1826. float cuv = atan(y / (x + 1e-8));
  1827. //printf(" ---------------------- cuv:%f\n",cuv);
  1828. return cuv;
  1829. }
  1830. void Navigation::ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response) {
  1831. return;
  1832. // pause_ = false;
  1833. // cancel_ = false;
  1834. // running_ = true;
  1835. // actionType_ = eReady;
  1836. // printf("ManualOperation: %d | start...\n", cmd.operation_type());
  1837. //
  1838. // float symbol = cmd.velocity() < 0 ? -1 : 1; // 判断为正方向or负方向
  1839. // double velocity;
  1840. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1841. // switch (cmd.operation_type()) {
  1842. // case 1: // 旋转
  1843. // velocity = 2.0 * M_PI / 180.0 * symbol;
  1844. // SendMoveCmd(move_mode_, eRotation, 0, velocity);
  1845. // actionType_ = eRotation;
  1846. // printf(" ManualOperation: %d | input angular_v: %f, down: %f\n",
  1847. // cmd.operation_type(), timedA_.Get(), velocity);
  1848. // break;
  1849. // case 2: // X平移
  1850. // velocity = 1.0 * symbol;
  1851. // SendMoveCmd(move_mode_, eVertical, velocity, 0);
  1852. // actionType_ = eVertical;
  1853. // printf(" ManualOperation: %d | input line_v : %f, down: %f\n",
  1854. // cmd.operation_type(), timedV_.Get(), velocity);
  1855. // break;
  1856. // case 3: // Y平移
  1857. // velocity = 1.0 * symbol;
  1858. // SendMoveCmd(move_mode_, eHorizontal, velocity, 0);
  1859. // actionType_ = eHorizontal;
  1860. // printf(" ManualOperation: %d | input line_v: %f, down: %f\n",
  1861. // cmd.operation_type(), timedV_.Get(), velocity);
  1862. // break;
  1863. // default:
  1864. // break;
  1865. // }
  1866. //
  1867. // actionType_ = eReady;
  1868. // response.set_ret(-3);
  1869. // response.set_info("ManualOperation: %d, canceled!!!", cmd.operation_type());
  1870. // printf("ManualOperation: %d | canceled!!!\n", cmd.operation_type());
  1871. // running_ = false;
  1872. }
  1873. bool Navigation::IsMasterAGV() {
  1874. return is_master_AGV;
  1875. }
  1876. void Navigation::ResetDoor(int32_t status) {
  1877. // printf("---------------receive status:%d\n",status);
  1878. DoorStatusMap doorStatusMap;
  1879. anasisDoorStatu(status, doorStatusMap);
  1880. // for (auto it = doorStatusMap.begin() ; it != doorStatusMap.end() ; ++it) {
  1881. // printf("doorID:%d, out:%d, ins:%d\n",it->first, it->second.first, it->second.second);
  1882. // }
  1883. timed_Door_.reset(doorStatusMap, 3);
  1884. }
  1885. void Navigation::anasisDoorStatu(int32_t i_door_status, DoorStatusMap& m_door_status) {
  1886. // int bytesize = sizeof(int32_t)*8;
  1887. m_door_status.clear();
  1888. for (int i = 1; i >= 0; --i) {
  1889. int statu = i_door_status>>(i*16);
  1890. int door_id = (statu&0xFF00)>>8;
  1891. int outside_door_status = statu&0x00FF;
  1892. int inside_door_status = outside_door_status>>2;
  1893. if ((outside_door_status & eDoorOpened) == eDoorOpened){
  1894. outside_door_status = eDoorOpened;
  1895. } else if ((outside_door_status & eDoorClosed) == eDoorClosed){
  1896. outside_door_status = eDoorClosed;
  1897. } else{
  1898. outside_door_status = eDoorInvalid;
  1899. }
  1900. if ((inside_door_status & eDoorOpened) == eDoorOpened){
  1901. inside_door_status = eDoorOpened;
  1902. } else if ((inside_door_status & eDoorClosed) == eDoorClosed){
  1903. inside_door_status = eDoorClosed;
  1904. } else{
  1905. inside_door_status = eDoorInvalid;
  1906. }
  1907. m_door_status[door_id] = DoorStatusInOnePort((DoorStatu)outside_door_status, (DoorStatu)inside_door_status);
  1908. }
  1909. }
  1910. #define SWAP_BYTES 0
  1911. void Navigation::anasisCarierStatu(int& regionID, CarrierStatuRegionMap& status, int32_t bytes){
  1912. int32_t reverseBytes=bytes;
  1913. if(SWAP_BYTES){
  1914. reverseBytes = (bytes>>24)| ((bytes&0xFF0000)>>8)|((bytes&0xFF00)<<8)|((bytes&0xFF)<<24);
  1915. }
  1916. int bytesize=sizeof(int32_t)*8;
  1917. regionID = (bytes>>((bytesize-1)*8));
  1918. for (int i = 0; i < (bytesize-8)/2; ++i) {
  1919. int statu =(reverseBytes>>(i*2) & 0x03);
  1920. if ((statu & eCarrierUp) == eCarrierUp) {
  1921. status[i+1]=eCarrierUp;
  1922. }
  1923. else if ((statu & eCarrierDown) == eCarrierDown){
  1924. status[i+1]=eCarrierDown;
  1925. }
  1926. else {
  1927. status[i+1]=eCarrierInvalid;
  1928. }
  1929. }
  1930. }
  1931. void Navigation::ResetCarrier(std::vector<int32_t> status) {
  1932. for(int32_t statu : status){
  1933. int regionID;
  1934. CarrierStatuRegionMap region;
  1935. anasisCarierStatu(regionID,region,statu);
  1936. timed_Carrier_[regionID].reset(region,5);
  1937. }
  1938. /*printf("---------------------------------------\n");
  1939. for(std::map<int,TimedLockData<CarrierStatuRegionMap>>::iterator it=timed_Carrier_.begin();
  1940. it!=timed_Carrier_.end();it++){
  1941. int regionID=it->first;
  1942. if(it->second.timeout())
  1943. continue;
  1944. printf(">>>>>>> Region ID :%d\n",regionID);
  1945. std::map<int,CarrierStatu> region=it->second.Get();
  1946. for(std::map<int,CarrierStatu>::iterator it1=region.begin();
  1947. it1!=region.end();++it1){
  1948. int id=it1->first;
  1949. CarrierStatu sta=it1->second;
  1950. printf("\t \tcarrier id :%d statu:%d\n",id,sta);
  1951. }
  1952. }*/
  1953. }
  1954. void Navigation::Stringsplit(const std::string& str, const std::string splits, std::vector<std::string>& res){
  1955. res.clear();
  1956. if(str.empty())
  1957. return;
  1958. std::string strs = str+splits;
  1959. size_t pos = strs.find(splits);
  1960. int step = splits.size();
  1961. while (pos!= std::string::npos){
  1962. std::string temp = strs.substr(0, pos);
  1963. res.push_back(temp);
  1964. strs = strs.substr(pos+step,strs.size());
  1965. pos = strs.find(splits);
  1966. }
  1967. }
  1968. bool Navigation::LoadSpaceNo2CarrierNo(){
  1969. std::string file_name = "../config/SpaceNO2CarrarierNO.txt";
  1970. std::ifstream in(file_name);
  1971. std::string line;
  1972. std::vector<std::string> data_in_one_line;
  1973. if (in){
  1974. short space_id, region_id, carrier_no;
  1975. while(std::getline(in,line)){
  1976. Stringsplit(line, " ",data_in_one_line);
  1977. space_id = std::stoi(data_in_one_line[0]);
  1978. region_id = std::stoi(data_in_one_line[1]);
  1979. carrier_no = std::stoi(data_in_one_line[2]);
  1980. spaceNo_2_region_carrier_excel_[space_id] = Region_Carry(region_id,carrier_no);
  1981. }
  1982. return true;
  1983. } else{
  1984. printf("no SpaceNO2CarrarierNO.txt in config!\n");
  1985. }
  1986. return false;
  1987. }
  1988. bool Navigation::SpaceNo2CarrierNo(int space_no, int& region_id, int& carrier_no) {
  1989. Region_Carry region_carry = spaceNo_2_region_carrier_excel_[space_no];
  1990. region_id = region_carry.first;
  1991. carrier_no = region_carry.second;
  1992. return true;
  1993. }
  1994. Navigation::CarrierStatu Navigation::GetCarrierStatusBySpaceID(int space_id) {
  1995. int region_id;
  1996. int carrier_no;
  1997. if (SpaceNo2CarrierNo(space_id,region_id,carrier_no) == false){
  1998. printf("no matched carrier!\n");
  1999. return eCarrierInvalid;
  2000. }
  2001. printf("space_id:%d, region_id:%d, carrier_no:%d\n",space_id,region_id,carrier_no);
  2002. if (timed_Carrier_[region_id].timeout()){
  2003. printf("carrier status is timeout!\n");
  2004. return eCarrierInvalid;
  2005. }
  2006. else{
  2007. return timed_Carrier_[region_id].Get()[carrier_no];
  2008. }
  2009. return eCarrierInvalid;
  2010. }
  2011. int Navigation::GetPortIDBySpace(NavMessage::PathNode node) {
  2012. int port_id = GetSpaceId(node);
  2013. if (port_id == 1101){
  2014. return 1;
  2015. }
  2016. else if (port_id == 1102){
  2017. return 2;
  2018. }
  2019. }