pang.jiabao
2024-06-22 62af9acaccedaea4c4c7efd4e68c8b702cf5a27d
src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -5,7 +5,6 @@
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnOpt;
@@ -16,7 +15,9 @@
import com.zy.core.CrnThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.*;
import com.zy.core.enums.CrnStatusType;
import com.zy.core.enums.CrnTaskModeType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.CrnSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.CrnCommand;
@@ -39,6 +40,15 @@
    private CrnSlave slave;
    private CrnProtocol crnProtocol;
    private boolean resetFlag = false;
    /**
     * 20230711 Add,判断是否允许下发堆垛机任务的命令标记,默认为true
     * 为了防止时间差导致的WCS连续下发多笔任务,WCS下发任务后,主线程轮询时还未读到堆垛机非空闲、有任务状态时,可能会重复下发
     * 主线程下发堆垛机任务的同时,cmdFlag置为false,当堆垛机处于等待确认状态并且resetFlag为true时,才复位cmdFlag为true
     * 所有堆垛机任务下发前,先判断cmdFlag为true后才能下发
     * 改动前,是通过判断工作档是否存在堆垛机执行中工作状态判断的,如果不生成工作档,或者是有充电、移车等任务漏判断了也会存在问题
     */
    private boolean cmdFlag = true;
    /**
     * 堆垛机是否在回原点运动中标记
@@ -149,7 +159,7 @@
     */
    private void readStatus(){
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56);
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 54);
            if (result.IsSuccess) {
                if (null == crnProtocol) {
                    crnProtocol = new CrnProtocol();
@@ -165,30 +175,55 @@
                crnProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 14));
                crnProtocol.setLoaded(siemensNet.getByteTransform().TransInt16(result.Content, 16));
                crnProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28));
                crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 36));
//                crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 34));
//                crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 36));
//                crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 38));
                crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40));
                crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 44));
                crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48));
//                crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40));
//                crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 42));
//                crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 44));
                crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 52));
//                crnProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 20));
//                crnProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 22));
//                crnProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 24));
//                crnProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                // 复位信号
                if (!Cools.isEmpty(crnProtocol.getStatusType()) && crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
//                if (!Cools.isEmpty(crnProtocol.getStatusType()) && crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
//                    if (resetFlag) {
//                        if(crnProtocol.getTaskNo()==9999){
//                            backHpFlag = false;
//                        }
//                        CrnCommand crnCommand = new CrnCommand();
//                        crnCommand.setAckFinish((short)1);
//                        if (write(crnCommand)) {
//                            resetFlag = false;
//                        }
//                    }
//                }
                if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
//                    News.error("-------------------------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台信号:{}]==>> 状态为10,等待确认!!",
//                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "有物" : "无物");
                    if (resetFlag) {
                        if(crnProtocol.getTaskNo()==9999){
                            backHpFlag = false;
                        }
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write(crnCommand)) {
                            resetFlag = false;
//                            cmdFlag = true;
                        }
                    }
                }
@@ -234,27 +269,33 @@
            News.error("SiemensCrn"+" - 6"+" - 堆垛机写入命令为空");
            return false;
        }
        OperateResult result;
//        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[10];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
        array[3] = command.getSourcePosX();
        array[4] = command.getSourcePosY();
        array[5] = command.getSourcePosZ();
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
        if (command.getAckFinish() == 1) {
            result = siemensNet.Write("DB100.0", (short) 1);
//            result = siemensNet.Write("DB100.18", 0);
        } else {
            command.setCrnNo(slave.getId());
            short[] array = new short[10];
            array[0] = command.getAckFinish();
            array[1] = command.getTaskNo();
            array[2] = command.getTaskMode();
            array[3] = command.getSourcePosX();
            array[4] = command.getSourcePosY();
            array[5] = command.getSourcePosZ();
            array[6] = command.getDestinationPosX();
            array[7] = command.getDestinationPosY();
            array[8] = command.getDestinationPosZ();
//        array[9] = command.getSourceStaNo();
//        array[10] = command.getDestinationStaNo();
        array[9] = command.getCommand();
        OperateResult result = siemensNet.Write("DB100.0", array);
            array[9] = command.getCommand();
            result = siemensNet.Write("DB100.0", array);
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.18", commandFinish);
//            if (command.getAckFinish() == 0) {
//                short commandFinish = 1;
//                Thread.sleep(100L);
//                result = siemensNet.Write("DB100.18", commandFinish);
//            }
        }
        try {
@@ -305,7 +346,8 @@
    public static void main(String[] args) throws InterruptedException {
        CrnSlave slave = new CrnSlave();
        slave.setId(1);
        slave.setIp("192.168.6.9");
        slave.setIp("10.10.10.10");
        slave.setPort(0);
        slave.setRack(0);
        slave.setSlot(0);
        SiemensCrnThread crnThread = new SiemensCrnThread(slave);
@@ -316,16 +358,17 @@
        // 1.入库 源和目标都发
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(1); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
////        command.setCrnNo(3); // 堆垛机编号
//        command.setTaskNo((short) 2); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.PAKIN); // 任务模式
//        command.setSourcePosX((short) 1);     // 源库位排
//        command.setSourcePosY((short) 0);     // 源库位列
//        command.setSourcePosZ((short) 1);     // 源库位层
//        command.setDestinationPosX((short) 2);     // 目标库位排
//        command.setDestinationPosY((short) 3);     // 目标库位列
//        command.setSourcePosX((short) 6);     // 源库位排
//        command.setSourcePosY((short) 2);     // 源库位列
//        command.setSourcePosZ((short) 2);     // 源库位层
//        command.setDestinationPosX((short) 6);     // 目标库位排
//        command.setDestinationPosY((short) 0);     // 目标库位列
//        command.setDestinationPosZ((short) 1);     // 目标库位层
//        command.setCommand((short)1);
//        crnThread.write(command);
        // 2.出库 源和目标都发