dispatch_manager.h 6.8 KB

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