自动化立体仓库 - WCS系统
Junjie
2023-06-27 d317c70cf46549a5cc190d1e3334bd8945ee46a8
src/main/java/com/zy/core/thread/ShuttleThread.java
@@ -8,16 +8,11 @@
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.BasShuttle;
import com.zy.asrs.entity.BasShuttleOpt;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.BasShuttleOptService;
import com.zy.asrs.service.BasShuttleService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.common.utils.*;
import com.zy.core.News;
import com.zy.core.ThreadHandler;
@@ -30,6 +25,7 @@
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.LiftProtocol;
import com.zy.core.model.protocol.ShuttleProtocol;
import com.zy.core.model.protocol.StaProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -134,20 +130,23 @@
                //电池电量百分比
                shuttleProtocol.setBatteryPower(modbusTcpNet.getByteTransform().TransInt16(content,4));
                //电池温度
                shuttleProtocol.setBatteryTemp(modbusTcpNet.getByteTransform().TransInt16(content,6));
                shuttleProtocol.setBatteryTemp(modbusTcpNet.getByteTransform().TransUInt16(content, 6));
                //错误编号
                shuttleProtocol.setErrorCode(modbusTcpNet.getByteTransform().TransInt16(content,8));
                //Plc输出状态IO
                boolean[] booleans = modbusTcpNet.getByteTransform().TransBool(content, 10, 2);
                shuttleProtocol.setPlcOutputLift(booleans[1]);
                shuttleProtocol.setPlcOutputTransfer(booleans[2]);
                shuttleProtocol.setPlcOutputBrake(booleans[3]);
                shuttleProtocol.setPlcOutputCharge(booleans[4]);
                int plcOutIo = modbusTcpNet.getByteTransform().TransUInt16(content, 10);
                int[] plcOutIos = CommonUtils.byteToBits((byte) plcOutIo);
                shuttleProtocol.setPlcOutputLift(plcOutIos[1] == 1);
                shuttleProtocol.setPlcOutputTransfer(plcOutIos[2] == 1);
                shuttleProtocol.setPlcOutputBrake(plcOutIos[3] == 1);
                shuttleProtocol.setPlcOutputCharge(plcOutIos[4] == 1);
                shuttleProtocol.setPlcOutputStatusIO(modbusTcpNet.getByteTransform().TransInt16(content, 10));
                //错误信息码
                shuttleProtocol.setStatusErrorCode(modbusTcpNet.getByteTransform().TransInt16(content,12));
                int plcInIo = modbusTcpNet.getByteTransform().TransUInt16(content, 14);
                int[] plcInIos = CommonUtils.byteToBits((byte) plcInIo);
                //PLC输入状态
                shuttleProtocol.setPlcInputStatus(modbusTcpNet.getByteTransform().TransInt16(content,14));
                shuttleProtocol.setPlcInputStatus((short) plcInIos[6]);
                //当前或者之前读到的二维码值
                shuttleProtocol.setCurrentOrBeforeCode(modbusTcpNet.getByteTransform().TransInt16(content,16));
                //读到的二维码X方向偏移量
@@ -155,7 +154,7 @@
                //读到的二维码Y方向偏移量
                shuttleProtocol.setCodeOffsetY(modbusTcpNet.getByteTransform().TransInt16(content,20));
                //当前的电压值
                shuttleProtocol.setCurrentVoltage(modbusTcpNet.getByteTransform().TransInt16(content,22));
                shuttleProtocol.setCurrentVoltage(modbusTcpNet.getByteTransform().TransUInt16(content, 22));
                //当前的模拟量值
                shuttleProtocol.setCurrentAnalogValue(modbusTcpNet.getByteTransform().TransInt16(content,24));
                //当前的升降伺服速度
@@ -174,18 +173,18 @@
                    shuttleProtocol.setPakMk(true);
                }
                if (shuttleProtocol.getErrorCode() != 0 && shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.WORKING) {
                    //出现错误
                    resetAndTryFix(shuttleProtocol.getTaskNo());
                }
                if(shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.FIXING
                        && shuttleProtocol.getTaskNo() != 0
                        && shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE){
                    //处于故障修复状态
                    //执行下一步指令
                    executeWork(shuttleProtocol.getTaskNo());
                }
