navigation.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 bool Start(const NavMessage::NavCmd& cmd);
  67. void Cancel();
  68. void Pause();
  69. bool Stop();
  70. bool SlowlyStop();
  71. virtual void SwitchMode(Monitor_emqx::ActionMode mode,float wheelBase);
  72. bool PoseTimeout();
  73. protected:
  74. virtual void HandleAgvStatu(const MqttMsg& msg);
  75. virtual void SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  76. double v,double angular);
  77. virtual void HandleNavCmd(const NavMessage::NavCmd& cmd);
  78. static void RobotPoseCallback(const MqttMsg& msg,void* context);
  79. static void RobotSpeedCallback(const MqttMsg& msg,void* context);
  80. static void NavCmdCallback(const MqttMsg& msg,void* context);
  81. static void BrotherAgvStatuCallback(const MqttMsg& msg,void* context);
  82. bool IsArrived(NavMessage::PathNode targetNode);
  83. bool PossibleToTarget(NavMessage::PathNode targetNode,bool directY);
  84. /*
  85. * 移动到目标点,不能到达则旋转
  86. */
  87. bool MoveToTarget(NavMessage::PathNode node,stLimit limit_v,stLimit limit_w,bool autoDirect,bool enable_rotate);
  88. /*
  89. * autoDirect:自动选择 纵向还是横向巡线
  90. */
  91. MpcResult MpcToTarget(NavMessage::PathNode node,stLimit limit_v,bool autoDirect);
  92. /*
  93. * 旋转摆正,朝向目标点,
  94. * anyDirect: 任意一个朝向对准目标点
  95. */
  96. virtual bool RotateReferToTarget(const Pose2d& target,stLimit limit_rotate,bool anyDirect);
  97. /*
  98. * 按照路径点导航
  99. */
  100. bool execute_nodes(NavMessage::NewAction action);
  101. /*
  102. * 进出库
  103. */
  104. bool execute_InOut_space(NavMessage::NewAction action);
  105. /*
  106. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  107. * space:输入车位点,
  108. * target:输出目标点
  109. */
  110. virtual bool RotateBeforeEnterSpace(NavMessage::PathNode space,double wheelbase,NavMessage::PathNode& target);
  111. virtual bool clamp_close();
  112. virtual bool clamp_open();
  113. bool mpc_once(Trajectory traj,stLimit limit_v,std::vector<double>& out,bool directY=false);
  114. void navigatting();
  115. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu& robotStatu);
  116. /*
  117. * 计算以当前点到目标点的曲率
  118. */
  119. float compute_cuv(const Pose2d& target);
  120. /*
  121. * 发布导航模块状态
  122. */
  123. static void pubStatuThreadFuc(void* p);
  124. virtual void publish_statu(NavMessage::NavStatu& statu);
  125. protected:
  126. std::mutex mtx_;
  127. bool exit_=false;
  128. ActionType actionType_=eReady;
  129. std::thread* pubthread_= nullptr;
  130. bool inited_=false;
  131. Monitor_emqx* monitor_= nullptr;
  132. Terminator_emqx* terminator_= nullptr; //本机emqx连接
  133. Terminator_emqx* brotherEmqx_= nullptr; //本机emqx连接
  134. TimedLockData<Pose2d> timedPose_; //当前位姿
  135. TimedLockData<double> timedV_; //底盘数据
  136. TimedLockData<double> timedA_;
  137. TimedLockData<ClampStatu> timed_clamp_;
  138. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  139. TimedLockData<double> timedBrotherV_; //底盘数据
  140. TimedLockData<double> timedBrotherA_;
  141. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  142. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  143. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  144. std::thread* thread_= nullptr;
  145. bool running_=false;
  146. bool pause_=false;
  147. bool cancel_=false;
  148. Monitor_emqx::ActionMode move_mode_=Monitor_emqx::eSingle;
  149. bool isInSpace_; //是否在车位或者正在进入车位
  150. std::string space_id_;
  151. TimedLockData<Trajectory> selected_traj_;
  152. TimedLockData<Trajectory> predict_traj_;
  153. NavParameter::Navigation_parameter parameter_;
  154. RWheelPosition RWheel_position_;
  155. };
  156. double limit_gause(double x,double min,double max);
  157. double limit(double x,double min,double max);
  158. double next_speed(double speed,double target_speed,double acc,double dt=0.1);
  159. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_