navigation.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Created by zx on 22-12-2.
  3. //
  4. #ifndef LIO_LIVOX_CPP_MPC_NAVIGATION_H_
  5. #define LIO_LIVOX_CPP_MPC_NAVIGATION_H_
  6. #include <glog/logging.h>
  7. #include "../define/timedlockdata.hpp"
  8. #include "pose2d.h"
  9. #include "trajectory.h"
  10. #include "monitor/monitor_emqx.h"
  11. #include "monitor/terminator_emqx.h"
  12. #include "parameter.pb.h"
  13. #include <queue>
  14. #include <thread>
  15. class Navigation {
  16. public:
  17. typedef struct {
  18. double min;
  19. double max;
  20. } stLimit;
  21. //舵轮位置
  22. enum RWheelPosition {
  23. eUnknow = 0,
  24. eX, //超前
  25. eY, //横移
  26. eR //旋转位
  27. };
  28. enum ClampStatu {
  29. eInvalid = 0,
  30. eClosed = 1,
  31. eOpened = 2
  32. };
  33. enum MpcResult {
  34. eMpcSuccess = 0,
  35. eMpcFailed,
  36. eImpossibleToTarget
  37. };
  38. public:
  39. Navigation();
  40. virtual ~Navigation();
  41. //连接AGV emqx服务
  42. bool Init(const NavParameter::Navigation_parameter &parameter);
  43. virtual void ResetPose(const Pose2d &pose);
  44. void ResetStatu(double v, double a);
  45. virtual void ResetClamp(ClampStatu statu);
  46. virtual void ResetLifter(LifterStatus status);
  47. TimedLockData<Pose2d> &RealTimePose() {
  48. return timedPose_;
  49. }
  50. TimedLockData<double> &RealTimeV() {
  51. return timedV_;
  52. }
  53. TimedLockData<double> &RealTimeA() {
  54. return timedA_;
  55. }
  56. virtual void Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  57. /* 手动旋转 */
  58. virtual void ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response);
  59. void Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  60. bool Stop();
  61. bool SlowlyStop();
  62. virtual void SwitchMode(int mode, float wheelBase);
  63. bool PoseTimeout();
  64. protected:
  65. virtual void HandleAgvStatu(const MqttMsg &msg);
  66. virtual void SendMoveCmd(int mode, ActionType type, double v, double angular);
  67. static void RobotPoseCallback(const MqttMsg &msg, void *context);
  68. static void RobotSpeedCallback(const MqttMsg &msg, void *context);
  69. static void BrotherAgvStatuCallback(const MqttMsg &msg, void *context);
  70. bool IsArrived(NavMessage::PathNode targetNode);
  71. bool PossibleToTarget(NavMessage::PathNode targetNode, bool directY);
  72. /*
  73. * 移动到目标点,不能到达则旋转
  74. */
  75. bool MoveToTarget(NavMessage::PathNode node, stLimit limit_v, stLimit limit_w, bool autoDirect, bool enable_rotate);
  76. /*
  77. * autoDirect:自动选择 纵向还是横向巡线
  78. */
  79. MpcResult MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect);
  80. /*
  81. * 旋转摆正,朝向目标点,
  82. * anyDirect: 任意一个朝向对准目标点
  83. */
  84. virtual bool RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect);
  85. virtual MpcResult RotateReferToTarget(NavMessage::PathNode target, stLimit limit_rotate, int directions);
  86. /*
  87. * 按照路径点导航
  88. */
  89. bool execute_nodes(NavMessage::NewAction action);
  90. /*
  91. * 进出库
  92. */
  93. bool execute_InOut_space(NavMessage::NewAction action);
  94. /*
  95. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  96. * space:输入车位点,
  97. * target:输出目标点
  98. */
  99. virtual Navigation::MpcResult
  100. RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target);
  101. virtual bool clamp_close();
  102. virtual bool clamp_open();
  103. virtual bool lifter_rise();
  104. virtual bool lifter_down();
  105. bool mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY = false);
  106. /* 原地旋转mpc
  107. *
  108. * */
  109. bool
  110. Rotation_mpc_once(Pose2d current_to_target, stLimit limit_wmg, std::vector<double> &out, bool all_direct = true);
  111. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu);
  112. /*
  113. * 计算以当前点到目标点的曲率
  114. */
  115. float compute_cuv(const Pose2d &target);
  116. /*
  117. * 发布导航模块状态
  118. */
  119. static void pubStatuThreadFuc(void *p);
  120. virtual void publish_statu(NavMessage::NavStatu &statu);
  121. protected:
  122. std::mutex mtx_;
  123. bool exit_ = false;
  124. ActionType actionType_ = eReady;
  125. std::thread *pubthread_ = nullptr;
  126. bool inited_ = false;
  127. Monitor_emqx *monitor_ = nullptr;
  128. Terminator_emqx *terminator_ = nullptr; //本机emqx连接
  129. Terminator_emqx *brotherEmqx_ = nullptr; //本机emqx连接
  130. TimedLockData<Pose2d> timedPose_; //当前位姿
  131. TimedLockData<double> timedV_; //底盘数据
  132. TimedLockData<double> timedA_;
  133. TimedLockData<ClampStatu> timed_clamp_; //加持杆
  134. TimedLockData<LifterStatus> timed_lifter_; //提升机构
  135. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  136. TimedLockData<double> timedBrotherV_; //底盘数据
  137. TimedLockData<double> timedBrotherA_;
  138. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  139. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  140. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  141. bool running_ = false;
  142. bool pause_ = false;
  143. bool cancel_ = false;
  144. int move_mode_ = eSingle;
  145. bool isInSpace_; //是否在车位或者正在进入车位
  146. std::string space_id_;
  147. TimedLockData<Trajectory> selected_traj_;
  148. TimedLockData<Trajectory> predict_traj_;
  149. NavParameter::Navigation_parameter parameter_;
  150. RWheelPosition RWheel_position_;
  151. };
  152. double limit_gause(double x, double min, double max);
  153. double limit(double x, double min, double max);
  154. double next_speed(double speed, double target_speed, double acc, double dt = 0.1);
  155. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_