navigation_main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // Created by zx on 23-5-8.
  3. //
  4. #include "navigation_main.h"
  5. #include "TimerRecord.h"
  6. NavigationMain::NavigationMain() {
  7. is_master_AGV = true;
  8. move_mode_ = eSingle;
  9. wheelBase_ = 0;
  10. }
  11. NavigationMain::~NavigationMain() {
  12. }
  13. void NavigationMain::ResetPose(const Pose2d &pose) {
  14. if (move_mode_ == eDouble) {
  15. if (timedBrotherPose_.timeout() == true) {
  16. std::cout << "Brother pose is timeout can not set MainAGV pose" << std::endl;
  17. return;
  18. }
  19. //Pose2d transform(-wheelBase_/2.0,0,0);
  20. //Navigation::ResetPose(pose * transform);
  21. Pose2d brother = timedBrotherPose_.Get();
  22. Pose2d diff = Pose2d::abs(Pose2d::relativePose(brother, pose));
  23. if (diff.x() > 3.6 || diff.x() < 2.2 || diff.y() > 0.5 || diff.theta() > 10 * M_PI / 180.0) {
  24. std::cout << " distance with two agv is too far diff: " << diff << std::endl;
  25. return;
  26. }
  27. Pose2d abs_diff = pose - brother;
  28. //计算两车朝向的方向
  29. float theta = pose.theta();//Pose2d::vector2yaw(abs_diff.x(), abs_diff.y());
  30. Pose2d agv = Pose2d((pose.x() + brother.x()) / 2.0, (pose.y() + brother.y()) / 2.0, theta);
  31. Pose2d abs_diff1 = pose - agv;
  32. Pose2d abs_diff2 = brother - agv;
  33. timeYawDiff1_.reset(pose.theta());
  34. timeYawDiff2_.reset((brother-pose).theta());
  35. Pose2d brotherInPose=Pose2d::relativePose(brother,pose);
  36. timeYawDiff1_.reset(brotherInPose.y());
  37. //timeYawDiff2_.reset(abs_diff2.theta());
  38. Navigation::ResetPose(pose);
  39. // timedPose_().m_diffYaw1=pose.theta()-theta;
  40. // timedPose_().m_diffYaw2=brother.theta()-theta;
  41. // printf("m_diffYaw1:%f, m_diffYaw2:%f\n",timedPose_().m_diffYaw1, timedPose_().m_diffYaw2);
  42. } else
  43. Navigation::ResetPose(pose);
  44. }
  45. void NavigationMain::publish_statu(NavMessage::NavStatu &statu) {
  46. statu.set_main_agv(true);
  47. Navigation::publish_statu(statu);
  48. }
  49. void NavigationMain::Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response) {
  50. /*if(move_mode_!=Monitor_emqx::eDouble)
  51. {
  52. printf(" navigation mode must set main,parameter:Pose2d\n");
  53. return false;
  54. }*/
  55. Navigation::Start(cmd, response);
  56. }
  57. void NavigationMain::SendMoveCmd(int clampLifterAction,int mode, ActionType type,
  58. double v[], double angular[]) {
  59. if (monitor_) {
  60. monitor_->set_ToAgvCmd(clampLifterAction,mode, type, v, angular, wheelBase_);
  61. }
  62. }
  63. void NavigationMain::SendMoveCmd(int clampLifterAction,int mode, ActionType type, double v[], double angular[],
  64. int space_id, double distance,double Y1,double Y2) {
  65. if (monitor_) {
  66. monitor_->set_ToAgvCmd(clampLifterAction,mode, type, v, angular, wheelBase_,
  67. space_id, distance,Y1,Y2);
  68. if (type == eRotation)
  69. RWheel_position_ = eR;
  70. if (type == eVertical)
  71. RWheel_position_ = eX;
  72. if (type == eHorizontal)
  73. RWheel_position_ = eY;
  74. }
  75. }
  76. bool NavigationMain::CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu) {
  77. if (Navigation::CreateRobotStatuMsg(robotStatu)) {
  78. robotStatu.mutable_agvstatu()->set_clamp_other(timed_other_clamp_.Get());
  79. robotStatu.mutable_agvstatu()->set_lifter_other(timed_other_lifter_.Get());
  80. return true;
  81. }
  82. //std::cout<<agvStatu.DebugString()<<std::endl;
  83. return false;
  84. }
  85. void NavigationMain::ResetOtherClamp(ClampStatu statu) {
  86. timed_other_clamp_.reset(statu, 1);
  87. }
  88. void NavigationMain::ResetOtherLifter(LifterStatus status) {
  89. timed_other_lifter_.reset(status, 1);
  90. }
  91. void NavigationMain::HandleAgvStatu(const MqttMsg &msg) {
  92. NavMessage::AgvStatu speed;
  93. if (IsMasterAGV()) {
  94. if (msg.toProtoMessage(speed)) {
  95. ResetStatu(speed.v(), speed.w());
  96. ResetClamp((ClampStatu) speed.clamp());
  97. ResetOtherClamp((ClampStatu) speed.clamp_other());
  98. ResetLifter((LifterStatus) speed.lifter());
  99. ResetOtherLifter((LifterStatus) speed.lifter_other());
  100. //printf(" clamp:%d other:%d\n",speed.clamp(),speed.clamp_other());
  101. }
  102. } else {
  103. if (msg.toProtoMessage(speed)) {
  104. ResetStatu(speed.v(), speed.w());
  105. ResetClamp((ClampStatu) speed.clamp());
  106. ResetLifter((LifterStatus) speed.lifter());
  107. }
  108. }
  109. ResetDoor(speed.door());
  110. std::vector<int32_t> vecStatus;
  111. for(int i=0;i<speed.zcb_size();++i){
  112. vecStatus.push_back(speed.zcb(i));
  113. }
  114. ResetCarrier(vecStatus);
  115. }
  116. bool NavigationMain::clamp_close() {
  117. if (move_mode_ == eSingle)
  118. return Navigation::clamp_close();
  119. printf("双车夹持\n");
  120. if (monitor_) {
  121. monitor_->clamp_close(move_mode_);
  122. while (cancel_ == false) {
  123. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  124. printf("timed clamp is timeout\n");
  125. return false;
  126. }
  127. if (timed_clamp_.Get() == eClosed && timed_other_clamp_.Get() == eClosed) {
  128. printf("双车夹持completed!!!\n");
  129. return true;
  130. }
  131. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  132. monitor_->clamp_close(move_mode_);
  133. }
  134. return false;
  135. }
  136. return false;
  137. }
  138. bool NavigationMain::clamp_half_open() {
  139. if (move_mode_ == eSingle)
  140. return Navigation::clamp_half_open();
  141. if (monitor_) {
  142. printf("双车松夹持\n");
  143. monitor_->clamp_half_open(move_mode_);
  144. while (cancel_ == false) {
  145. if (timed_clamp_.timeout() || timed_other_clamp_.timeout()) {
  146. printf("timed clamp is timeout\n");
  147. return false;
  148. }
  149. if (timed_clamp_.Get() == eHalfOpened && timed_other_clamp_.Get() == eHalfOpened) {
  150. printf("双车松夹持completed!!!\n");
  151. return true;
  152. }
  153. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  154. monitor_->clamp_half_open(move_mode_);
  155. }
  156. return false;
  157. }
  158. return false;
  159. }
  160. bool NavigationMain::lifter_rise() {
  161. if (move_mode_ == eSingle) {
  162. return Navigation::lifter_rise();
  163. }
  164. if (monitor_) {
  165. printf("双车提升机构提升\n");
  166. actionType_ = eLifterRise;
  167. if (timed_lifter_.Get() == eRose && timed_other_lifter_.Get()==eRose)
  168. return true;
  169. else {
  170. if(timed_clamp_.Get()!=eHalfOpened && timed_clamp_.Get()!=eClosed){
  171. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  172. if(!clamp_half_open()){
  173. printf(" clamp half open failed\n");
  174. return false;
  175. }
  176. }
  177. }
  178. while (cancel_ == false) {
  179. if (timed_lifter_.timeout() || timed_other_lifter_.timeout()) {
  180. printf("timed lifter is timeout\n");
  181. return false;
  182. }
  183. if (timed_lifter_.Get() == eRose && timed_other_lifter_.Get() == eRose) {
  184. printf("双车提升机构提升completed!!!\n");
  185. return true;
  186. }
  187. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  188. monitor_->lifter_rise(move_mode_);
  189. }
  190. return false;
  191. }
  192. return false;
  193. }
  194. bool NavigationMain::lifter_down() {
  195. if (move_mode_ == eSingle) {
  196. return Navigation::lifter_down();
  197. }
  198. if (monitor_) {
  199. printf("双车提升机构下降\n");
  200. actionType_ = eLifterDown;
  201. if (timed_lifter_.Get() == eDowned && timed_other_lifter_.Get() == eDowned)
  202. return true;
  203. else {
  204. if(timed_clamp_.Get()!=eHalfOpened && timed_clamp_.Get()!=eClosed){
  205. printf(" clamp statu !=eHalfOpened or eClosed, clamp_half_open...\n ");
  206. if(!clamp_half_open()){
  207. printf(" clamp half open failed\n");
  208. return false;
  209. }
  210. }
  211. }
  212. while (cancel_ == false) {
  213. if (timed_lifter_.timeout() || timed_other_lifter_.timeout()) {
  214. printf("timed lifter is timeout\n");
  215. return false;
  216. }
  217. if (timed_lifter_.Get() == eDowned && timed_other_lifter_.Get() == eDowned) {
  218. printf("双车提升机构下降completed!!!\n");
  219. return true;
  220. }
  221. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  222. monitor_->lifter_down(move_mode_);
  223. }
  224. return false;
  225. }
  226. return false;
  227. }
  228. Navigation::MpcResult
  229. NavigationMain::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase,
  230. NavMessage::PathNode &target,int clampLifterAction) {
  231. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  232. // if (timedPose_.timeout()) {
  233. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  234. return eMpcFailed;
  235. }
  236. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  237. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  238. double acc_angular = 25 * M_PI / 180.0;
  239. double dt = 0.1;
  240. Pose2d current = timedPose_.Get();
  241. double x = space.x();
  242. double y = space.y();
  243. Pose2d vec(x - current.x(), y - current.y(), 0);
  244. double vecTheta = Pose2d::vector2yaw(vec.x(), vec.y());
  245. bool isFront=isFront_.Get();
  246. if(vecTheta<0.)
  247. isFront=!isFront;
  248. //前车先到,后车进入2点,保持与车位一致的朝向
  249. if (isFront==false ) {
  250. printf("后车 RotateBeforeEnterSpace | , __LINE__ = %d\n", __LINE__);
  251. if (move_mode_ == eSingle) {
  252. x -= wheelbase * cos(vecTheta);
  253. y -= wheelbase * sin(vecTheta);
  254. printf("确定车位点:eSingle\n");
  255. }
  256. } else { //当后车先到,倒车入库
  257. printf("前车RotateBeforeEnterSpace | __LINE__ = %d\n", __LINE__);
  258. //rotated.mutable_theta() = space.theta();
  259. }
  260. if (move_mode_ == eDouble){
  261. if((!isFront_.Get() && vecTheta>0) || (isFront_.Get()&&vecTheta<=0)){
  262. x -= wheelbase * cos(vecTheta);
  263. y -= wheelbase * sin(vecTheta);
  264. }
  265. printf("RotateBeforeEnterSpace | 整车模式, __LINE__ = %d\n", __LINE__);
  266. }
  267. Pose2d spaceInCurrent=Pose2d::relativePose(Pose2d(space.x(),space.y(),0),current);
  268. target.set_x(x);
  269. target.set_y(y);
  270. target.set_theta(current.theta());
  271. target.set_l(0.02);
  272. target.set_w(0.5);
  273. target.set_id(space.id());
  274. printf("RotateBeforeEnterSpace | target:[x:%f,y:%f,theta:%f]\n",x,y,target.theta());
  275. if (move_mode_ == eDouble && space.id().find("S1101") != -1){
  276. Pose2d nt(x,y,0);
  277. Pose2d ntInCurrent=Pose2d::relativePose(nt,current);
  278. nt=current*Pose2d(ntInCurrent.x(),0,0);
  279. nt.set_theta(current.theta());
  280. target.set_x(nt.x());
  281. target.set_y(nt.y());
  282. target.set_theta(nt.theta());
  283. target.set_l(0.02);
  284. target.set_w(0.5);
  285. target.set_id(space.id());
  286. std::cout<< "整车进出口:" << nt <<std::endl;
  287. return eMpcSuccess;
  288. }
  289. double angular_pericision = 0.5;
  290. if(GetSequenceType() == eSequencePick && TargetIsExport(space)){
  291. angular_pericision = 5;
  292. }
  293. while (cancel_ == false) {
  294. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  295. Pose2d current_now = timedPose_.Get();
  296. if(spaceInCurrent.x()<0.)
  297. current_now = current_now.rotate(current_now.x(), current_now.y(), M_PI);
  298. double yawDiff = vecTheta - current_now.theta();
  299. if (move_mode_ == eDouble) {
  300. double yawDiff2 = vecTheta - (Pose2d(0, 0, M_PI) + current_now).theta();
  301. if (fabs(yawDiff) > fabs(yawDiff2)) {
  302. yawDiff=yawDiff2;
  303. }
  304. }
  305. //一次变速
  306. std::vector<double> out;
  307. bool ret;
  308. TimerRecord::Execute([&, this]() {
  309. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  310. }, "Rotation_mpc_once");
  311. if (ret == false) {
  312. Stop();
  313. return eMpcFailed;
  314. }
  315. //下发速度
  316. if (fabs(yawDiff) < angular_pericision * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  317. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  318. Stop();
  319. if(WaitClampLifterStatu(clampLifterAction)==false)
  320. return eMpcFailed;
  321. usleep(1000*1000);
  322. Pose2d current_2 = timedPose_.Get();
  323. if(spaceInCurrent.x()<0.)
  324. current_2 = current_2.rotate(current_2.x(), current_2.y(), M_PI);
  325. yawDiff = vecTheta - current_2.theta();
  326. if (move_mode_ == eDouble) {
  327. double yawDiff2 = vecTheta - (Pose2d(0, 0, M_PI) + current_now).theta();
  328. if (fabs(yawDiff) > fabs(yawDiff2)) {
  329. yawDiff=yawDiff2;
  330. }
  331. }
  332. if(fabs(yawDiff) < 0.3){
  333. return eMpcSuccess;
  334. }else{
  335. continue;
  336. }
  337. } else{
  338. const int down_count = 3;
  339. double v[down_count] = {0,0,0};
  340. double w[down_count] = {out[0], out[1], out[2]};
  341. SendMoveCmd(clampLifterAction,move_mode_, eRotation, v, w);
  342. actionType_ = eRotation;
  343. /*
  344. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  345. timedA_.Get(), out[0], out[0]/M_PI*180, yawDiff);
  346. */
  347. }
  348. continue;
  349. }
  350. return eMpcFailed;
  351. }