//                if (shuttleProtocol.getErrorCode() != 0 && shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.WORKING) {
//                    //出现错误
//                    resetAndTryFix(shuttleProtocol.getTaskNo());
//                }
//
//                if(shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.FIXING
//                        && shuttleProtocol.getTaskNo() != 0
//                        && shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE){
//                    //处于故障修复状态
//                    //执行下一步指令
//                    executeWork(shuttleProtocol.getTaskNo());
//                }
                //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE
@@ -195,22 +194,22 @@
                    executeWork(shuttleProtocol.getTaskNo());
                }
                //检测是否有提升机锁定标记,有则检测提升机是否到位,是否能走下一步命令
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE
                        && shuttleProtocol.getTaskNo() != 0) {
                    Object o = redisUtil.get("shuttle_wrk_no_" + shuttleProtocol.getTaskNo());
                    if (o != null) {
                        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
                        if (redisCommand.getLiftSecurityMk()) {
                            //执行下一步指令
                            executeWork(shuttleProtocol.getTaskNo());
                        }
                    }
                }
//                //检测是否有提升机锁定标记,有则检测提升机是否到位,是否能走下一步命令
//                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE
//                        && shuttleProtocol.getTaskNo() != 0) {
//                    Object o = redisUtil.get("shuttle_wrk_no_" + shuttleProtocol.getTaskNo());
//                    if (o != null) {
//                        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
//                        if (redisCommand.getLiftSecurityMk()) {
//                            //执行下一步指令
//                            executeWork(shuttleProtocol.getTaskNo());
//                        }
//                    }
//                }
                //将四向穿梭车状态保存至数据库
                BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
                BasShuttle basShuttle = shuttleService.selectById(shuttleProtocol.getShuttleNo());
                BasShuttle basShuttle = shuttleService.selectById(slave.getId());
                if (basShuttle == null) {
                    basShuttle = new BasShuttle();
                    //四向穿梭车号
@@ -370,6 +369,11 @@
        OperateResult result = modbusTcpNet.Write("0", array);;
        if (result != null && result.IsSuccess) {
            try {
                Thread.sleep(3000);//命令下发后休眠1s
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            News.info("四向穿梭车命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
@@ -391,21 +395,35 @@
    //分配任务
    private void assignWork(ShuttleAssignCommand assignCommand) {
        ShuttleRedisCommand redisCommand = new ShuttleRedisCommand();
        if (!assignCommand.getAuto()) {
            List<NavigateNode> allNode = new ArrayList<>();
            List<ShuttleCommand> commands = new ArrayList<>();
            LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
            BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
            NavigateMapData navigateMapData;
            //获取小车移动速度
            BasShuttle basShuttle = shuttleService.selectById(slave.getId());
            Integer runSpeed = 1000;
            if (basShuttle != null) {
                Integer runSpeed1 = basShuttle.getRunSpeed();
                if (runSpeed1 != null) {
                    runSpeed = runSpeed1;
                }
            }
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            switch (assignCommand.getTaskMode()) {
                case 1://入库
                case 2://出库
                    //小车移动到提升机口,计算路径
                    //计算小车起点到中点所需命令
                    LocMast currentLocMast = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString());
                    List<NavigateNode> firstMastResult = NavigateUtils.calc(currentLocMast.getLocNo(), assignCommand.getSourceLocNo(), ShuttleTaskModeType.PAK_IN.id);
                    //所使用的路径进行锁定禁用
                    navigateMapData = new NavigateMapData(Utils.getLev(currentLocMast.getLocNo()));
                    navigateMapData.writeNavigateNodeToRedisMap(firstMastResult, true);////所使用的路径进行锁定禁用
                    List<NavigateNode> firstMastResult = NavigateUtils.calc(currentLocMast.getLocNo(), assignCommand.getSourceLocNo(), NavigationMapType.NORMAL.id);//小车到中点,处于无货状态,使用正常通道地图
                    if (firstMastResult != null) {
                        allNode.addAll(firstMastResult);//将节点进行保存
@@ -415,14 +433,18 @@
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes);//计算中间点到目标点行走距离
                            //正常移动命令
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), 1);//开始二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), 1);//目标二维码
                            commands.add(getMoveCommand(startCode,distCode,allDistance,ShuttleRunDirection.get(startPath.getDirection()).id));
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//目标二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                        //托盘顶升
@@ -437,8 +459,7 @@
                    }
                    //计算中点到终点路径
                    List<NavigateNode> secMastResult = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getLocNo(), ShuttleTaskModeType.PAK_IN.id);
                    navigateMapData.writeNavigateNodeToRedisMap(firstMastResult, true);////所使用的路径进行锁定禁用
                    List<NavigateNode> secMastResult = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getLocNo(), NavigationMapType.DFX.id);//小车从中点到终点,处于有货状态,使用DFX地图
                    if (secMastResult != null) {
                        allNode.addAll(secMastResult);//将节点进行保存
@@ -448,38 +469,57 @@
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes);//计算中间点到目标点行走距离
                            //正常移动命令
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), 1);//开始二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), 1);//目标二维码
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id));
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                        //托盘下降
                        commands.add(getPalletCommand((short) 2));
                    }
                    if (firstMastResult == null || secMastResult == null) {
                        throw new CoolException(MessageFormat.format( "四向穿梭车出入库路径搜索失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
                    }
                    navigateMapData = new NavigateMapData(Utils.getLev(currentLocMast.getLocNo()));
                    //所使用的路径进行锁定禁用
                    navigateMapData.writeNavigateNodeToRedisMap(firstMastResult, true);////所使用的路径进行锁定禁用
                    navigateMapData.writeNavigateNodeToRedisMap(secMastResult, true);////所使用的路径进行锁定禁用
                    break;
                case 3://托盘顶升
                case 4://托盘下降
                    commands.add(getPalletCommand(assignCommand.getTaskMode() == 3 ? (short) 1 : (short) 2));
                    break;
                case 5://强制左移
                    commands.add(getForceMoveCommand((short) 2));
                    break;
                case 6://强制右移
                    commands.add(getForceMoveCommand((short) 1));
                    break;
                case 7://强制上移
                    commands.add(getForceMoveCommand((short) 3));
                    break;
                case 8://强制下移
                    commands.add(getForceMoveCommand((short) (assignCommand.getTaskMode() - 4)));
                    commands.add(getForceMoveCommand((short) 4));
                    break;
                case 9://状态复位
                    ShuttleCommand reset = getResetCommand();
                    commands.add(reset);
                    break;
                case 10://向正方向(左)寻库位
                case 10://正方向(右)寻库位
                    commands.add(getFindLocCommand((short) 1));
                    break;
                case 11://向负方向(右)寻库位
                case 11://负方向(左)寻库位
                    commands.add(getFindLocCommand((short) 2));
                    break;
                case 12://向正方向(前)寻库位
@@ -489,13 +529,28 @@
                    commands.add(getFindLocCommand((short) 3));
                    break;
                case 14://移动到目标库位
                    LocMast locMast = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString());
                    List<NavigateNode> result = NavigateUtils.calc(locMast.getLocNo(), assignCommand.getLocNo(), ShuttleTaskModeType.PAK_IN.id);
                    //所使用的路径进行锁定禁用
                    navigateMapData = new NavigateMapData(Utils.getLev(locMast.getLocNo()));
                    navigateMapData.writeNavigateNodeToRedisMap(result, true);////所使用的路径进行锁定禁用
                    String startQr = shuttleProtocol.getCurrentCode().toString();//起始位置
                    //如果穿梭车在提升机内,移动时需要先下发出提升机命令
                    if (liftProtocol.getBarcode().intValue() == shuttleProtocol.getCurrentCode().intValue()) {
                        //穿梭车出提升机
                        Short liftArrival = liftProtocol.getPositionArrivalFeedback();//提升机位置反馈
                        String liftSiteLocNo = Utils.liftArrivalToOutInStaLocNo(liftArrival);
                        LocMast locMast1 = locMastService.selectById(liftSiteLocNo);
                        ShuttleCommand moveCommand = getMoveCommand(liftProtocol.getBarcode(), Short.parseShort(locMast1.getQrCodeValue()), 1400, ShuttleRunDirection.BOTTOM.id, liftProtocol.getBarcode(), 1400, runSpeed);
                        commands.add(moveCommand);
                        //起始位置修改为提升机口站点位置
                        startQr = locMast1.getQrCodeValue();
                    }
                    LocMast locMast = locMastService.queryByQrCode(startQr);
                    List<NavigateNode> result = NavigateUtils.calc(locMast.getLocNo(), assignCommand.getLocNo(), NavigationMapType.NONE.id);//手动命令-移动命令,使用无过滤地图
                    if (result != null) {
                        //所使用的路径进行锁定禁用
                        navigateMapData = new NavigateMapData(Utils.getLev(locMast.getLocNo()));
                        navigateMapData.writeNavigateNodeToRedisMap(result, true);////所使用的路径进行锁定禁用
                        allNode.addAll(result);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(result);
@@ -503,20 +558,66 @@
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            LocMast distLocMast = locMastService.queryByLoc(assignCommand.getLocNo());
                            String qrCodeValue = distLocMast.getQrCodeValue();
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes);//计算中间点到目标点行走距离
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            //正常移动命令
                            commands.add(getMoveCommand(shuttleProtocol.getCurrentCode(),(short) Integer.parseInt(qrCodeValue),allDistance,ShuttleRunDirection.get(startPath.getDirection()).id));
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                    }
                    break;
                case 15://充电开关
                    commands.add(getChargeSwitchCommand());
                    break;
                case 16://移动到提升机
                    LocMast locMast1 = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString());
                    int lev = Utils.getLev(locMast1.getLocNo());//穿梭车当前高度
                    String liftSiteLocNo = Utils.levToOutInStaLocNo(lev);//当前楼层站点库位号
                    LocMast liftSitelocMast = locMastService.selectById(liftSiteLocNo);
                    List<NavigateNode> result1 = NavigateUtils.calc(locMast1.getLocNo(), liftSiteLocNo, NavigationMapType.NONE.id);//移动到提升机,使用无过滤地图
                    Short endStartCode = null;
                    if (result1 != null) {
                        //所使用的路径进行锁定禁用
                        navigateMapData = new NavigateMapData(Utils.getLev(locMast1.getLocNo()));
                        navigateMapData.writeNavigateNodeToRedisMap(result1, true);////所使用的路径进行锁定禁用
                        allNode.addAll(result1);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(result1);
                        //将每一段路径分成command指令
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes);//计算中间点到目标点行走距离
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            endStartCode = distCode;
                            //正常移动命令
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                    }
                    if (endStartCode == null && shuttleProtocol.getCurrentCode() == Short.parseShort(liftSitelocMast.getQrCodeValue())) {
                        //穿梭车已经在提升机站点口
                        endStartCode = shuttleProtocol.getCurrentCode();
                    }
                    //增加移动进提升机命令
                    ShuttleCommand moveCommand = getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1400, ShuttleRunDirection.TOP.id, endStartCode, 1400, runSpeed);
                    commands.add(moveCommand);
                    break;
                default:
            }
