navigation_main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. 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_open() {
  115. if (move_mode_ == eSingle)
  116. return Navigation::clamp_open();
  117. if (monitor_) {
  118. printf("双车松夹持\n");
  119. monitor_->clamp_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() == eOpened && timed_other_clamp_.Get() == eOpened) {
  126. printf("双车松夹持completed!!!\n");
  127. return true;
  128. }
  129. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  130. monitor_->clamp_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. monitor_->lifter_rise(move_mode_);
  143. while (cancel_ == false) {
  144. if (timed_lifter_.timeout() || timed_other_lifter_.timeout()) {
  145. printf("timed lifter is timeout\n");
  146. return false;
  147. }
  148. if (timed_lifter_.Get() == eRose && timed_other_lifter_.Get() == eRose) {
  149. printf("双车提升机构提升completed!!!\n");
  150. return true;
  151. }
  152. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  153. monitor_->lifter_rise(move_mode_);
  154. }
  155. return false;
  156. }
  157. return false;
  158. }
  159. bool NavigationMain::lifter_down() {
  160. if (move_mode_ == eSingle) {
  161. return Navigation::lifter_down();
  162. }
  163. if (monitor_) {
  164. printf("双车提升机构下降\n");
  165. monitor_->lifter_down(move_mode_);
  166. while (cancel_ == false) {
  167. if (timed_lifter_.timeout() || timed_other_lifter_.timeout()) {
  168. printf("timed lifter is timeout\n");
  169. return false;
  170. }
  171. if (timed_lifter_.Get() == eDowned && timed_other_lifter_.Get() == eDowned) {
  172. printf("双车提升机构下降completed!!!\n");
  173. return true;
  174. }
  175. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  176. monitor_->lifter_down(move_mode_);
  177. }
  178. return false;
  179. }
  180. return false;
  181. }
  182. Navigation::MpcResult
  183. NavigationMain::RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target) {
  184. if (timedBrotherNavStatu_.timeout() || timedPose_.timeout()) {
  185. // if (timedPose_.timeout()) {
  186. printf(" rotate failed : timedBrotherNavStatu_ or pose timeout\n");
  187. return eMpcFailed;
  188. }
  189. NavMessage::NavStatu brother = timedBrotherNavStatu_.Get();
  190. stLimit limit_rotate = {2 * M_PI / 180.0, 15 * M_PI / 180.0};
  191. double acc_angular = 25 * M_PI / 180.0;
  192. double dt = 0.1;
  193. Pose2d rotated = Pose2d(space.x(), space.y(), space.theta());
  194. double x = space.x();
  195. double y = space.y();
  196. //后车先到,前车进入2点,保持与后车一致的朝向
  197. if (brother.in_space() && brother.space_id() == space.id()) {
  198. printf("RotateBeforeEnterSpace | 后车先到, __LINE__ = %d\n", __LINE__);
  199. rotated.mutable_theta() = brother.odom().theta();
  200. if (move_mode_ == eSingle) {
  201. x += wheelbase * cos(rotated.theta());
  202. y += wheelbase * sin(rotated.theta());
  203. }
  204. } else { //当前车先到,正向
  205. printf("RotateBeforeEnterSpace | 前车先到, __LINE__ = %d\n", __LINE__);
  206. rotated.mutable_theta() = space.theta();
  207. }
  208. if (move_mode_ == eDouble){
  209. x -= wheelbase/2 * cos(rotated.theta());
  210. y -= wheelbase/2 * sin(rotated.theta());
  211. printf("RotateBeforeEnterSpace | 整车模式, __LINE__ = %d\n", __LINE__);
  212. }
  213. target.set_x(x);
  214. target.set_y(y);
  215. target.set_theta(rotated.theta());
  216. target.set_l(0.02);
  217. target.set_w(0.05);
  218. target.set_id(space.id());
  219. printf("RotateBeforeEnterSpace | target:[x:%f,y:%f,theta:%f]\n",x,y,target.theta());
  220. if (move_mode_ == eDouble) {
  221. double yawDiff1 = (rotated - timedPose_.Get()).theta();
  222. double yawDiff2 = (rotated + Pose2d(0, 0, M_PI) - timedPose_.Get()).theta();
  223. if (fabs(yawDiff1) > fabs(yawDiff2)) {
  224. rotated = rotated + Pose2d(0, 0, M_PI);
  225. }
  226. }
  227. while (cancel_ == false) {
  228. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  229. Pose2d current = timedPose_.Get();
  230. double yawDiff = (rotated - current).theta();
  231. //一次变速
  232. std::vector<double> out;
  233. bool ret;
  234. TimerRecord::Execute([&, this]() {
  235. ret = Rotation_mpc_once(Pose2d(0, 0, yawDiff), limit_rotate, out);
  236. }, "Rotation_mpc_once");
  237. if (ret == false) {
  238. Stop();
  239. return eMpcFailed;
  240. }
  241. //下发速度
  242. if (fabs(yawDiff) < 0.3 * M_PI / 180.0 && fabs(timedA_.Get()) < 5 * M_PI / 180.0) {
  243. printf(" RotateBeforeEnterSpace refer target completed\\n,cuv:%f\n", yawDiff);
  244. Stop();
  245. return eMpcSuccess;
  246. } else{
  247. const int down_count = 3;
  248. double v[down_count] = {0,0,0};
  249. double w[down_count] = {out[0], out[1], out[2]};
  250. SendMoveCmd(move_mode_, eRotation, v, w);
  251. actionType_ = eRotation;
  252. printf(" RotateBeforeEnterSpace | input anguar:%f, down:%f(%f), diff:%f anyDirect:false\n",
  253. timedA_.Get(), out[0], out[0]/M_PI*180, yawDiff);
  254. }
  255. continue;
  256. }
  257. return eMpcFailed;
  258. }