navigation.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. eMPC,
  29. eClampOpen,
  30. eClampClose
  31. };
  32. enum ClampStatu
  33. {
  34. eInvalid=0,
  35. eClosed=1,
  36. eOpened=2
  37. };
  38. enum AdjustYawMode{
  39. eX=0,
  40. eY,
  41. eXY
  42. };
  43. enum MpcResult{
  44. eMpcSuccess=0,
  45. eMpcFailed,
  46. eImpossibleToTarget
  47. };
  48. public:
  49. Navigation();
  50. virtual ~Navigation();
  51. //连接AGV emqx服务
  52. bool Init(const NavParameter::Navigation_parameter& parameter);
  53. virtual void ResetPose(const Pose2d& pose);
  54. void ResetStatu(double v,double a);
  55. virtual void ResetClamp(ClampStatu statu);
  56. TimedLockData<Pose2d>& RealTimePose(){
  57. return timedPose_;
  58. }
  59. TimedLockData<double>& RealTimeV(){
  60. return timedV_;
  61. }
  62. TimedLockData<double>& RealTimeA(){
  63. return timedA_;
  64. }
  65. virtual bool Start(const NavMessage::NavCmd& cmd);
  66. void Cancel();
  67. void Pause();
  68. bool Stop();
  69. virtual void SwitchMode(Monitor_emqx::ActionMode mode,float wheelBase);
  70. bool PoseTimeout();
  71. protected:
  72. virtual void HandleAgvStatu(const MqttMsg& msg);
  73. virtual void SendMoveCmd(Monitor_emqx::ActionMode mode,Monitor_emqx::ActionType type,
  74. double v,double angular);
  75. virtual void HandleNavCmd(const NavMessage::NavCmd& cmd);
  76. static void RobotPoseCallback(const MqttMsg& msg,void* context);
  77. static void RobotSpeedCallback(const MqttMsg& msg,void* context);
  78. static void NavCmdCallback(const MqttMsg& msg,void* context);
  79. static void BrotherAgvStatuCallback(const MqttMsg& msg,void* context);
  80. /*
  81. * direct : 0:严格朝向一致,1:x轴一致 2:xy轴任意朝向一致
  82. */
  83. static bool IsArrived(const Pose2d& cur,double velocity,double angular,const Pose2d& target,
  84. const Pose2d& diff,int direct);
  85. /*
  86. * autoDirect:自动选择 纵向还是横向巡线
  87. */
  88. MpcResult execute_along_action(const Pose2d& begin,const Pose2d& target,const Pose2d& target_diff,
  89. stLimit limit_v,bool autoDirect);
  90. /*
  91. * 调整到目标点,nearest_yaw:xy轴线任一朝向对准目标朝向即可
  92. */
  93. bool execute_adjust_action(const Pose2d& target,const Pose2d& target_diff,stLimit limit_v,
  94. stLimit limit_h,stLimit limit_rotate,bool anyDitect);
  95. /*
  96. * 按照路径点导航
  97. */
  98. bool execute_nodes(NavMessage::NewAction action);
  99. /*
  100. * 按照汽车模型横移导航
  101. */
  102. bool execute_vehicle_mode(NavMessage::NewAction action);
  103. /*
  104. * 进库
  105. */
  106. bool execute_InOut_space(NavMessage::NewAction action);
  107. virtual bool clamp_close();
  108. virtual bool clamp_open();
  109. bool mpc_once(Trajectory traj,stLimit limit_v,std::vector<double>& out,bool directY=false);
  110. bool mpc_possible_to_end(const Pose2d& target,const Pose2d& diff,bool directY);
  111. void navigatting();
  112. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu& robotStatu);
  113. /*
  114. * 发布导航模块状态
  115. */
  116. static void pubStatuThreadFuc(void* p);
  117. virtual void publish_statu(NavMessage::NavStatu& statu);
  118. protected:
  119. std::mutex mtx_;
  120. bool exit_=false;
  121. ActionType actionType_=eReady;
  122. std::thread* pubthread_= nullptr;
  123. bool inited_=false;
  124. Monitor_emqx* monitor_= nullptr;
  125. Terminator_emqx* terminator_= nullptr; //本机emqx连接
  126. Terminator_emqx* brotherEmqx_= nullptr; //本机emqx连接
  127. TimedLockData<Pose2d> timedPose_; //当前位姿
  128. TimedLockData<double> timedV_; //底盘数据
  129. TimedLockData<double> timedA_;
  130. TimedLockData<ClampStatu> timed_clamp_;
  131. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  132. TimedLockData<double> timedBrotherV_; //底盘数据
  133. TimedLockData<double> timedBrotherA_;
  134. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  135. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  136. std::thread* thread_= nullptr;
  137. bool running_=false;
  138. bool pause_=false;
  139. bool cancel_=false;
  140. Monitor_emqx::ActionMode move_mode_=Monitor_emqx::eSingle;
  141. TimedLockData<Trajectory> selected_traj_;
  142. TimedLockData<Trajectory> predict_traj_;
  143. NavParameter::Navigation_parameter parameter_;
  144. };
  145. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_