@@ -524,7 +625,6 @@
            assignCommand.setNodes(allNode);//当前任务所占用的节点list
        }
        ShuttleRedisCommand redisCommand = new ShuttleRedisCommand();
        redisCommand.setShuttleNo(assignCommand.getShuttleNo());//四向穿梭车号
        redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号
        redisCommand.setCommandStep(0);//命令执行步序
@@ -556,9 +656,6 @@
            return false;
        }
        //将标记置为false(防止重发)
        shuttleProtocol.setPakMk(false);
        List<ShuttleCommand> errorCommands = redisCommand.getErrorCommands();
        if (errorCommands.size() > 0) {
            //优先执行该指令
@@ -583,7 +680,7 @@
                    shuttleProtocol.setPakMk(true);
                    return true;
                }else {
                    List<NavigateNode> result = NavigateUtils.calc(locMast.getLocNo(), distLocMast.getLocNo(), ShuttleTaskModeType.PAK_IN.id);
                    List<NavigateNode> result = NavigateUtils.calc(locMast.getLocNo(), distLocMast.getLocNo(), NavigationMapType.DFX.id);//错误恢复,使用DFX地图
                    if (result != null) {
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(result);
@@ -639,6 +736,63 @@
        int size = commands.size();
        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
        if (commandStep != 0) {
            //判断上一条指令是否完成
            ShuttleCommand command = commands.get(commandStep - 1);
            if (command.getCommandWord().intValue() == 1) {
                //移动命令
                if (command.getDistCodeNum().intValue() == shuttleProtocol.getCurrentCode().intValue()) {
                    //上一条指令的目标位置和当前小车位置相同,则认定上一条任务完成
                    command.setComplete(true);
                }
            } else if (command.getCommandWord().intValue() == 2) {
                //托盘顶升命令
                if (command.getPalletLift().intValue() == 1) {
                    //顶升
                    //判断是否顶升到位
                    if (shuttleProtocol.getPlcOutputLift()) {
                        //自动模式
                        if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 1) {
                            //顶升到位,且托盘雷达有物,认定任务完成
                            command.setComplete(true);
                        }else {
                            //手动模式,不判断托盘雷达
                            //顶升到位,认定任务完成
                            command.setComplete(true);
                        }
                    }
                }else {
                    //下降
                    //判断是否下降到位,判断托盘雷达是否无物
                    if (!shuttleProtocol.getPlcOutputLift() && !shuttleProtocol.getPlcOutputTransfer()) {
                        //自动模式
                        if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 0) {
                            //下降到位,且托盘雷达无物,认定任务完成
                            command.setComplete(true);
                        }else {
                            //手动模式,不判断托盘雷达
                            //下降到位,且托盘雷达无物,认定任务完成
                            command.setComplete(true);
                        }
                    }
                }
            } else if (command.getCommandWord().intValue() == 5) {
                //充电命令
                //判断小车充电开关
                if (shuttleProtocol.getPlcOutputCharge()) {
                    //正常充电,认定任务完成
                    command.setComplete(true);
                }
            }
            //任务数据保存到redis
            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            if (!command.getComplete()) {
                //上一条任务未完成,禁止下发命令
                return false;
            }
        }
        //取出命令
        ShuttleCommand command = commands.get(commandStep);
