自动化立体仓库 - WCS系统
Junjie
2023-03-31 87c4efe93e6fe31a9c989b4aa5cc0c8f559b845d
src/main/java/com/zy/core/thread/ShuttleThread.java
@@ -5,12 +5,13 @@
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.ModBus.ModbusTcpNet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasShuttle;
import com.zy.asrs.entity.BasShuttleOpt;
import com.zy.asrs.service.BasShuttleOptService;
import com.zy.asrs.service.BasShuttleService;
import com.zy.common.model.NavigateNode;
import com.zy.common.utils.CommonUtils;
import com.zy.common.utils.NavigatePositionConvert;
@@ -20,10 +21,7 @@
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.ShuttleRunDirection;
import com.zy.core.enums.ShuttleStatusType;
import com.zy.core.enums.ShuttleTaskModeType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.*;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.ShuttleAssignCommand;
@@ -121,6 +119,7 @@
                if (null == shuttleProtocol) {
                    shuttleProtocol = new ShuttleProtocol();
                    shuttleProtocol.setShuttleNo(slave.getId().shortValue());
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                }
                //----------读取四向穿梭车状态-----------
@@ -163,18 +162,74 @@
                ///读取四向穿梭车状态-end
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                //小车处于忙碌状态,将标记置为true
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.BUSY) {
                    shuttleProtocol.setPakMk(true);
                }
                //四向穿梭车空闲、有任务且标记为true,需要执行任务的下一条指令
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE && shuttleProtocol.getTaskNo() != 0 && shuttleProtocol.getPakMk()) {
                //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE
                        && shuttleProtocol.getTaskNo() != 0
                        && shuttleProtocol.getPakMk()
                        && shuttleProtocol.getAssignCommand() != null) {
                    //执行下一步指令
                    executeWork(shuttleProtocol.getAssignCommand());
                }
                //将四向穿梭车状态保存至数据库
                BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
                BasShuttle basShuttle = shuttleService.selectById(shuttleProtocol.getShuttleNo());
                if (basShuttle == null) {
                    basShuttle = new BasShuttle();
                    //四向穿梭车号
                    basShuttle.setShuttleNo(slave.getId());
                    shuttleService.insert(basShuttle);
                }
                //小车忙状态位
                basShuttle.setBusyStatus(shuttleProtocol.getBusyStatus().intValue());
                //当前二维码
                basShuttle.setCurrentCode(shuttleProtocol.getCurrentCode().intValue());
                //电池电量百分比
                basShuttle.setBatteryPower(shuttleProtocol.getBatteryPower().intValue());
                //电池温度
                basShuttle.setBatteryTemp(shuttleProtocol.getBatteryTemp().intValue());
                //错误编号
                basShuttle.setErrorCode(shuttleProtocol.getErrorCode().intValue());
                //Plc输出状态IO
                basShuttle.setPlcOutputStatusIo(shuttleProtocol.getPlcOutputStatusIO().intValue());
                //错误信息码
                basShuttle.setStatusErrorCode(shuttleProtocol.getStatusErrorCode().intValue());
                //PLC输入状态
                basShuttle.setPlcInputStatus(shuttleProtocol.getPlcInputStatus().intValue());
                //当前或者之前读到的二维码值
                basShuttle.setCurrentOrBeforeCode(shuttleProtocol.getCurrentOrBeforeCode().intValue());
                //读到的二维码X方向偏移量
                basShuttle.setCodeOffsetX(shuttleProtocol.getCodeOffsetX().intValue());
                //读到的二维码Y方向偏移量
                basShuttle.setCodeOffsetY(shuttleProtocol.getCodeOffsetY().intValue());
                //当前的电压值
                basShuttle.setCurrentVoltage(shuttleProtocol.getCurrentVoltage().intValue());
                //当前的模拟量值
                basShuttle.setCurrentAnalogValue(shuttleProtocol.getCurrentAnalogValue().intValue());
                //当前的升降伺服速度
                basShuttle.setCurrentLiftServoSpeed(shuttleProtocol.getCurrentLiftServoSpeed().intValue());
                //当前的行走伺服速度
                basShuttle.setCurrentMoveServoSpeed(shuttleProtocol.getCurrentMoveServoSpeed().intValue());
                //当前的升降伺服负载率
                basShuttle.setCurrentLiftServoLoad(shuttleProtocol.getCurrentLiftServoLoad().intValue());
                //当前的行走伺服负载率
                basShuttle.setCurrentMoveServoLoad(shuttleProtocol.getCurrentMoveServoLoad().intValue());
                //当前小车状态(内部自我维护)
                basShuttle.setShuttleStatus(shuttleProtocol.getProtocolStatus());
                //任务号
                basShuttle.setWrkNo(shuttleProtocol.getTaskNo().intValue());
                //修改时间
                basShuttle.setUpdateTime(new Date());
                //作业标记
                basShuttle.setPakMk(shuttleProtocol.getPakMk());
                if (shuttleService.updateById(basShuttle)) {
                    OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
//                    log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                }
            }else {
@@ -290,19 +345,32 @@
    }
    //分配任务
    private void assignWork(ShuttleAssignCommand assignCommand) {
    private void assignWork(ShuttleAssignCommand assignCommand) throws Exception {
        //将此map存入redis中
        HashMap<String, Object> map = new HashMap<>();
        //计算路径
        List<NavigateNode> calc = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getDistLocNo(), assignCommand.getTaskMode().intValue());
        if (calc != null) {
            //获取分段路径
            ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
            //路径数据
            map.put("path", data);
        if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id.shortValue()
                || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id.shortValue()) {
            //入库或出库模式,计算路径
            //计算路径
            List<NavigateNode> calc = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getLocNo(), assignCommand.getTaskMode().intValue());
            if (calc != null) {
                //获取分段路径
                ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
                //路径数据
                map.put("path", data);
                //路径次数
                map.put("pathSize", data.size());
            }else {
                if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id.shortValue() || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id.shortValue()) {
                    //属于入库出库,必须要计算路径,则抛出异常
                    throw new Exception("未能找到运行路径");
                }
            }
        }
        //四向穿梭车号
        map.put("shuttle_no", assignCommand.getShuttleNo());
        //工作号
        map.put("wrk_no", assignCommand.getTaskNo());
        //命令执行步序
