dispatch_manager.h 11 KB

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