@@ -651,12 +805,75 @@
            }
        }
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
        LiftProtocol liftProtocol = liftThread.getLiftProtocol();
        //判断小车当前二维码是否为提升机二维码
        if (shuttleProtocol.getCurrentCode().intValue() == liftProtocol.getBarcode().intValue()) {
            //小车当前命令起始位置就是提升机二维码,说明小车需要向提升机外移动,则需要判断状态是否满足
            if (command.getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()){
                //提升机是否空闲,提升机是否到达目标楼层,目标楼层是否给出提升机到位信号位
                if (!liftProtocol.isIdleNoTask()) {
                    return false;//提升机忙,禁止下发命令
                }
                if (liftProtocol.getTaskNo().intValue() != wrkNo) {
                    //提升机工作号和当前工作不相同,禁止下发命令
                    return false;
                }
                Short distCodeNum = command.getDistCodeNum();//目标二维码
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                BasDevp basDevp = basDevpService.queryByQrCode(distCodeNum.intValue());//目标站点
                if (basDevp == null) {
                    return false;//找不到目标站,禁止下发命令
                }
                int lev = Utils.getLev(basDevp.getLocNo());//目标二维码所在楼层
                int liftLev = liftProtocol.getLev().intValue();//提升机所在楼层
                if (liftLev != lev) {
                    return false;//提升机不在目标楼层,禁止下发命令
                }
                //获取目标站信息
                SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                StaProtocol staProtocol = devpThread.getStation().get(basDevp.getDevNo());
                if (staProtocol == null) {
                    return false;//站点信息不存在,禁止下发命令
                }
                if (!staProtocol.isLiftArrival()) {
                    return false;//站点提升机到位信号false,禁止下发命令
                }
            }
        }
        //下发命令
        if (!write(command)) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            return false;
        } else {
            News.info("四向穿梭车命令下发成功,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            //将标记置为false(防止重发)
            shuttleProtocol.setPakMk(false);
            //保存数据到数据库做流水
            BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class);
            if (shuttleOptService != null) {
                BasShuttleOpt opt = new BasShuttleOpt(
                        assignCommand.getTaskNo().intValue(),
                        assignCommand.getShuttleNo().intValue(),
                        new Date(),
                        ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                        assignCommand.getSourceLocNo(),
                        assignCommand.getLocNo(),
                        null,
                        null,
                        null,
                        JSON.toJSONString(command)
                );
                shuttleOptService.insert(opt);
            }
            //判断数据是否执行完成
            if (commandStep < size - 1) {
@@ -668,37 +885,20 @@
                redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            }else {
                //已执行完成
                //保存数据到数据库做流水
                BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class);
                if (shuttleOptService != null) {
                    BasShuttleOpt opt = new BasShuttleOpt(
                            assignCommand.getTaskNo().intValue(),
                            assignCommand.getShuttleNo().intValue(),
                            new Date(),
                            ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                            assignCommand.getSourceLocNo(),
                            assignCommand.getLocNo(),
                            null,
                            null,
                            null,
                            JSON.toJSONString(assignCommand)
                    );
                    shuttleOptService.insert(opt);
                }
                if (redisCommand.getLiftSecurityMk()) {
                    //曾锁定过提升机,需要进行解锁
                    LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
                    LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                    if (liftProtocol != null) {
                        liftProtocol.setSecurityMk(false);
                    }
                }
                String locNo = shuttleProtocol.getLocNo() == null ? shuttleProtocol.getSourceLocNo() : shuttleProtocol.getLocNo();
                //解除锁定的库位路径
                NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(locNo));
                navigateMapData.writeNavigateNodeToRedisMap(redisCommand.getAssignCommand().getNodes(), false);
                if (locNo != null) {
                    //解除锁定的库位路径
                    NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(locNo));
                    navigateMapData.writeNavigateNodeToRedisMap(redisCommand.getAssignCommand().getNodes(), false);
                }
                //删除redis
                redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo());
