navigation.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. return false;
  52. } else {
  53. if (timedPose_.timeout()) {
  54. printf(" current pose is timeout \n");
  55. }
  56. if (timedBrotherPose_.timeout()) {
  57. printf(" brother pose is timeout \n");
  58. }
  59. return true;
  60. }
  61. }
  62. bool Navigation::Init(const NavParameter::Navigation_parameter &parameter) {
  63. parameter_ = parameter;
  64. NavParameter::AgvEmqx_parameter agv_p = parameter.agv_emqx();
  65. if (monitor_ == nullptr) {
  66. monitor_ = new Monitor_emqx(agv_p.nodeid());
  67. if (monitor_->Connect(agv_p.ip(), agv_p.port()) == false) {
  68. printf(" agv emqx connected failed\n");
  69. return false;
  70. }
  71. monitor_->set_speedcmd_topic(agv_p.pubspeedtopic());
  72. monitor_->AddCallback(agv_p.subposetopic(), Navigation::RobotPoseCallback, this);
  73. monitor_->AddCallback(agv_p.subspeedtopic(), Navigation::RobotSpeedCallback, this);
  74. }
  75. NavParameter::Emqx_parameter terminal_p = parameter.terminal_emqx();
  76. if (terminator_ == nullptr) {
  77. terminator_ = new Terminator_emqx(terminal_p.nodeid());
  78. if (terminator_->Connect(terminal_p.ip(), terminal_p.port()) == false) {
  79. printf(" terminator emqx connected failed\n");
  80. return false;
  81. }
  82. }
  83. if (parameter.has_brother_emqx()) {
  84. NavParameter::BrotherEmqx brotherEmqx = parameter.brother_emqx();
  85. if (brotherEmqx_ == nullptr) {
  86. brotherEmqx_ = new Terminator_emqx(brotherEmqx.nodeid());
  87. if (brotherEmqx_->Connect(brotherEmqx.ip(), brotherEmqx.port()) == true) {
  88. brotherEmqx_->AddCallback(brotherEmqx.subbrotherstatutopic(), Navigation::BrotherAgvStatuCallback,
  89. this);
  90. } else {
  91. printf(" brother emqx connected failed\n");
  92. // return false;
  93. }
  94. }
  95. }
  96. inited_ = true;
  97. if (pubthread_ != nullptr) {
  98. exit_ = true;
  99. if (pubthread_->joinable())
  100. pubthread_->join();
  101. delete pubthread_;
  102. }
  103. exit_ = false;
  104. pubthread_ = new std::thread(&Navigation::pubStatuThreadFuc, this);
  105. return true;
  106. }
  107. void Navigation::RobotPoseCallback(const MqttMsg &msg, void *context) {
  108. Navigation *navigator = (Navigation *) context;
  109. NavMessage::LidarOdomStatu statu;
  110. if (msg.toProtoMessage(statu)) {
  111. navigator->ResetPose(Pose2d(statu.x(), statu.y(), statu.theta()));
  112. }
  113. }
  114. void Navigation::HandleAgvStatu(const MqttMsg &msg) {
  115. NavMessage::AgvStatu speed;
  116. if (msg.toProtoMessage(speed)) {
  117. ResetStatu(speed.v(), speed.w());
  118. ResetClamp((ClampStatu) speed.clamp());
  119. }
  120. }
  121. void Navigation::RobotSpeedCallback(const MqttMsg &msg, void *context) {
  122. Navigation *navigator = (Navigation *) context;
  123. navigator->HandleAgvStatu(msg);
  124. }
  125. void Navigation::pubStatuThreadFuc(void *p) {
  126. Navigation *navogator = (Navigation *) p;
  127. if (navogator) {
  128. while (navogator->exit_ == false) {
  129. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  130. NavMessage::NavStatu statu;
  131. statu.set_main_agv(false);
  132. navogator->publish_statu(statu);
  133. }
  134. }
  135. }
  136. void Navigation::publish_statu(NavMessage::NavStatu &statu) {
  137. if (timedPose_.timeout() == false) {
  138. NavMessage::LidarOdomStatu odom;
  139. odom.set_x(timedPose_.Get().x());
  140. odom.set_y(timedPose_.Get().y());
  141. odom.set_theta(timedPose_.Get().theta());
  142. if (timedV_.timeout() == false)
  143. odom.set_v(timedV_.Get());
  144. if (timedA_.timeout() == false)
  145. odom.set_vth(timedA_.Get());
  146. statu.mutable_odom()->CopyFrom(odom);
  147. }
  148. statu.set_in_space(isInSpace_);
  149. if (isInSpace_) statu.set_space_id(space_id_);
  150. //发布nav状态
  151. statu.set_statu(actionType_); //
  152. statu.set_move_mode(move_mode_);
  153. if (running_) {
  154. statu.set_key(global_navCmd_.key());
  155. }
  156. //发布MPC信息
  157. //发布mpc 预选点
  158. if (selected_traj_.timeout() == false) {
  159. for (int i = 0; i < selected_traj_.Get().size(); ++i) {
  160. Pose2d pt = selected_traj_.Get()[i];
  161. NavMessage::Pose2d *pose = statu.mutable_selected_traj()->add_poses();
  162. pose->set_x(pt.x());
  163. pose->set_y(pt.y());
  164. pose->set_theta(pt.theta());
  165. }
  166. }
  167. //发布 mpc 预测点
  168. if (predict_traj_.timeout() == false) {
  169. for (int i = 0; i < predict_traj_.Get().size(); ++i) {
  170. Pose2d pt = predict_traj_.Get()[i];
  171. NavMessage::Pose2d *pose = statu.mutable_predict_traj()->add_poses();
  172. pose->set_x(pt.x());
  173. pose->set_y(pt.y());
  174. pose->set_theta(pt.theta());
  175. }
  176. }
  177. //std::cout<<"nav statu:"<<statu.DebugString()<<std::endl;
  178. MqttMsg msg;
  179. msg.fromProtoMessage(statu);
  180. if (terminator_)
  181. terminator_->Publish(parameter_.terminal_emqx().pubnavstatutopic(), msg);
  182. //发布位姿 ------robot状态--------------------------
  183. NavMessage::RobotStatu robot;
  184. if (CreateRobotStatuMsg(robot)) {
  185. if (terminator_) {
  186. MqttMsg msg;
  187. msg.fromProtoMessage(robot);
  188. terminator_->Publish(parameter_.terminal_emqx().pubstatutopic(), msg);
  189. }
  190. }
  191. }
  192. bool Navigation::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  193. //printf(" %d %d %d \n",timedPose_.timeout(),timedV_.timeout(),timedA_.timeout());
  194. if (timedPose_.timeout() == false) {
  195. Pose2d pose = timedPose_.Get();
  196. robotStatu.set_x(pose.x());
  197. robotStatu.set_y(pose.y());
  198. robotStatu.set_theta(pose.theta());
  199. if ((timedV_.timeout() == false && timedA_.timeout() == false)) {
  200. NavMessage::AgvStatu plc;
  201. plc.set_v(timedV_.Get());
  202. plc.set_w(timedA_.Get());
  203. plc.set_clamp(timed_clamp_.Get());
  204. //printf(" timed_clamp_:%d\n ",timed_clamp_.Get());
  205. robotStatu.mutable_agvstatu()->CopyFrom(plc);
  206. }
  207. return true;
  208. }
  209. return false;
  210. }
  211. void Navigation::ResetStatu(double v, double a) {
  212. timedV_.reset(v, 0.5);
  213. timedA_.reset(a, 0.5);
  214. }
  215. void Navigation::ResetClamp(ClampStatu statu) {
  216. timed_clamp_.reset(statu, 1);
  217. }
  218. void Navigation::ResetPose(const Pose2d &pose) {
  219. timedPose_.reset(pose, 1.0);
  220. }
  221. void Navigation::Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  222. cancel_ = true;
  223. response.set_ret(0);
  224. }
  225. bool Navigation::Stop() {
  226. if (monitor_) {
  227. monitor_->stop();
  228. while (cancel_ == false) {
  229. if (timedV_.timeout() == false && timedA_.timeout() == false) {
  230. if (fabs(timedV_.Get()) < 1e-2 && fabs(timedA_.Get()) < 1e-3)
  231. return true;
  232. else
  233. monitor_->stop();
  234. printf("Stoped wait V/A ==0(1e-4,1e-4),V:%f,A:%f\n", fabs(timedV_.Get()), fabs(timedA_.Get()));
  235. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  236. } else {
  237. printf("Stop failed V/A timeout\n");
  238. return false;
  239. }
  240. }
  241. }
  242. printf("stoped ret false.\n");
  243. return false;
  244. }
  245. bool Navigation::SlowlyStop() {
  246. double acc = 3.0;
  247. double dt = 0.1;
  248. while (cancel_ == false) {
  249. if (timedV_.timeout() == false) {
  250. double v = timedV_.Get();
  251. if (fabs(v < 1e-2 && fabs(timedA_.Get()) < 1e-3))
  252. return true;
  253. if (v > 0) {
  254. double new_v = v - acc * dt;
  255. new_v = new_v > 0 ? new_v : 0;
  256. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  257. } else {
  258. double new_v = v + acc * dt;
  259. new_v = new_v < 0 ? new_v : 0;
  260. SendMoveCmd(move_mode_, actionType_, new_v, 0);
  261. }
  262. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  263. } else {
  264. printf("Stop failed V/A timeout\n");
  265. return false;
  266. }
  267. }
  268. printf("stoped ret false.\n");
  269. return false;
  270. }
  271. Navigation::Navigation() {
  272. isInSpace_ = false; //是否在车位或者正在进入车位
  273. RWheel_position_ = eUnknow;
  274. move_mode_ = eSingle;
  275. monitor_ = nullptr;
  276. terminator_ = nullptr;
  277. timedBrotherPose_.reset(Pose2d(-100, 0, 0), 0.5);
  278. }
  279. void Navigation::BrotherAgvStatuCallback(const MqttMsg &msg, void *context) {
  280. Navigation *navigator = (Navigation *) context;
  281. NavMessage::NavStatu brother_statu;
  282. if (msg.toProtoMessage(brother_statu) == false) {
  283. std::cout << " msg transform to AGVStatu failed,msg:" << msg.data() << std::endl;
  284. return;
  285. }
  286. //std::cout<<brother_nav.DebugString()<<std::endl<<std::endl;
  287. if (brother_statu.has_odom()) {
  288. NavMessage::LidarOdomStatu odom = brother_statu.odom();
  289. Pose2d pose(odom.x(), odom.y(), odom.theta());
  290. navigator->timedBrotherPose_.reset(pose, 1);
  291. navigator->timedBrotherV_.reset(odom.v(), 1);
  292. navigator->timedBrotherA_.reset(odom.vth(), 1);
  293. navigator->timedBrotherNavStatu_.reset(brother_statu, 0.1);
  294. }
  295. }
  296. bool Navigation::RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect) {
  297. while (cancel_ == false) {
  298. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  299. if (timedPose_.timeout()) {
  300. printf(" RotateBytarget failed pose timeout\n");
  301. return false;
  302. }
  303. if (timedA_.timeout()) {
  304. printf(" RotateBytarget failed wmg timeout\n");
  305. return false;
  306. }
  307. Pose2d targetInPose = Pose2d::relativePose(target, timedPose_.Get());
  308. float yawDiff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  309. float minYawdiff = yawDiff;
  310. if (anyDirect) {
  311. Pose2d p0 = timedPose_.Get();
  312. float yawdiff[4] = {0};
  313. yawdiff[0] = yawDiff;
  314. Pose2d relativePose;
  315. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI / 2));
  316. yawdiff[1] = Pose2d::vector2yaw(relativePose.x(), relativePose.y()); ////使用减法,保证角度在【-pi pi】
  317. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, M_PI));
  318. yawdiff[2] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  319. relativePose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, 3 * M_PI / 2));
  320. yawdiff[3] = Pose2d::vector2yaw(relativePose.x(), relativePose.y());
  321. for (int i = 1; i < 4; ++i) {
  322. if (fabs(yawdiff[i]) < fabs(minYawdiff)) {
  323. minYawdiff = yawdiff[i];
  324. targetInPose = Pose2d::relativePose(target, p0 - Pose2d(0, 0, i * M_PI / 2.0));
  325. }
  326. }
  327. }
  328. //std::cout<<" Rotate refer to target:"<<target<<",targetInPose:"
  329. //<<targetInPose<<",anyDirect:"<<anyDirect<<std::endl;
  330. //以当前点为原点,想方向经过目标点的二次曲线曲率>0.25,则需要调整 y=a x*x
  331. float cuv = compute_cuv(targetInPose);
  332. float dt = 0.1;
  333. float acc_angular = 15 * M_PI / 180.0;
  334. if (cuv > 2 * M_PI / 180.0) {
  335. double theta = limit_gause(minYawdiff, limit_rotate.min, limit_rotate.max);
  336. double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  337. double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  338. SendMoveCmd(move_mode_, eRotation, 0, limit_angular);
  339. actionType_ = eRotation;
  340. printf(" Rotate | input angular:%f,next angular:%f,down:%f diff:%f anyDirect:%d\n",
  341. timedA_.Get(), angular, limit_angular, minYawdiff, anyDirect);
  342. continue;
  343. } else {
  344. if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  345. printf(" Rotate refer target completed,cuv:%f\n", cuv);
  346. return true;
  347. }
  348. continue;
  349. }
  350. }
  351. return false;
  352. }
  353. Navigation::MpcResult Navigation::RotateReferToTarget(NavMessage::PathNode node, stLimit limit_rotate, int directions) {
  354. if (inited_ == false) {
  355. printf(" MPC_rotate has not inited\n");
  356. return eMpcFailed;
  357. }
  358. if (PoseTimeout()) {
  359. printf(" MPC_rotate Error:Pose is timeout \n");
  360. return eMpcFailed;
  361. }
  362. printf(" exec MPC_rotate autoDirect:%d\n", directions);
  363. while (cancel_ == false) {
  364. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  365. if (pause_ == true) {
  366. //发送暂停消息
  367. continue;
  368. }
  369. // if (PoseTimeout()) {
  370. // printf(" MPC_rotate Error:Pose is timeout \n");
  371. // return eMpcFailed;
  372. // }
  373. // if (timedA_.timeout()) {
  374. // printf(" MPC_rotate Error:v/a is timeout \n");
  375. // return eMpcFailed;
  376. // }
  377. Pose2d current = timedPose_.Get();
  378. Pose2d target(node.x(), node.y(), node.theta());
  379. Pose2d targetInPose = Pose2d::relativePose(target, current);
  380. // 需要旋转的角度
  381. float target_yaw_diff = Pose2d::vector2yaw(targetInPose.x(), targetInPose.y());
  382. // 自由方向
  383. if (directions) {
  384. std::vector<double> yaw_diffs;
  385. if (directions & Direction::eForward) {
  386. yaw_diffs.push_back(target_yaw_diff);
  387. }
  388. if (directions & Direction::eBackward) {
  389. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, M_PI));
  390. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  391. }
  392. if (directions & Direction::eLeft) {
  393. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 1.0 / 2 * M_PI));
  394. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  395. }
  396. if (directions & Direction::eRight) {
  397. Pose2d relative_pose = Pose2d::relativePose(targetInPose, Pose2d(0, 0, 3.0 / 2 * M_PI));
  398. yaw_diffs.push_back(Pose2d::vector2yaw(relative_pose.x(), relative_pose.y()));
  399. }
  400. for (auto i = 0; i < yaw_diffs.size(); ++i) {
  401. if (fabs(yaw_diffs[i]) < fabs(target_yaw_diff))
  402. target_yaw_diff = yaw_diffs[i];
  403. }
  404. std::cout << std::endl;
  405. // std::cout << std::endl << " min_diff: " << current_to_target.theta() << std::endl;
  406. }
  407. //一次变速
  408. std::vector<double> out;
  409. bool ret;
  410. TimerRecord::Execute([&, this]() {
  411. ret = Rotation_mpc_once(Pose2d(0, 0, target_yaw_diff), limit_rotate, out);
  412. }, "Rotation_mpc_once");
  413. if (ret == false) {
  414. Stop();
  415. return eMpcFailed;
  416. }
  417. //下发速度
  418. if (fabs(target_yaw_diff) > 2 * M_PI / 180.0) {
  419. SendMoveCmd(move_mode_, eRotation, 0, out[0]);
  420. actionType_ = eRotation;
  421. printf(" MPC_rotate | input angular_v:%f, down:%f, diff:%f autoDirect:%d\n",
  422. timedA_.Get(), out[0], target_yaw_diff, directions);
  423. } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  424. printf(" MPC_rotate refer target completed,cuv:%f\n", target_yaw_diff);
  425. return eMpcSuccess;
  426. }
  427. continue;
  428. }
  429. return eMpcFailed;
  430. }
  431. bool Navigation::MoveToTarget(NavMessage::PathNode node, Navigation::stLimit limit_v, Navigation::stLimit limit_w,
  432. bool anyDirect, bool enable_rotate) {
  433. if (IsArrived(node)) {
  434. return true;
  435. }
  436. bool already_in_mpc = false;
  437. while (cancel_ == false) {
  438. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  439. if (PoseTimeout()) {
  440. printf(" navigation Error:Pose is timeout \n");
  441. break;
  442. }
  443. Pose2d current = timedPose_.Get();
  444. if (IsArrived(node)) {
  445. break;
  446. }
  447. //两个方向都无法到达目标点,旋转 朝向目标点
  448. bool x_enable = PossibleToTarget(node, false);
  449. bool y_enable = PossibleToTarget(node, true);
  450. bool enableTotarget = x_enable || y_enable;
  451. if (anyDirect == false) {
  452. enableTotarget = x_enable;
  453. }
  454. //巡线无法到达目标点
  455. if (enableTotarget == false) {
  456. if (already_in_mpc && enable_rotate == false) {
  457. printf(" Move to node failed ,impossible to target and rotate is not enabled\n");
  458. return false;
  459. }
  460. Pose2d target(node.x(), node.y(), 0);
  461. // if (RotateReferToTarget(target, limit_w, anyDirect) == false) {
  462. int directions = Direction::eForward;
  463. if (anyDirect)
  464. directions = Direction::eForward | Direction::eBackward |
  465. Direction::eLeft | Direction::eRight;
  466. if (RotateReferToTarget(node, limit_w, directions) != eMpcSuccess) {
  467. std::cout << " Rotate refer to target failed,target: " << target <<
  468. " directions: " << std::hex << directions << " line_: " << __LINE__ << std::endl;
  469. return false;
  470. }
  471. continue;
  472. }
  473. already_in_mpc = true;
  474. MpcResult ret = MpcToTarget(node, limit_v, anyDirect);
  475. if (ret == eMpcSuccess) {
  476. printf(" MPC to target:%f %f l:%f,w:%f theta:%f completed\n",
  477. node.x(), node.y(), node.l(), node.w(), node.theta());
  478. break;
  479. } else if (ret == eImpossibleToTarget) {
  480. printf(" MPC to target:%f %f l:%f,w:%f theta:%f impossible ,retry ..............................\n",
  481. node.x(), node.y(), node.l(), node.w(), node.theta());
  482. } else {
  483. return false;
  484. }
  485. }
  486. if (cancel_) {
  487. return false;
  488. }
  489. return true;
  490. }
  491. bool Navigation::execute_nodes(NavMessage::NewAction action) {
  492. if (action.type() == 3 || action.type() == 4) //最优动作导航 or 导航中保证朝向严格一致
  493. {
  494. if (!parameter_.has_nodeangularlimit() || !parameter_.has_nodevelocitylimit() || action.pathnodes_size() == 0) {
  495. std::cout << "execute pathNodes failed ,Action invalid:" << action.DebugString() << std::endl;
  496. return false;
  497. }
  498. bool anyDirect = (action.type() == 3);
  499. //速度限制
  500. stLimit adjust_angular = {parameter_.nodeangularlimit().min(), parameter_.nodeangularlimit().max()};
  501. stLimit mpc_velocity = {parameter_.nodevelocitylimit().min(), parameter_.nodevelocitylimit().max()};
  502. for (int i = 0; i < action.pathnodes_size(); ++i) {
  503. NavMessage::PathNode node = action.pathnodes(i);
  504. if (i + 1 < action.pathnodes_size()) {
  505. NavMessage::PathNode next = action.pathnodes(i + 1);
  506. Pose2d vec(next.x() - node.x(), next.y() - node.y(), 0);
  507. node.set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  508. node.set_l(0.3);
  509. node.set_w(0.1);
  510. } else {
  511. node.set_theta(0);
  512. node.set_w(0.2);
  513. node.set_l(0.2);
  514. }
  515. if (MoveToTarget(node, mpc_velocity, adjust_angular, anyDirect, true) == false)
  516. return false;
  517. else
  518. isInSpace_ = false;
  519. }
  520. return true;
  521. }
  522. printf("execute PathNode failed ,action type is not 3/4 :%d\n", action.type());
  523. return false;
  524. }
  525. Navigation::MpcResult
  526. Navigation::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target) {
  527. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  528. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  529. return eMpcFailed;
  530. }
  531. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  532. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  533. double acc_angular = 20 * M_PI / 180.0;
  534. double dt = 0.1;
  535. Pose2d rotated = timedPose_.Get();
  536. //前车先到,当前车进入2点,保持与前车一致的朝向
  537. if (brother.in_space() && brother.space_id() == space.id()) {
  538. rotated.mutable_theta() = brother.odom().theta();
  539. } else { //当前车先到(后车),倒车入库
  540. rotated.mutable_theta() = space.theta();
  541. rotated = rotated.rotate(rotated.x(), rotated.y(), M_PI);
  542. }
  543. double x = space.x() - wheelbase / 2 * cos(rotated.theta());
  544. double y = space.y() - wheelbase / 2 * sin(rotated.theta());
  545. target.set_x(x);
  546. target.set_y(y);
  547. target.set_theta(rotated.theta());
  548. target.set_l(0.05);
  549. target.set_w(0.03);
  550. target.set_id(space.id());
  551. //整车协调的时候,保持前后与车位一致即可
  552. if (move_mode_ == eDouble) {
  553. double yawDiff1 = (rotated - timedPose_.Get()).theta();
  554. double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  555. if (fabs(yawDiff1) < fabs(yawDiff2)) {
  556. rotated = rotated + Pose2d(0, 0, M_PI);
  557. }
  558. }
  559. while (cancel_ == false) {
  560. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  561. Pose2d current = timedPose_.Get();
  562. double yawDiff = (rotated - current).theta();
  563. //一次变速
  564. std::vector<double> out;
  565. bool ret;
  566. TimerRecord::Execute([&, this]() {
  567. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  568. }, "Rotation_mpc_once");
  569. if (ret == false) {
  570. Stop();
  571. return eMpcFailed;
  572. }
  573. //下发速度
  574. if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  575. SendMoveCmd(move_mode_, eRotation, 0, out[0]);
  576. actionType_ = eRotation;
  577. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f diff:%f anyDirect:false\n",
  578. timedA_.Get(), out[0], yawDiff);
  579. } else if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  580. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  581. return eMpcSuccess;
  582. }
  583. continue;
  584. // if (fabs(yawDiff) > 1 * M_PI / 180.0) {
  585. // double theta = limit_gause(yawDiff, limit_rotate.min, limit_rotate.max);
  586. // double angular = next_speed(timedA_.Get(), theta, acc_angular, dt);
  587. // double limit_angular = limit(angular, limit_rotate.min, limit_rotate.max);
  588. //
  589. // SendMoveCmd(move_mode_, Monitor_emqx::eRotation, 0, limit_angular);
  590. // actionType_ = eRotation;
  591. // printf(" RotateBeforeEnterSpace | input anguar:%f,next angular:%f,down:%f diff:%f anyDirect:false\n",
  592. // timedA_.Get(), angular, limit_angular, yawDiff);
  593. //
  594. // continue;
  595. // } else {
  596. // if (fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  597. // printf(" RotateBeforeEnterSpace refer target completed\n");
  598. // printf("---------------- update new target :%f %f %f \n", target.x(), target.y(), target.theta());
  599. // return true;
  600. // }
  601. // }
  602. }
  603. return eMpcFailed;
  604. }
  605. bool Navigation::execute_InOut_space(NavMessage::NewAction action) {
  606. if (action.type() != 1 && action.type() != 2) {
  607. printf(" Inout_space failed : msg action type must ==1\n ");
  608. return false;
  609. }
  610. if (!parameter_.has_inoutvlimit() || !action.has_spacenode() ||
  611. !action.has_streetnode()) {
  612. std::cout << "execute enter_space failed ,Action miss some infos:" << action.DebugString() << std::endl;
  613. return false;
  614. }
  615. if (PoseTimeout() == true) {
  616. printf(" inout_space failed type:%d : current pose is timeout\n", action.type());
  617. return false;
  618. }
  619. stLimit limit_v = {parameter_.inoutvlimit().min(), parameter_.inoutvlimit().max()};
  620. stLimit limit_w = {2 * M_PI / 180.0, 20 * M_PI / 180.0};
  621. //入库,起点为streetNode
  622. if (action.type() == 1) {
  623. Pose2d vec(action.spacenode().x() - action.streetnode().x(), action.spacenode().y() - action.streetnode().y(),
  624. 0);
  625. action.mutable_streetnode()->set_l(0.2);
  626. action.mutable_streetnode()->set_w(0.05);
  627. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  628. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, true) == false) {
  629. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  630. return false;
  631. }
  632. //移动到库位点, 禁止任意方向// 不允许巡线中停下旋转(当无法到达目标点时,返回false)
  633. printf("inout ----------------------------------------\n");
  634. //计算车位朝向
  635. action.mutable_spacenode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  636. //计算当前车是进入车位1点还是2点
  637. NavMessage::PathNode new_target;
  638. if (RotateBeforeEnterSpace(action.spacenode(), action.wheelbase(), new_target) == eMpcFailed) {
  639. return false;
  640. }
  641. if (MoveToTarget(new_target, limit_v, limit_w, true, false) == false) {
  642. printf(" Enter space failed type:%d: MoveTo StreetNode failed\n", action.type());
  643. return false;
  644. }
  645. } else if (action.type() == 2) {
  646. Pose2d space(action.spacenode().x(), action.spacenode().y(), 0);
  647. Pose2d diff = Pose2d::abs(Pose2d::relativePose(space, timedPose_.Get()));
  648. if (diff.y() < 0.05) {
  649. //出库,移动到马路点,允许自由方向,不允许中途旋转
  650. Pose2d vec(action.streetnode().x() - action.spacenode().x(),
  651. action.streetnode().y() - action.spacenode().y(), 0);
  652. action.mutable_streetnode()->set_theta(Pose2d::vector2yaw(vec.x(), vec.y()));
  653. action.mutable_streetnode()->set_l(0.2);
  654. action.mutable_streetnode()->set_w(0.1);
  655. if (MoveToTarget(action.streetnode(), limit_v, limit_w, true, false) == false) {
  656. printf(" out space failed type:%d: MoveTo StreetNode failed\n", action.type());
  657. return false;
  658. }
  659. } else {
  660. std::cout << " Out space failed: current pose too far from space node:" << space << ",diff:" << diff
  661. << std::endl;
  662. return false;
  663. }
  664. }
  665. return true;
  666. }
  667. /* current_to_target: current_to_target.theta()为所需旋转的角度
  668. * */
  669. bool Navigation::Rotation_mpc_once(Pose2d current_to_target, Navigation::stLimit limit_wmg, std::vector<double> &out,
  670. bool all_direct) {
  671. if (PoseTimeout() == true) {
  672. printf(" Rotation_mpc_once Error:Pose is timeout \n");
  673. return false;
  674. }
  675. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  676. printf(" Rotation_mpc_once Error:V/A is timeout \n");
  677. return false;
  678. }
  679. Pose2d pose = timedPose_.Get();
  680. double line_velocity = timedV_.Get(); //从plc获取状态
  681. double angular_velocity = timedA_.Get();
  682. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  683. statu[0] = pose.x();
  684. statu[1] = pose.y();
  685. statu[2] = pose.theta();
  686. statu[3] = line_velocity;
  687. statu[4] = angular_velocity;
  688. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  689. double obs_w = 2;//0.85;
  690. double obs_h = 3;//1.45;
  691. MpcError ret = failed;
  692. Pose2d brother = timedBrotherPose_.Get();
  693. Pose2d brother_in_self = Pose2d::relativePose(brother, pose);
  694. // std::cout<<" brother_in_self: "<<brother_in_self<<std::endl;
  695. RotateMPC MPC(brother_in_self, obs_w, obs_h, limit_wmg.min, limit_wmg.max);
  696. RotateMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  697. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  698. ret = MPC.solve(current_to_target, statu, parameter, out);
  699. if (ret == no_solution) {
  700. printf(" Rotation_mpc solve no solution set v/w = 0\n");
  701. out.clear();
  702. out.push_back(0);
  703. }
  704. return ret == success || ret == no_solution;
  705. }
  706. bool Navigation::mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY) {
  707. if (PoseTimeout() == true) {
  708. printf(" MPC once Error:Pose is timeout \n");
  709. return false;
  710. }
  711. if (timedV_.timeout() == true || timedA_.timeout() == true) {
  712. printf(" MPC once Error:V/A is timeout \n");
  713. return false;
  714. }
  715. if (traj.size() == 0) {
  716. printf("traj size ==0\n");
  717. return false;
  718. }
  719. Pose2d pose = timedPose_.Get();
  720. double velocity = timedV_.Get(); //从plc获取状态
  721. double angular = timedA_.Get();
  722. Eigen::VectorXd statu = Eigen::VectorXd::Zero(5);//agv状态
  723. statu[0] = pose.x();
  724. statu[1] = pose.y();
  725. statu[2] = pose.theta();
  726. statu[3] = velocity;
  727. statu[4] = angular;
  728. Trajectory optimize_trajectory;
  729. Trajectory selected_trajectory;
  730. NavParameter::MPC_parameter mpc_parameter = parameter_.x_mpc_parameter();
  731. double obs_w = 0.85;
  732. double obs_h = 1.45;
  733. if (directY == true) {
  734. //将车身朝向旋转90°,车身朝向在(-pi,pi]
  735. statu[2] += M_PI / 2;
  736. if (statu[2] > M_PI)
  737. statu[2] -= 2 * M_PI;
  738. mpc_parameter = parameter_.y_mpc_parameter();
  739. obs_w = 1.45;
  740. obs_h = 0.85;
  741. }
  742. Pose2d brother = timedBrotherPose_.Get();
  743. Pose2d relative = Pose2d::relativePose(brother, pose);
  744. if (directY)
  745. relative = Pose2d::relativePose(brother.rotate(brother.x(), brother.y(), M_PI / 2),
  746. pose.rotate(pose.x(), pose.y(), M_PI / 2));//计算另一节在当前小车坐标系下的坐标
  747. //std::cout<<"pose:"<<pose<<", brother:"<<brother<<", relative:"<<relative<<std::endl;
  748. if (move_mode_ == ActionMode::eDouble)
  749. relative = Pose2d(-1e8, -1e8, 0);
  750. MpcError ret = failed;
  751. LoadedMPC MPC(relative, obs_w, obs_h, limit_v.min, limit_v.max);
  752. LoadedMPC::MPC_parameter parameter = {mpc_parameter.shortest_radius(), mpc_parameter.dt(),
  753. mpc_parameter.acc_velocity(), mpc_parameter.acc_angular()};
  754. //printf(" r:%f dt:%f acc:%f acc_a:%f\n",mpc_parameter.shortest_radius(),
  755. // mpc_parameter.dt(),mpc_parameter.acc_velocity(),mpc_parameter.acc_angular());
  756. ret = MPC.solve(traj, traj[traj.size() - 1], statu, parameter,
  757. out, selected_trajectory, optimize_trajectory);
  758. //std::cout<<" traj size %d %d -------- "<<selected_trajectory.size()<<","<<optimize_trajectory.size()<<std::endl;
  759. if (ret == no_solution) {
  760. printf(" mpc solve no solution set v/w = 0\n");
  761. out.clear();
  762. out.push_back(0);
  763. out.push_back(0);
  764. }
  765. predict_traj_.reset(optimize_trajectory, 1);
  766. selected_traj_.reset(selected_trajectory, 1);
  767. return ret == success || ret == no_solution;
  768. }
  769. bool Navigation::IsArrived(NavMessage::PathNode targetNode) {
  770. if (timedPose_.timeout() || timedV_.timeout() || timedA_.timeout()) {
  771. printf(" pose / v / w timeout,IsArrived return false\n");
  772. return false;
  773. }
  774. Pose2d current = timedPose_.Get();
  775. double x = current.x();
  776. double y = current.y();
  777. double tx = targetNode.x();
  778. double ty = targetNode.y();
  779. double l = fabs(targetNode.l());
  780. double w = fabs(targetNode.w());
  781. double theta = -targetNode.theta();
  782. if (l < 1e-10 || w < 1e-10) {
  783. printf("IsArrived: Error target l or w == 0\n");
  784. return false;
  785. }
  786. double a = (x - tx) * cos(theta) - (y - ty) * sin(theta);
  787. double b = (x - tx) * sin(theta) + (y - ty) * cos(theta);
  788. if (pow(a / l, 8) + pow(b / w, 8) <= 1) {
  789. if (fabs(timedV_.Get()) < 0.06 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  790. printf(" Arrived target: %f %f l:%f w:%f theta:%f\n", tx, ty, l, w, theta);
  791. return true;
  792. }
  793. }
  794. return false;
  795. }
  796. void Navigation::SendMoveCmd(int mode, ActionType type,
  797. double v, double angular) {
  798. if (monitor_) {
  799. monitor_->set_speed(mode, type, v, angular);
  800. if (type == eRotation)
  801. RWheel_position_ = eR;
  802. if (type == eVertical)
  803. RWheel_position_ = eX;
  804. if (type == eHorizontal)
  805. RWheel_position_ = eY;
  806. }
  807. }
  808. bool Navigation::clamp_close() {
  809. if (monitor_) {
  810. printf("Clamp closing...\n");
  811. monitor_->clamp_close(move_mode_);
  812. actionType_ = eClampClose;
  813. while (cancel_ == false) {
  814. if (timed_clamp_.timeout()) {
  815. printf("timed clamp is timeout\n");
  816. return false;
  817. }
  818. if (timed_clamp_.Get() == eClosed)
  819. return true;
  820. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  821. monitor_->clamp_close(move_mode_);
  822. actionType_ = eClampClose;
  823. }
  824. return false;
  825. }
  826. return false;
  827. }
  828. bool Navigation::clamp_open() {
  829. if (monitor_) {
  830. printf("Clamp openning...\n");
  831. monitor_->clamp_open(move_mode_);
  832. actionType_ = eClampOpen;
  833. while (cancel_ == false) {
  834. if (timed_clamp_.timeout()) {
  835. printf("timed clamp is timeout\n");
  836. return false;
  837. }
  838. if (timed_clamp_.Get() == eOpened)
  839. return true;
  840. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  841. monitor_->clamp_open(move_mode_);
  842. actionType_ = eClampOpen;
  843. }
  844. return false;
  845. }
  846. return false;
  847. }
  848. Navigation::MpcResult Navigation::MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect) {
  849. if (IsArrived(node)) {
  850. printf(" current already in target completed !!!\n");
  851. return eMpcSuccess;
  852. }
  853. if (inited_ == false) {
  854. printf(" navigation has not inited\n");
  855. return eMpcFailed;
  856. }
  857. if (PoseTimeout()) {
  858. printf(" navigation Error:Pose is timeout \n");
  859. return eMpcFailed;
  860. }
  861. Pose2d current = timedPose_.Get();
  862. Pose2d target(node.x(), node.y(), 0);
  863. //生成轨迹
  864. Trajectory traj = Trajectory::create_line_trajectory(current, target);
  865. if (traj.size() == 0)
  866. return eMpcSuccess;
  867. //判断 巡线方向
  868. bool directY = false;
  869. if (autoDirect) {
  870. Pose2d target_relative = Pose2d::relativePose(target, timedPose_.Get());
  871. if (fabs(target_relative.y()) > fabs(target_relative.x())) { //目标轨迹点在当前点Y轴上
  872. std::cout << " MPC Y axis target_relative:" << target_relative << std::endl;
  873. directY = true;
  874. } else {
  875. std::cout << " MPC X axis target_relative:" << target_relative << std::endl;
  876. }
  877. }
  878. printf(" exec along autoDirect:%d\n", autoDirect);
  879. while (cancel_ == false) {
  880. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  881. if (pause_ == true) {
  882. //发送暂停消息
  883. continue;
  884. }
  885. if (PoseTimeout()) {
  886. printf(" navigation Error:Pose is timeout \n");
  887. return eMpcFailed;
  888. }
  889. if (timedV_.timeout() || timedA_.timeout()) {
  890. printf(" navigation Error:v/a is timeout | __LINE__:%d \n", __LINE__);
  891. return eMpcFailed;
  892. }
  893. //判断是否到达终点
  894. if (IsArrived(node)) {
  895. if (Stop()) {
  896. printf(" exec along completed !!!\n");
  897. return eMpcSuccess;
  898. }
  899. }
  900. //一次变速
  901. std::vector<double> out;
  902. bool ret;
  903. TimerRecord::Execute([&, this]() {
  904. ret = mpc_once(traj, limit_v, out, directY);
  905. }, "mpc_once");
  906. if (ret == false) {
  907. Stop();
  908. return eMpcFailed;
  909. }
  910. /*
  911. * AGV 在终点附近低速巡航时,设置最小速度0.05
  912. */
  913. Pose2d target_in_agv = Pose2d::relativePose(target, timedPose_.Get());
  914. if (directY) {
  915. target_in_agv = target_in_agv.rotate(M_PI / 2.0);
  916. }
  917. if (Pose2d::abs(target_in_agv) < Pose2d(0.2, 2, M_PI * 2)) {
  918. if (out[0] >= 0 && out[0] < limit_v.min)
  919. out[0] = limit_v.min;
  920. if (out[0] < 0 && out[0] > -limit_v.min)
  921. out[0] = -limit_v.min;
  922. }
  923. //下发速度
  924. printf(" nav input :%f out:%f\n",timedV_.Get(),out[0]);
  925. if (directY == false)
  926. SendMoveCmd(move_mode_, eVertical, out[0], out[1]);
  927. else
  928. SendMoveCmd(move_mode_, eHorizontal, out[0], out[1]);
  929. actionType_ = directY ? eHorizontal : eVertical;
  930. /*
  931. * 判断车辆是否在终点附近徘徊,无法到达终点
  932. */
  933. if (PossibleToTarget(node, directY) == false) {
  934. printf(" --------------MPC imposible to target -------------------------------------------\n");
  935. if (fabs(timedV_.Get()) > 0.1 || fabs(timedA_.Get()) > 10 * M_PI / 180.0)
  936. SlowlyStop();
  937. else
  938. Stop();
  939. return eImpossibleToTarget;
  940. }
  941. }
  942. return eMpcFailed;
  943. }
  944. bool Navigation::PossibleToTarget(NavMessage::PathNode targetNode, bool directY) {
  945. if (timedPose_.timeout()) {
  946. return false;
  947. }
  948. Pose2d current = timedPose_.Get();
  949. double tx = targetNode.x();
  950. double ty = targetNode.y();
  951. double l = fabs(targetNode.l());
  952. double w = fabs(targetNode.w());
  953. double theta = -targetNode.theta();
  954. double W = 2.45;
  955. double L = 1.3;
  956. double minYaw = 3 * M_PI / 180.0;
  957. if (move_mode_ == eDouble) {
  958. L = 1.3 + 2.7;
  959. }
  960. if (directY) {
  961. current = current.rotate(current.x(), current.y(), M_PI / 2);
  962. double t = W;
  963. W = L;
  964. L = t;
  965. }
  966. double minR = W / 2 + L / tan(minYaw);
  967. //printf("l:%f,w:%f\n",l,w);
  968. std::vector<Pose2d> poses = Pose2d::generate_rectangle_vertexs(Pose2d(tx, ty, 0), l * 2, w * 2);
  969. bool nagY = false;
  970. bool posiY = false;
  971. for (int i = 0; i < poses.size(); ++i) {
  972. Pose2d rpos = poses[i].rotate(tx, ty, theta);
  973. Pose2d relative = Pose2d::relativePose(rpos, current);
  974. double ax = fabs(relative.x());
  975. double ay = fabs(relative.y());
  976. if (relative.y() > 0) posiY = true;
  977. if (relative.y() < 0) nagY = true;
  978. if (ax * ax + pow(ay - minR, 2) > minR * minR) {
  979. /*printf(" possible to target:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n",
  980. targetNode.x(),targetNode.y(),targetNode.l(),targetNode.w(),targetNode.theta(),
  981. minR,ax,ay);*/
  982. return true;
  983. } else if (nagY && posiY) {
  984. std::cout << "nagY && posiY" << std::endl;
  985. return true;
  986. }
  987. printf("directY:%d targetInPose:%f %f l:%f,w:%f theta:%f minR:%f ax:%f,ay:%f\n", directY,
  988. relative.x(), relative.y(), targetNode.l(), targetNode.w(), targetNode.theta(),
  989. minR, ax, ay);
  990. }
  991. printf(" impossible to target:%f %f l:%f,w:%f theta:%f\n",
  992. targetNode.x(), targetNode.y(), targetNode.l(), targetNode.w(), targetNode.theta());
  993. return false;
  994. }
  995. void Navigation::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  996. if (inited_ == false) {
  997. response.set_ret(-1);
  998. response.set_info("navigation has not inited");
  999. printf(" navigation has not inited\n");
  1000. return;
  1001. }
  1002. if (newUnfinished_cations_.empty() == false) //正在运行中,上一次指令未完成
  1003. {
  1004. response.set_ret(-2);
  1005. response.set_info("navigation is running pls cancel before");
  1006. printf(" navigation is running pls cancel before\n");
  1007. return;
  1008. }
  1009. //add 先检查当前点与起点的距离
  1010. global_navCmd_ = cmd;
  1011. for (int i = 0; i < cmd.newactions_size(); ++i)
  1012. newUnfinished_cations_.push(cmd.newactions(i));
  1013. pause_ = false;
  1014. cancel_ = false;
  1015. running_ = true;
  1016. actionType_ = eReady;
  1017. printf(" navigation beg...\n");
  1018. while (newUnfinished_cations_.empty() == false && cancel_ == false) {
  1019. std::cout << "unfinished size:" << newUnfinished_cations_.size() << std::endl;
  1020. NavMessage::NewAction act = newUnfinished_cations_.front();
  1021. if (act.type() == 1) { //入库
  1022. space_id_ = act.spacenode().id();
  1023. isInSpace_ = true;
  1024. if (!execute_InOut_space(act)) {
  1025. printf(" In space failed\n");
  1026. break;
  1027. }
  1028. } else if (act.type() == 2) { //出库
  1029. if (!execute_InOut_space(act)) {
  1030. printf(" out space failed\n");
  1031. break;
  1032. } else {
  1033. isInSpace_ = false;
  1034. }
  1035. } else if (act.type() == 3 || act.type() == 4) { //马路导航
  1036. if (!execute_nodes(act))
  1037. break;
  1038. } else if (act.type() == 5) {
  1039. printf(" 汽车模型导航....\n");
  1040. } else if (act.type() == 6) {//夹持
  1041. if (this->clamp_close() == false) {
  1042. printf("夹持failed ...\n");
  1043. break;
  1044. }
  1045. } else if (act.type() == 7) {
  1046. if (this->clamp_open() == false) {
  1047. printf("打开夹持 failed...\n");
  1048. break;
  1049. }
  1050. } else if (act.type() == 8) { //切换模式
  1051. SwitchMode(ActionMode(act.changedmode()), act.wheelbase());
  1052. } else {
  1053. printf(" action type invalid not handled !!\n");
  1054. break;
  1055. }
  1056. newUnfinished_cations_.pop();
  1057. }
  1058. actionType_ = eReady;
  1059. Stop();
  1060. if (cancel_ == true) {
  1061. response.set_ret(-3);
  1062. response.set_info("navigation canceled");
  1063. printf(" navigation canceled\n");
  1064. while (newUnfinished_cations_.empty() == false)
  1065. newUnfinished_cations_.pop();
  1066. } else {
  1067. if (newUnfinished_cations_.empty()) {
  1068. response.set_ret(0);
  1069. response.set_info("navigation completed!!!");
  1070. printf("navigation completed!!!\n");
  1071. } else {
  1072. response.set_ret(-4);
  1073. response.set_info("navigation Failed!!!!");
  1074. printf(" navigation Failed\n");
  1075. while (newUnfinished_cations_.empty() == false)
  1076. newUnfinished_cations_.pop();
  1077. }
  1078. }
  1079. running_ = false;
  1080. }
  1081. void Navigation::SwitchMode(int mode, float wheelBase) {
  1082. printf("Child AGV can not Switch Mode\n");
  1083. }
  1084. float Navigation::compute_cuv(const Pose2d &target) {
  1085. float x = fabs(target.x());
  1086. float y = fabs(target.y());
  1087. float cuv = atan(y / (x + 1e-8));
  1088. //printf(" ---------------------- cuv:%f\n",cuv);
  1089. return cuv;
  1090. }
  1091. void Navigation::ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response) {
  1092. pause_ = false;
  1093. cancel_ = false;
  1094. running_ = true;
  1095. actionType_ = eReady;
  1096. printf("ManualOperation: %d | start...\n", cmd.operation_type());
  1097. float symbol = cmd.velocity() < 0 ? -1 : 1; // 判断为正方向or负方向
  1098. double velocity;
  1099. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1100. switch (cmd.operation_type()) {
  1101. case 1: // 旋转
  1102. velocity = 2.0 * M_PI / 180.0 * symbol;
  1103. SendMoveCmd(move_mode_, eRotation, 0, velocity);
  1104. actionType_ = eRotation;
  1105. printf(" ManualOperation: %d | input angular_v: %f, down: %f\n",
  1106. cmd.operation_type(), timedA_.Get(), velocity);
  1107. break;
  1108. case 2: // X平移
  1109. velocity = 1.0 * symbol;
  1110. SendMoveCmd(move_mode_, eVertical, velocity, 0);
  1111. actionType_ = eVertical;
  1112. printf(" ManualOperation: %d | input line_v : %f, down: %f\n",
  1113. cmd.operation_type(), timedV_.Get(), velocity);
  1114. break;
  1115. case 3: // Y平移
  1116. velocity = 1.0 * symbol;
  1117. SendMoveCmd(move_mode_, eHorizontal, velocity, 0);
  1118. actionType_ = eHorizontal;
  1119. printf(" ManualOperation: %d | input line_v: %f, down: %f\n",
  1120. cmd.operation_type(), timedV_.Get(), velocity);
  1121. break;
  1122. default:
  1123. break;
  1124. }
  1125. actionType_ = eReady;
  1126. response.set_ret(-3);
  1127. response.set_info("ManualOperation: %d, canceled!!!", cmd.operation_type());
  1128. printf("ManualOperation: %d | canceled!!!\n", cmd.operation_type());
  1129. running_ = false;
  1130. }