navigation.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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) {
  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. int space_id = GetSpaceId(spaceNode);
  816. if ( space_id < 0)
  817. return false;
  818. if (space_id > 99 && space_id < 1000) {
  819. return true;
  820. }
  821. return false;
  822. }
  823. int Navigation::GetSpaceId(NavMessage::PathNode spaceNode) {
  824. if(spaceNode.has_id()==false){
  825. return -1;
  826. }
  827. std::string space_flag = "S_";
  828. if (spaceNode.id().find(space_flag) >= 0) {
  829. std::string id = spaceNode.id().substr(2, spaceNode.id().length() - 1);
  830. int space_id = stoi(id);
  831. return space_id;
  832. }
  833. return -1;
  834. }
  835. bool Navigation::TargetIsEntrance(NavMessage::PathNode node) {
  836. int space_id = GetSpaceId(node);
  837. return space_id==1101;
  838. }
  839. bool Navigation::TargetIsExport(NavMessage::PathNode node) {
  840. int space_id = GetSpaceId(node);
  841. return space_id==1101;
  842. }
  843. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  844. printf("execute_InOut_space\n");
  845. if (action.type() != 1 && action.type() != 2) {
  846. printf(" Inout_space failed : msg action type must ==1\n ");
  847. return false;
  848. }
  849. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  850. !action.has_streetnode()) {
  851. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  852. return false;
  853. }
  854. if (PoseTimeout() == true) {
  855. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  856. return false;
  857. }
  858. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  859. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  860. //入库,起点为streetNode
  861. if (action.type() == 1) {
  862. Pose2d vec(action.spacenode().x() - action.streetnode().x(),
  863. action.spacenode().y() - action.streetnode().y(),
  864. 0);
  865. action.mutable_streetnode()->set_l(0.02);
  866. action.mutable_streetnode()->set_w(0.2);
  867. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()) + M_PI / 2.0);
  868. /*
  869. * 是否需要添加判断,距离较远才先运动到马路点=============================
  870. */
  871. int clampLifterAction = 0;
  872. printf("IsUperSpace\n");
  873. if (IsUperSpace(action.spacenode())) { //进载车板库前提升机构上升
  874. clampLifterAction |= eByteLifterUp;
  875. } else { //否则下降
  876. clampLifterAction |= eByteLifterDown;
  877. }
  878. if (move_mode_ == eSingle)
  879. clampLifterAction |= eByteClampHalfOpen;
  880. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true, clampLifterAction) == false) {
  881. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  882. return false;
  883. }
  884. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  885. printf("inout ----------------------------------------\n");
  886. //取车流程,送车到出口前旋转180
  887. if (move_mode_ == eDouble && TargetIsExport(action.spacenode())) {
  888. Pose2d current_pose = timedPose_.Get();
  889. double current_theta = current_pose.theta();
  890. if (RotateBeforeIntoExport() != eMpcSuccess) {
  891. printf("Rotate before in entrance failed\n");
  892. return false;
  893. }
  894. }
  895. //计算车位朝向
  896. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  897. //计算当前车是进入车位1点还是2点,
  898. NavMessage::PathNode new_target;
  899. bool retRotate = RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(),
  900. new_target, clampLifterAction) == eMpcSuccess;
  901. if (retRotate == false) {
  902. return false;
  903. }
  904. //去出入口,等内门打开
  905. if (TargetIsExport(action.spacenode()) || TargetIsEntrance(action.spacenode())) {
  906. printf(" Wait Inside door opened.......\n");
  907. int port_id = GetPortIDBySpace(action.spacenode());
  908. if (WaitInsideDoorCompleted(port_id, eDoorOpened) == false ) {
  909. return false;
  910. }
  911. printf(" Wait Inside door opened completed!!!\n");
  912. } else {
  913. //去车位点
  914. if (IsUperSpace(action.spacenode())) {
  915. printf(" Wait Carrier down.......\n");
  916. if (WaitCarrierCompleted(GetSpaceId(action.spacenode()), eCarrierDown) == false) {
  917. return false;
  918. }
  919. printf(" Wait Carrier down completed!!!\n");
  920. } else {
  921. if(!TargetIsExport(action.spacenode()) && !TargetIsEntrance(action.spacenode())) {
  922. printf(" Wait Carrier up.......\n");
  923. if (WaitCarrierCompleted(GetSpaceId(action.spacenode()), eCarrierUp) == false) {
  924. return false;
  925. }
  926. printf(" Wait Carrier up completed!!!\n");
  927. }else{
  928. printf(" Wait inside door open.......\n");
  929. if(WaitInsideDoorCompleted(GetSpaceId(action.spacenode()),eDoorOpened)==false)
  930. return false;
  931. printf("inside door opened\n");
  932. }
  933. }
  934. }
  935. if (IsUperSpace(action.spacenode())) {
  936. if(lifter_rise()== false){
  937. return false;
  938. }
  939. }
  940. // 单车进库必须全打开
  941. if (move_mode_ == eSingle){
  942. if (clamp_fully_open() == false) {
  943. return false;
  944. }
  945. }
  946. //入库
  947. Pose2d rotated = timedPose_.Get();
  948. double x = new_target.x();
  949. double y = new_target.y();
  950. Pose2d vecSpace(x - rotated.x(), y - rotated.y(), 0);
  951. rotated.mutable_theta() = Pose2d::vector2yaw(vecSpace.x(), vecSpace.y());
  952. bool isFront = isFront_.Get();
  953. if (rotated.theta() < 0.)
  954. isFront = !isFront;
  955. // 后进车,且单车,且先进车已过后进车的目标点
  956. Pose2d target(new_target.x(), new_target.y(), 0);
  957. if (isFront == false && move_mode_ == eSingle) {
  958. while (true) {
  959. if (cancel_) {
  960. return false;
  961. }if(move_mode_==eDouble){
  962. clampLifterAction |= eLifterDown;
  963. }
  964. Pose2d current = timedPose_.Get();
  965. Pose2d brother_pose = timedBrotherPose_.Get();
  966. Pose2d vecCurent2T = current - target;
  967. Pose2d vecBrother2T = brother_pose - target;
  968. double dot = vecCurent2T.x() * vecBrother2T.x() + vecCurent2T.y() * vecBrother2T.y();
  969. if (timedBrotherNavStatu_.Get().in_space() == true && dot < 0.) {
  970. printf("先进车已经过后进车目标点,后进车停止等待 dot=%f\n", dot);
  971. break;
  972. }
  973. printf(" 后车 等待 先车先入库。。。\n");
  974. usleep(1000 * 500);
  975. continue;
  976. }
  977. }
  978. isInSpace_ = true;
  979. printf("Enter space beg...\n");
  980. clampLifterAction = 0;
  981. if (IsUperSpace(action.spacenode())) { //进载车板库前提升机构上升
  982. clampLifterAction |= eByteLifterUp;
  983. } else { //否则下降
  984. clampLifterAction |= eByteLifterDown;
  985. }
  986. printf(" clampLifter_statu1 :%d\n", clampLifterAction);
  987. if (move_mode_ == eSingle)
  988. clampLifterAction |= eByteClampFullyOpen; //////// 后续再改为全打开位
  989. else
  990. clampLifterAction |= eByteCLampClose;
  991. printf(" clampLifter_statu2 :%d\n", clampLifterAction);
  992. bool retMove = MoveToTarget(new_target, limit_v, limit_w, true, false, clampLifterAction);
  993. if (retMove == false) {
  994. printf(" Enter space:%s failed. type:%d\n", action.spacenode().id().data(), action.type());
  995. return false;
  996. }
  997. } else if (action.type() == 2) {
  998. // LogWriter("Out space beg-");
  999. // 计算当前位置,在车位点到马路点的连线方向上,与车位点的相对位姿
  1000. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  1001. action.streetnode().y() - action.spacenode().y(), 0);
  1002. float theta = Pose2d::vector2yaw(vec.x(), vec.y());
  1003. Pose2d space(action.spacenode().x(), action.spacenode().y(), theta);
  1004. Pose2d current = timedPose_.Get();
  1005. Pose2d diff = Pose2d::abs(Pose2d::relativePose(Pose2d(current.x(), current.y(), 0), space));
  1006. if (diff.x() < 0.05 || diff.y() < 0.10) {
  1007. //出库,移动到马路点,允许自由方向,不允许中途旋转
  1008. action.mutable_streetnode()->set_theta(theta);
  1009. action.mutable_streetnode()->set_l(0.03);
  1010. action.mutable_streetnode()->set_w(0.2);
  1011. std::cout << " Out space target street node:" << space << std::endl;
  1012. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  1013. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  1014. return false;
  1015. }
  1016. } else {
  1017. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  1018. << std::endl;
  1019. return false;
  1020. }
  1021. isInSpace_ = false;
  1022. //摆正
  1023. if (DoubleOutSpaceCorrectTheta() != eMpcSuccess)
  1024. return false;
  1025. }
  1026. return true;
  1027. }
  1028. /* current_to_target: current_to_target.theta()为所需旋转的角度
  1029. * */
  1030. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  1031. bool all_direct) {
  1032. if (PoseTimeout() == true) {
  1033. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  1034. return false;
  1035. }
  1036. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  1037. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  1038. return false;
  1039. }
  1040. Pose2d pose = timedPose_.Get();
  1041. double line_velocity = timedV_.Get(); //从plc获取状态
  1042. double angular_velocity = timedA_.Get();
  1043. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  1044. statu[0] = pose.x();
  1045. statu[1] = pose.y();
  1046. statu[2] = pose.theta();
  1047. statu[3] = line_velocity;
  1048. statu[4] = angular_velocity;
  1049. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  1050. double obs_w = 0.95;//0.85;
  1051. double obs_h = 1.55;//1.45;
  1052. MpcError ret = failed;
  1053. Pose2d brother = timedBrotherPose_.Get();
  1054. Pose2d brother_in_self = Pose2d::relativePose(brother, pose);
  1055. // std::cout<<" brother_in_self: "<<brother_in_self<<std::endl;
  1056. if (move_mode_ == eDouble) {
  1057. brother_in_self = Pose2d(pose.x() + 100, pose.y() + 100, 0);
  1058. }
  1059. RotateMPC MPC(brother_in_self, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  1060. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  1061. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  1062. ret = MPC.solve(current_to_target, statu, parameter, out);
  1063. if (ret == no_solution) {
  1064. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  1065. out.clear();
  1066. out.push_back(0);
  1067. out.push_back(0);
  1068. out.push_back(0);
  1069. } else {
  1070. // double min_angular_velocity = 1.0 / 180 * M_PI;
  1071. double min_angular_velocity = 0.01;
  1072. const int down_count = 3;
  1073. for (int i = 0; i < down_count; ++i) {
  1074. if (fabs(out[i]) < min_angular_velocity) {
  1075. if (out[i] > 0)
  1076. out[i] = min_angular_velocity;
  1077. else if (out[i] < 0)
  1078. out[i] = -min_angular_velocity;
  1079. else {}
  1080. }
  1081. }
  1082. }
  1083. return ret == success || ret == no_solution;
  1084. }
  1085. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  1086. if (PoseTimeout() == true) {
  1087. printf(" MPC once Error:Pose is timeout \n");
  1088. return false;
  1089. }
  1090. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  1091. printf(" MPC once Error:V/A is timeout \n");
  1092. return false;
  1093. }
  1094. if (traj.size() == 0) {
  1095. printf("traj size ==0\n");
  1096. return false;
  1097. }
  1098. Pose2d pose = timedPose_.Get();
  1099. double velocity = timedV_.Get(); //从plc获取状态
  1100. double angular = timedA_.Get();
  1101. // float diffYaw1=timedPose_.Get().m_diffYaw1;
  1102. // float diffYaw2=timedPose_.Get().m_diffYaw2;
  1103. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  1104. statu[0] = pose.x();
  1105. statu[1] = pose.y();
  1106. statu[2] = pose.theta();
  1107. statu[3] = velocity;
  1108. statu[4] = angular;
  1109. Trajectory optimize_trajectory;
  1110. Trajectory selected_trajectory;
  1111. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  1112. double obs_w = obs_w_;// 0.95;//0.85;
  1113. double obs_h = obs_h_;// 1.55;//1.45;
  1114. if (directY == true) {
  1115. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  1116. statu[2] += M_PI / 2;
  1117. if (statu[2] > M_PI)
  1118. statu[2] -= 2 * M_PI;
  1119. mpc_parameter = parameter_.y_mpc_parameter();
  1120. obs_w = obs_h_;// 0.95;//0.85;
  1121. obs_h = obs_w_;//1.55;//1.45;
  1122. }
  1123. Pose2d brother = timedBrotherPose_.Get();
  1124. Pose2d relative = Pose2d::relativePose(brother, pose);
  1125. if (directY)
  1126. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  1127. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  1128. std::cout << "pose:" << pose << ", brother:" << brother << ", relative:" << relative << std::endl;
  1129. if (move_mode_ == eDouble)
  1130. relative = Pose2d(-1e8, -1e8, 0);
  1131. MpcError ret = failed;
  1132. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  1133. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  1134. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  1135. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  1136. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  1137. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  1138. out, selected_trajectory, optimize_trajectory, directY);
  1139. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  1140. if (ret == no_solution) {
  1141. printf(" mpc solve no solution set v/w = 0\n");
  1142. out.clear();
  1143. out.push_back(0);
  1144. out.push_back(0);
  1145. out.push_back(0);
  1146. out.push_back(0);
  1147. out.push_back(0);
  1148. out.push_back(0);
  1149. }
  1150. //
  1151. // diff_yaw.push_back(0);
  1152. // diff_yaw.push_back(0);
  1153. predict_traj_.reset(optimize_trajectory, 1);
  1154. selected_traj_.reset(selected_trajectory, 1);
  1155. return ret == success || ret == no_solution;
  1156. }
  1157. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  1158. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  1159. printf(" pose / v / w timeout,IsArrived return false\n");
  1160. return false;
  1161. }
  1162. Pose2d current = timedPose_.Get();
  1163. double x = current.x();
  1164. double y = current.y();
  1165. double tx = targetNode.x();
  1166. double ty = targetNode.y();
  1167. double l = fabs(targetNode.l());
  1168. double w = fabs(targetNode.w());
  1169. double theta = -targetNode.theta();
  1170. /*if (newUnfinished_cations_.front().type() == 1 && GetSpaceId(targetNode) > 0) {//入库只判断前后方向上是否到达
  1171. tx = x;
  1172. }*/
  1173. if (l < 1e-10 || w < 1e-10) {
  1174. printf("IsArrived: Error target l or w == 0\n");
  1175. return false;
  1176. }
  1177. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  1178. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  1179. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  1180. if (fabs(timedV_.Get()) < 0.05 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  1181. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  1182. return true;
  1183. }
  1184. }
  1185. return false;
  1186. }
  1187. void Navigation::SendMoveCmd(int mode, ActionType type,
  1188. double v, double angular) {
  1189. if (monitor_) {
  1190. monitor_->set_speed(mode, type, v, angular);
  1191. if (type == eRotation)
  1192. RWheel_position_ = eR;
  1193. if (type == eVertical)
  1194. RWheel_position_ = eX;
  1195. if (type == eHorizontal)
  1196. RWheel_position_ = eY;
  1197. }
  1198. }
  1199. void Navigation::SendMoveCmd(int clampLifterAction, int mode, ActionType type,
  1200. double v[], double angular[]) {
  1201. if (monitor_) {
  1202. monitor_->set_ToAgvCmd(clampLifterAction, mode, type, v, angular);
  1203. if (type == eRotation)
  1204. RWheel_position_ = eR;
  1205. if (type == eVertical)
  1206. RWheel_position_ = eX;
  1207. if (type == eHorizontal)
  1208. RWheel_position_ = eY;
  1209. }
  1210. }
  1211. /*void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[],
  1212. int space_id, double distance,double Y1,double Y2) {
  1213. if (monitor_) {
  1214. monitor_->set_ToAgvCmd(mode, type, v, angular, 0, space_id, distance,Y1,Y2);
  1215. if (type == eRotation)
  1216. RWheel_position_ = eR;
  1217. if (type == eVertical)
  1218. RWheel_position_ = eX;
  1219. if (type == eHorizontal)
  1220. RWheel_position_ = eY;
  1221. }
  1222. }*/
  1223. void Navigation::SendMoveCmd(int clampLifterAction, int mode, ActionType type, double v[], double angular[],
  1224. int space_id, double distance, double Y1, double Y2) {
  1225. if (monitor_) {
  1226. monitor_->set_ToAgvCmd(clampLifterAction, mode, type, v, angular, 0, space_id, distance, Y1, Y2);
  1227. if (type == eRotation)
  1228. RWheel_position_ = eR;
  1229. if (type == eVertical)
  1230. RWheel_position_ = eX;
  1231. if (type == eHorizontal)
  1232. RWheel_position_ = eY;
  1233. }
  1234. }
  1235. //void Navigation::SendMoveCmd(int mode, ActionType type, double v[], double angular[], double yaw_diff[] ,int space_id, double distance) {
  1236. // if (monitor_) {
  1237. // monitor_->set_ToAgvCmd(mode, type, v, angular, yaw_diff ,0,space_id, distance);
  1238. // if (type == eRotation)
  1239. // RWheel_position_ = eR;
  1240. // if (type == eVertical)
  1241. // RWheel_position_ = eX;
  1242. // if (type == eHorizontal)
  1243. // RWheel_position_ = eY;
  1244. // }
  1245. //}
  1246. bool Navigation::clamp_close() {
  1247. if (monitor_) {
  1248. printf("Clamp closing...\n");
  1249. monitor_->clamp_close(move_mode_);
  1250. actionType_ = eClampClose;
  1251. while (cancel_ == false) {
  1252. if (timed_clamp_.timeout()) {
  1253. printf("timed clamp is timeout\n");
  1254. return false;
  1255. }
  1256. if (timed_clamp_.Get() == eClosed)
  1257. return true;
  1258. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1259. monitor_->clamp_close(move_mode_);
  1260. actionType_ = eClampClose;
  1261. }
  1262. return false;
  1263. }
  1264. return false;
  1265. }
  1266. bool Navigation::clamp_half_open() {
  1267. if (monitor_) {
  1268. printf("Clamp openning...\n");
  1269. monitor_->clamp_half_open(move_mode_);
  1270. actionType_ = eClampHalfOpen;
  1271. while (cancel_ == false) {
  1272. if (timed_clamp_.timeout()) {
  1273. printf("timed clamp is timeout\n");
  1274. return false;
  1275. }
  1276. if (timed_clamp_.Get() == eHalfOpened)
  1277. return true;
  1278. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1279. monitor_->clamp_half_open(move_mode_);
  1280. actionType_ = eClampHalfOpen;
  1281. }
  1282. return false;
  1283. }
  1284. return false;
  1285. }
  1286. bool Navigation::singleClamp_fullyOpen_until_inspaceBegin() {
  1287. if (move_mode_ == eDouble) {
  1288. return true;
  1289. }
  1290. while (cancel_ == false) {
  1291. if (timedV_.timeout()) {
  1292. return false;
  1293. }
  1294. if (fabs(timedV_.Get()) < 0.02) {
  1295. usleep(1000 * 300);
  1296. continue;
  1297. }
  1298. break;
  1299. }
  1300. return clamp_fully_open();
  1301. };
  1302. bool Navigation::clamp_fully_open() {
  1303. if (monitor_) {
  1304. if (timed_clamp_.Get() == eFullyOpened)
  1305. return true;
  1306. printf("Clamp fully openning...\n");
  1307. monitor_->clamp_fully_open(move_mode_);
  1308. actionType_ = eClampFullyOpen;
  1309. while (cancel_ == false) {
  1310. if (timed_clamp_.timeout()) {
  1311. printf("timed clamp is timeout\n");
  1312. return false;
  1313. }
  1314. if (timed_clamp_.Get() == eFullyOpened)
  1315. return true;
  1316. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1317. monitor_->clamp_fully_open(move_mode_);
  1318. actionType_ = eClampFullyOpen;
  1319. }
  1320. return false;
  1321. }
  1322. return false;
  1323. }
  1324. bool Navigation::lifter_rise() {
  1325. if (monitor_) {
  1326. printf("Lifter upping...\n");
  1327. actionType_ = eLifterRise;
  1328. if (timed_lifter_.Get() == eRose)
  1329. return true;
  1330. if (timed_lifter_.Get() != eRose) {
  1331. if (timed_clamp_.Get() != eHalfOpened && timed_clamp_.Get() != eClosed) {
  1332. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  1333. if (!clamp_half_open()) {
  1334. printf(" clamp half open failed\n");
  1335. return false;
  1336. }
  1337. }
  1338. }
  1339. while (cancel_ == false) {
  1340. if (timed_lifter_.timeout()) {
  1341. printf("timed lifter is timeout\n");
  1342. return false;
  1343. }
  1344. if (timed_lifter_.timeout()) {
  1345. return false;
  1346. }
  1347. if (timed_lifter_.Get() == eRose)
  1348. return true;
  1349. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1350. monitor_->lifter_rise(move_mode_);
  1351. actionType_ = eLifterRise;
  1352. }
  1353. return false;
  1354. }
  1355. return false;
  1356. }
  1357. bool Navigation::lifter_down() {
  1358. if (monitor_) {
  1359. printf("Lifter downing...\n");
  1360. if (timed_lifter_.Get() == eDowned)
  1361. return true;
  1362. actionType_ = eLifterDown;
  1363. if (timed_lifter_.Get() != eDowned) {
  1364. if (timed_clamp_.Get() != eHalfOpened && timed_clamp_.Get() != eClosed) {
  1365. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  1366. if (!clamp_half_open()) {
  1367. printf(" clamp half open failed\n");
  1368. return false;
  1369. }
  1370. }
  1371. }
  1372. while (cancel_ == false) {
  1373. if (timed_lifter_.timeout()) {
  1374. printf("timed lifter is timeout\n");
  1375. return false;
  1376. }
  1377. if (timed_lifter_.timeout()) {
  1378. return false;
  1379. }
  1380. if (timed_lifter_.Get() == eDowned)
  1381. return true;
  1382. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1383. monitor_->lifter_down(move_mode_);
  1384. actionType_ = eLifterDown;
  1385. }
  1386. return false;
  1387. }
  1388. return false;
  1389. }
  1390. bool Navigation::WaitInsideDoorCompleted(int doorID, DoorStatu status) {
  1391. while (cancel_ == false) {
  1392. if (timed_Door_.timeout()) {
  1393. return false;
  1394. }
  1395. if (timed_Door_.Get()[doorID].second == status) {
  1396. return true;
  1397. }
  1398. usleep(1000 * 100);
  1399. printf("waiting door_id(ins):%d, to:%d...\n",doorID, status);
  1400. }
  1401. if (cancel_ == true) {
  1402. return false;
  1403. }
  1404. return false;
  1405. }
  1406. bool Navigation::WaitCarrierCompleted(int spaceID, CarrierStatu statu) {
  1407. while (cancel_ == false) {
  1408. int region_id=0, carrier_no=0;
  1409. SpaceNo2CarrierNo(spaceID, region_id, carrier_no);
  1410. if(region_id<0){
  1411. return true;
  1412. }
  1413. if (timed_Carrier_[region_id].timeout()) {
  1414. printf("spaceID:%d, timed_Carrier_[%d].timeout carrier_no id:%d\n",spaceID,region_id,carrier_no);
  1415. return false;
  1416. }
  1417. CarrierStatu cur_ = GetCarrierStatusBySpaceID(spaceID);
  1418. if ( cur_ == statu){
  1419. return true;
  1420. }
  1421. usleep(1000 * 100);
  1422. printf("waiting space_id:%d, carrier_id[%d,%d] to:%d, cur:%d...\n",spaceID, region_id, carrier_no, statu, cur_);
  1423. }
  1424. if (cancel_ == true) {
  1425. return false;
  1426. }
  1427. return false;
  1428. }
  1429. bool Navigation::WaitClampLifterStatu(int clampLifterAction) {
  1430. int currentStatu = 0;
  1431. while (cancel_ == false) {
  1432. if (timed_clamp_.timeout() || timed_lifter_.timeout()) {
  1433. return false;
  1434. }
  1435. if (timed_clamp_.Get() == eClosed) {
  1436. currentStatu |= eByteCLampClose;
  1437. }
  1438. if (timed_clamp_.Get() == eHalfOpened) {
  1439. currentStatu |= eByteClampHalfOpen;
  1440. }
  1441. if (timed_clamp_.Get() == eFullyOpened) {
  1442. currentStatu |= eByteClampFullyOpen;
  1443. }
  1444. if (timed_lifter_.Get() == eRose) {
  1445. currentStatu |= eByteLifterUp;
  1446. }
  1447. if (timed_lifter_.Get() == eDowned) {
  1448. currentStatu |= eByteLifterDown;
  1449. }
  1450. printf(" current statu:%d action status:%d last:%d\n",
  1451. currentStatu,clampLifterAction,currentStatu&clampLifterAction);
  1452. if((currentStatu&clampLifterAction) == clampLifterAction)
  1453. return true;
  1454. double down_v[3] = {0, 0, 0};//下发线速度0
  1455. double down_w[3] = {0, 0, 0};//下发角速度0
  1456. SendMoveCmd(clampLifterAction, move_mode_, actionType_, down_v, down_w);
  1457. usleep(1000 * 100);
  1458. }
  1459. if (cancel_ == true) {
  1460. return false;
  1461. }
  1462. return false;
  1463. }
  1464. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v,
  1465. bool autoDirect, int clampLifterAction) {
  1466. if (IsArrived(node)) {
  1467. printf(" current already in target completed !!!\n");
  1468. return eMpcSuccess;
  1469. }
  1470. if (inited_ == false) {
  1471. printf(" navigation has not inited\n");
  1472. return eMpcFailed;
  1473. }
  1474. if (PoseTimeout()) {
  1475. printf(" navigation Error:Pose is timeout \n");
  1476. return eMpcFailed;
  1477. }
  1478. Pose2d current = timedPose_.Get();
  1479. Pose2d target(node.x(), node.y(), 0);
  1480. //生成轨迹
  1481. Trajectory traj = Trajectory::create_line_trajectory(current, target, 0.1);
  1482. if (traj.size() == 0)
  1483. return eMpcSuccess;
  1484. //判断 巡线方向
  1485. bool directY = false;
  1486. if (autoDirect) {
  1487. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  1488. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  1489. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  1490. directY = true;
  1491. } else {
  1492. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  1493. }
  1494. if (!PossibleToTarget(node, directY)) {
  1495. directY = !directY;
  1496. }
  1497. }
  1498. printf(" exec along autoDirect:%d\n", autoDirect);
  1499. //文件log
  1500. std::ofstream ofs; //创建流对象
  1501. std::string log_file_dir = "../data/";
  1502. time_t t = time(0);
  1503. char tmp[32] = {NULL};
  1504. strftime(tmp, sizeof(tmp), "%Y_%m_%d_%H_%M_%S.txt", localtime(&t));
  1505. std::string log_file_name = tmp;
  1506. ofs.open(log_file_dir + log_file_name, std::ios::app); //打开的地址和方式
  1507. auto beg_time = std::chrono::steady_clock::now();
  1508. while (cancel_ == false) {
  1509. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  1510. if (pause_ == true) {
  1511. //发送暂停消息
  1512. continue;
  1513. }
  1514. if (PoseTimeout()) {
  1515. printf(" navigation Error:Pose is timeout \n");
  1516. ofs.close();//关闭文件
  1517. return eMpcFailed;
  1518. }
  1519. if (timedV_.timeout() || timedA_.timeout()) {
  1520. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  1521. ofs.close();//关闭文件
  1522. return eMpcFailed;
  1523. }
  1524. //判断是否到达终点
  1525. if (IsArrived(node)) {
  1526. if (Stop()) {
  1527. printf(" exec along completed !!!\n");
  1528. ofs.close();//关闭文件
  1529. bool waited = WaitClampLifterStatu(clampLifterAction);
  1530. if (waited) return eMpcSuccess;
  1531. else return eMpcFailed;
  1532. }
  1533. }
  1534. //一次变速
  1535. std::vector<double> out;
  1536. // std::vector<double> diff_yaw; //两车分别与整车的相对角度
  1537. bool ret;
  1538. TimerRecord::Execute([&, this]() {
  1539. ret = mpc_once(traj, limit_v, out, directY);
  1540. }, "mpc_once");
  1541. if (ret == false) {
  1542. SlowlyStop();
  1543. ofs.close();//关闭文件
  1544. return eMpcFailed;
  1545. }
  1546. const int down_count = 3;//下发前多少步
  1547. double down_v[down_count] = {out[0], out[2], out[4]};//下发线速度
  1548. double down_w[down_count] = {out[1], out[3], out[5]};//下发角速度
  1549. /*
  1550. * AGV 在终点附近低速巡航时,设置最小速度0.05
  1551. */
  1552. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  1553. if (directY) {
  1554. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  1555. }
  1556. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  1557. for (int i = 0; i < down_count; ++i) {
  1558. if (down_v[i] >= 0 && down_v[i] < limit_v.min)
  1559. down_v[i] = limit_v.min;
  1560. if (down_v[i] < 0 && down_v[i] > -limit_v.min)
  1561. down_v[i] = -limit_v.min;
  1562. }
  1563. }
  1564. //放大角速度
  1565. // out[1] *= 10;
  1566. //入库,行进方向上最后一米不纠偏
  1567. if (newUnfinished_cations_.front().type() == 1 && GetSpaceId(node) > 0) {//入库
  1568. if (fabs(target_in_agv.x()) < 0.5) {
  1569. printf("target_in_agv: %f", target_in_agv.x());
  1570. for (double &i: down_w) {
  1571. i = 0.0;
  1572. }
  1573. }
  1574. } else if (newUnfinished_cations_.front().type() == 2) {//出库,行进方向上开始一米不纠偏
  1575. Pose2d agv_in_space =
  1576. Pose2d::relativePose(
  1577. Pose2d(newUnfinished_cations_.front().spacenode().x(),
  1578. newUnfinished_cations_.front().spacenode().y(), 0),
  1579. timedPose_.Get());
  1580. if (fabs(agv_in_space.x()) < 0.3) {
  1581. printf("agv_in_space: %f", agv_in_space.x());
  1582. for (double &i: down_w) {
  1583. i = 0.0;
  1584. }
  1585. }
  1586. }
  1587. //下发速度
  1588. //printf(" nav input :%f out:%f\n",timedV_.Get(),out[0]);
  1589. //添加车位号,距目标点距离信息
  1590. double distance = Pose2d::distance(target_in_agv, Pose2d(0, 0, 0));
  1591. int space_id = GetSpaceId(node);
  1592. double Y1 = 0.0, Y2 = 0.0;
  1593. if (move_mode_ == eDouble) {
  1594. double dy = -timeYawDiff1_.Get();
  1595. Y1 = 0.2 * (1.0 / (1 + exp(-16. * dy)) - 0.5) * 1.0;
  1596. Y2 = timeYawDiff2_.Get();
  1597. if (directY == false) {
  1598. Y1 = 0;
  1599. }
  1600. }
  1601. if (directY == false)
  1602. SendMoveCmd(clampLifterAction, move_mode_, eVertical, down_v, down_w, space_id, distance, Y1, Y2);
  1603. else
  1604. SendMoveCmd(clampLifterAction, move_mode_, eHorizontal, down_v, down_w, space_id, distance, Y1, Y2);
  1605. actionType_ = directY ? eHorizontal : eVertical;
  1606. //日志打印
  1607. std::string log_inf_line = std::to_string(move_mode_);
  1608. log_inf_line += " ";
  1609. log_inf_line += std::to_string(parameter_.x_mpc_parameter().shortest_radius()) + " " +
  1610. std::to_string(parameter_.x_mpc_parameter().acc_angular());
  1611. log_inf_line += " ";
  1612. Pose2d cur_pose = timedPose_.Get();//自身x,y,theta
  1613. log_inf_line += std::to_string(cur_pose.x()) + " " + std::to_string(cur_pose.y()) + " " +
  1614. std::to_string(cur_pose.theta());
  1615. log_inf_line += " ";
  1616. Pose2d cur_Bro_pose = timedBrotherPose_.Get();//另一节x,y,theta
  1617. log_inf_line += std::to_string(cur_Bro_pose.x()) + " " + std::to_string(cur_Bro_pose.y()) + " " +
  1618. std::to_string(cur_Bro_pose.theta());
  1619. log_inf_line += " ";
  1620. log_inf_line += std::to_string(out[0]) + " " + std::to_string(out[1]);//下发速度
  1621. log_inf_line += " ";
  1622. log_inf_line += std::to_string(out[2]) + " " + std::to_string(out[3]);//下发速度
  1623. log_inf_line += " ";
  1624. log_inf_line += std::to_string(out[4]) + " " + std::to_string(out[5]);//下发速度
  1625. log_inf_line += " ";
  1626. log_inf_line += std::to_string(Y1) + " " + std::to_string(Y2);//下发速度
  1627. log_inf_line += " ";
  1628. auto cur_time = std::chrono::steady_clock::now();
  1629. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(cur_time - beg_time);
  1630. double time =
  1631. double(duration.count()) * std::chrono::microseconds::period::num /
  1632. std::chrono::microseconds::period::den;
  1633. log_inf_line += std::to_string(time);
  1634. ofs << log_inf_line << std::endl; //一行日志的内容
  1635. /*
  1636. * 判断车辆是否在终点附近徘徊,无法到达终点
  1637. */
  1638. if (PossibleToTarget(node, directY) == false) {
  1639. printf(" --------------MPC imposible to target -------------------------------------------\n");
  1640. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  1641. SlowlyStop();
  1642. else
  1643. Stop();
  1644. ofs.close();//关闭文件
  1645. return eImpossibleToTarget;
  1646. }
  1647. }
  1648. ofs.close();//关闭文件
  1649. return eMpcFailed;
  1650. }
  1651. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  1652. if (timedPose_.timeout()) {
  1653. return false;
  1654. }
  1655. Pose2d current = timedPose_.Get();
  1656. // if (GetSpaceId(targetNode) > 0) {
  1657. // printf("车位点强制可到达\n");
  1658. // //车位点强制可到达
  1659. // return true;
  1660. // }
  1661. double tx = targetNode.x();
  1662. double ty = targetNode.y();
  1663. double l = fabs(targetNode.l());
  1664. double w = fabs(targetNode.w());
  1665. double theta = -targetNode.theta();
  1666. // double W = 2.45;
  1667. // double L = 1.3;
  1668. // double minYaw = 3 * M_PI / 180.0;
  1669. // if (move_mode_ == eDouble) {
  1670. // L = 1.3 + 2.7;
  1671. //
  1672. // }
  1673. double W = 2.5;
  1674. double L = 1.565;
  1675. double minYaw = 5 * M_PI / 180.0;
  1676. if (move_mode_ == eDouble) {
  1677. L = 1.565 + 2.78;
  1678. }
  1679. if (directY) {
  1680. current = current.rotate(current.x(), current.y(), M_PI / 2);
  1681. double t = W;
  1682. W = L;
  1683. L = t;
  1684. }
  1685. double minR = W / 2 + L / tan(minYaw);
  1686. //printf("l:%f,w:%f\n",l,w);
  1687. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  1688. bool nagY = false;
  1689. bool posiY = false;
  1690. for (int i = 0; i < poses.size(); ++i) {
  1691. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  1692. Pose2d relative = Pose2d::relativePose(rpos, current);
  1693. double ax = fabs(relative.x());
  1694. double ay = fabs(relative.y());
  1695. if (relative.y() > 0) posiY = true;
  1696. if (relative.y() < 0) nagY = true;
  1697. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  1698. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  1699. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  1700. minR,ax,ay);*/
  1701. return true;
  1702. } else if (nagY && posiY) {
  1703. // std::cout << "nagY && posiY" << std::endl;
  1704. return true;
  1705. }
  1706. // printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  1707. // relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  1708. // minR, ax, ay);
  1709. }
  1710. printf(" impossible to target:%f %f l:%f,w:%f theta:%f, ",
  1711. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  1712. printf(" current:%f, %f, %f\n",
  1713. current.x(), current.y(), current.theta());
  1714. return false;
  1715. }
  1716. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  1717. if (inited_ == false) {
  1718. response.set_ret(-1);
  1719. response.set_info("navigation has not inited");
  1720. printf(" navigation has not inited\n");
  1721. return;
  1722. }
  1723. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  1724. {
  1725. response.set_ret(-2);
  1726. response.set_info("navigation is running pls cancel before");
  1727. printf(" navigation is running pls cancel before\n");
  1728. return;
  1729. }
  1730. //add 先检查当前点与起点的距离
  1731. global_navCmd_ = cmd;
  1732. for (int i = 0; i < cmd.newactions_size(); ++i)
  1733. newUnfinished_cations_.push(cmd.newactions(i));
  1734. pause_ = false;
  1735. cancel_ = false;
  1736. running_ = true;
  1737. actionType_ = eReady;
  1738. printf("Navigation beg...\n");
  1739. bool ret=false;
  1740. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  1741. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  1742. NavMessage::NewAction act = newUnfinished_cations_.front();
  1743. NavMessage::NewAction last_act = newUnfinished_cations_.back();
  1744. if (act.type() == 1) { //入库
  1745. printf("入库\n");
  1746. space_id_ = act.spacenode().id();
  1747. obs_w_ = 1.1;
  1748. obs_h_ = 1.87;
  1749. ret=execute_InOut_space(act);
  1750. if (!ret) {
  1751. printf(" In space failed\n");
  1752. isInSpace_ = false;
  1753. break;
  1754. }
  1755. } else if (act.type() == 2) { //出库
  1756. printf("出库\n");
  1757. obs_w_ = 1.25;
  1758. obs_h_ = 1.87;
  1759. ret=execute_InOut_space(act);
  1760. if (!ret) {
  1761. printf(" out space failed\n");
  1762. break;
  1763. } else {
  1764. isInSpace_ = false;
  1765. }
  1766. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1767. printf("马路导航\n");
  1768. obs_w_ = 1.1;
  1769. obs_h_ = 1.87;
  1770. ret=execute_nodes(act);
  1771. if (!ret) {
  1772. printf(" street nav failed\n");
  1773. break;
  1774. }
  1775. } else if (act.type() == 5) {
  1776. printf(" 汽车模型导航....\n");
  1777. } else if (act.type() == 6) {//夹持
  1778. ret=clamp_close();
  1779. if (ret== false) {
  1780. printf("夹持failed ...\n");
  1781. break;
  1782. }
  1783. } else if (act.type() == 7) {
  1784. if (this->clamp_fully_open() == false) {
  1785. printf("打开夹持 failed...\n");
  1786. break;
  1787. }
  1788. } else if (act.type() == 8) { //切换模式
  1789. SwitchMode(act.changedmode(), act.wheelbase());
  1790. ret=true;
  1791. } else if (act.type() == 9) { //提升机构提升
  1792. ret=lifter_rise();
  1793. if (ret == false) {
  1794. printf("提升failed ...\n");
  1795. break;
  1796. }
  1797. } else if (act.type() == 10) { //提升机构下降
  1798. ret=lifter_down();
  1799. if (ret == false) {
  1800. printf("下降failed ...\n");
  1801. break;
  1802. }
  1803. } else {
  1804. printf(" action type invalid not handled !!\n");
  1805. break;
  1806. }
  1807. newUnfinished_cations_.pop();
  1808. }
  1809. actionType_ = eReady;
  1810. Stop();
  1811. if(ret==false){
  1812. response.set_ret(-4);
  1813. response.set_info("navigation ret false");
  1814. printf(" navigation ret false\n");
  1815. while (newUnfinished_cations_.empty() == false)
  1816. newUnfinished_cations_.pop();
  1817. }
  1818. else if (cancel_ == true) {
  1819. response.set_ret(-3);
  1820. response.set_info("navigation canceled");
  1821. printf(" navigation canceled\n");
  1822. while (newUnfinished_cations_.empty() == false)
  1823. newUnfinished_cations_.pop();
  1824. } else {
  1825. if (newUnfinished_cations_.empty()) {
  1826. response.set_ret(0);
  1827. response.set_info("navigation completed!!!");
  1828. printf("navigation completed!!!\n");
  1829. } else {
  1830. response.set_ret(-4);
  1831. response.set_info("navigation Failed!!!!");
  1832. printf(" navigation Failed\n");
  1833. while (newUnfinished_cations_.empty() == false)
  1834. newUnfinished_cations_.pop();
  1835. }
  1836. }
  1837. running_ = false;
  1838. }
  1839. bool Navigation::SwitchMode(int mode, float wheelBase) {
  1840. wheelBase_ = wheelBase;
  1841. printf("Child AGV can not Switch Mode\n");
  1842. return true;
  1843. }
  1844. float Navigation::compute_cuv(const Pose2d &target) {
  1845. float x = fabs(target.x());
  1846. float y = fabs(target.y());
  1847. float cuv = atan(y / (x + 1e-8));
  1848. //printf(" ---------------------- cuv:%f\n",cuv);
  1849. return cuv;
  1850. }
  1851. void Navigation::ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response) {
  1852. return;
  1853. // pause_ = false;
  1854. // cancel_ = false;
  1855. // running_ = true;
  1856. // actionType_ = eReady;
  1857. // printf("ManualOperation: %d | start...\n", cmd.operation_type());
  1858. //
  1859. // float symbol = cmd.velocity() < 0 ? -1 : 1; // 判断为正方向or负方向
  1860. // double velocity;
  1861. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1862. // switch (cmd.operation_type()) {
  1863. // case 1: // 旋转
  1864. // velocity = 2.0 * M_PI / 180.0 * symbol;
  1865. // SendMoveCmd(move_mode_, eRotation, 0, velocity);
  1866. // actionType_ = eRotation;
  1867. // printf(" ManualOperation: %d | input angular_v: %f, down: %f\n",
  1868. // cmd.operation_type(), timedA_.Get(), velocity);
  1869. // break;
  1870. // case 2: // X平移
  1871. // velocity = 1.0 * symbol;
  1872. // SendMoveCmd(move_mode_, eVertical, velocity, 0);
  1873. // actionType_ = eVertical;
  1874. // printf(" ManualOperation: %d | input line_v : %f, down: %f\n",
  1875. // cmd.operation_type(), timedV_.Get(), velocity);
  1876. // break;
  1877. // case 3: // Y平移
  1878. // velocity = 1.0 * symbol;
  1879. // SendMoveCmd(move_mode_, eHorizontal, velocity, 0);
  1880. // actionType_ = eHorizontal;
  1881. // printf(" ManualOperation: %d | input line_v: %f, down: %f\n",
  1882. // cmd.operation_type(), timedV_.Get(), velocity);
  1883. // break;
  1884. // default:
  1885. // break;
  1886. // }
  1887. //
  1888. // actionType_ = eReady;
  1889. // response.set_ret(-3);
  1890. // response.set_info("ManualOperation: %d, canceled!!!", cmd.operation_type());
  1891. // printf("ManualOperation: %d | canceled!!!\n", cmd.operation_type());
  1892. // running_ = false;
  1893. }
  1894. bool Navigation::IsMasterAGV() {
  1895. return is_master_AGV;
  1896. }
  1897. void Navigation::ResetDoor(int32_t status) {
  1898. // printf("---------------receive status:%d\n",status);
  1899. DoorStatusMap doorStatusMap;
  1900. anasisDoorStatu(status, doorStatusMap);
  1901. // for (auto it = doorStatusMap.begin() ; it != doorStatusMap.end() ; ++it) {
  1902. // printf("doorID:%d, out:%d, ins:%d\n",it->first, it->second.first, it->second.second);
  1903. // }
  1904. timed_Door_.reset(doorStatusMap, 3);
  1905. }
  1906. void Navigation::anasisDoorStatu(int32_t i_door_status, DoorStatusMap& m_door_status) {
  1907. // int bytesize = sizeof(int32_t)*8;
  1908. m_door_status.clear();
  1909. for (int i = 1; i >= 0; --i) {
  1910. int statu = i_door_status>>(i*16);
  1911. int door_id = (statu&0xFF00)>>8;
  1912. int outside_door_status = statu&0x00FF;
  1913. int inside_door_status = outside_door_status>>2;
  1914. if ((outside_door_status & eDoorOpened) == eDoorOpened){
  1915. outside_door_status = eDoorOpened;
  1916. } else if ((outside_door_status & eDoorClosed) == eDoorClosed){
  1917. outside_door_status = eDoorClosed;
  1918. } else{
  1919. outside_door_status = eDoorInvalid;
  1920. }
  1921. if ((inside_door_status & eDoorOpened) == eDoorOpened){
  1922. inside_door_status = eDoorOpened;
  1923. } else if ((inside_door_status & eDoorClosed) == eDoorClosed){
  1924. inside_door_status = eDoorClosed;
  1925. } else{
  1926. inside_door_status = eDoorInvalid;
  1927. }
  1928. m_door_status[door_id] = DoorStatusInOnePort((DoorStatu)outside_door_status, (DoorStatu)inside_door_status);
  1929. }
  1930. }
  1931. #define SWAP_BYTES 0
  1932. void Navigation::anasisCarierStatu(int& regionID, CarrierStatuRegionMap& status, int32_t bytes){
  1933. int32_t reverseBytes=bytes;
  1934. if(SWAP_BYTES){
  1935. reverseBytes = (bytes>>24)| ((bytes&0xFF0000)>>8)|((bytes&0xFF00)<<8)|((bytes&0xFF)<<24);
  1936. }
  1937. int bytesize=sizeof(int32_t)*8;
  1938. regionID = (bytes>>((bytesize-1)*8));
  1939. for (int i = 0; i < (bytesize-8)/2; ++i) {
  1940. int statu =(reverseBytes>>(i*2) & 0x03);
  1941. if ((statu & eCarrierUp) == eCarrierUp) {
  1942. status[i+1]=eCarrierUp;
  1943. }
  1944. else if ((statu & eCarrierDown) == eCarrierDown){
  1945. status[i+1]=eCarrierDown;
  1946. }
  1947. else {
  1948. status[i+1]=eCarrierInvalid;
  1949. }
  1950. }
  1951. }
  1952. void Navigation::ResetCarrier(std::vector<int32_t> status) {
  1953. for(int32_t statu : status){
  1954. int regionID;
  1955. CarrierStatuRegionMap region;
  1956. anasisCarierStatu(regionID,region,statu);
  1957. timed_Carrier_[regionID].reset(region,5);
  1958. }
  1959. /*printf("---------------------------------------\n");
  1960. for(std::map<int,TimedLockData<CarrierStatuRegionMap>>::iterator it=timed_Carrier_.begin();
  1961. it!=timed_Carrier_.end();it++){
  1962. int regionID=it->first;
  1963. if(it->second.timeout())
  1964. continue;
  1965. printf(">>>>>>> Region ID :%d\n",regionID);
  1966. std::map<int,CarrierStatu> region=it->second.Get();
  1967. for(std::map<int,CarrierStatu>::iterator it1=region.begin();
  1968. it1!=region.end();++it1){
  1969. int id=it1->first;
  1970. CarrierStatu sta=it1->second;
  1971. printf("\t \tcarrier id :%d statu:%d\n",id,sta);
  1972. }
  1973. }*/
  1974. }
  1975. void Navigation::Stringsplit(const std::string& str, const std::string splits, std::vector<std::string>& res){
  1976. res.clear();
  1977. if(str.empty())
  1978. return;
  1979. std::string strs = str+splits;
  1980. size_t pos = strs.find(splits);
  1981. int step = splits.size();
  1982. while (pos!= std::string::npos){
  1983. std::string temp = strs.substr(0, pos);
  1984. res.push_back(temp);
  1985. strs = strs.substr(pos+step,strs.size());
  1986. pos = strs.find(splits);
  1987. }
  1988. }
  1989. bool Navigation::LoadSpaceNo2CarrierNo(){
  1990. std::string file_name = "../config/SpaceNO2CarrarierNO.txt";
  1991. std::ifstream in(file_name);
  1992. std::string line;
  1993. std::vector<std::string> data_in_one_line;
  1994. if (in){
  1995. short space_id, region_id, carrier_no;
  1996. while(std::getline(in,line)){
  1997. Stringsplit(line, " ",data_in_one_line);
  1998. space_id = std::stoi(data_in_one_line[0]);
  1999. region_id = std::stoi(data_in_one_line[1]);
  2000. carrier_no = std::stoi(data_in_one_line[2]);
  2001. spaceNo_2_region_carrier_excel_[space_id] = Region_Carry(region_id,carrier_no);
  2002. }
  2003. return true;
  2004. } else{
  2005. printf("no SpaceNO2CarrarierNO.txt in config!\n");
  2006. }
  2007. return false;
  2008. }
  2009. bool Navigation::SpaceNo2CarrierNo(int space_no, int& region_id, int& carrier_no) {
  2010. Region_Carry region_carry = spaceNo_2_region_carrier_excel_[space_no];
  2011. region_id = region_carry.first;
  2012. carrier_no = region_carry.second;
  2013. return true;
  2014. }
  2015. Navigation::CarrierStatu Navigation::GetCarrierStatusBySpaceID(int space_id) {
  2016. int region_id;
  2017. int carrier_no;
  2018. if (SpaceNo2CarrierNo(space_id,region_id,carrier_no) == false){
  2019. printf("no matched carrier!\n");
  2020. return eCarrierInvalid;
  2021. }
  2022. printf("space_id:%d, region_id:%d, carrier_no:%d\n",space_id,region_id,carrier_no);
  2023. if (timed_Carrier_[region_id].timeout()){
  2024. printf("carrier status is timeout!\n");
  2025. return eCarrierInvalid;
  2026. }
  2027. else{
  2028. return timed_Carrier_[region_id].Get()[carrier_no];
  2029. }
  2030. return eCarrierInvalid;
  2031. }
  2032. int Navigation::GetPortIDBySpace(NavMessage::PathNode node) {
  2033. int port_id = GetSpaceId(node);
  2034. if (port_id == 1101){
  2035. return 1;
  2036. }
  2037. else if (port_id == 1102){
  2038. return 2;
  2039. }
  2040. return -1;
  2041. }