dispatch_manager.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Created by huli on 2020/7/20.
  3. //
  4. #ifndef NNXX_TESTS_DISPATCH_MANAGER_H
  5. #define NNXX_TESTS_DISPATCH_MANAGER_H
  6. #include "../error_code/error_code.h"
  7. #include "../communication/communication_message.h"
  8. #include "../system/system_communication.h"
  9. #include "../tool/singleton.h"
  10. #include "../tool/thread_condition.h"
  11. #include "../tool/TaskQueue/TQFactory.h"
  12. #include "../tool/TaskQueue/BaseTask.h"
  13. //#include "../dispatch/carrier.h"
  14. //#include "../dispatch/catcher.h"
  15. //#include "../dispatch/passageway.h"
  16. //#include "../dispatch/dispatch_process.h"
  17. #include "../message/dispatch_control.pb.h"
  18. #include "../message/dispatch_message.pb.h"
  19. #include "../dispatch/dispatch_parameter.pb.h"
  20. #include "../dispatch/dispatch_plc.h"
  21. #include "../dispatch/dispatch_ground_lidar.h"
  22. #include "../message/measure_message.pb.h"
  23. #include <vector>
  24. #include <glog/logging.h>
  25. #include <atomic>
  26. //lacate测量结果结构体, 整车的信息,
  27. typedef struct Locate_information
  28. {
  29. float locate_x; //整车的中心点x值, 四轮的中心
  30. float locate_y; //整车的中心点y值, 四轮的中心
  31. float locate_angle; //整车的旋转角, 四轮的旋转角
  32. float locate_length; //整车的长度, 用于规避碰撞
  33. float locate_width; //整车的宽度, 用于规避碰撞
  34. float locate_height; //整车的高度, 用于规避碰撞
  35. float locate_wheel_base; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车
  36. float locate_wheel_width; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车
  37. bool locate_correct; //整车的校准标记位
  38. //注:理论上, 车宽和左右轮距应该是一样的, 但是实际上车宽比左右轮距略大,
  39. }Locate_information;
  40. //调度管理模块
  41. class Dispatch_manager:public Singleton<Dispatch_manager>
  42. {
  43. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  44. friend class Singleton<Dispatch_manager>;
  45. public:
  46. //调度设备参数
  47. #define DISPATCH_DEVICE_PARAMETER_PATH "../setting/dispatch_device.prototxt"
  48. //存车任务数量的限制, 大于等于限则, 则使用缓存位, 默认5个
  49. #define DISPATCH_MANAHER_STORE_LIST_SIZE_LIMIT 5
  50. //调度管理 的状态
  51. enum Dispatch_manager_status
  52. {
  53. E_DISPATCH_MANAGER_UNKNOW = 0, //未知
  54. E_DISPATCH_MANAGER_READY = 1, //准备,待机
  55. E_DISPATCH_MANAGER_STORE = 2, //正在存车
  56. E_DISPATCH_MANAGER_PICKUP = 3, //正在取车
  57. E_DISPATCH_MANAGER_FAULT = 100, //故障
  58. };
  59. //调度方向, 停车取车
  60. enum Dispatch_motion_direction
  61. {
  62. E_STORE_CAR =0, //停车, 出入口 -> 停车位
  63. E_PICKUP_CAR =1, //取车, 停车位 -> 出入口
  64. };
  65. private:
  66. // 父类的构造函数必须保护,子类的构造函数必须私有。
  67. Dispatch_manager();
  68. public:
  69. //必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  70. Dispatch_manager(const Dispatch_manager& other) = delete;
  71. Dispatch_manager& operator =(const Dispatch_manager& other) = delete;
  72. ~Dispatch_manager();
  73. public://API functions
  74. //调度管理 初始化
  75. Error_manager dispatch_manager_init(int dispatch_manager_id);
  76. Error_manager dispatch_manager_init();
  77. //初始化 调度管理 模块。从文件读取
  78. Error_manager dispatch_manager_init_from_protobuf(std::string prototxt_path);
  79. //初始化 调度管理 模块。从protobuf读取
  80. Error_manager dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all);
  81. //调度管理 反初始化
  82. Error_manager dispatch_manager_uninit();
  83. //调度管理 设备复位
  84. Error_manager dispatch_manager_device_reset();
  85. //对外的接口函数,负责接受并处理任务单,
  86. Error_manager execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction);
  87. //检查能否执行消息指令
  88. Error_manager check_execute_msg(Communication_message* p_msg);
  89. //检查状态
  90. Error_manager check_status();
  91. //调度模块 //执行搬运请求(主控->调度管理)
  92. Error_manager execute_for_dispatch_request_msg(message::Dispatch_request_msg &dispatch_request_msg);
  93. //调度模块 答复数据异常
  94. Error_manager send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error);
  95. //调度模块 //调度总规划的答复(调度算法->调度管理)
  96. Error_manager execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg);
  97. //调度模块 //调度控制的任务请求(调度算法->调度管理)
  98. Error_manager execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg);
  99. //定时发送 调度管理的状态
  100. Error_manager encapsulate_send_dispatch_manager_status();
  101. //在流程的map 里面释放指定的流程
  102. Error_manager release_dispatch_process(std::string command_key);
  103. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  104. Error_manager execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg);
  105. public://get or set member variable
  106. Dispatch_manager_status get_dispatch_manager_status();
  107. int get_dispatch_manager_id();
  108. void set_dispatch_manager_id(int dispatch_id);
  109. protected:
  110. //资源分配
  111. void resource_allocation();
  112. public://member variable
  113. Dispatch_manager_status m_dispatch_manager_status; //调度管理 的状态
  114. int m_dispatch_manager_id; //调度模块的id, (楚天项目就是单元号, 0~2)
  115. //流程控制
  116. std::mutex m_lock; //线程池的锁, 增删流程时要加锁.
  117. //调度plc
  118. Dispatch_plc m_dispatch_plc; //调度plc
  119. Dispatch_ground_lidar m_dispatch_ground_lidar[2]; //调度地面雷达
  120. //请求指令的list, 按照创建顺序排序, 先来后到, 停车和停车不能插队, 但是停车和取车可以插队.
  121. std::list<message::Dispatch_request_msg> m_dispatch_request_store_list; //存车请求指令的list, 内存由线程池管理
  122. std::list<message::Dispatch_request_msg> m_dispatch_request_pickup_list; //取车请求指令的list, 内存由线程池管理
  123. Common_data::Dispatch_motion_direction m_dispatch_motion_direction_next; //下一次的调度方向, 保证存车取车交替进行
  124. //调度总管理的线程, 负责资源分配
  125. std::thread* m_dispatch_manager_thread; //调度总管理的线程, 总控全局, 控制每个流程的先后顺序, 并合理的分配资源.
  126. Thread_condition m_dispatch_manager_condition; //调度总管理的条件变量, 总控全局, 控制每个流程的先后顺序, 并合理的分配资源.
  127. private:
  128. };
  129. #endif //NNXX_TESTS_DISPATCH_MANAGER_H