@@ -719,7 +919,7 @@
                    }else {
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
                    }
                    News.info("四向穿梭车任务执行完成等待确认中,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                    News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }
            }
@@ -737,46 +937,6 @@
            return false;
        }
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
        //当前步序
        int commandStep = redisCommand.getCommandStep();
        //检测是否存在提升机口的指令
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
        ArrayList<Short> qrCodeValues = new ArrayList<>();
        for (BasDevp basDevp : basDevpService.selectList(null)) {
            //将所有提升机口二维码存入list
            qrCodeValues.add(Short.parseShort(basDevp.getQrCodeValue()));
        }
        //遍历所有指令,判断是否有到提升机口的指令,并获取到达该提升机口所需步序
        int step = 0;
        ShuttleCommand command = null;
        for (int i = 1; i < commands.size(); i++) {
            command = commands.get(i);
            if (qrCodeValues.contains(command.getDistCodeNum())) {
                //存在
                step = i + 1;
                break;
            }
        }
        if (step == 0) {
            //无需后续检测,直接放行
            return true;
        }
        //判断下一步是否为提升机口
        if (commandStep + 1 != step) {
            //下一步不是提升机口,跳过后续流程
            return true;
        }
        //拿到提升机线程
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
        if (liftThread == null) {
@@ -787,55 +947,138 @@
            return false;
        }
        //获取四向穿梭车当前楼层
        String shuttleLocNo = shuttleProtocol.getCurrentLocNo();//二维码对应库位号
        Integer shuttleLocNoLev = Integer.parseInt(shuttleLocNo.substring(shuttleLocNo.length() - 2, shuttleLocNo.length()));//库位号对应层高
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
        //当前步序
        int commandStep = redisCommand.getCommandStep();
        //程序走到这,表示提升机可能一直就在当前层,可能经过了移动到达了该层
        if (liftProtocol.getProtocolStatusType() == LiftProtocolStatusType.WAITING) {
            //提升机等待确认
            //设置提升机为空闲状态
            liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
            //任务号清零
            liftProtocol.setTaskNo((short) 0);
            //标记复位
            liftProtocol.setPakMk(true);
            //任务指令清零
            liftProtocol.setAssignCommand(null);
            //提升机解锁
            liftProtocol.setLiftLock(false);
        //检测是否存在提升机口的指令
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        if (commands.size() > 0) {
            if (commands.get(commandStep).getCommandWord() != 1) {
                //不是行走命令,直接放行
                return true;
            }
            if (commands.get(0).getStartCodeNum() == null) {
                return false;
            }
            //命令起始位置就是提升机二维码,则不进行校验
            if (commands.get(0).getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()) {
                return true;
            }
        }
        //判断提升机是否在目标楼层
        if (liftProtocol.getLev().intValue() == shuttleLocNoLev) {
            //同一层,直接放行
        BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
        ArrayList<Short> qrCodeValues = new ArrayList<>();
        for (BasDevp basDevp : basDevpService.selectList(null)) {
            //将所有提升机口二维码存入list
            qrCodeValues.add(Short.parseShort(basDevp.getQrCodeValue()));
        }
        //遍历所有指令,判断是否有到提升机口的指令或从提升机口前往提升机内的指令,并获取到达该提升机口所需步序
        int step = 0;
        Integer siteNo = null;//站点号
        ShuttleCommand command = null;
        for (int i = 0; i < commands.size(); i++) {
            command = commands.get(i);
            for (Short qrCodeValue : qrCodeValues) {
                //目标位置是提升机口,或起点位置是提升机口且目标是去提升机内
                if (command.getDistCodeNum() == null || command.getStartCodeNum() == null) {
                    continue;
                }
                if (qrCodeValue.intValue() == command.getDistCodeNum().intValue() || (qrCodeValue.intValue() == command.getStartCodeNum().intValue() && command.getDistCodeNum().intValue() == liftProtocol.getBarcode().intValue())) {
                    //存在
                    step = i + 1;
                    BasDevp basDevp = basDevpService.queryByQrCode(qrCodeValue.intValue());
                    siteNo = basDevp.getDevNo();
                    break;
                }
            }
        }
        if (step == 0) {
            //无需后续检测,直接放行
            return true;
        }
        //提升机和穿梭车处于不同楼层,需要进行调度
        if (!liftProtocol.isIdle()) {
            //提升机不是空闲
        //判断下一步是否为提升机口或提升机内
        if (commandStep < commands.size()) {
            ShuttleCommand command1 = commands.get(commandStep);
            Short distCodeNum = command1.getDistCodeNum();
            if (distCodeNum != null) {
                BasDevp basDevp = basDevpService.queryByQrCode(Integer.valueOf(distCodeNum));
                if (basDevp == null && distCodeNum.intValue() != liftProtocol.getBarcode().intValue()) {
                    return true;//下一步不是提升机口,跳过后续流程
                }
            }
        }
//        if (commandStep + 1 != step) {
//            //下一步不是提升机口,跳过后续流程
//            return true;
//        }
        //获取四向穿梭车当前楼层
        String shuttleLocNo = shuttleProtocol.getCurrentLocNo();//二维码对应库位号
        Integer shuttleLocNoLev = shuttleLocNo == null ? 0 : Utils.getLev(shuttleLocNo);//库位号对应层高
        //判断穿梭车和提升机是否在目标楼层
        if (shuttleLocNoLev >= 2) {
            shuttleLocNoLev++;
        }
        //判断输送线站点是否给出提升机到位信号
        if (siteNo != null) {
            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
            StaProtocol staProtocol = siemensDevpThread.getStation().get(siteNo);
            if (!staProtocol.isLiftArrival()) {
                //输送线反馈提升机没有到位
                executeLift(liftThread, liftProtocol, redisCommand, shuttleLocNoLev);//调度提升机
                return false;
            }
            if (shuttleProtocol.getCurrentCode().intValue() == liftProtocol.getBarcode().intValue()) {
                //小车处于提升机内
                return true;
            }else {
                if (liftProtocol.getPositionArrivalFeedback$() == shuttleLocNoLev) {
                    return true;//提升机到位
                }
                executeLift(liftThread, liftProtocol, redisCommand, shuttleLocNoLev);//调度提升机
                return false;//提升机没有到位
            }
        }
        return false;
    }
    private boolean executeLift(LiftThread liftThread, LiftProtocol liftProtocol, ShuttleRedisCommand redisCommand, Integer shuttleLocNoLev) {//调度提升机
        if (liftProtocol.getRunning()) {
            //提升机运行中,禁止下发
            return false;
        }
        if (redisCommand.getLiftSecurityMk()) {
            //已经执行过提升机命令,禁止下发
            return false;
        }
        //给提升机分配任务
        liftProtocol.setLiftLock(true);//锁定提升机
        liftProtocol.setTaskNo(shuttleProtocol.getTaskNo());//设置任务号
        liftProtocol.setShuttleNo(shuttleProtocol.getShuttleNo());//设置四向穿梭车号
        liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
        liftProtocol.setSecurityMk(true);//标记置为true,防止其他任务占用当前提升机
        redisCommand.setLiftSecurityMk(true);//标记置为true,防止其他任务占用当前提升机
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + wrkNo, JSON.toJSONString(redisCommand));
        redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
        //命令list
        ArrayList<LiftCommand> liftCommands = new ArrayList<>();
        LiftCommand liftCommand = new LiftCommand();
        liftCommand.setLiftNo(liftProtocol.getLiftNo());//提升机号
        liftCommand.setTaskNo(liftProtocol.getTaskNo());//任务号
        liftCommand.setRun((short) 1);//升降
        liftCommand.setDistPosition(shuttleLocNoLev.shortValue());//目标楼层(穿梭车所在楼层)
        liftCommand.setLiftLock(true);//锁定提升机
        LiftCommand liftCommand = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), shuttleLocNoLev);
        liftCommands.add(liftCommand);//将命令添加进list
        LiftAssignCommand liftAssignCommand = new LiftAssignCommand();
