navigation_main.cpp 12 KB

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