dispatch_manager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "../dispatch/dispatch_network.h"
  10. #include "../tool/singleton.h"
  11. #include "../tool/thread_condition.h"
  12. #include "../tool/TaskQueue/TQFactory.h"
  13. #include "../tool/TaskQueue/BaseTask.h"
  14. //#include "../dispatch/carrier.h"
  15. //#include "../dispatch/catcher.h"
  16. //#include "../dispatch/passageway.h"
  17. //#include "../dispatch/dispatch_process.h"
  18. //#include "../message/dispatch_control.pb.h"
  19. //#include "../message/dispatch_message.pb.h"
  20. #include "../dispatch/dispatch_parameter.pb.h"
  21. #include "../dispatch/dispatch_plc.h"
  22. #include "../dispatch/dispatch_ground_lidar.h"
  23. #include "../dispatch/dispatch_singlechip.h"
  24. #include "../dispatch/dispatch_command.h"
  25. //#include "../message/measure_message.pb.h"
  26. #include "../message/message.pb.h"
  27. #include <vector>
  28. #include <glog/logging.h>
  29. #include <atomic>
  30. //lacate测量结果结构体, 整车的信息,
  31. typedef struct Locate_information
  32. {
  33. float locate_x; //整车的中心点x值, 四轮的中心
  34. float locate_y; //整车的中心点y值, 四轮的中心
  35. float locate_angle; //整车的旋转角, 四轮的旋转角
  36. float locate_length; //整车的长度, 用于规避碰撞
  37. float locate_width; //整车的宽度, 用于规避碰撞
  38. float locate_height; //整车的高度, 用于规避碰撞
  39. float locate_wheel_base; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车
  40. float locate_wheel_width; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车
  41. bool locate_correct; //整车的校准标记位
  42. //注:理论上, 车宽和左右轮距应该是一样的, 但是实际上车宽比左右轮距略大,
  43. }Locate_information;
  44. //调度管理模块
  45. class Dispatch_manager:public Singleton<Dispatch_manager>
  46. {
  47. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  48. friend class Singleton<Dispatch_manager>;
  49. public:
  50. //调度设备参数
  51. #define DISPATCH_DEVICE_PARAMETER_PATH "../setting/dispatch_device.prototxt"
  52. //存车任务数量的限制, 大于等于限则, 则使用缓存位, 默认5个
  53. #define DISPATCH_MANAHER_STORE_LIST_SIZE_LIMIT 5
  54. //调度管理 的状态
  55. enum Dispatch_manager_status
  56. {
  57. E_DISPATCH_MANAGER_UNKNOW = 0, //未知
  58. E_DISPATCH_MANAGER_READY = 1, //准备,待机
  59. E_DISPATCH_MANAGER_BUSY = 2, //工作正忙
  60. E_DISPATCH_MANAGER_DISCONNECT = 3, //断连
  61. E_DISPATCH_MANAGER_REQUEST = 4, //给plc发送请求
  62. E_DISPATCH_MANAGER_WORKING = 5, //plc工作中
  63. E_DISPATCH_MANAGER_REALLOCATE = 6, //重新分配车位
  64. E_DISPATCH_MANAGER_RESPONSE = 7, //给主控答复
  65. E_DISPATCH_MANAGER_STORE = 8, //正在存车
  66. E_DISPATCH_MANAGER_PICKUP = 9, //正在取车
  67. //新版
  68. E_DISPATCH_MANAGER_COUNT_NEW = 11, //第一步, 接受排序模块的计数, (当数据库有指令时,排序模块会给调度发一个排队总数)
  69. E_DISPATCH_MANAGER_REQUEST_NEW = 12, //第二步, 发送调度任务申请,(当调度模块空闲时,向排序模块发送调度任务申请)
  70. E_DISPATCH_MANAGER_POST_NEW = 13, //第三不, 接受调度指令(具体的存车取车任务)
  71. E_DISPATCH_MANAGER_COMPLETED_NEW = 14, //第四步, 答复调度指令(具体的存车取车任务)
  72. E_DISPATCH_MANAGER_FAULT = 100, //故障
  73. };
  74. //调度方向, 停车取车
  75. // enum Dispatch_motion_direction
  76. // {
  77. // E_STORE_CAR =0, //停车, 出入口 -> 停车位
  78. // E_PICKUP_CAR =1, //取车, 停车位 -> 出入口
  79. // };
  80. private:
  81. // 父类的构造函数必须保护,子类的构造函数必须私有。
  82. Dispatch_manager();
  83. public:
  84. //必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  85. Dispatch_manager(const Dispatch_manager& other) = delete;
  86. Dispatch_manager& operator =(const Dispatch_manager& other) = delete;
  87. ~Dispatch_manager();
  88. public://API functions
  89. //调度管理 初始化
  90. Error_manager dispatch_manager_init(int dispatch_manager_id);
  91. Error_manager dispatch_manager_init();
  92. //初始化 调度管理 模块。从文件读取
  93. Error_manager dispatch_manager_init_from_protobuf(std::string prototxt_path);
  94. //初始化 调度管理 模块。从protobuf读取
  95. Error_manager dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all);
  96. //调度管理 反初始化
  97. Error_manager dispatch_manager_uninit();
  98. //调度管理 设备复位
  99. Error_manager dispatch_manager_device_reset();
  100. //新的对外接口, 负责接受Rabbitmq_message,然后内部处理
  101. Error_manager execute_rabbitmq_message_new(Rabbitmq_message* p_msg);
  102. //给dispatch_plc底层提供接口, ack答复服务器,
  103. Error_manager ack_rabbitmq_message_new();
  104. //网络通信的对外接口. 对单片机通信
  105. Error_manager execute_network_message_new(Network_message* p_msg);
  106. //对外的接口函数,负责接受并处理任务单,
  107. // Error_manager execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction);
  108. //检查能否执行消息指令
  109. Error_manager check_execute_msg(Rabbitmq_message* p_msg);
  110. //检查状态
  111. Error_manager check_status();
  112. //调度模块 //执行搬运请求(主控->调度管理)
  113. // Error_manager execute_for_dispatch_request_msg(message::Dispatch_request_msg &dispatch_request_msg);
  114. // //调度模块 答复数据异常
  115. // Error_manager send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error);
  116. //
  117. // //调度模块 //调度总规划的答复(调度算法->调度管理)
  118. // Error_manager execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg);
  119. // //调度模块 //调度控制的任务请求(调度算法->调度管理)
  120. // Error_manager execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg);
  121. //定时发送 调度管理的状态
  122. Error_manager encapsulate_send_dispatch_manager_status();
  123. //定时发送 调度管理的状态
  124. Error_manager encapsulate_send_dispatch_manager_status_new();
  125. //在流程的map 里面释放指定的流程
  126. Error_manager release_dispatch_process(std::string command_key);
  127. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  128. // Error_manager execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg);
  129. //调度模块 ///单片机的状态消息
  130. // Error_manager execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity);
  131. //获取空闲的出口,返回0表示没有空闲出口, 返回1~6表示对应的出口
  132. int get_outlet_for_ready();
  133. public://get or set member variable
  134. Dispatch_manager_status get_dispatch_manager_status();
  135. int get_dispatch_manager_id();
  136. void set_dispatch_manager_id(int dispatch_id);
  137. protected:
  138. //资源分配
  139. // void resource_allocation();
  140. //新版流程控制
  141. // void process_control();
  142. //新版流程控制
  143. void process_sql();
  144. public://member variable
  145. Dispatch_manager_status m_dispatch_manager_status; //调度管理 的状态
  146. int m_dispatch_manager_id; //调度模块的id, (楚天项目就是单元号, 0~2)
  147. //流程控制
  148. std::mutex m_lock; //线程池的锁, 增删流程时要加锁.
  149. //调度plc
  150. Dispatch_plc m_dispatch_plc; //调度plc
  151. Dispatch_ground_lidar m_dispatch_ground_lidar[2]; //调度地面雷达
  152. Dispatch_singlechip m_dispatch_singlechip[4]; //调度单片机
  153. int m_plc_floor; //穿梭机所在的楼层, 存车完在对应的车位楼层, 取车完在1楼
  154. //请求指令的list, 按照创建顺序排序, 先来后到, 停车和停车不能插队, 但是停车和取车可以插队.
  155. // std::list<message::Dispatch_request_msg> m_dispatch_request_store_list; //存车请求指令的list, 内存由线程池管理
  156. // std::list<message::Dispatch_request_msg> m_dispatch_request_pickup_list; //取车请求指令的list, 内存由线程池管理
  157. // Common_data::Dispatch_motion_direction m_dispatch_motion_direction_next; //下一次的调度方向, 保证存车取车交替进行
  158. // std::map<std::chrono::system_clock::time_point , message::Dispatch_response_msg>
  159. // m_dispatch_response_store_map; //存车dafu de map, 内存由线程池管理, time_point
  160. // std::map<int , message::Dispatch_response_msg> m_dispatch_response_pickup_map; //取车dafu de map, 内存由线程池管理, int:outlet_id
  161. // std::chrono::system_clock::time_point m_store_updata_time; //
  162. // std::chrono::system_clock::time_point m_pickup_updata_time; //
  163. //调度总管理的线程, 负责资源分配
  164. std::thread* m_dispatch_manager_thread; //调度总管理的线程, 总控全局, 控制每个流程的先后顺序, 并合理的分配资源.
  165. Thread_condition m_dispatch_manager_condition; //调度总管理的条件变量, 总控全局, 控制每个流程的先后顺序, 并合理的分配资源.
  166. //新版调度,消息缓存
  167. int m_device_floor; //设别楼层, 初始化为0
  168. bool m_count_flag; //接受排序模块的计数 的标志位,
  169. Rabbitmq_message m_count_command_signal_msg; //第一步, 接受排序模块的计数, (当数据库有指令时,排序模块会给调度发一个排队总数)
  170. Rabbitmq_message m_request_command_msg; //第二步, 发送调度任务申请,(当调度模块空闲时,向排序模块发送调度任务申请)
  171. bool m_post_flag; //接受调度指令 的标志位,
  172. Rabbitmq_message m_post_command_msg; //第三不, 接受调度指令(具体的存车取车任务)
  173. Rabbitmq_message m_command_completed_msg; //第四步, 答复调度指令(具体的存车取车任务)
  174. Common_data::Dispatch_process_type m_dispatch_process_type ; //调度流程类型
  175. park_table m_park_table_msg; //停车表单
  176. pick_table m_pick_table_msg; //取车表单
  177. Common_data::Car_type m_car_type; //车的大小
  178. Dispatch_command m_dispatch_command; //调度指令模块
  179. //地面雷达
  180. Rabbitmq_message m_measure_statu_msg_1;
  181. Rabbitmq_message m_measure_statu_msg_2;
  182. measure_info m_measure_info_1;
  183. measure_info m_measure_info_2;
  184. //出入口单片机
  185. Rabbitmq_message m_in_mcpu_statu_msg_1;
  186. Rabbitmq_message m_in_mcpu_statu_msg_2;
  187. in_mcpu_statu m_in_mcpu_statu_1;
  188. in_mcpu_statu m_in_mcpu_statu_2;
  189. Rabbitmq_message m_out_mcpu_statu_msg_1;
  190. Rabbitmq_message m_out_mcpu_statu_msg_2;
  191. out_mcpu_statu m_out_mcpu_statu_1;
  192. out_mcpu_statu m_out_mcpu_statu_2;
  193. std::chrono::system_clock::time_point m_request_updata_time; //请求更新时间
  194. private:
  195. };
  196. #endif //NNXX_TESTS_DISPATCH_MANAGER_H