dispatch_manager.h 7.4 KB

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