carrier_base.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. //
  2. // Created by huli on 2020/7/20.
  3. //
  4. #include "carrier_base.h"
  5. #include "dispatch_communication.h"
  6. Carrier_base::Carrier_base()
  7. {
  8. m_carrier_status = E_UNKNOW;
  9. m_carrier_id = 0;
  10. m_parkspace_unit_id = 0; //车位单元号(楚天项目为1~3)
  11. m_parkspace_floor_id = 0; //车位楼层号(楚天项目为1~14)
  12. m_parkspace_room_id = 0; //车位同层的房间号 (楚天项目一楼为1~4, 楼上为1~6)
  13. // m_command_key = 0; //任务唯一码
  14. m_motion_direction = 0; //调度方向, 根据停车取车选择出入口, 1=入口,0=非入口
  15. m_car_center_x = 0; //汽车的中心坐标x
  16. m_car_center_y = 0; //汽车的中心坐标y
  17. m_car_angle = 0; //汽车的旋转角(角度-90~90)
  18. m_carrier_coordinate_x = 0; //搬运器坐标x轴, 中跑车控制横向移动
  19. m_carrier_coordinate_y = 0; //搬运器坐标y轴, (本模块默认为0, 由AGV模块去控制纵向移动)
  20. m_carrier_coordinate_z = 0; //搬运器坐标z轴, 电梯控制上下移动
  21. m_respons_status = RESPONS_NORMAL; //指令完成状态, 搬运器答复指令, 返回任务完成的情况
  22. //硬件状态, 目前只做显示判断
  23. m_carrier_device_status = E_UNKNOW; //搬运器设备的状态, 楚天项目就是中跑车
  24. memset(m_carrier_error_code, 0, 50); //搬运器设备的报警信息位
  25. // m_carrier_error_description = 0;//搬运器设备的报警状态描述
  26. m_elevator_device_status = E_UNKNOW; //升降机设备状态, 楚天项目就是电梯
  27. memset(m_elevator_error_code, 0, 50); //升降机设备的报警信息位
  28. // m_elevator_error_description = 0;//升降机设备的报警状态描述
  29. mp_execute_thread = NULL;
  30. }
  31. Carrier_base::~Carrier_base()
  32. {
  33. carrier_base_uninit();
  34. }
  35. //搬运器 初始化
  36. Error_manager Carrier_base::carrier_base_init()
  37. {
  38. // 线程默认开启
  39. m_execute_condition.reset(false, true, false);
  40. mp_execute_thread = new std::thread(&Carrier_base::execute_thread_fun, this);
  41. m_carrier_status = E_READY;
  42. return Error_code::SUCCESS;
  43. }
  44. //搬运器 反初始化
  45. Error_manager Carrier_base::carrier_base_uninit()
  46. {
  47. if (mp_execute_thread)
  48. {
  49. m_execute_condition.kill_all();
  50. }
  51. if (mp_execute_thread)
  52. {
  53. mp_execute_thread->join();
  54. delete mp_execute_thread;
  55. mp_execute_thread = NULL;
  56. }
  57. m_carrier_status = E_UNKNOW;
  58. return Error_code::SUCCESS;
  59. }
  60. //处理一级任务, 普通任务
  61. Error_manager Carrier_base::execute_one_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
  62. {
  63. Error_manager t_error;
  64. Error_manager t_result;
  65. //检查指针
  66. if (p_carrier_task == NULL) {
  67. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  68. "Carrier_base::execute_one_level_task failed, POINTER_IS_NULL");
  69. }
  70. //检查任务类型,
  71. if (p_carrier_task->get_task_type() != CARRIER_TASK)
  72. {
  73. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  74. "Carrier_base::execute_one_level_task get_task_type() != CARRIER_TASK ");
  75. }
  76. //检查接收方的状态
  77. t_error = check_one_level_task();
  78. if ( t_error != SUCCESS )
  79. {
  80. t_result.compare_and_cover_error(t_error);
  81. }
  82. else
  83. {
  84. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  85. mp_carrier_one_level_task = p_carrier_task;
  86. mp_carrier_one_level_task->set_task_statu(TASK_SIGNED);
  87. //这里不用检查任务内容, 直接下发给底层设备
  88. //启动定位管理模块,的核心工作线程
  89. m_carrier_status = E_ONE_LEVEL_WORK;
  90. m_execute_condition.notify_all(true);
  91. //通知 thread_work 子线程启动。
  92. //将任务的状态改为 TASK_WORKING 处理中
  93. mp_carrier_one_level_task->set_task_statu(TASK_WORKING);
  94. }
  95. if ( t_result != Error_code::SUCCESS )
  96. {
  97. return t_result;
  98. }
  99. return Error_code::SUCCESS;
  100. }
  101. //处理二级任务, 插队任务
  102. Error_manager Carrier_base::execute_two_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
  103. {
  104. Error_manager t_error;
  105. Error_manager t_result;
  106. //检查指针
  107. if (p_carrier_task == NULL) {
  108. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  109. "Carrier_base::execute_two_level_task failed, POINTER_IS_NULL");
  110. }
  111. //检查任务类型,
  112. if (p_carrier_task->get_task_type() != CARRIER_TASK)
  113. {
  114. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  115. "Carrier_base::execute_two_level_task get_task_type() != CARRIER_TASK ");
  116. }
  117. //检查接收方的状态
  118. t_error = check_two_level_task();
  119. if ( t_error != SUCCESS )
  120. {
  121. t_result.compare_and_cover_error(t_error);
  122. }
  123. else
  124. {
  125. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  126. mp_carrier_two_level_task = p_carrier_task;
  127. mp_carrier_two_level_task->set_task_statu(TASK_SIGNED);
  128. //这里不用检查任务内容, 直接下发给底层设备
  129. //启动定位管理模块,的核心工作线程
  130. m_carrier_status = E_TWO_LEVEL_WORK;
  131. m_execute_condition.notify_all(true);
  132. //通知 thread_work 子线程启动。
  133. //将任务的状态改为 TASK_WORKING 处理中
  134. mp_carrier_two_level_task->set_task_statu(TASK_WORKING);
  135. }
  136. if ( t_result != Error_code::SUCCESS )
  137. {
  138. return t_result;
  139. }
  140. return Error_code::SUCCESS;
  141. }
  142. //处理三级任务, 核心任务
  143. Error_manager Carrier_base::execute_three_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
  144. {
  145. Error_manager t_error;
  146. Error_manager t_result;
  147. //检查指针
  148. if (p_carrier_task == NULL) {
  149. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  150. "Carrier_base::execute_three_level_task failed, POINTER_IS_NULL");
  151. }
  152. //检查任务类型,
  153. if (p_carrier_task->get_task_type() != CARRIER_TASK)
  154. {
  155. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  156. "Carrier_base::execute_three_level_task get_task_type() != CARRIER_TASK ");
  157. }
  158. //检查接收方的状态
  159. t_error = check_three_level_task();
  160. if ( t_error != SUCCESS )
  161. {
  162. t_result.compare_and_cover_error(t_error);
  163. }
  164. else
  165. {
  166. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  167. mp_carrier_three_level_task = p_carrier_task;
  168. mp_carrier_three_level_task->set_task_statu(TASK_SIGNED);
  169. //这里不用检查任务内容, 直接下发给底层设备
  170. //启动定位管理模块,的核心工作线程
  171. m_carrier_status = E_THREE_LEVEL_WORK;
  172. m_execute_condition.notify_all(true);
  173. //通知 thread_work 子线程启动。
  174. //将任务的状态改为 TASK_WORKING 处理中
  175. mp_carrier_three_level_task->set_task_statu(TASK_WORKING);
  176. }
  177. if ( t_result != Error_code::SUCCESS )
  178. {
  179. return t_result;
  180. }
  181. return Error_code::SUCCESS;
  182. }
  183. //检查状态,是否正常运行
  184. Error_manager Carrier_base::check_status()
  185. {
  186. if ( m_carrier_status == E_READY )
  187. {
  188. return Error_code::SUCCESS;
  189. }
  190. else if ( m_carrier_status >= E_BUSY && m_carrier_status <= E_THREE_LEVEL_OVER)
  191. {
  192. return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  193. " Carrier_base::check_status is busy ");
  194. }
  195. else
  196. {
  197. return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
  198. " Carrier_base::check_status error ");
  199. }
  200. return Error_code::SUCCESS;
  201. }
  202. //判断能否执行一级任务
  203. Error_manager Carrier_base::check_one_level_task()
  204. {
  205. if ( m_carrier_status == E_READY )
  206. {
  207. return Error_code::SUCCESS;
  208. }
  209. else if ( m_carrier_status >= E_BUSY && m_carrier_status <= E_THREE_LEVEL_OVER)
  210. {
  211. return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  212. " Carrier_base::check_status is busy ");
  213. }
  214. else
  215. {
  216. return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
  217. " Carrier_base::check_status error ");
  218. }
  219. return Error_code::SUCCESS;
  220. }
  221. //判断能否执行二级任务
  222. Error_manager Carrier_base::check_two_level_task()
  223. {
  224. if ( m_carrier_status == E_READY ||
  225. m_carrier_status == E_ONE_LEVEL_WORK ||
  226. m_carrier_status == E_ONE_LEVEL_OVER )
  227. {
  228. return Error_code::SUCCESS;
  229. }
  230. else if ( m_carrier_status >= E_TWO_LEVEL_WORK && m_carrier_status <= E_THREE_LEVEL_OVER)
  231. {
  232. return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  233. " Carrier_base::check_status is busy ");
  234. }
  235. else
  236. {
  237. return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
  238. " Carrier_base::check_status error ");
  239. }
  240. return Error_code::SUCCESS;
  241. }
  242. //判断能否执行三级任务
  243. Error_manager Carrier_base::check_three_level_task()
  244. {
  245. if ( m_carrier_status == E_READY ||
  246. m_carrier_status == E_ONE_LEVEL_WORK ||
  247. m_carrier_status == E_ONE_LEVEL_OVER )
  248. {
  249. return Error_code::SUCCESS;
  250. }
  251. else if ( m_carrier_status >= E_TWO_LEVEL_WORK && m_carrier_status <= E_THREE_LEVEL_OVER)
  252. {
  253. return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  254. " Carrier_base::check_status is busy ");
  255. }
  256. else
  257. {
  258. return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
  259. " Carrier_base::check_status error ");
  260. }
  261. return Error_code::SUCCESS;
  262. }
  263. //结束任务单,里面会根据任务的故障等级修正 任务单的状态
  264. Error_manager Carrier_base::end_task(std::shared_ptr<Carrier_task> carrier_task)
  265. {
  266. LOG(INFO) << " ---Carrier_base::end_task run---"<< this;
  267. carrier_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  268. //注:这里只修改任务单的状态, 搬运器的状态不管
  269. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  270. //判断任务单的错误等级,
  271. if ( carrier_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
  272. {
  273. //强制改为TASK_OVER,不管它当前在做什么。
  274. carrier_task->set_task_statu(TASK_OVER);
  275. }
  276. else
  277. {
  278. //强制改为 TASK_ERROR,不管它当前在做什么。
  279. carrier_task->set_task_statu(TASK_ERROR);
  280. }
  281. return Error_code::SUCCESS;
  282. }
  283. //取消任务单,由发送方提前取消任务单
  284. Error_manager Carrier_base::cancel_task(std::shared_ptr<Carrier_task> carrier_task)
  285. {
  286. //找到对应的任务单
  287. if ( carrier_task->get_task_id() == mp_carrier_one_level_task->get_task_id() )
  288. {
  289. if ( m_carrier_status == E_ONE_LEVEL_WORK || m_carrier_status == E_ONE_LEVEL_OVER )
  290. {
  291. //如果正在执行一级任务, 那么取消当前指令, 然后降级
  292. m_execute_condition.notify_all(false);
  293. //确保内部线程已经停下
  294. while (m_execute_condition.is_working())
  295. {
  296. }
  297. cancel_command();
  298. mp_carrier_one_level_task.reset();
  299. m_carrier_status = E_READY;
  300. m_execute_condition.notify_all(true);
  301. }
  302. else
  303. {
  304. //否则直接销毁任务单
  305. mp_carrier_one_level_task.reset();
  306. }
  307. }
  308. else if ( carrier_task->get_task_id() == mp_carrier_two_level_task->get_task_id() )
  309. {
  310. if ( m_carrier_status == E_TWO_LEVEL_WORK || m_carrier_status == E_TWO_LEVEL_OVER )
  311. {
  312. //如果正在执行二级任务, 那么取消当前指令, 然后降级
  313. m_execute_condition.notify_all(false);
  314. //确保内部线程已经停下
  315. while (m_execute_condition.is_working())
  316. {
  317. }
  318. cancel_command();
  319. mp_carrier_two_level_task.reset();
  320. m_carrier_status = E_ONE_LEVEL_WORK;
  321. m_execute_condition.notify_all(true);
  322. }
  323. else
  324. {
  325. //否则直接销毁任务单
  326. mp_carrier_two_level_task.reset();
  327. }
  328. }
  329. else if ( carrier_task->get_task_id() == mp_carrier_three_level_task->get_task_id() )
  330. {
  331. if ( m_carrier_status == E_THREE_LEVEL_WORK || m_carrier_status == E_THREE_LEVEL_OVER )
  332. {
  333. //如果正在执行三级任务, 那么取消当前指令, 然后降级
  334. m_execute_condition.notify_all(false);
  335. //确保内部线程已经停下
  336. while (m_execute_condition.is_working())
  337. {
  338. }
  339. cancel_command();
  340. mp_carrier_three_level_task.reset();
  341. m_carrier_status = E_TWO_LEVEL_WORK;
  342. m_execute_condition.notify_all(true);
  343. }
  344. else
  345. {
  346. //否则直接销毁任务单
  347. mp_carrier_two_level_task.reset();
  348. }
  349. }
  350. else
  351. {
  352. return Error_manager(Error_code::CARRIER_TASK_NOTHINGNESS, Error_level::MINOR_ERROR,
  353. " carrier_task->get_task_id() is nothingness ");
  354. }
  355. carrier_task->set_task_statu(TASK_DEAD);
  356. return Error_code::SUCCESS;
  357. }
  358. //判断是否为待机,如果已经准备好,则可以执行任务。
  359. bool Carrier_base::is_ready()
  360. {
  361. return (m_carrier_status == E_READY);
  362. }
  363. Carrier_base::Carrier_status Carrier_base::get_carrier_status()
  364. {
  365. return m_carrier_status;
  366. }
  367. //执行外界任务的执行函数
  368. void Carrier_base::execute_thread_fun()
  369. {
  370. LOG(INFO) << " execute_thread_fun start "<< this;
  371. // while (1);
  372. Error_manager t_error;
  373. while (m_execute_condition.is_alive())
  374. {
  375. m_execute_condition.wait();
  376. if ( m_execute_condition.is_alive() )
  377. {
  378. std::this_thread::sleep_for(std::chrono::microseconds(1));
  379. std::this_thread::yield();
  380. switch ( (Carrier_status)m_carrier_status )
  381. {
  382. //核心任务, (三级任务)
  383. case E_THREE_LEVEL_WORK:
  384. {
  385. if ( mp_carrier_three_level_task.get() != NULL )
  386. {
  387. //执行三级任务
  388. std::unique_lock<std::mutex> t_lock(mp_carrier_three_level_task->m_lock);
  389. m_parkspace_unit_id = mp_carrier_three_level_task->m_parkspace_unit_id;
  390. m_parkspace_floor_id = mp_carrier_three_level_task->m_parkspace_floor_id;
  391. m_parkspace_room_id = mp_carrier_three_level_task->m_parkspace_room_id;
  392. m_command_key = mp_carrier_three_level_task->m_command_key;
  393. m_motion_direction = mp_carrier_three_level_task->m_motion_direction;
  394. m_car_center_x = mp_carrier_three_level_task->m_car_center_x;
  395. m_car_center_y = mp_carrier_three_level_task->m_car_center_y;
  396. m_car_angle = mp_carrier_three_level_task->m_car_angle;
  397. //更新通信
  398. update_device_communication();
  399. //检查设备的答复
  400. if ( m_respons_status == RESPONS_OVER )
  401. {
  402. mp_carrier_three_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  403. end_task(mp_carrier_three_level_task);
  404. m_carrier_status = E_THREE_LEVEL_OVER;
  405. }
  406. else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  407. {
  408. mp_carrier_three_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  409. //添加错误码
  410. Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  411. mp_carrier_three_level_task->set_task_error_manager(t_error);
  412. end_task(mp_carrier_three_level_task);
  413. m_carrier_status = E_THREE_LEVEL_OVER;
  414. }
  415. else
  416. {
  417. //设备正常运行
  418. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  419. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  420. }
  421. }
  422. else
  423. {
  424. //直接降级
  425. m_carrier_status = E_TWO_LEVEL_WORK;
  426. }
  427. break;
  428. }
  429. case E_THREE_LEVEL_OVER:
  430. {
  431. if ( mp_carrier_three_level_task.get() != NULL )
  432. {
  433. //检查任务状态,
  434. //在 E_THREE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  435. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  436. if ( mp_carrier_three_level_task->get_task_statu() == TASK_WITHDRAW )
  437. {
  438. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  439. mp_carrier_three_level_task->set_task_statu(TASK_FREE);
  440. mp_carrier_three_level_task.reset();
  441. m_carrier_status = E_TWO_LEVEL_WORK;
  442. }
  443. //else //保持不动, 直到发送方给定新的任务,
  444. }
  445. else
  446. {
  447. //直接降级
  448. m_carrier_status = E_TWO_LEVEL_WORK;
  449. }
  450. break;
  451. }
  452. case E_TWO_LEVEL_WORK:
  453. {
  454. if ( mp_carrier_two_level_task.get() != NULL )
  455. {
  456. //执行三级任务
  457. std::unique_lock<std::mutex> t_lock(mp_carrier_two_level_task->m_lock);
  458. m_parkspace_unit_id = mp_carrier_two_level_task->m_parkspace_unit_id;
  459. m_parkspace_floor_id = mp_carrier_two_level_task->m_parkspace_floor_id;
  460. m_parkspace_room_id = mp_carrier_two_level_task->m_parkspace_room_id;
  461. m_command_key = mp_carrier_two_level_task->m_command_key;
  462. m_motion_direction = mp_carrier_two_level_task->m_motion_direction;
  463. m_car_center_x = mp_carrier_two_level_task->m_car_center_x;
  464. m_car_center_y = mp_carrier_two_level_task->m_car_center_y;
  465. m_car_angle = mp_carrier_two_level_task->m_car_angle;
  466. //更新通信
  467. update_device_communication();
  468. //检查设备的答复
  469. if ( m_respons_status == RESPONS_OVER )
  470. {
  471. mp_carrier_two_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  472. end_task(mp_carrier_two_level_task);
  473. m_carrier_status = E_TWO_LEVEL_OVER;
  474. }
  475. else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  476. {
  477. mp_carrier_two_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  478. //添加错误码
  479. Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  480. mp_carrier_two_level_task->set_task_error_manager(t_error);
  481. end_task(mp_carrier_two_level_task);
  482. m_carrier_status = E_TWO_LEVEL_OVER;
  483. }
  484. else
  485. {
  486. //设备正常运行
  487. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  488. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  489. }
  490. }
  491. else
  492. {
  493. //直接降级
  494. m_carrier_status = E_ONE_LEVEL_WORK;
  495. }
  496. break;
  497. }
  498. case E_TWO_LEVEL_OVER:
  499. {
  500. if ( mp_carrier_two_level_task.get() != NULL )
  501. {
  502. //检查任务状态,
  503. //在 E_TWO_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  504. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  505. if ( mp_carrier_two_level_task->get_task_statu() == TASK_WITHDRAW )
  506. {
  507. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  508. mp_carrier_two_level_task->set_task_statu(TASK_FREE);
  509. mp_carrier_two_level_task.reset();
  510. m_carrier_status = E_ONE_LEVEL_WORK;
  511. }
  512. //else //保持不动, 直到发送方给定新的任务,
  513. }
  514. else
  515. {
  516. //直接降级
  517. m_carrier_status = E_ONE_LEVEL_WORK;
  518. }
  519. break;
  520. }
  521. case E_ONE_LEVEL_WORK:
  522. {
  523. if ( mp_carrier_one_level_task.get() != NULL )
  524. {
  525. //执行三级任务
  526. std::unique_lock<std::mutex> t_lock(mp_carrier_one_level_task->m_lock);
  527. m_parkspace_unit_id = mp_carrier_one_level_task->m_parkspace_unit_id;
  528. m_parkspace_floor_id = mp_carrier_one_level_task->m_parkspace_floor_id;
  529. m_parkspace_room_id = mp_carrier_one_level_task->m_parkspace_room_id;
  530. m_command_key = mp_carrier_one_level_task->m_command_key;
  531. m_motion_direction = mp_carrier_one_level_task->m_motion_direction;
  532. m_car_center_x = mp_carrier_one_level_task->m_car_center_x;
  533. m_car_center_y = mp_carrier_one_level_task->m_car_center_y;
  534. m_car_angle = mp_carrier_one_level_task->m_car_angle;
  535. //更新通信
  536. update_device_communication();
  537. //检查设备的答复
  538. if ( m_respons_status == RESPONS_OVER )
  539. {
  540. mp_carrier_one_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  541. end_task(mp_carrier_one_level_task);
  542. m_carrier_status = E_ONE_LEVEL_OVER;
  543. }
  544. else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  545. {
  546. mp_carrier_one_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  547. //添加错误码
  548. Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  549. mp_carrier_one_level_task->set_task_error_manager(t_error);
  550. end_task(mp_carrier_one_level_task);
  551. m_carrier_status = E_ONE_LEVEL_OVER;
  552. }
  553. else if(m_carrier_status == E_ONE_LEVEL_WORK)
  554. {
  555. //设备正常运行
  556. //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
  557. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  558. }
  559. }
  560. else
  561. {
  562. //直接降级
  563. m_carrier_status = E_READY;
  564. }
  565. break;
  566. }
  567. case E_ONE_LEVEL_OVER:
  568. {
  569. if ( mp_carrier_one_level_task.get() != NULL )
  570. {
  571. //检查任务状态,
  572. //在 E_ONE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
  573. //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
  574. if ( mp_carrier_one_level_task->get_task_statu() == TASK_WITHDRAW )
  575. {
  576. //这里会通知任务已经释放, 然后销毁任务单, 并降级
  577. mp_carrier_one_level_task->set_task_statu(TASK_FREE);
  578. mp_carrier_one_level_task.reset();
  579. m_carrier_status = E_READY;
  580. }
  581. //else //保持不动, 直到发送方给定新的任务,
  582. }
  583. else
  584. {
  585. //直接降级
  586. m_carrier_status = E_READY;
  587. }
  588. break;
  589. }
  590. case E_FAULT:
  591. {
  592. //更新通信
  593. update_device_communication();
  594. //所有任务报错, 并销毁任务.
  595. if ( mp_carrier_one_level_task.get() != NULL )
  596. {
  597. //添加错误码
  598. Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  599. mp_carrier_one_level_task->set_task_error_manager(t_error);
  600. end_task(mp_carrier_one_level_task);
  601. mp_carrier_one_level_task.reset();
  602. }
  603. if ( mp_carrier_two_level_task.get() != NULL )
  604. {
  605. //添加错误码
  606. Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  607. mp_carrier_two_level_task->set_task_error_manager(t_error);
  608. end_task(mp_carrier_two_level_task);
  609. mp_carrier_two_level_task.reset();
  610. }
  611. if ( mp_carrier_three_level_task.get() != NULL )
  612. {
  613. //添加错误码
  614. Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
  615. mp_carrier_three_level_task->set_task_error_manager(t_error);
  616. end_task(mp_carrier_three_level_task);
  617. mp_carrier_three_level_task.reset();
  618. }
  619. break;
  620. }
  621. case E_DISCONNECT:
  622. {
  623. //更新通信
  624. update_device_communication();
  625. //所有任务报错, 并销毁任务.
  626. if ( mp_carrier_one_level_task.get() != NULL )
  627. {
  628. //添加错误码
  629. Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  630. mp_carrier_one_level_task->set_task_error_manager(t_error);
  631. end_task(mp_carrier_one_level_task);
  632. mp_carrier_one_level_task.reset();
  633. }
  634. if ( mp_carrier_two_level_task.get() != NULL )
  635. {
  636. //添加错误码
  637. Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  638. mp_carrier_two_level_task->set_task_error_manager(t_error);
  639. end_task(mp_carrier_two_level_task);
  640. mp_carrier_two_level_task.reset();
  641. }
  642. if ( mp_carrier_three_level_task.get() != NULL )
  643. {
  644. //添加错误码
  645. Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
  646. mp_carrier_three_level_task->set_task_error_manager(t_error);
  647. end_task(mp_carrier_three_level_task);
  648. mp_carrier_three_level_task.reset();
  649. }
  650. break;
  651. }
  652. case E_READY:
  653. {
  654. //更新通信
  655. update_device_communication();
  656. break;
  657. }
  658. default:
  659. {
  660. break;
  661. }
  662. }
  663. }
  664. }
  665. LOG(INFO) << " execute_thread_fun end "<< this;
  666. return;
  667. }
  668. //更新设备底层通信数据
  669. Error_manager Carrier_base::update_device_communication()
  670. {
  671. //加锁
  672. std::mutex * tp_data_lock = Dispatch_communication::get_instance_references().get_data_lock();
  673. std::unique_lock<std::mutex> t_lock(*tp_data_lock);
  674. //请求消息, 调度->plc
  675. Dispatch_communication::Request_from_dispatch_to_plc * tp_request_from_dispatch_to_plc =
  676. Dispatch_communication::get_instance_references().get_request_from_dispatch_to_plc();
  677. tp_request_from_dispatch_to_plc->m_block_id = m_parkspace_unit_id;
  678. tp_request_from_dispatch_to_plc->m_floor_id = m_parkspace_floor_id;
  679. tp_request_from_dispatch_to_plc->m_parkspace_id = m_parkspace_room_id;
  680. memcpy(tp_request_from_dispatch_to_plc->m_command_key, m_command_key.c_str(), m_command_key.size());
  681. tp_request_from_dispatch_to_plc->m_is_entrance_flag = m_motion_direction;
  682. tp_request_from_dispatch_to_plc->m_center_x = m_car_center_x;
  683. tp_request_from_dispatch_to_plc->m_center_y = m_car_center_y;
  684. tp_request_from_dispatch_to_plc->m_car_angle = m_car_angle;
  685. //答复消息, plc->调度
  686. Dispatch_communication::Response_from_plc_to_dispatch * tp_response_from_plc_to_dispatch =
  687. Dispatch_communication::get_instance_references().get_response_from_plc_to_dispatch();
  688. m_respons_key = (char*)(tp_response_from_plc_to_dispatch->m_command_key);
  689. if ( m_respons_key == m_command_key )
  690. {
  691. if ( tp_response_from_plc_to_dispatch->m_task_status == 0 )
  692. {
  693. if ( tp_response_from_plc_to_dispatch->m_command_flag == 0 )
  694. {
  695. m_respons_status = RESPONS_NORMAL;
  696. }
  697. else
  698. {
  699. m_respons_status = RESPONS_OVER;
  700. }
  701. }
  702. else if ( tp_response_from_plc_to_dispatch->m_task_status == 1 )
  703. {
  704. m_respons_status = RESPONS_MINOR_ERROR;
  705. }
  706. else
  707. {
  708. m_respons_status = RESPONS_CRITICAL_ERROR;
  709. }
  710. }
  711. else
  712. {
  713. m_respons_status = RESPONS_NORMAL;
  714. }
  715. //状态消息, 调度->plc
  716. Dispatch_communication::Status_from_dispatch_to_plc * tp_status_from_dispatch_to_plc =
  717. Dispatch_communication::get_instance_references().get_status_from_dispatch_to_plc();
  718. tp_status_from_dispatch_to_plc->m_heartbeat++;
  719. //状态消息, plc->调度
  720. Dispatch_communication::Status_from_plc_to_dispatch * tp_status_from_plc_to_dispatch =
  721. Dispatch_communication::get_instance_references().get_status_from_plc_to_dispatch();
  722. //通过心跳帧来判断通信是否正常
  723. if ( m_last_heartbeat != tp_status_from_plc_to_dispatch->m_heartbeat )
  724. {
  725. m_last_heartbeat = tp_status_from_plc_to_dispatch->m_heartbeat;
  726. m_status_updata_time = std::chrono::system_clock::now();
  727. m_carrier_coordinate_x = tp_status_from_plc_to_dispatch->m_carrier_coordinates;
  728. m_carrier_coordinate_z = tp_status_from_plc_to_dispatch->m_elevator_coordinates;
  729. m_carrier_angle = tp_status_from_plc_to_dispatch->m_carrier_angle;
  730. m_elevator_device_status = tp_status_from_plc_to_dispatch->m_elevator_information;
  731. memcpy(m_elevator_error_code, tp_status_from_plc_to_dispatch->m_elevator_error_code, 50);
  732. m_elevator_error_description = (char*)(tp_status_from_plc_to_dispatch->m_carrier_error_string);
  733. m_carrier_device_status = tp_status_from_plc_to_dispatch->m_carrier_information;
  734. memcpy(m_carrier_error_code, tp_status_from_plc_to_dispatch->m_carrier_error_code, 50);
  735. m_carrier_error_description = (char*)(tp_status_from_plc_to_dispatch->m_carrier_error_string);
  736. if ( m_carrier_status == E_DISCONNECT )
  737. {
  738. m_carrier_status = E_THREE_LEVEL_WORK;
  739. }
  740. }
  741. else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(CARRIER_COMMUNICATION_OVER_TIME_MS))
  742. {
  743. m_carrier_status = E_DISCONNECT;
  744. }
  745. return Error_code::SUCCESS;
  746. }
  747. //取消下发的指令
  748. Error_manager Carrier_base::cancel_command()
  749. {
  750. //以后再写 need programe
  751. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  752. //这里先空着,以后再写.
  753. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  754. return Error_code::SUCCESS;
  755. }