navigation_main.cpp 9.6 KB

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