@@ -844,8 +1087,7 @@
        liftAssignCommand.setTaskNo(liftProtocol.getTaskNo());
        //下发任务
        MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, liftAssignCommand));
        return false;
        return true;
    }
    /**
@@ -935,20 +1177,27 @@
    }
    /**
     * 正常移动命令
     * @return
     * 正常移动命令,默认移动速度1000
     */
    public ShuttleCommand getMoveCommand(Short startCodeNum, Short distCodeNum, Integer startToDistDistance, Short runDirection) {
    public ShuttleCommand getMoveCommand(Short startCodeNum, Short distCodeNum, Integer startToDistDistance, Short runDirection, Short middleCodeNum, Integer middleToDistDistance) {
        return getMoveCommand(startCodeNum, distCodeNum, startToDistDistance, runDirection, middleCodeNum, middleToDistDistance, 500);
    }
    /**
     * 正常移动命令
     */
    public ShuttleCommand getMoveCommand(Short startCodeNum, Short distCodeNum, Integer startToDistDistance, Short runDirection, Short middleCodeNum, Integer middleToDistDistance, Integer runSpeed) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 1);
        command.setStartCodeNum(startCodeNum);
        command.setMiddleCodeNum((short) 1);
        command.setMiddleCodeNum(middleCodeNum);
        command.setDistCodeNum(distCodeNum);
        command.setStartToDistDistance(startToDistDistance);
        command.setMiddleToDistDistance(middleToDistDistance);
        command.setRunDirection(runDirection);
        command.setForceMoveDistance(0);
        command.setIOControl((short) 0);
        command.setRunSpeed((short) 1000);
        command.setRunSpeed(runSpeed.shortValue());
        command.setCommandEnd((short) 1);
        return command;
    }
@@ -1021,8 +1270,8 @@
     */
    public ShuttleCommand getChargeSwitchCommand() {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 8);
        command.setIOControl((short) 4);
        command.setCommandWord((short) 5);
        command.setChargeSwitch((short) 1);
        command.setCommandEnd((short) 1);
        return command;
    }