@@ -311,6 +379,7 @@
        map.put("assignCommand", assignCommand);
        shuttleProtocol.setTaskNo(assignCommand.getTaskNo());
        shuttleProtocol.setAssignCommand(assignCommand);
        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING);
        //任务数据保存到redis
        redisUtil.set("wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(map));
        //执行下发任务
@@ -318,9 +387,19 @@
    }
    //执行下发的指令
    private void executeWork(ShuttleAssignCommand assignCommand) {
    private boolean executeWork(ShuttleAssignCommand assignCommand) {
        //读取redis数据
        if (assignCommand == null) {
            return false;
        }
        //将标记置为false(防止重发)
        shuttleProtocol.setPakMk(false);
        Object o = redisUtil.get("wrk_no_" + assignCommand.getTaskNo());
        if (o == null) {
            return false;
        }
        HashMap map = JSON.parseObject(o.toString(), HashMap.class);
        //当前步序
        int commandStep = Integer.parseInt(map.get("commandStep").toString());
@@ -336,21 +415,27 @@
                ArrayList pathList = JSON.parseObject(data.toString(), ArrayList.class);
                //取第一条路径
                Object o1 = pathList.get(commandStep);
                ArrayList path = JSON.parseObject(o1.toString(), ArrayList.class);
                ArrayList objectPath = JSON.parseObject(o1.toString(), ArrayList.class);
                ArrayList<NavigateNode> path = new ArrayList<>();
                for (Object o2 : objectPath) {
                    NavigateNode navigateNode = JSON.parseObject(o2.toString(), NavigateNode.class);
                    path.add(navigateNode);
                }
                Integer allDistance = NavigateUtils.getCurrentPathAllDistance(path);//计算当前路径行走总距离
                size = path.size();
                size = Integer.parseInt(map.get("pathSize").toString());
                //开始路径
                JSONObject startPath = JSON.parseObject(path.get(0).toString());
                NavigateNode startPath = path.get(0);
                //目标路径
                JSONObject endPath = JSON.parseObject(path.get(size - 1).toString());
                NavigateNode endPath = path.get(path.size() - 1);
                command.setCommandWord((short) 1);
                command.setStartCodeNum(NavigatePositionConvert.xyToPosition(startPath.getIntValue("x"), startPath.getIntValue("y")));
                command.setStartCodeNum(NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY()));
                command.setMiddleCodeNum((short) 0);
                command.setDistCodeNum(NavigatePositionConvert.xyToPosition(endPath.getIntValue("x"), endPath.getIntValue("y")));
                command.setStartToDistDistance(1000);
                command.setDistCodeNum(NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY()));
                command.setStartToDistDistance(allDistance);
                command.setMiddleToDistDistance(1000);
                command.setRunDirection(ShuttleRunDirection.get(startPath.get("direction").toString()).id);
                command.setRunDirection(ShuttleRunDirection.get(startPath.getDirection()).id);
                command.setPalletLift((short) 1);
                command.setForceMoveDistance(1000);
                command.setChargeSwitch((short) 2);
@@ -374,20 +459,30 @@
                command.setRunDirection((short) (assignCommand.getTaskMode() - 4));
                command.setCommandEnd((short) 1);
                break;
            case 9://状态复位
                command.setCommandWord((short) 0);
                //设置四向穿梭车为空闲状态
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                //任务号清零
                shuttleProtocol.setTaskNo((short) 0);
                //标记复位
                shuttleProtocol.setPakMk(true);
                //任务指令清零
                shuttleProtocol.setAssignCommand(null);
                break;
            default:
        }
        command.setCommandEnd((short) 1);
        //下发命令
        if (!write(command)) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            return false;
        } else {
            News.info("四向穿梭车命令下发成功,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            //将标记置为false(防止重发)
            shuttleProtocol.setPakMk(false);
            //判断数据是否执行完成
            if (commandStep < size) {
            if (commandStep < size - 1) {
                //更新redis数据
                //步序增加
                commandStep++;
@@ -405,19 +500,30 @@
                            new Date(),
                            ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                            assignCommand.getSourceLocNo(),
                            assignCommand.getDistLocNo(),
                            assignCommand.getLocNo(),
                            null,
                            null,
                            null,
                            JSON.toJSONString(command)
                            JSON.toJSONString(assignCommand)
                    );
                    shuttleOptService.insert(opt);
                }
                //删除redis
                redisUtil.del("wrk_no_" + map.get("wrk_no").toString());
                if (!assignCommand.getAuto()) {
                    //手动模式不抛出等待状态,直接复位
                    News.info("四向穿梭车手动任务执行完成,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }else {
                    //对主线程抛出等待确认状态waiting
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WAITING);
                    News.info("四向穿梭车任务执行完成等待确认中,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }
            }
        }
        return true;
    }
    /******************************************************************************************/