navigation.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. void 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 void 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. protected:
  95. static void anasisCarierStatu(int& regionID, CarrierStatuRegionMap& status, int32_t bytes);
  96. virtual void HandleAgvStatu(const MqttMsg &msg);
  97. virtual void SendMoveCmd(int mode, ActionType type, double v, double angular);
  98. virtual void SendMoveCmd(int clampLifterAction,int mode, ActionType type, double v[], double angular[]);
  99. /*virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[],
  100. int space_id, double distance,double Y1=0.0,double Y2=0.0);*/
  101. virtual void SendMoveCmd(int clampLifterAction,int mode, ActionType type, double v[], double angular[],
  102. int space_id, double distance,double Y1=0.0,double Y2=0.0);
  103. // virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[], double diff_yaw[], int space_id, double distance);
  104. static void RobotPoseCallback(const MqttMsg &msg, void *context);
  105. static void RobotSpeedCallback(const MqttMsg &msg, void *context);
  106. static void BrotherAgvStatuCallback(const MqttMsg &msg, void *context);
  107. bool IsArrived(NavMessage::PathNode targetNode);
  108. bool PossibleToTarget(NavMessage::PathNode targetNode, bool directY);
  109. /*
  110. * 移动到目标点,不能到达则旋转
  111. */
  112. bool MoveToTarget(NavMessage::PathNode node, stLimit limit_v, stLimit limit_w,
  113. bool autoDirect, bool enable_rotate,int clampLifterAction=0);
  114. /*
  115. * autoDirect:自动选择 纵向还是横向巡线
  116. */
  117. MpcResult MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect,int clampLifterAction=0);
  118. /*
  119. * 旋转摆正,朝向目标点,
  120. * anyDirect: 任意一个朝向对准目标点
  121. */
  122. //virtual bool RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect);
  123. virtual MpcResult RotateReferToTarget(NavMessage::PathNode target, stLimit limit_rotate, int directions);
  124. virtual bool AdjustReferToTarget(const Pose2d& target, stLimit limit_v,int directions);
  125. /*
  126. * 按照路径点导航
  127. */
  128. bool execute_nodes(NavMessage::NewAction action,int next_actionTypeID=-1);
  129. /*
  130. * 进出库
  131. */
  132. bool execute_InOut_space(NavMessage::NewAction action);
  133. /*
  134. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  135. * space:输入车位点,
  136. * target:输出目标点
  137. */
  138. virtual Navigation::MpcResult
  139. RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase,
  140. NavMessage::PathNode &target,int clampLifterAction=0);
  141. virtual Navigation::MpcResult RotateBeforeIntoExport();
  142. virtual Navigation::MpcResult DoubleOutSpaceCorrectTheta();
  143. virtual bool clamp_close();
  144. virtual bool clamp_half_open();
  145. virtual bool clamp_fully_open();
  146. virtual bool lifter_rise();
  147. virtual bool lifter_down();
  148. virtual bool singleClamp_fullyOpen_until_inspaceBegin();
  149. static bool IsUperSpace(NavMessage::PathNode spaceNode);
  150. virtual int GetSpaceId(NavMessage::PathNode spaceNode);
  151. bool mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY = false);
  152. /* 原地旋转mpc
  153. *
  154. * */
  155. bool
  156. Rotation_mpc_once(Pose2d current_to_target, stLimit limit_wmg, std::vector<double> &out, bool all_direct = true);
  157. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu);
  158. /*
  159. * 计算以当前点到目标点的曲率
  160. */
  161. float compute_cuv(const Pose2d &target);
  162. /*
  163. * 发布导航模块状态
  164. */
  165. static void pubStatuThreadFuc(void *p);
  166. virtual void publish_statu(NavMessage::NavStatu &statu);
  167. protected:
  168. std::mutex mtx_;
  169. bool exit_ = false;
  170. ActionType actionType_ = eReady;
  171. std::thread *pubthread_ = nullptr;
  172. bool inited_ = false;
  173. Monitor_emqx *monitor_ = nullptr;
  174. Terminator_emqx *terminator_ = nullptr; //本机emqx连接
  175. Terminator_emqx *brotherEmqx_ = nullptr; //本机emqx连接
  176. TimedLockData<double> timeYawDiff1_;
  177. TimedLockData<double> timeYawDiff2_;
  178. TimedLockData<Pose2d> timedPose_; //当前位姿
  179. TimedLockData<double> timedV_; //底盘数据
  180. TimedLockData<double> timedA_;
  181. TimedLockData<DoorStatusMap> timed_Door_; //门(1外内,2外内)
  182. std::map<int,TimedLockData<CarrierStatuRegionMap>> timed_Carrier_; //载车板
  183. TimedLockData<ClampStatu> timed_clamp_; //夹持杆
  184. TimedLockData<LifterStatus> timed_lifter_; //提升机构
  185. TimedLockData<ClampStatu> timed_other_clamp_;
  186. TimedLockData<LifterStatus> timed_other_lifter_;
  187. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  188. TimedLockData<double> timedBrotherV_; //底盘数据
  189. TimedLockData<double> timedBrotherA_;
  190. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  191. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  192. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  193. bool running_ = false;
  194. bool pause_ = false;
  195. bool cancel_ = false;
  196. int move_mode_ = eSingle;
  197. bool is_master_AGV = false; // 是否是主车
  198. float wheelBase_; //两节agv的位姿与单节的位姿变换
  199. bool isInSpace_; //是否在车位或者正在进入车位
  200. std::string space_id_;
  201. TimedLockData<Trajectory> selected_traj_;
  202. TimedLockData<Trajectory> predict_traj_;
  203. SpaceNo2Region_Carry spaceNo_2_region_carrier_excel_;
  204. NavParameter::Navigation_parameter parameter_;
  205. RWheelPosition RWheel_position_;
  206. double obs_w_;
  207. double obs_h_;
  208. TimedLockData<bool> isFront_;
  209. };
  210. double limit_gause(double x, double min, double max);
  211. double limit(double x, double min, double max);
  212. double next_speed(double speed, double target_speed, double acc, double dt = 0.1);
  213. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_