navigation.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include <fstream>
  16. #include <time.h>
  17. class Navigation {
  18. public:
  19. typedef struct {
  20. double min;
  21. double max;
  22. } stLimit;
  23. //舵轮位置
  24. enum RWheelPosition {
  25. eUnknow = 0,
  26. eX, //超前
  27. eY, //横移
  28. eR //旋转位
  29. };
  30. enum ClampStatu {
  31. eInvalid = 0,
  32. eClosed = 1,
  33. eHalfOpened = 2, //半开位(默认)
  34. eFullyOpened = 3 //全开位
  35. };
  36. enum CarrierStatu{
  37. eCarrierInvalid=0,
  38. eCarrierUp=1,
  39. eCarrierDown=2
  40. };
  41. typedef std::map<int,CarrierStatu> CarrierStatuRegionMap;
  42. enum DoorStatu{
  43. eDoorInvalid=0,
  44. eDoorOpened=1,
  45. eDoorClosed=2
  46. };
  47. typedef std::pair<DoorStatu,DoorStatu> DoorStatusInOnePort;//<out, ins>
  48. typedef std::map<int,DoorStatusInOnePort> DoorStatusMap;
  49. enum MpcResult {
  50. eMpcSuccess = 0,
  51. eMpcFailed,
  52. eImpossibleToTarget
  53. };
  54. public:
  55. Navigation();
  56. virtual ~Navigation();
  57. //连接AGV emqx服务
  58. bool Init(const NavParameter::Navigation_parameter &parameter);
  59. bool LoadSpaceNo2CarrierNo();
  60. bool SpaceNo2CarrierNo(int space_no, int& region_id, int& carrier_no);
  61. CarrierStatu GetCarrierStatusBySpaceID(int space_id);
  62. bool Stringsplit(const std::string &str, const std::string splits, std::vector<std::string> &res);
  63. static void anasisDoorStatu(int32_t i_door_status, DoorStatusMap& m_door_status);
  64. virtual bool IsMasterAGV();
  65. virtual void ResetPose(const Pose2d &pose);
  66. void ResetStatu(double v, double a);
  67. virtual void ResetClamp(ClampStatu statu);
  68. virtual void ResetLifter(LifterStatus status);
  69. virtual void ResetDoor(int32_t status);
  70. virtual void ResetCarrier(std::vector<int32_t> status);
  71. TimedLockData<Pose2d> &RealTimePose() {
  72. return timedPose_;
  73. }
  74. TimedLockData<double> &RealTimeV() {
  75. return timedV_;
  76. }
  77. TimedLockData<double> &RealTimeA() {
  78. return timedA_;
  79. }
  80. virtual void Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  81. /* 手动旋转 */
  82. virtual void ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response);
  83. void Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  84. bool Stop();
  85. bool SlowlyStop();
  86. virtual bool SwitchMode(int mode, float wheelBase);
  87. bool PoseTimeout();
  88. bool WaitClampLifterStatu(int clampLifterAction);
  89. bool WaitInsideDoorCompleted(int doorID, DoorStatu status);
  90. bool WaitCarrierCompleted(int spaceID,CarrierStatu statu);
  91. bool TargetIsEntrance(NavMessage::PathNode node);
  92. bool TargetIsExport(NavMessage::PathNode node);
  93. int GetPortIDBySpace(NavMessage::PathNode node);
  94. // 获取当前未完成动作数量
  95. virtual int GetRemainActionsCount();
  96. virtual NavMessage::NewAction GetCurrentAction();
  97. protected:
  98. static void anasisCarierStatu(int& regionID, CarrierStatuRegionMap& status, int32_t bytes);
  99. virtual void HandleAgvStatu(const MqttMsg &msg);
  100. virtual void SendMoveCmd(int mode, ActionType type, double v, double angular);
  101. virtual void SendMoveCmd(int clampLifterAction,int mode, ActionType type, double v[], double angular[]);
  102. /*virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[],
  103. int space_id, double distance,double Y1=0.0,double Y2=0.0);*/
  104. virtual void SendMoveCmd(int clampLifterAction,int mode, ActionType type, double v[], double angular[],
  105. int space_id, double distance,double Y1=0.0,double Y2=0.0);
  106. // virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[], double diff_yaw[], int space_id, double distance);
  107. static void RobotPoseCallback(const MqttMsg &msg, void *context);
  108. static void RobotSpeedCallback(const MqttMsg &msg, void *context);
  109. static void BrotherAgvStatuCallback(const MqttMsg &msg, void *context);
  110. bool IsArrived(NavMessage::PathNode targetNode);
  111. bool PossibleToTarget(NavMessage::PathNode targetNode, bool directY);
  112. /*
  113. * 移动到目标点,不能到达则旋转
  114. */
  115. bool MoveToTarget(NavMessage::PathNode node, stLimit limit_v, stLimit limit_w,
  116. bool autoDirect, bool enable_rotate,int clampLifterAction=0);
  117. /*
  118. * autoDirect:自动选择 纵向还是横向巡线
  119. */
  120. MpcResult MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect,int clampLifterAction=0);
  121. /*
  122. * 旋转摆正,朝向目标点,
  123. * anyDirect: 任意一个朝向对准目标点
  124. */
  125. //virtual bool RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect);
  126. virtual MpcResult RotateReferToTarget(NavMessage::PathNode target, stLimit limit_rotate, int directions);
  127. virtual bool AdjustReferToTarget(const Pose2d& target, stLimit limit_v,int directions);
  128. /*
  129. * 按照路径点导航
  130. */
  131. bool execute_nodes(NavMessage::NewAction action);
  132. /*
  133. * 进出库
  134. */
  135. bool execute_InOut_space(NavMessage::NewAction action);
  136. /*
  137. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  138. * space:输入车位点,
  139. * target:输出目标点
  140. */
  141. virtual Navigation::MpcResult
  142. RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase,
  143. NavMessage::PathNode &target,int clampLifterAction=0);
  144. virtual Navigation::MpcResult RotateBeforeIntoExport();
  145. /// 双车出库摆正
  146. virtual Navigation::MpcResult DoubleOutSpaceCorrectTheta();
  147. virtual bool clamp_close();
  148. virtual bool clamp_half_open();
  149. virtual bool clamp_fully_open();
  150. virtual bool lifter_rise();
  151. virtual bool lifter_down();
  152. virtual bool singleClamp_fullyOpen_until_inspaceBegin();
  153. bool IsUperSpace(NavMessage::PathNode spaceNode);
  154. virtual int GetSpaceId(NavMessage::PathNode spaceNode);
  155. bool mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY = false);
  156. /* 原地旋转mpc
  157. *
  158. * */
  159. bool
  160. Rotation_mpc_once(Pose2d current_to_target, stLimit limit_wmg, std::vector<double> &out, bool all_direct = true);
  161. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu);
  162. /*
  163. * 计算以当前点到目标点的曲率
  164. */
  165. float compute_cuv(const Pose2d &target);
  166. /*
  167. * 发布导航模块状态
  168. */
  169. static void pubStatuThreadFuc(void *p);
  170. virtual void publish_statu(NavMessage::NavStatu &statu);
  171. protected:
  172. std::mutex mtx_;
  173. bool exit_ = false;
  174. ActionType actionType_ = eReady;
  175. std::thread *pubthread_ = nullptr;
  176. bool inited_ = false;
  177. Monitor_emqx *monitor_ = nullptr;
  178. Terminator_emqx *terminator_ = nullptr; //本机emqx连接
  179. Terminator_emqx *brotherEmqx_ = nullptr; //本机emqx连接
  180. TimedLockData<double> timeYawDiff1_;
  181. TimedLockData<double> timeYawDiff2_;
  182. TimedLockData<Pose2d> timedPose_; //当前位姿
  183. TimedLockData<double> timedV_; //底盘数据
  184. TimedLockData<double> timedA_;
  185. TimedLockData<DoorStatusMap> timed_Door_; //门(1外内,2外内)
  186. std::map<int,TimedLockData<CarrierStatuRegionMap>> timed_Carrier_; //载车板
  187. TimedLockData<ClampStatu> timed_clamp_; //夹持杆
  188. TimedLockData<LifterStatus> timed_lifter_; //提升机构
  189. TimedLockData<ClampStatu> timed_other_clamp_;
  190. TimedLockData<LifterStatus> timed_other_lifter_;
  191. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  192. TimedLockData<double> timedBrotherV_; //底盘数据
  193. TimedLockData<double> timedBrotherA_;
  194. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  195. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  196. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  197. bool running_ = false;
  198. bool pause_ = false;
  199. bool cancel_ = false;
  200. int move_mode_ = eSingle;
  201. bool is_master_AGV = false; // 是否是主车
  202. float wheelBase_; //两节agv的位姿与单节的位姿变换
  203. bool isInSpace_; //是否在车位或者正在进入车位
  204. std::string space_id_;
  205. TimedLockData<Trajectory> selected_traj_;
  206. TimedLockData<Trajectory> predict_traj_;
  207. SpaceNo2Region_Carry spaceNo_2_region_carrier_excel_;
  208. NavParameter::Navigation_parameter parameter_;
  209. RWheelPosition RWheel_position_;
  210. double obs_w_;
  211. double obs_h_;
  212. TimedLockData<bool> isFront_;
  213. };
  214. double limit_gause(double x, double min, double max);
  215. double limit(double x, double min, double max);
  216. double next_speed(double speed, double target_speed, double acc, double dt = 0.1);
  217. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_