自动化立体仓库 - WCS系统
Junjie
2023-06-10 e1783e196a4ada918268b9f81bfb3a19641e0626
src/main/java/com/zy/core/thread/LiftThread.java
@@ -5,18 +5,13 @@
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.ModBus.ModbusTcpNet;
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasLift;
import com.zy.asrs.entity.BasLiftOpt;
import com.zy.asrs.entity.BasShuttle;
import com.zy.asrs.entity.BasShuttleOpt;
import com.zy.asrs.service.BasLiftOptService;
import com.zy.asrs.service.BasLiftService;
import com.zy.asrs.service.BasShuttleOptService;
import com.zy.asrs.service.BasShuttleService;
import com.zy.common.utils.CommonUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
@@ -26,10 +21,7 @@
import com.zy.core.enums.*;
import com.zy.core.model.LiftSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.LiftAssignCommand;
import com.zy.core.model.command.LiftCommand;
import com.zy.core.model.command.ShuttleAssignCommand;
import com.zy.core.model.command.ShuttleCommand;
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.LiftProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -37,7 +29,6 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
@@ -118,7 +109,7 @@
    private void readStatus() {
        try {
            OperateResultExOne<byte[]> result = modbusTcpNet.Read("29", (short) 50);
            OperateResultExOne<byte[]> result = modbusTcpNet.Read("41117", (short) 50);
            if (result.IsSuccess) {
                if (null == liftProtocol) {
                    liftProtocol = new LiftProtocol();
@@ -132,46 +123,49 @@
                liftProtocol.setLiftLock(CommonUtils.shortToBoolean(modbusTcpNet.getByteTransform().TransInt16(content, 0)));
                //位置到达反馈
                liftProtocol.setPositionArrivalFeedback(modbusTcpNet.getByteTransform().TransInt16(content,2));
                //准备就绪
                liftProtocol.setReady(CommonUtils.shortToBoolean(modbusTcpNet.getByteTransform().TransInt16(content,4)));
                //运行中
                liftProtocol.setRunning(CommonUtils.shortToBoolean(modbusTcpNet.getByteTransform().TransInt16(content,6)));
                //联机/单机
                liftProtocol.setMode(CommonUtils.shortToBoolean(modbusTcpNet.getByteTransform().TransInt16(content,8)));
                //以下参数并未进行调整(需要后续针对实机进行获取和调试)
                byte by = modbusTcpNet.getByteTransform().TransByte(content, 5);
                int[] datas = CommonUtils.byteToBits(by);
                //准备就绪
                liftProtocol.setReady(CommonUtils.intToBoolean(datas[0]));
                //运行中
                liftProtocol.setRunning(CommonUtils.intToBoolean(datas[1]));
                //联机/单机
                liftProtocol.setMode(CommonUtils.intToBoolean(datas[2]));
                //输送线前端光电有货
                liftProtocol.setLineFrontHasStock(modbusTcpNet.getByteTransform().TransBool(content,10));
                liftProtocol.setLineFrontHasStock(CommonUtils.intToBoolean(datas[3]));
                //输送线正转反馈
                liftProtocol.setForwardRotationFeedback(modbusTcpNet.getByteTransform().TransBool(content,12));
                liftProtocol.setForwardRotationFeedback(CommonUtils.intToBoolean(datas[4]));
                //输送线反转反馈
                liftProtocol.setReverseFeedback(modbusTcpNet.getByteTransform().TransBool(content,14));
                liftProtocol.setReverseFeedback(CommonUtils.intToBoolean(datas[5]));
                //输送线电机过载
                liftProtocol.setMotorOverload(modbusTcpNet.getByteTransform().TransBool(content,16));
                liftProtocol.setMotorOverload(CommonUtils.intToBoolean(datas[6]));
                //输送线末端光电有货
                liftProtocol.setLineEndHasStock(modbusTcpNet.getByteTransform().TransBool(content,18));
                liftProtocol.setLineEndHasStock(CommonUtils.intToBoolean(datas[7]));
                //进输送线卡托盘报警
                liftProtocol.setInConveyLineCardTrayAlarm(modbusTcpNet.getByteTransform().TransBool(content,20));
                liftProtocol.setInConveyLineCardTrayAlarm(CommonUtils.intToBoolean(datas[8]));
                //出输送线卡托盘报警
                liftProtocol.setOutConveyLineCardTrayAlarm(modbusTcpNet.getByteTransform().TransBool(content,22));
                liftProtocol.setOutConveyLineCardTrayAlarm(CommonUtils.intToBoolean(datas[9]));
                //平台位置偏差报警
                liftProtocol.setPlatPositionDeviationAlarm(modbusTcpNet.getByteTransform().TransBool(content,24));
                liftProtocol.setPlatPositionDeviationAlarm(CommonUtils.intToBoolean(datas[10]));
                //平台扭矩偏差报警
                liftProtocol.setPlatTorqueDeviationAlarm(modbusTcpNet.getByteTransform().TransBool(content,26));
                liftProtocol.setPlatTorqueDeviationAlarm(CommonUtils.intToBoolean(datas[11]));
                //平台四向车检测
                liftProtocol.setPlatShuttleCheck(modbusTcpNet.getByteTransform().TransBool(content,28));
                liftProtocol.setPlatShuttleCheck(CommonUtils.intToBoolean(datas[12]));
                //未就绪状态
                liftProtocol.setNotReady(modbusTcpNet.getByteTransform().TransInt16(content,30));
                liftProtocol.setNotReady(modbusTcpNet.getByteTransform().TransInt16(content,6));
                //伺服1错误
                liftProtocol.setServoError1(modbusTcpNet.getByteTransform().TransInt16(content,32));
                liftProtocol.setServoError1(modbusTcpNet.getByteTransform().TransInt16(content,8));
                //伺服2错误
                liftProtocol.setServoError2(modbusTcpNet.getByteTransform().TransInt16(content,34));
                liftProtocol.setServoError2(modbusTcpNet.getByteTransform().TransInt16(content,10));
                //伺服3错误
                liftProtocol.setServoError3(modbusTcpNet.getByteTransform().TransInt16(content,36));
                liftProtocol.setServoError3(modbusTcpNet.getByteTransform().TransInt16(content,12));
                //伺服4错误
                liftProtocol.setServoError4(modbusTcpNet.getByteTransform().TransInt16(content,38));
                liftProtocol.setServoError4(modbusTcpNet.getByteTransform().TransInt16(content,14));
                //提升机实际速度反馈
                liftProtocol.setLiftActualSpeed(modbusTcpNet.getByteTransform().TransInt16(content,40));
                liftProtocol.setLiftActualSpeed(modbusTcpNet.getByteTransform().TransInt16(content,16));
                ///读取提升机状态-end
@@ -185,10 +179,9 @@
                        && !liftProtocol.getRunning()
                        && liftProtocol.getReady()
                        && liftProtocol.getPakMk()
                        && liftProtocol.getTaskNo() != 0
                        && liftProtocol.getAssignCommand() != null) {
                        && liftProtocol.getTaskNo() != 0) {
                    //还有未完成的命令
                    executeWork(liftProtocol.getAssignCommand());
                    executeWork(liftProtocol.getTaskNo());
                }
                //将提升机状态保存至数据库
@@ -271,12 +264,16 @@
            //四层高度设定
            array[5] = command.getHeight4();
        }
        if (command.getHeight5() != null) {
            //四层高度设定
            array[6] = command.getHeight5();
        }
        if (command.getLiftLockShortValue() != null) {
            //提升机锁定
            array[29] = command.getLiftLockShortValue();
        }
        OperateResult result = modbusTcpNet.Write("0", array);;
        OperateResult result = modbusTcpNet.Write("41088", array);;
        if (result != null && result.IsSuccess) {
            News.info("提升机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
@@ -290,9 +287,6 @@
    //分配任务
    private void assignWork(LiftAssignCommand assignCommand) {
        //将此map存入redis中
        HashMap<String, Object> map = new HashMap<>();
        //手动模式指令
        if (!assignCommand.getAuto()) {
            LiftCommand command = new LiftCommand();
@@ -304,23 +298,26 @@
            }
            liftProtocol.setTaskNo(assignCommand.getTaskNo());
            switch (assignCommand.getTaskMode()) {
                case 1://上升一层
                    command.setRun((short) 1);//升降
                    command.setDistPosition(++lev);
                case 1://移动提升机
                    command = getLiftUpDownCommand(assignCommand.getLev());//获取提升机上升下降命令
                    break;
                case 2://下降一层
                    command.setRun((short) 1);//下降
                    command.setDistPosition(--lev);
                case 3://无货正转
                    command = getLiftTurnCommand(3);//获取提升机转动命令
                    break;
                case 3://有货正转
                    command.setRun((short) 6);
                case 4://无货反转
                    command = getLiftTurnCommand(4);//获取提升机转动命令
                    break;
                case 4://有货反转
                    command.setRun((short) 3);
                case 5://有货正转
                    command = getLiftTurnCommand(1);//获取提升机转动命令
                    break;
                case 5://复位
                    command.setRun((short) 0);
                    command.setLiftLock(false);
                case 6://有货反转
                    command = getLiftTurnCommand(2);//获取提升机转动命令
                    break;
                case 7://链条停止
                    command = getLiftTurnCommand(5);//获取提升机转动命令
                    break;
                case 0://复位
                    command = getResetCommand();//获取复位命令
                    break;
                default:
            }
@@ -328,42 +325,40 @@
            assignCommand.setCommands(commands);
        }
        //提升机号
        map.put("lift_no", assignCommand.getLiftNo());
        //工作号
        map.put("wrk_no", assignCommand.getTaskNo());
        //命令执行步序
        map.put("commandStep", 0);
        //命令
        map.put("assignCommand", assignCommand);
        LiftRedisCommand redisCommand = new LiftRedisCommand();
        redisCommand.setLiftNo(assignCommand.getLiftNo());//提升机号
        redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号
        redisCommand.setCommandStep(0);//命令执行步序
        redisCommand.setAssignCommand(assignCommand);//命令
        //任务数据保存到redis
        redisUtil.set("lift_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(map));
        redisUtil.set("lift_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand));
        liftProtocol.setAssignCommand(assignCommand);
        liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);
        //执行下发任务
        executeWork(assignCommand);
        executeWork(assignCommand.getTaskNo());
    }
    //执行任务
    private boolean executeWork(LiftAssignCommand assignCommand) {
    private boolean executeWork(Short wrkNo) {
        //读取redis数据
        if (assignCommand == null) {
        if (wrkNo == null) {
            return false;
        }
        //将标记置为false(防止重发)
        liftProtocol.setPakMk(false);
        Object o = redisUtil.get("lift_wrk_no_" + assignCommand.getTaskNo());
        Object o = redisUtil.get("lift_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
        HashMap map = JSON.parseObject(o.toString(), HashMap.class);
        List<LiftCommand> commands = assignCommand.getCommands();
        LiftRedisCommand redisCommand = JSON.parseObject(o.toString(), LiftRedisCommand.class);
        List<LiftCommand> commands = redisCommand.getAssignCommand().getCommands();
        //当前步序
        int commandStep = Integer.parseInt(map.get("commandStep").toString());
        int commandStep = redisCommand.getCommandStep();
        //总步序
        int size = commands.size();
        LiftAssignCommand assignCommand = redisCommand.getAssignCommand();
        //取出命令
        LiftCommand command = commands.get(commandStep);
@@ -379,17 +374,17 @@
                //更新redis数据
                //步序增加
                commandStep++;
                map.put("commandStep", commandStep);
                redisCommand.setCommandStep(commandStep);
                //任务数据保存到redis
                redisUtil.set("lift_wrk_no_" + map.get("wrk_no").toString(), JSON.toJSONString(map));
                redisUtil.set("lift_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            }else {
                //已执行完成
                //保存数据到数据库做流水
                BasLiftOptService liftOptService = SpringUtils.getBean(BasLiftOptService.class);
                if (liftOptService != null) {
                    BasLiftOpt opt = new BasLiftOpt(
                            assignCommand.getTaskNo().intValue(),
                            assignCommand.getLiftNo().intValue(),
                            redisCommand.getWrkNo().intValue(),
                            redisCommand.getLiftNo().intValue(),
                            new Date(),
                            null,
                            null,
@@ -399,7 +394,7 @@
                    liftOptService.insert(opt);
                }
                //删除redis
                redisUtil.del("lift_wrk_no_" + map.get("wrk_no").toString());
                redisUtil.del("lift_wrk_no_" + redisCommand.getWrkNo());
                if (assignCommand.getAuto()) {
                    //对主线程抛出等待确认状态waiting
@@ -407,23 +402,12 @@
                    News.info("提升机任务执行完成等待确认中,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
                }else {
                    //手动模式不抛出等待状态
                    if (assignCommand.getTaskMode() == 5) {
                        liftProtocol.setTaskNo((short) 0);
                        liftProtocol.setShuttleNo((short) 0);
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        liftProtocol.setPakMk(true);
                        liftProtocol.setSecurityMk(false);
                    }
                    liftProtocol.setTaskNo((short) 0);
                    liftProtocol.setShuttleNo((short) 0);
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                    liftProtocol.setPakMk(true);
                    liftProtocol.setSecurityMk(false);
                    News.info("提升机手动任务执行完成,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
                }
                //下发一条提升机解锁命令
                LiftCommand liftCommand = new LiftCommand();
                liftCommand.setRun((short) 0);
                liftCommand.setLiftNo(command.getLiftNo());
                liftCommand.setLiftLock(false);
                if (write(liftCommand)) {
                    News.info("提升机状态已解锁,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
                }
            }
@@ -433,6 +417,128 @@
    }
    /**
     * 获取提升机解锁命令
     */
    public LiftCommand getUnlockCommand(Short liftNo) {
        LiftCommand command = new LiftCommand();
        command.setRun((short) 0);
        command.setLiftNo(liftNo);
        command.setLiftLock(false);
        return command;
    }
    /**
     * 获取复位命令
     */
    public LiftCommand getResetCommand() {
        LiftCommand command = new LiftCommand();
        command.setRun((short) 5);
        command.setLiftLock(false);
        return command;
    }
    /**
     * 获取提升机上升下降命令
     */
    public LiftCommand getLiftUpDownCommand(Integer lev) {
        LiftCommand command = new LiftCommand();
        command.setRun((short) 1);//升降
        Short position = 1;
        switch (lev) {
            case 1:
                position = 1;
                break;
            case 2:
                position = 2;
                break;
            case 3:
                position = 4;
                break;
            case 4:
                position = 8;
                break;
            case 5:
                position = 16;
                break;
            default:
                position = 1;
        }
        command.setDistPosition(position);
        command.setSpeed((short) 200);
        command.setHeight2((short) 180);
        command.setHeight3((short) 1645);
        command.setHeight4((short) (2 * 1645));
        command.setHeight5((short) (3 * 1645));
        return command;
    }
    /**
     * 获取提升机上升下降命令
     */
    public LiftCommand getLiftUpDownCommand(Short liftNo, Short taskNo, Short lev) {
        LiftCommand command = new LiftCommand();
        command.setRun((short) 1);//升降
        command.setLiftNo(liftNo);//提升机号
        command.setTaskNo(taskNo);//任务号
        command.setDistPosition(lev);//目标楼层1层
        return command;
    }
    /**
     * 获取提升机转动命令,direction:1=》有货正转,2=》有货反转,3=>无货正转,4=>无货反转
     */
    public LiftCommand getLiftTurnCommand(Integer direction) {
        LiftCommand command = new LiftCommand();
        Short run = 0;
        switch (direction) {
            case 1:
                run = 6;//有货正转
                break;
            case 2:
                run = 3;//有货反转
                break;
            case 3:
                run = 2;//无货正转
                break;
            case 4:
                run = 7;//无货反转
                break;
            case 5:
                run = 4;//链条停止
        }
        command.setRun(run);
        return command;
    }
    /**
     * 获取提升机转动命令,direction:1=》有货正转,2=》有货反转
     */
    public LiftCommand getLiftTurnCommand(Short liftNo, Short taskNo, Integer direction) {
        LiftCommand command = new LiftCommand();
        Short run = 0;
        switch (direction) {
            case 1:
                run = 6;//有货正转
                break;
            case 2:
                run = 3;//有货反转
                break;
            case 3:
                run = 2;//无货正转
                break;
            case 4:
                run = 7;//无货反转
                break;
            case 5:
                run = 4;//链条停止
        }
        command.setRun(run);
        command.setLiftNo(liftNo);//提升机号
        command.setTaskNo(taskNo);//任务号
        return command;
    }
    /**
     * 初始化提升机
     */
    private void initLift() {