123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847 |
- //
- // Created by huli on 2020/7/20.
- //
- #include "carrier_base.h"
- #include "dispatch_communication.h"
- Carrier_base::Carrier_base()
- {
- m_carrier_status = E_UNKNOW;
- m_carrier_id = 0;
- m_parkspace_unit_id = 0; //车位单元号(楚天项目为1~3)
- m_parkspace_floor_id = 0; //车位楼层号(楚天项目为1~14)
- m_parkspace_room_id = 0; //车位同层的房间号 (楚天项目一楼为1~4, 楼上为1~6)
- // m_command_key = 0; //任务唯一码
- m_motion_direction = 0; //调度方向, 根据停车取车选择出入口, 1=入口,0=非入口
- m_car_center_x = 0; //汽车的中心坐标x
- m_car_center_y = 0; //汽车的中心坐标y
- m_car_angle = 0; //汽车的旋转角(角度-90~90)
- m_carrier_coordinate_x = 0; //搬运器坐标x轴, 中跑车控制横向移动
- m_carrier_coordinate_y = 0; //搬运器坐标y轴, (本模块默认为0, 由AGV模块去控制纵向移动)
- m_carrier_coordinate_z = 0; //搬运器坐标z轴, 电梯控制上下移动
- m_respons_status = RESPONS_NORMAL; //指令完成状态, 搬运器答复指令, 返回任务完成的情况
- //硬件状态, 目前只做显示判断
- m_carrier_device_status = E_UNKNOW; //搬运器设备的状态, 楚天项目就是中跑车
- memset(m_carrier_error_code, 0, 50); //搬运器设备的报警信息位
- // m_carrier_error_description = 0;//搬运器设备的报警状态描述
- m_elevator_device_status = E_UNKNOW; //升降机设备状态, 楚天项目就是电梯
- memset(m_elevator_error_code, 0, 50); //升降机设备的报警信息位
- // m_elevator_error_description = 0;//升降机设备的报警状态描述
- mp_execute_thread = NULL;
- }
- Carrier_base::~Carrier_base()
- {
- carrier_base_uninit();
- }
- //搬运器 初始化
- Error_manager Carrier_base::carrier_base_init()
- {
- // 线程默认开启
- m_execute_condition.reset(false, true, false);
- mp_execute_thread = new std::thread(&Carrier_base::execute_thread_fun, this);
- m_carrier_status = E_READY;
- return Error_code::SUCCESS;
- }
- //搬运器 反初始化
- Error_manager Carrier_base::carrier_base_uninit()
- {
- if (mp_execute_thread)
- {
- m_execute_condition.kill_all();
- }
- if (mp_execute_thread)
- {
- mp_execute_thread->join();
- delete mp_execute_thread;
- mp_execute_thread = NULL;
- }
- m_carrier_status = E_UNKNOW;
- return Error_code::SUCCESS;
- }
- //处理一级任务, 普通任务
- Error_manager Carrier_base::execute_one_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
- {
- Error_manager t_error;
- Error_manager t_result;
- //检查指针
- if (p_carrier_task == NULL) {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- "Carrier_base::execute_one_level_task failed, POINTER_IS_NULL");
- }
- //检查任务类型,
- if (p_carrier_task->get_task_type() != CARRIER_TASK)
- {
- return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
- "Carrier_base::execute_one_level_task get_task_type() != CARRIER_TASK ");
- }
- //检查接收方的状态
- t_error = check_one_level_task();
- if ( t_error != SUCCESS )
- {
- t_result.compare_and_cover_error(t_error);
- }
- else
- {
- //接受任务,并将任务的状态改为TASK_SIGNED已签收
- mp_carrier_one_level_task = p_carrier_task;
- mp_carrier_one_level_task->set_task_statu(TASK_SIGNED);
- //这里不用检查任务内容, 直接下发给底层设备
- //启动定位管理模块,的核心工作线程
- m_carrier_status = E_ONE_LEVEL_WORK;
- m_execute_condition.notify_all(true);
- //通知 thread_work 子线程启动。
- //将任务的状态改为 TASK_WORKING 处理中
- mp_carrier_one_level_task->set_task_statu(TASK_WORKING);
- }
- if ( t_result != Error_code::SUCCESS )
- {
- return t_result;
- }
- return Error_code::SUCCESS;
- }
- //处理二级任务, 插队任务
- Error_manager Carrier_base::execute_two_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
- {
- Error_manager t_error;
- Error_manager t_result;
- //检查指针
- if (p_carrier_task == NULL) {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- "Carrier_base::execute_two_level_task failed, POINTER_IS_NULL");
- }
- //检查任务类型,
- if (p_carrier_task->get_task_type() != CARRIER_TASK)
- {
- return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
- "Carrier_base::execute_two_level_task get_task_type() != CARRIER_TASK ");
- }
- //检查接收方的状态
- t_error = check_two_level_task();
- if ( t_error != SUCCESS )
- {
- t_result.compare_and_cover_error(t_error);
- }
- else
- {
- //接受任务,并将任务的状态改为TASK_SIGNED已签收
- mp_carrier_two_level_task = p_carrier_task;
- mp_carrier_two_level_task->set_task_statu(TASK_SIGNED);
- //这里不用检查任务内容, 直接下发给底层设备
- //启动定位管理模块,的核心工作线程
- m_carrier_status = E_TWO_LEVEL_WORK;
- m_execute_condition.notify_all(true);
- //通知 thread_work 子线程启动。
- //将任务的状态改为 TASK_WORKING 处理中
- mp_carrier_two_level_task->set_task_statu(TASK_WORKING);
- }
- if ( t_result != Error_code::SUCCESS )
- {
- return t_result;
- }
- return Error_code::SUCCESS;
- }
- //处理三级任务, 核心任务
- Error_manager Carrier_base::execute_three_level_task(std::shared_ptr<Carrier_task> p_carrier_task)
- {
- Error_manager t_error;
- Error_manager t_result;
- //检查指针
- if (p_carrier_task == NULL) {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- "Carrier_base::execute_three_level_task failed, POINTER_IS_NULL");
- }
- //检查任务类型,
- if (p_carrier_task->get_task_type() != CARRIER_TASK)
- {
- return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
- "Carrier_base::execute_three_level_task get_task_type() != CARRIER_TASK ");
- }
- //检查接收方的状态
- t_error = check_three_level_task();
- if ( t_error != SUCCESS )
- {
- t_result.compare_and_cover_error(t_error);
- }
- else
- {
- //接受任务,并将任务的状态改为TASK_SIGNED已签收
- mp_carrier_three_level_task = p_carrier_task;
- mp_carrier_three_level_task->set_task_statu(TASK_SIGNED);
- //这里不用检查任务内容, 直接下发给底层设备
- //启动定位管理模块,的核心工作线程
- m_carrier_status = E_THREE_LEVEL_WORK;
- m_execute_condition.notify_all(true);
- //通知 thread_work 子线程启动。
- //将任务的状态改为 TASK_WORKING 处理中
- mp_carrier_three_level_task->set_task_statu(TASK_WORKING);
- }
- if ( t_result != Error_code::SUCCESS )
- {
- return t_result;
- }
- return Error_code::SUCCESS;
- }
- //检查状态,是否正常运行
- Error_manager Carrier_base::check_status()
- {
- if ( m_carrier_status == E_READY )
- {
- return Error_code::SUCCESS;
- }
- else if ( m_carrier_status >= E_BUSY && m_carrier_status <= E_THREE_LEVEL_OVER)
- {
- return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
- " Carrier_base::check_status is busy ");
- }
- else
- {
- return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
- " Carrier_base::check_status error ");
- }
- return Error_code::SUCCESS;
- }
- //判断能否执行一级任务
- Error_manager Carrier_base::check_one_level_task()
- {
- if ( m_carrier_status == E_READY )
- {
- return Error_code::SUCCESS;
- }
- else if ( m_carrier_status >= E_BUSY && m_carrier_status <= E_THREE_LEVEL_OVER)
- {
- return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
- " Carrier_base::check_status is busy ");
- }
- else
- {
- return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
- " Carrier_base::check_status error ");
- }
- return Error_code::SUCCESS;
- }
- //判断能否执行二级任务
- Error_manager Carrier_base::check_two_level_task()
- {
- if ( m_carrier_status == E_READY ||
- m_carrier_status == E_ONE_LEVEL_WORK ||
- m_carrier_status == E_ONE_LEVEL_OVER )
- {
- return Error_code::SUCCESS;
- }
- else if ( m_carrier_status >= E_TWO_LEVEL_WORK && m_carrier_status <= E_THREE_LEVEL_OVER)
- {
- return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
- " Carrier_base::check_status is busy ");
- }
- else
- {
- return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
- " Carrier_base::check_status error ");
- }
- return Error_code::SUCCESS;
- }
- //判断能否执行三级任务
- Error_manager Carrier_base::check_three_level_task()
- {
- if ( m_carrier_status == E_READY ||
- m_carrier_status == E_ONE_LEVEL_WORK ||
- m_carrier_status == E_ONE_LEVEL_OVER )
- {
- return Error_code::SUCCESS;
- }
- else if ( m_carrier_status >= E_TWO_LEVEL_WORK && m_carrier_status <= E_THREE_LEVEL_OVER)
- {
- return Error_manager(Error_code::CARRIER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
- " Carrier_base::check_status is busy ");
- }
- else
- {
- return Error_manager(Error_code::CARRIER_STATUS_ERROR, Error_level::MINOR_ERROR,
- " Carrier_base::check_status error ");
- }
- return Error_code::SUCCESS;
- }
- //结束任务单,里面会根据任务的故障等级修正 任务单的状态
- Error_manager Carrier_base::end_task(std::shared_ptr<Carrier_task> carrier_task)
- {
- LOG(INFO) << " ---Carrier_base::end_task run---"<< this;
- carrier_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- //注:这里只修改任务单的状态, 搬运器的状态不管
- //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
- //判断任务单的错误等级,
- if ( carrier_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
- {
- //强制改为TASK_OVER,不管它当前在做什么。
- carrier_task->set_task_statu(TASK_OVER);
- }
- else
- {
- //强制改为 TASK_ERROR,不管它当前在做什么。
- carrier_task->set_task_statu(TASK_ERROR);
- }
- return Error_code::SUCCESS;
- }
- //取消任务单,由发送方提前取消任务单
- Error_manager Carrier_base::cancel_task(std::shared_ptr<Carrier_task> carrier_task)
- {
- //找到对应的任务单
- if ( carrier_task->get_task_id() == mp_carrier_one_level_task->get_task_id() )
- {
- if ( m_carrier_status == E_ONE_LEVEL_WORK || m_carrier_status == E_ONE_LEVEL_OVER )
- {
- //如果正在执行一级任务, 那么取消当前指令, 然后降级
- m_execute_condition.notify_all(false);
- //确保内部线程已经停下
- while (m_execute_condition.is_working())
- {
- }
- cancel_command();
- mp_carrier_one_level_task.reset();
- m_carrier_status = E_READY;
- m_execute_condition.notify_all(true);
- }
- else
- {
- //否则直接销毁任务单
- mp_carrier_one_level_task.reset();
- }
- }
- else if ( carrier_task->get_task_id() == mp_carrier_two_level_task->get_task_id() )
- {
- if ( m_carrier_status == E_TWO_LEVEL_WORK || m_carrier_status == E_TWO_LEVEL_OVER )
- {
- //如果正在执行二级任务, 那么取消当前指令, 然后降级
- m_execute_condition.notify_all(false);
- //确保内部线程已经停下
- while (m_execute_condition.is_working())
- {
- }
- cancel_command();
- mp_carrier_two_level_task.reset();
- m_carrier_status = E_ONE_LEVEL_WORK;
- m_execute_condition.notify_all(true);
- }
- else
- {
- //否则直接销毁任务单
- mp_carrier_two_level_task.reset();
- }
- }
- else if ( carrier_task->get_task_id() == mp_carrier_three_level_task->get_task_id() )
- {
- if ( m_carrier_status == E_THREE_LEVEL_WORK || m_carrier_status == E_THREE_LEVEL_OVER )
- {
- //如果正在执行三级任务, 那么取消当前指令, 然后降级
- m_execute_condition.notify_all(false);
- //确保内部线程已经停下
- while (m_execute_condition.is_working())
- {
- }
- cancel_command();
- mp_carrier_three_level_task.reset();
- m_carrier_status = E_TWO_LEVEL_WORK;
- m_execute_condition.notify_all(true);
- }
- else
- {
- //否则直接销毁任务单
- mp_carrier_two_level_task.reset();
- }
- }
- else
- {
- return Error_manager(Error_code::CARRIER_TASK_NOTHINGNESS, Error_level::MINOR_ERROR,
- " carrier_task->get_task_id() is nothingness ");
- }
- carrier_task->set_task_statu(TASK_DEAD);
- return Error_code::SUCCESS;
- }
- //判断是否为待机,如果已经准备好,则可以执行任务。
- bool Carrier_base::is_ready()
- {
- return (m_carrier_status == E_READY);
- }
- Carrier_base::Carrier_status Carrier_base::get_carrier_status()
- {
- return m_carrier_status;
- }
- //执行外界任务的执行函数
- void Carrier_base::execute_thread_fun()
- {
- LOG(INFO) << " execute_thread_fun start "<< this;
- // while (1);
- Error_manager t_error;
- while (m_execute_condition.is_alive())
- {
- m_execute_condition.wait();
- if ( m_execute_condition.is_alive() )
- {
- std::this_thread::sleep_for(std::chrono::microseconds(1));
- std::this_thread::yield();
- switch ( (Carrier_status)m_carrier_status )
- {
- //核心任务, (三级任务)
- case E_THREE_LEVEL_WORK:
- {
- if ( mp_carrier_three_level_task.get() != NULL )
- {
- //执行三级任务
- std::unique_lock<std::mutex> t_lock(mp_carrier_three_level_task->m_lock);
- m_parkspace_unit_id = mp_carrier_three_level_task->m_parkspace_unit_id;
- m_parkspace_floor_id = mp_carrier_three_level_task->m_parkspace_floor_id;
- m_parkspace_room_id = mp_carrier_three_level_task->m_parkspace_room_id;
- m_command_key = mp_carrier_three_level_task->m_command_key;
- m_motion_direction = mp_carrier_three_level_task->m_motion_direction;
- m_car_center_x = mp_carrier_three_level_task->m_car_center_x;
- m_car_center_y = mp_carrier_three_level_task->m_car_center_y;
- m_car_angle = mp_carrier_three_level_task->m_car_angle;
- //更新通信
- update_device_communication();
- //检查设备的答复
- if ( m_respons_status == RESPONS_OVER )
- {
- mp_carrier_three_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- end_task(mp_carrier_three_level_task);
- m_carrier_status = E_THREE_LEVEL_OVER;
- }
- else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
- {
- mp_carrier_three_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- //添加错误码
- Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_three_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_three_level_task);
- m_carrier_status = E_THREE_LEVEL_OVER;
- }
- else
- {
- //设备正常运行
- //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- else
- {
- //直接降级
- m_carrier_status = E_TWO_LEVEL_WORK;
- }
- break;
- }
- case E_THREE_LEVEL_OVER:
- {
- if ( mp_carrier_three_level_task.get() != NULL )
- {
- //检查任务状态,
- //在 E_THREE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
- //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
- if ( mp_carrier_three_level_task->get_task_statu() == TASK_WITHDRAW )
- {
- //这里会通知任务已经释放, 然后销毁任务单, 并降级
- mp_carrier_three_level_task->set_task_statu(TASK_FREE);
- mp_carrier_three_level_task.reset();
- m_carrier_status = E_TWO_LEVEL_WORK;
- }
- //else //保持不动, 直到发送方给定新的任务,
- }
- else
- {
- //直接降级
- m_carrier_status = E_TWO_LEVEL_WORK;
- }
- break;
- }
- case E_TWO_LEVEL_WORK:
- {
- if ( mp_carrier_two_level_task.get() != NULL )
- {
- //执行三级任务
- std::unique_lock<std::mutex> t_lock(mp_carrier_two_level_task->m_lock);
- m_parkspace_unit_id = mp_carrier_two_level_task->m_parkspace_unit_id;
- m_parkspace_floor_id = mp_carrier_two_level_task->m_parkspace_floor_id;
- m_parkspace_room_id = mp_carrier_two_level_task->m_parkspace_room_id;
- m_command_key = mp_carrier_two_level_task->m_command_key;
- m_motion_direction = mp_carrier_two_level_task->m_motion_direction;
- m_car_center_x = mp_carrier_two_level_task->m_car_center_x;
- m_car_center_y = mp_carrier_two_level_task->m_car_center_y;
- m_car_angle = mp_carrier_two_level_task->m_car_angle;
- //更新通信
- update_device_communication();
- //检查设备的答复
- if ( m_respons_status == RESPONS_OVER )
- {
- mp_carrier_two_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- end_task(mp_carrier_two_level_task);
- m_carrier_status = E_TWO_LEVEL_OVER;
- }
- else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
- {
- mp_carrier_two_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- //添加错误码
- Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_two_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_two_level_task);
- m_carrier_status = E_TWO_LEVEL_OVER;
- }
- else
- {
- //设备正常运行
- //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- else
- {
- //直接降级
- m_carrier_status = E_ONE_LEVEL_WORK;
- }
- break;
- }
- case E_TWO_LEVEL_OVER:
- {
- if ( mp_carrier_two_level_task.get() != NULL )
- {
- //检查任务状态,
- //在 E_TWO_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
- //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
- if ( mp_carrier_two_level_task->get_task_statu() == TASK_WITHDRAW )
- {
- //这里会通知任务已经释放, 然后销毁任务单, 并降级
- mp_carrier_two_level_task->set_task_statu(TASK_FREE);
- mp_carrier_two_level_task.reset();
- m_carrier_status = E_ONE_LEVEL_WORK;
- }
- //else //保持不动, 直到发送方给定新的任务,
- }
- else
- {
- //直接降级
- m_carrier_status = E_ONE_LEVEL_WORK;
- }
- break;
- }
- case E_ONE_LEVEL_WORK:
- {
- if ( mp_carrier_one_level_task.get() != NULL )
- {
- //执行三级任务
- std::unique_lock<std::mutex> t_lock(mp_carrier_one_level_task->m_lock);
- m_parkspace_unit_id = mp_carrier_one_level_task->m_parkspace_unit_id;
- m_parkspace_floor_id = mp_carrier_one_level_task->m_parkspace_floor_id;
- m_parkspace_room_id = mp_carrier_one_level_task->m_parkspace_room_id;
- m_command_key = mp_carrier_one_level_task->m_command_key;
- m_motion_direction = mp_carrier_one_level_task->m_motion_direction;
- m_car_center_x = mp_carrier_one_level_task->m_car_center_x;
- m_car_center_y = mp_carrier_one_level_task->m_car_center_y;
- m_car_angle = mp_carrier_one_level_task->m_car_angle;
- //更新通信
- update_device_communication();
- //检查设备的答复
- if ( m_respons_status == RESPONS_OVER )
- {
- mp_carrier_one_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- end_task(mp_carrier_one_level_task);
- m_carrier_status = E_ONE_LEVEL_OVER;
- }
- else if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
- {
- mp_carrier_one_level_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
- //添加错误码
- Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_one_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_one_level_task);
- m_carrier_status = E_ONE_LEVEL_OVER;
- }
- else if(m_carrier_status == E_ONE_LEVEL_WORK)
- {
- //设备正常运行
- //延时1ms, snap7的通信效率偏慢, 不需要高频率检查
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- else
- {
- //直接降级
- m_carrier_status = E_READY;
- }
- break;
- }
- case E_ONE_LEVEL_OVER:
- {
- if ( mp_carrier_one_level_task.get() != NULL )
- {
- //检查任务状态,
- //在 E_ONE_LEVEL_WORK 里面, 已经 设为 TASK_OVER 了.
- //等待发送方的新指令, (发送方会把状态改为回收, 表示这个任务结束)
- if ( mp_carrier_one_level_task->get_task_statu() == TASK_WITHDRAW )
- {
- //这里会通知任务已经释放, 然后销毁任务单, 并降级
- mp_carrier_one_level_task->set_task_statu(TASK_FREE);
- mp_carrier_one_level_task.reset();
- m_carrier_status = E_READY;
- }
- //else //保持不动, 直到发送方给定新的任务,
- }
- else
- {
- //直接降级
- m_carrier_status = E_READY;
- }
- break;
- }
- case E_FAULT:
- {
- //更新通信
- update_device_communication();
- //所有任务报错, 并销毁任务.
- if ( mp_carrier_one_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_one_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_one_level_task);
- mp_carrier_one_level_task.reset();
- }
- if ( mp_carrier_two_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_two_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_two_level_task);
- mp_carrier_two_level_task.reset();
- }
- if ( mp_carrier_three_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_ERROR, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_three_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_three_level_task);
- mp_carrier_three_level_task.reset();
- }
- break;
- }
- case E_DISCONNECT:
- {
- //更新通信
- update_device_communication();
- //所有任务报错, 并销毁任务.
- if ( mp_carrier_one_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_one_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_one_level_task);
- mp_carrier_one_level_task.reset();
- }
- if ( mp_carrier_two_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_two_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_two_level_task);
- mp_carrier_two_level_task.reset();
- }
- if ( mp_carrier_three_level_task.get() != NULL )
- {
- //添加错误码
- Error_manager t_error(CARRIER_STATUS_DISCONNECT, MINOR_ERROR, "m_respons_status is error");
- mp_carrier_three_level_task->set_task_error_manager(t_error);
- end_task(mp_carrier_three_level_task);
- mp_carrier_three_level_task.reset();
- }
- break;
- }
- case E_READY:
- {
- //更新通信
- update_device_communication();
- break;
- }
- default:
- {
- break;
- }
- }
- }
- }
- LOG(INFO) << " execute_thread_fun end "<< this;
- return;
- }
- //更新设备底层通信数据
- Error_manager Carrier_base::update_device_communication()
- {
- //加锁
- std::mutex * tp_data_lock = Dispatch_communication::get_instance_references().get_data_lock();
- std::unique_lock<std::mutex> t_lock(*tp_data_lock);
- //请求消息, 调度->plc
- Dispatch_communication::Request_from_dispatch_to_plc * tp_request_from_dispatch_to_plc =
- Dispatch_communication::get_instance_references().get_request_from_dispatch_to_plc();
- tp_request_from_dispatch_to_plc->m_block_id = m_parkspace_unit_id;
- tp_request_from_dispatch_to_plc->m_floor_id = m_parkspace_floor_id;
- tp_request_from_dispatch_to_plc->m_parkspace_id = m_parkspace_room_id;
- memcpy(tp_request_from_dispatch_to_plc->m_command_key, m_command_key.c_str(), m_command_key.size());
- tp_request_from_dispatch_to_plc->m_is_entrance_flag = m_motion_direction;
- tp_request_from_dispatch_to_plc->m_center_x = m_car_center_x;
- tp_request_from_dispatch_to_plc->m_center_y = m_car_center_y;
- tp_request_from_dispatch_to_plc->m_car_angle = m_car_angle;
- //答复消息, plc->调度
- Dispatch_communication::Response_from_plc_to_dispatch * tp_response_from_plc_to_dispatch =
- Dispatch_communication::get_instance_references().get_response_from_plc_to_dispatch();
- m_respons_key = (char*)(tp_response_from_plc_to_dispatch->m_command_key);
- if ( m_respons_key == m_command_key )
- {
- if ( tp_response_from_plc_to_dispatch->m_task_status == 0 )
- {
- if ( tp_response_from_plc_to_dispatch->m_command_flag == 0 )
- {
- m_respons_status = RESPONS_NORMAL;
- }
- else
- {
- m_respons_status = RESPONS_OVER;
- }
- }
- else if ( tp_response_from_plc_to_dispatch->m_task_status == 1 )
- {
- m_respons_status = RESPONS_MINOR_ERROR;
- }
- else
- {
- m_respons_status = RESPONS_CRITICAL_ERROR;
- }
- }
- else
- {
- m_respons_status = RESPONS_NORMAL;
- }
- //状态消息, 调度->plc
- Dispatch_communication::Status_from_dispatch_to_plc * tp_status_from_dispatch_to_plc =
- Dispatch_communication::get_instance_references().get_status_from_dispatch_to_plc();
- tp_status_from_dispatch_to_plc->m_heartbeat++;
- //状态消息, plc->调度
- Dispatch_communication::Status_from_plc_to_dispatch * tp_status_from_plc_to_dispatch =
- Dispatch_communication::get_instance_references().get_status_from_plc_to_dispatch();
- //通过心跳帧来判断通信是否正常
- if ( m_last_heartbeat != tp_status_from_plc_to_dispatch->m_heartbeat )
- {
- m_last_heartbeat = tp_status_from_plc_to_dispatch->m_heartbeat;
- m_status_updata_time = std::chrono::system_clock::now();
- m_carrier_coordinate_x = tp_status_from_plc_to_dispatch->m_carrier_coordinates;
- m_carrier_coordinate_z = tp_status_from_plc_to_dispatch->m_elevator_coordinates;
- m_carrier_angle = tp_status_from_plc_to_dispatch->m_carrier_angle;
- m_elevator_device_status = tp_status_from_plc_to_dispatch->m_elevator_information;
- memcpy(m_elevator_error_code, tp_status_from_plc_to_dispatch->m_elevator_error_code, 50);
- m_elevator_error_description = (char*)(tp_status_from_plc_to_dispatch->m_carrier_error_string);
- m_carrier_device_status = tp_status_from_plc_to_dispatch->m_carrier_information;
- memcpy(m_carrier_error_code, tp_status_from_plc_to_dispatch->m_carrier_error_code, 50);
- m_carrier_error_description = (char*)(tp_status_from_plc_to_dispatch->m_carrier_error_string);
- if ( m_carrier_status == E_DISCONNECT )
- {
- m_carrier_status = E_THREE_LEVEL_WORK;
- }
- }
- else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(CARRIER_COMMUNICATION_OVER_TIME_MS))
- {
- m_carrier_status = E_DISCONNECT;
- }
- return Error_code::SUCCESS;
- }
- //取消下发的指令
- Error_manager Carrier_base::cancel_command()
- {
- //以后再写 need programe
- //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
- //这里先空着,以后再写.
- //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
- return Error_code::SUCCESS;
- }
|