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