navigation_main.cpp 12 KB

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