navigation.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. {
  17. public:
  18. typedef struct{
  19. double min;
  20. double max;
  21. }stLimit;
  22. enum ActionType
  23. {
  24. eReady=0,
  25. eRotation,
  26. eHorizon,
  27. eVertical,
  28. eClampOpen,
  29. eClampClose
  30. };
  31. //舵轮位置
  32. enum RWheelPosition{
  33. eUnknow=0,
  34. eX, //超前
  35. eY, //横移
  36. eR //旋转位
  37. };
  38. enum ClampStatu
  39. {
  40. eInvalid=0,
  41. eClosed=1,
  42. eOpened=2
  43. };
  44. enum MpcResult{
  45. eMpcSuccess=0,
  46. eMpcFailed,
  47. eImpossibleToTarget
  48. };
  49. public:
  50. Navigation();
  51. virtual ~Navigation();
  52. //连接AGV emqx服务
  53. bool Init(const NavParameter::Navigation_parameter& parameter);
  54. virtual void ResetPose(const Pose2d& pose);
  55. void ResetStatu(double v,double a);
  56. virtual void ResetClamp(ClampStatu statu);
  57. TimedLockData<Pose2d>& RealTimePose(){
  58. return timedPose_;
  59. }
  60. TimedLockData<double>& RealTimeV(){
  61. return timedV_;
  62. }
  63. TimedLockData<double>& RealTimeA(){
  64. return timedA_;
  65. }
  66. virtual void Start(const NavMessage::NavCmd& cmd,NavMessage::NavResponse& response);
  67. void Cancel(const NavMessage::NavCmd& cmd,NavMessage::NavResponse& response);
  68. bool Stop();
  69. bool SlowlyStop();
  70. virtual void SwitchMode(Monitor_emqx::ActionMode mode,float wheelBase);
  71. bool PoseTimeout();
  72. protected:
  73. virtual void HandleAgvStatu(const MqttMsg& msg);
  74. virtual void SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  75. double v,double angular);
  76. static void RobotPoseCallback(const MqttMsg& msg,void* context);
  77. static void RobotSpeedCallback(const MqttMsg& msg,void* context);
  78. static void BrotherAgvStatuCallback(const MqttMsg& msg,void* context);
  79. bool IsArrived(NavMessage::PathNode targetNode);
  80. bool PossibleToTarget(NavMessage::PathNode targetNode,bool directY);
  81. /*
  82. * 移动到目标点,不能到达则旋转
  83. */
  84. bool MoveToTarget(NavMessage::PathNode node,stLimit limit_v,stLimit limit_w,bool autoDirect,bool enable_rotate);
  85. /*
  86. * autoDirect:自动选择 纵向还是横向巡线
  87. */
  88. MpcResult MpcToTarget(NavMessage::PathNode node,stLimit limit_v,bool autoDirect);
  89. /*
  90. * 旋转摆正,朝向目标点,
  91. * anyDirect: 任意一个朝向对准目标点
  92. */
  93. virtual bool RotateReferToTarget(const Pose2d& target,stLimit limit_rotate,bool anyDirect);
  94. /*
  95. * 按照路径点导航
  96. */
  97. bool execute_nodes(NavMessage::NewAction action);
  98. /*
  99. * 进出库
  100. */
  101. bool execute_InOut_space(NavMessage::NewAction action);
  102. /*
  103. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  104. * space:输入车位点,
  105. * target:输出目标点
  106. */
  107. virtual bool RotateBeforeEnterSpace(NavMessage::PathNode space,double wheelbase,NavMessage::PathNode& target);
  108. virtual bool clamp_close();
  109. virtual bool clamp_open();
  110. bool mpc_once(Trajectory traj,stLimit limit_v,std::vector<double>& out,bool directY=false);
  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<Pose2d> timedBrotherPose_; //当前位姿
  135. TimedLockData<double> timedBrotherV_; //底盘数据
  136. TimedLockData<double> timedBrotherA_;
  137. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  138. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  139. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  140. bool running_=false;
  141. bool pause_=false;
  142. bool cancel_=false;
  143. Monitor_emqx::ActionMode move_mode_=Monitor_emqx::eSingle;
  144. bool isInSpace_; //是否在车位或者正在进入车位
  145. std::string space_id_;
  146. TimedLockData<Trajectory> selected_traj_;
  147. TimedLockData<Trajectory> predict_traj_;
  148. NavParameter::Navigation_parameter parameter_;
  149. RWheelPosition RWheel_position_;
  150. };
  151. double limit_gause(double x,double min,double max);
  152. double limit(double x,double min,double max);
  153. double next_speed(double speed,double target_speed,double acc,double dt=0.1);
  154. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_