navigation.cpp 62 KB

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