navigation.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 MpcResult {
  37. eMpcSuccess = 0,
  38. eMpcFailed,
  39. eImpossibleToTarget
  40. };
  41. public:
  42. Navigation();
  43. virtual ~Navigation();
  44. //连接AGV emqx服务
  45. bool Init(const NavParameter::Navigation_parameter &parameter);
  46. virtual void ResetPose(const Pose2d &pose);
  47. void ResetStatu(double v, double a);
  48. virtual void ResetClamp(ClampStatu statu);
  49. virtual void ResetLifter(LifterStatus status);
  50. TimedLockData<Pose2d> &RealTimePose() {
  51. return timedPose_;
  52. }
  53. TimedLockData<double> &RealTimeV() {
  54. return timedV_;
  55. }
  56. TimedLockData<double> &RealTimeA() {
  57. return timedA_;
  58. }
  59. virtual void Start(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  60. /* 手动旋转 */
  61. virtual void ManualOperation(const NavMessage::ManualCmd &cmd, NavMessage::NavResponse &response);
  62. void Cancel(const NavMessage::NavCmd &cmd, NavMessage::NavResponse &response);
  63. bool Stop();
  64. bool SlowlyStop();
  65. virtual void SwitchMode(int mode, float wheelBase);
  66. bool PoseTimeout();
  67. // 测试日志打印
  68. bool LogWriter(std::string lines){
  69. {
  70. std::ofstream ofs; //创建流对象
  71. std::string log_file_dir = "../data/12_12/";
  72. std::string log_file_name = "12_12.txt";
  73. ofs.open(log_file_dir+log_file_name,std::ios::app); //打开的地址和方式
  74. time_t t = time(0);
  75. char tmp[32]={NULL};
  76. strftime(tmp, sizeof(tmp), "%Y_%m_%d_%H_%M_%S",localtime(&t));
  77. std::string time = tmp;
  78. std::string log_inf_line = lines + ". " + time;
  79. ofs << log_inf_line << std::endl; //一行日志的内容
  80. ofs.close();
  81. }
  82. return true;
  83. }
  84. protected:
  85. virtual void HandleAgvStatu(const MqttMsg &msg);
  86. virtual void SendMoveCmd(int mode, ActionType type, double v, double angular);
  87. virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[]);
  88. virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[],
  89. int space_id, double distance,double Y1=0.0,double Y2=0.0);
  90. // virtual void SendMoveCmd(int mode, ActionType type, double v[], double angular[], double diff_yaw[], int space_id, double distance);
  91. static void RobotPoseCallback(const MqttMsg &msg, void *context);
  92. static void RobotSpeedCallback(const MqttMsg &msg, void *context);
  93. static void BrotherAgvStatuCallback(const MqttMsg &msg, void *context);
  94. bool IsArrived(NavMessage::PathNode targetNode);
  95. bool PossibleToTarget(NavMessage::PathNode targetNode, bool directY);
  96. /*
  97. * 移动到目标点,不能到达则旋转
  98. */
  99. bool MoveToTarget(NavMessage::PathNode node, stLimit limit_v, stLimit limit_w, bool autoDirect, bool enable_rotate);
  100. /*
  101. * autoDirect:自动选择 纵向还是横向巡线
  102. */
  103. MpcResult MpcToTarget(NavMessage::PathNode node, stLimit limit_v, bool autoDirect);
  104. /*
  105. * 旋转摆正,朝向目标点,
  106. * anyDirect: 任意一个朝向对准目标点
  107. */
  108. virtual bool RotateReferToTarget(const Pose2d &target, stLimit limit_rotate, bool anyDirect);
  109. virtual MpcResult RotateReferToTarget(NavMessage::PathNode target, stLimit limit_rotate, int directions);
  110. virtual bool AdjustReferToTarget(const Pose2d& target, stLimit limit_v);
  111. /*
  112. * 按照路径点导航
  113. */
  114. bool execute_nodes(NavMessage::NewAction action,int next_actionTypeID=-1);
  115. /*
  116. * 进出库
  117. */
  118. bool execute_InOut_space(NavMessage::NewAction action);
  119. /*
  120. * 根据自身和配对车辆状态,调整入库姿态,先到的进入车位1点,后车先到的倒车入库,后到的车与先到的车保持朝向一致
  121. * space:输入车位点,
  122. * target:输出目标点
  123. */
  124. virtual Navigation::MpcResult
  125. RotateBeforeEnterSpace(NavMessage::PathNode space, double wheelbase, NavMessage::PathNode &target);
  126. virtual Navigation::MpcResult
  127. RotateAfterOutSpace();
  128. virtual bool clamp_close();
  129. virtual bool clamp_half_open();
  130. virtual bool clamp_fully_open();
  131. virtual bool lifter_rise();
  132. virtual bool lifter_down();
  133. virtual bool singleClamp_fullyOpen_until_inspaceBegin();
  134. static bool IsUperSpace(NavMessage::PathNode spaceNode);
  135. bool mpc_once(Trajectory traj, stLimit limit_v, std::vector<double> &out, bool directY = false);
  136. /* 原地旋转mpc
  137. *
  138. * */
  139. bool
  140. Rotation_mpc_once(Pose2d current_to_target, stLimit limit_wmg, std::vector<double> &out, bool all_direct = true);
  141. virtual bool CreateRobotStatuMsg(NavMessage::RobotStatu &robotStatu);
  142. /*
  143. * 计算以当前点到目标点的曲率
  144. */
  145. float compute_cuv(const Pose2d &target);
  146. /*
  147. * 发布导航模块状态
  148. */
  149. static void pubStatuThreadFuc(void *p);
  150. virtual void publish_statu(NavMessage::NavStatu &statu);
  151. protected:
  152. std::mutex mtx_;
  153. bool exit_ = false;
  154. ActionType actionType_ = eReady;
  155. std::thread *pubthread_ = nullptr;
  156. bool inited_ = false;
  157. Monitor_emqx *monitor_ = nullptr;
  158. Terminator_emqx *terminator_ = nullptr; //本机emqx连接
  159. Terminator_emqx *brotherEmqx_ = nullptr; //本机emqx连接
  160. TimedLockData<double> timeYawDiff1_;
  161. TimedLockData<double> timeYawDiff2_;
  162. TimedLockData<Pose2d> timedPose_; //当前位姿
  163. TimedLockData<double> timedV_; //底盘数据
  164. TimedLockData<double> timedA_;
  165. TimedLockData<ClampStatu> timed_clamp_; //加持杆
  166. TimedLockData<LifterStatus> timed_lifter_; //提升机构
  167. TimedLockData<Pose2d> timedBrotherPose_; //当前位姿
  168. TimedLockData<double> timedBrotherV_; //底盘数据
  169. TimedLockData<double> timedBrotherA_;
  170. TimedLockData<NavMessage::NavStatu> timedBrotherNavStatu_;
  171. NavMessage::NavCmd global_navCmd_; //导航任务,包含一系列动作
  172. std::queue<NavMessage::NewAction> newUnfinished_cations_; //未完成的动作
  173. bool running_ = false;
  174. bool pause_ = false;
  175. bool cancel_ = false;
  176. int move_mode_ = eSingle;
  177. float wheelBase_; //两节agv的位姿与单节的位姿变换
  178. bool isInSpace_; //是否在车位或者正在进入车位
  179. std::string space_id_;
  180. TimedLockData<Trajectory> selected_traj_;
  181. TimedLockData<Trajectory> predict_traj_;
  182. NavParameter::Navigation_parameter parameter_;
  183. RWheelPosition RWheel_position_;
  184. double obs_w_;
  185. double obs_h_;
  186. TimedLockData<bool> isFront_;
  187. };
  188. double limit_gause(double x, double min, double max);
  189. double limit(double x, double min, double max);
  190. double next_speed(double speed, double target_speed, double acc, double dt = 0.1);
  191. #endif //LIO_LIVOX_CPP_MPC_NAVIGATION_H_