dispatch_manager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // Created by huli on 2020/7/20.
  3. //
  4. #include "dispatch_manager.h"
  5. #include "../tool/proto_tool.h"
  6. #include <cstdlib>
  7. Dispatch_manager::Dispatch_manager()
  8. {
  9. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  10. m_dispatch_manager_id = -1;
  11. m_dispatch_manager_thread = nullptr;
  12. }
  13. Dispatch_manager::~Dispatch_manager()
  14. {
  15. dispatch_manager_uninit();
  16. }
  17. //调度管理 初始化
  18. Error_manager Dispatch_manager::dispatch_manager_init(int dispatch_manager_id)
  19. {
  20. m_dispatch_manager_id = dispatch_manager_id;
  21. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  22. }
  23. //调度管理 初始化
  24. Error_manager Dispatch_manager::dispatch_manager_init()
  25. {
  26. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  27. }
  28. //初始化 调度管理 模块。从文件读取
  29. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(std::string prototxt_path)
  30. {
  31. Dispatch_proto::Dispatch_device_parameter_all t_dispatch_device_parameter_all;
  32. if(! proto_tool::read_proto_param(prototxt_path,t_dispatch_device_parameter_all) )
  33. {
  34. return Error_manager(DISPATCH_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,
  35. "Dispatch_manager read_proto_param failed");
  36. }
  37. return dispatch_manager_init_from_protobuf(t_dispatch_device_parameter_all);
  38. }
  39. //初始化 调度管理 模块。从protobuf读取
  40. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all)
  41. {
  42. LOG(INFO) << " ----Dispatch_manager::dispatch_manager_init_from_protobuf----- "<< this;
  43. Error_manager t_error;
  44. m_dispatch_plc.dispatch_plc_init(m_dispatch_manager_id);
  45. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  46. // 线程默认开启
  47. m_dispatch_manager_condition.reset(false, true, false);
  48. m_dispatch_manager_thread = new std::thread(&Dispatch_manager::resource_allocation, this);
  49. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  50. return Error_code::SUCCESS;
  51. }
  52. //调度管理 反初始化
  53. Error_manager Dispatch_manager::dispatch_manager_uninit()
  54. {
  55. if (m_dispatch_manager_thread)
  56. {
  57. m_dispatch_manager_condition.kill_all();
  58. }
  59. if (m_dispatch_manager_thread)
  60. {
  61. m_dispatch_manager_thread->join();
  62. delete m_dispatch_manager_thread;
  63. m_dispatch_manager_thread = NULL;
  64. }
  65. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  66. m_dispatch_manager_id = -1;
  67. return Error_code::SUCCESS;
  68. }
  69. //调度管理 设备复位
  70. Error_manager Dispatch_manager::dispatch_manager_device_reset()
  71. {
  72. //以后再写
  73. return Error_code::SUCCESS;
  74. }
  75. //对外的接口函数,负责接受并处理任务单,
  76. Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
  77. {
  78. return Error_code::SUCCESS;
  79. // std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
  80. if ( dispatch_motion_direction == E_PICKUP_CAR )
  81. {
  82. return Error_code::SUCCESS;
  83. }
  84. // return Error_code::SUCCESS;
  85. // srand(0);
  86. unsigned int t_probability = rand();
  87. if ( t_probability%100 >=20 )
  88. {
  89. return Error_code::SUCCESS;
  90. }
  91. else
  92. {
  93. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  94. " Dispatch_manager::execute_task() error ");
  95. }
  96. }
  97. //检查能否执行消息指令
  98. Error_manager Dispatch_manager::check_execute_msg(Communication_message* p_msg)
  99. {
  100. Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
  101. if ( t_error == Error_code::SUCCESS )
  102. {
  103. return Error_code::SUCCESS;
  104. }
  105. else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  106. {
  107. std::cout << "Dispatch_manager _is_busy , " << std::endl;
  108. //返回繁忙之后, 通信模块1秒后再次调用check
  109. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  110. }
  111. switch ( p_msg->get_message_type() )
  112. {
  113. case Communication_message::Message_type::eDispatch_request_msg:
  114. {
  115. return Error_code::SUCCESS;
  116. break;
  117. }
  118. case Communication_message::Message_type::eDispatch_plan_response_msg:
  119. {
  120. return Error_code::SUCCESS;
  121. break;
  122. }
  123. case Communication_message::Message_type::eDispatch_control_request_msg:
  124. {
  125. return Error_code::SUCCESS;
  126. break;
  127. }
  128. default :
  129. {
  130. //无效的消息,
  131. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  132. " INVALID_MESSAGE error ");
  133. break;
  134. }
  135. }
  136. return Error_code::SUCCESS;
  137. }
  138. //检查状态
  139. Error_manager Dispatch_manager::check_status()
  140. {
  141. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  142. {
  143. return Error_code::SUCCESS;
  144. }
  145. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE )
  146. {
  147. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  148. " Dispatch_manager::check_status() error ");
  149. }
  150. else
  151. {
  152. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  153. " Dispatch_manager::check_status() error ");
  154. }
  155. return Error_code::SUCCESS;
  156. }
  157. //调度模块 //执行搬运请求(主控->调度管理)
  158. Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  159. {
  160. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
  161. LOG(INFO) << " dispatch_request_msg->m_command_key = "<<dispatch_request_msg.command_key()<< " "<< this;
  162. Error_manager t_error;
  163. std::unique_lock<std::mutex> t_lock(m_lock);
  164. if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  165. {
  166. m_dispatch_request_store_list.push_back(dispatch_request_msg);
  167. }
  168. else
  169. {
  170. m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
  171. }
  172. //need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
  173. // m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
  174. return Error_code::SUCCESS;
  175. }
  176. //调度模块 答复数据异常
  177. Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
  178. {
  179. message::Dispatch_response_msg t_dispatch_response_msg;
  180. t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  181. t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
  182. t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  183. t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  184. t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
  185. t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
  186. t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
  187. t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
  188. System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
  189. return Error_code::SUCCESS;
  190. }
  191. //调度模块 //调度总规划的答复(调度算法->调度管理)
  192. Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
  193. {
  194. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
  195. LOG(INFO) << " dispatch_plan_response_msg->m_command_key = "<<dispatch_plan_response_msg.command_key()<< " "<< this;
  196. Error_manager t_error;
  197. return Error_code::SUCCESS;
  198. }
  199. //调度模块 //调度控制的任务请求(调度算法->调度管理)
  200. Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
  201. {
  202. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
  203. LOG(INFO) << " dispatch_control_request_msg->m_command_key = "<<dispatch_control_request_msg.command_key()<< " "<< this;
  204. Error_manager t_error;
  205. return Error_code::SUCCESS;
  206. }
  207. //定时发送 调度管理的状态
  208. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
  209. {
  210. Error_manager t_error;
  211. int t_dispatch_manager_id = get_dispatch_manager_id();
  212. std::string t_msg1;
  213. //创建一条 调度管理总管理的状态 旧版
  214. message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  215. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  216. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  217. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  218. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  219. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_manager_id*2);
  220. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  221. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
  222. t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  223. System_communication::get_instance_references().encapsulate_msg(t_msg1);
  224. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_manager_id*2+1);
  225. t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  226. System_communication::get_instance_references().encapsulate_msg(t_msg1);
  227. std::string t_msg2;
  228. //创建一条 调度管理总管理的状态 新版
  229. message::Dispatch_manager_status_msg t_dispatch_manager_status_msg;
  230. t_dispatch_manager_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_manager_status_msg);
  231. t_dispatch_manager_status_msg.mutable_base_info()->set_timeout_ms(5000);
  232. t_dispatch_manager_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  233. t_dispatch_manager_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  234. t_dispatch_manager_status_msg.set_dispatch_id(t_dispatch_manager_id*2);
  235. Dispatch_manager::Dispatch_manager_status t_dispatch_manager_status = get_dispatch_manager_status();
  236. t_dispatch_manager_status_msg.set_dispatch_manager_status((message::Dispatch_manager_status)t_dispatch_manager_status);
  237. t_msg2 = t_dispatch_manager_status_msg.SerializeAsString();
  238. System_communication::get_instance_references().encapsulate_msg(t_msg2);
  239. t_dispatch_manager_status_msg.set_dispatch_id(t_dispatch_manager_id*2+1);
  240. t_msg2 = t_dispatch_manager_status_msg.SerializeAsString();
  241. System_communication::get_instance_references().encapsulate_msg(t_msg2);
  242. return Error_code::SUCCESS;
  243. }
  244. //在流程的map 里面释放指定的流程
  245. Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
  246. {
  247. std::unique_lock<std::mutex> t_lock(m_lock);
  248. return Error_code::SUCCESS;
  249. }
  250. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  251. Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
  252. {
  253. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  254. int t_inlet_id = ground_status_msg.terminal_id() %2;
  255. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  256. Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_data * p_response_data = & Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_data[t_inlet_id];
  257. Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_key * p_response_key = & Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_key[t_inlet_id];
  258. Dispatch_communication::Ground_lidar_request_from_plc_to_manager * p_request = & Dispatch_communication::get_instance_references().m_ground_lidar_request_from_plc_to_manager[t_inlet_id];
  259. if ( p_request->m_communication_mode == 0 )
  260. {
  261. p_response_key->m_heartbeat++;
  262. p_response_key->m_communication_mode = p_request->m_communication_mode;
  263. p_response_key->m_refresh_command = p_request->m_refresh_command;
  264. p_response_data->m_response_car_center_x = ground_status_msg.locate_information_realtime().locate_x();
  265. p_response_data->m_response_car_center_y = ground_status_msg.locate_information_realtime().locate_y();
  266. p_response_data->m_response_car_angle = ground_status_msg.locate_information_realtime().locate_angle();
  267. p_response_data->m_response_car_front_theta = ground_status_msg.locate_information_realtime().locate_front_theta();
  268. p_response_data->m_response_car_length = ground_status_msg.locate_information_realtime().locate_length();
  269. p_response_data->m_response_car_width = ground_status_msg.locate_information_realtime().locate_width();
  270. p_response_data->m_response_car_height = ground_status_msg.locate_information_realtime().locate_height();
  271. p_response_data->m_response_car_wheel_base = ground_status_msg.locate_information_realtime().locate_wheel_base();
  272. p_response_data->m_response_car_wheel_width = ground_status_msg.locate_information_realtime().locate_wheel_width();
  273. p_response_data->m_response_locate_correct = ground_status_msg.locate_information_realtime().locate_correct();
  274. }
  275. else if ( p_response_key->m_refresh_command == p_request->m_refresh_command )
  276. {
  277. p_response_key->m_heartbeat++;
  278. p_response_key->m_communication_mode = p_request->m_communication_mode;
  279. p_response_key->m_refresh_command = p_request->m_refresh_command;
  280. }
  281. else
  282. {
  283. p_response_key->m_heartbeat++;
  284. p_response_key->m_communication_mode = p_request->m_communication_mode;
  285. p_response_key->m_refresh_command = p_request->m_refresh_command;
  286. p_response_data->m_response_car_center_x = ground_status_msg.locate_information_realtime().locate_x();
  287. p_response_data->m_response_car_center_y = ground_status_msg.locate_information_realtime().locate_y();
  288. p_response_data->m_response_car_angle = ground_status_msg.locate_information_realtime().locate_angle();
  289. p_response_data->m_response_car_front_theta = ground_status_msg.locate_information_realtime().locate_front_theta();
  290. p_response_data->m_response_car_length = ground_status_msg.locate_information_realtime().locate_length();
  291. p_response_data->m_response_car_width = ground_status_msg.locate_information_realtime().locate_width();
  292. p_response_data->m_response_car_height = ground_status_msg.locate_information_realtime().locate_height();
  293. p_response_data->m_response_car_wheel_base = ground_status_msg.locate_information_realtime().locate_wheel_base();
  294. p_response_data->m_response_car_wheel_width = ground_status_msg.locate_information_realtime().locate_wheel_width();
  295. p_response_data->m_response_locate_correct = ground_status_msg.locate_information_realtime().locate_correct();
  296. }
  297. return Error_code::SUCCESS;
  298. }
  299. Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
  300. {
  301. return m_dispatch_manager_status;
  302. }
  303. int Dispatch_manager::get_dispatch_manager_id()
  304. {
  305. return m_dispatch_manager_id;
  306. }
  307. void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
  308. {
  309. m_dispatch_manager_id = dispatch_manager_id;
  310. }
  311. //资源分配
  312. void Dispatch_manager::resource_allocation()
  313. {
  314. LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
  315. Error_manager t_error;
  316. while (m_dispatch_manager_condition.is_alive())
  317. {
  318. m_dispatch_manager_condition.wait();
  319. if ( m_dispatch_manager_condition.is_alive() )
  320. {
  321. std::this_thread::sleep_for(std::chrono::microseconds(1));
  322. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  323. // std::this_thread::sleep_for(std::chrono::seconds(1));
  324. std::this_thread::yield();
  325. std::unique_lock<std::mutex> t_lock(m_lock);
  326. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  327. {
  328. if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
  329. {
  330. auto iter = m_dispatch_request_pickup_list.begin();
  331. if ( iter != m_dispatch_request_pickup_list.end() )
  332. {
  333. m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  334. m_dispatch_request_pickup_list.erase(iter);
  335. }
  336. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  337. }
  338. else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
  339. {
  340. auto iter = m_dispatch_request_store_list.begin();
  341. if ( iter != m_dispatch_request_store_list.end() )
  342. {
  343. m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  344. m_dispatch_request_store_list.erase(iter);
  345. }
  346. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  347. }
  348. else
  349. {
  350. ///////
  351. }
  352. }
  353. }
  354. }
  355. LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
  356. return;
  357. }