dispatch_plc.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Dispatch_plc 调度plc模块, 主要是负责对接plc的通信数据缓存,以及调度主流程
  3. // 本地缓存DB块的的数据, 内部线程负责下发任务指令和验收任务指令.
  4. //
  5. #ifndef NNXX_TESTS_DISPATCH_PLC_H
  6. #define NNXX_TESTS_DISPATCH_PLC_H
  7. #include "../error_code/error_code.h"
  8. #include <glog/logging.h>
  9. #include <thread>
  10. #include <mutex>
  11. #include "../tool/thread_condition.h"
  12. #include "../tool/common_data.h"
  13. #include "../tool/time_tool.h"
  14. #include "../task/task_base.h"
  15. #include "../message/dispatch_message.pb.h"
  16. #include "../dispatch/dispatch_communication.h"
  17. #include "../tool/string_convert.h"
  18. //调度plc
  19. class Dispatch_plc
  20. {
  21. //设备底层通信延时5000ms
  22. #define COMMUNICATION_OVER_TIME_MS 5000
  23. //调度指令超时时间 300000ms = 5min
  24. #define DISPATCH_PROCESS_TIMEOUT_MS 600000
  25. //调度指令超时时间 衰减值 在原有的基础上减少10秒
  26. #define DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS 10000
  27. public:
  28. //调度plc状态
  29. enum Dispatch_plc_status
  30. {
  31. DISPATCH_PLC_UNKNOW = 0, //未知
  32. DISPATCH_PLC_READY = 1, //准备,待机
  33. DISPATCH_PLC_BUSY = 2, //工作正忙
  34. DISPATCH_PLC_DISCONNECT = 3, //断连
  35. DISPATCH_PLC_REQUEST = 4, //给plc发送请求
  36. DISPATCH_PLC_WORKING = 5, //plc工作中
  37. DISPATCH_PLC_RESPONSE = 6, //给主控答复
  38. DISPATCH_PLC_FAULT = 10, //故障
  39. };
  40. //指令完成状态, 设备答复指令, 返回任务完成的情况
  41. enum Response_status
  42. {
  43. RESPONS_WORKING = 0, //任务进行中
  44. RESPONS_OVER = 1, //任务完成
  45. RESPONS_MINOR_ERROR = 100, //一般故障, 可恢复
  46. RESPONS_CRITICAL_ERROR = 101, //致命故障,不可恢复
  47. };
  48. public:
  49. Dispatch_plc();
  50. Dispatch_plc(const Dispatch_plc& other)= default;
  51. Dispatch_plc& operator =(const Dispatch_plc& other)= default;
  52. ~Dispatch_plc();
  53. public://API functions
  54. //调度plc 初始化
  55. Error_manager dispatch_plc_init(int plc_id);
  56. //调度plc 反初始化
  57. Error_manager dispatch_plc_uninit();
  58. //调度plc 执行请求
  59. Error_manager execute_for_dispatch_request_msg(message::Dispatch_request_msg &dispatch_request_msg);
  60. public://get or set member variable
  61. Dispatch_plc::Dispatch_plc_status get_dispatch_plc_status();
  62. message::Dispatch_request_msg get_dispatch_request_msg();
  63. void clear_request_msg();
  64. protected://member functions
  65. //jiancha qingqiu xiaoxi
  66. Error_manager check_dispatch_request_msg(message::Dispatch_request_msg & dispatch_request_msg);
  67. //执行外界任务的执行函数
  68. void execute_thread_fun();
  69. //封装 给plc的调度请求
  70. Error_manager encapsulate_dispatch_request_to_plc();
  71. //更新plc通信
  72. Error_manager update_dispatch_plc_communication();
  73. //检查plc答复反馈
  74. Error_manager check_response_from_plc();
  75. //发送调度答复信息给主控
  76. Error_manager send_dispatch_response_to_main_control();
  77. protected://member variable
  78. std::atomic<Dispatch_plc_status> m_dispatch_plc_status; //设备总状态, 控制任务流程(长流程)
  79. int m_plc_id; //设备id, 索引, 就是楚天车库的单元号.
  80. std::mutex m_lock; //锁
  81. //任务执行线程
  82. std::thread* mp_execute_thread; //执行的线程指针,内存由本类管理
  83. Thread_condition m_execute_condition; //执行的条件变量
  84. //指令
  85. Common_data::Dispatch_process_type m_dispatch_process_type ; //调度流程类型
  86. message::Dispatch_request_msg m_dispatch_request_msg; //1执行搬运请求(主控->调度管理)
  87. message::Dispatch_response_msg m_dispatch_response_msg; //6搬运动作执行完成后反馈结果(调度管理->主控)
  88. std::string m_command_key; //任务唯一码, 索引
  89. int m_timeout_ms; //超时时间,单位ms
  90. std::chrono::system_clock::time_point m_start_time; //流程开始时间
  91. Error_manager m_result; //中间的错误码
  92. //数据缓存
  93. //调度管理给plc发送调度指令
  94. std::string m_request_key; //请求唯一码, 用作识别
  95. unsigned int m_request_status = 0; //请求确认标志
  96. // 调度指令的起点,终点,方向
  97. Common_data::Dispatch_motion_direction m_request_dispatch_motion_direction ; //调度方向 0=未知,1=存车,2=取车
  98. unsigned int m_request_passageway_id = 0; //出入口id 6个入口和6个出口
  99. Common_data::Passageway_direction m_request_passageway_direction ; //出入口方向 0=未知,1=入口,2=出口
  100. unsigned int m_request_parkingspace_index_id = 0; //楼上车位索引id 1~180(3*6*13)
  101. unsigned int m_request_parkingspace_unit_id = 0; //楼上车位单元号 1~3
  102. unsigned int m_request_parkingspace_label_id = 0; //楼上车位标签号 1~78
  103. unsigned int m_request_parkingspace_floor_id = 0; //楼上车位楼层号 2~11
  104. unsigned int m_request_parkingspace_room_id = 0; //楼上车位房间号 1~6
  105. Common_data::Parkingspace_direction m_request_parkingspace_direction ; //楼上车位方向 0=未知,1=朝南,2=朝北
  106. //汽车的定位信息(地面雷达)
  107. float m_request_car_center_x = 0; //整车的中心点x值, 四轮的中心, 单位:米 m
  108. float m_request_car_center_y = 0; //整车的中心点y值, 四轮的中心, 单位:米 m
  109. float m_request_car_angle = 0; //整车的车身旋转角, 单位:度 (-90~90)
  110. float m_request_car_front_theta = 0; //整车的前轮的旋转角, 单位:度 (-90~90)
  111. float m_request_car_length = 0; //整车的长度, 用于规避碰撞, 单位:米 m
  112. float m_request_car_width = 0; //整车的宽度, 用于规避碰撞, 单位:米 m
  113. float m_request_car_height = 0; //整车的高度, 用于规避碰撞, 单位:米 m
  114. float m_request_car_wheel_base = 0; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车, 单位:米 m
  115. float m_request_car_wheel_width = 0; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车, 单位:米 m
  116. std::string m_request_car_license; //车牌号(汽车唯一标示) 例如: 鄂A12345
  117. Common_data::Car_type m_request_car_type; //车的大小
  118. float m_request_uniformed_car_x = 0; //转角复位后,车辆中心点x
  119. float m_request_uniformed_car_y = 0; //转角复位后,车辆中心点y
  120. //防撞雷达
  121. Common_data::Anticollision_lidar_flag m_request_anticollision_lidar_flag ; //汽车是否停到正确的位置, 防撞雷达 预留, 楚天暂时不用,0=未知,1=位置正常,2=位置异常
  122. //轮距雷达
  123. float m_request_car_wheel_base_exact_value = 0; //汽车前后轮距,轮距雷达的精确值 预留, 楚天暂时不用,单位:米 m
  124. //plc给调度管理发送调度答复
  125. std::string m_response_key; //答复唯一码, 用作识别
  126. Response_status m_response_status; //指令完成状态, 机器人答复指令, 返回任务完成的情况
  127. // 调度指令的起点,终点,方向
  128. Common_data::Dispatch_motion_direction m_response_dispatch_motion_direction ; //调度方向 0=未知,1=存车,2=取车
  129. unsigned int m_response_passageway_id = 0; //出入口id 6个入口和6个出口
  130. Common_data::Passageway_direction m_response_passageway_direction ; //出入口方向 0=未知,1=入口,2=出口
  131. unsigned int m_response_parkingspace_index_id = 0; //楼上车位索引id 1~180(3*6*13)
  132. unsigned int m_response_parkingspace_unit_id = 0; //楼上车位单元号 1~3
  133. unsigned int m_response_parkingspace_label_id = 0; //楼上车位标签号 1~78
  134. unsigned int m_response_parkingspace_floor_id = 0; //楼上车位楼层号 2~11
  135. unsigned int m_response_parkingspace_room_id = 0; //楼上车位房间号 1~6
  136. Common_data::Parkingspace_direction m_response_parkingspace_direction ; //楼上车位方向 0=未知,1=朝南,2=朝北
  137. //汽车的定位信息(地面雷达)
  138. float m_response_car_center_x = 0; //整车的中心点x值, 四轮的中心, 单位:米 m
  139. float m_response_car_center_y = 0; //整车的中心点y值, 四轮的中心, 单位:米 m
  140. float m_response_car_angle = 0; //整车的车身旋转角, 单位:度 (-90~90)
  141. float m_response_car_front_theta = 0; //整车的前轮的旋转角, 单位:度 (-90~90)
  142. float m_response_car_length = 0; //整车的长度, 用于规避碰撞, 单位:米 m
  143. float m_response_car_width = 0; //整车的宽度, 用于规避碰撞, 单位:米 m
  144. float m_response_car_height = 0; //整车的高度, 用于规避碰撞, 单位:米 m
  145. float m_response_car_wheel_base = 0; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车, 单位:米 m
  146. float m_response_car_wheel_width = 0; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车, 单位:米 m
  147. std::string m_response_car_license; //车牌号(汽车唯一标示) 例如: 鄂A12345
  148. Common_data::Car_type m_response_car_type; //车的大小
  149. float m_response_uniformed_car_x = 0; //转角复位后,车辆中心点x
  150. float m_response_uniformed_car_y = 0; //转角复位后,车辆中心点y
  151. //防撞雷达
  152. Common_data::Anticollision_lidar_flag m_response_anticollision_lidar_flag ; //汽车是否停到正确的位置, 防撞雷达 预留, 楚天暂时不用,0=未知,1=位置正常,2=位置异常
  153. //轮距雷达
  154. float m_response_car_wheel_base_exact_value = 0; //汽车前后轮距,轮距雷达的精确值 预留, 楚天暂时不用,单位:米 m
  155. //硬件状态, 目前只做显示判断
  156. std::chrono::system_clock::time_point m_status_updata_time; //状态更新时间点
  157. unsigned char m_last_heartbeat; //上一次的心跳
  158. private:
  159. };
  160. #endif //NNXX_TESTS_DISPATCH_PLC_H