navigation_main.cpp 11 KB

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