#ZH
野心家
2025-05-27 c9d334e61ff33aa2f90b71e34d5a7a0340765bfe
src/main/java/com/zy/core/thread/RgvThread.java
@@ -25,10 +25,7 @@
import com.zy.core.model.RgvSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.RgvCommand;
import com.zy.core.model.protocol.RgvProtocol;
import com.zy.core.model.protocol.RgvTaskProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.model.protocol.TaskProtocol;
import com.zy.core.model.protocol.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@@ -56,6 +53,7 @@
    private Long trackBenchmark = 1L;
    //  # 避让距离
    private Long avoidDistance = 5000L;
    private String errorRgv = "-";
    /**
     * 工位1复位信号
@@ -120,7 +118,7 @@
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "rgv连接失败"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
//                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
@@ -132,6 +130,7 @@
        while (true) {
            try {
                Thread.sleep(20);
//                System.out.println("读线程"+ slave.getId());
                readStatus();
@@ -141,7 +140,7 @@
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据读取线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
//                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
@@ -174,7 +173,7 @@
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据任务下发复位线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
//                    log.error("e2:"+e2.getMessage());
                }
                initRgv();
//                e.printStackTrace();
@@ -187,115 +186,132 @@
    /**
     * 任务下发
     */
    private void taskWalkIssued2() {
        while (true) {
            try {
                // 休眠 1 秒
                Thread.sleep(100);
                if (!deviceDetection()) {
                    continue;
                }
                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                if (rgvProtocol == null || rgvTaskProtocol == null) {
                    initRgv();
                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                }
                if (rgvTaskProtocol.getAvoid() != 0) {
                    continue;
                }
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                    continue;
                }
                if (rgvProtocol.getLoaded() == -1){
                    continue;
                }
                List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getAllWalkTaskProtocol();
                for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                    if (taskProtocol.getIsRunning() == 1) {//准备下发
                        // 双车
                        if (rgvOtherStatusEnable()) {
                            //另一台车是否允许此台车执行
                            if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                continue;
                            }
                        }
                        if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                            break;
                        } else {
                            TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                            write(issued);
                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("RGV任务下发线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV任务下发线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
                }
//                e.printStackTrace();
            }
        }
    }
//    private void taskWalkIssued2() {
//        while (true) {
//            try {
//                // 休眠 1 秒
//                Thread.sleep(100);
//                if (!deviceDetection()) {
//                    continue;
//                }
//                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
//                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
//                if (rgvProtocol == null || rgvTaskProtocol == null) {
//                    initRgv();
//                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
//                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
//                }
//                if (rgvTaskProtocol.getAvoid() != 0) {
//                    continue;
//                }
//                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
//                    continue;
//                }
//                if (rgvProtocol.getLoaded() == -1){
//                    continue;
//                }
//
//                List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getAllWalkTaskProtocol();
//                for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
//                    if (taskProtocol.getIsRunning() == 1) {//准备下发
//                        // 双车
//                        if (rgvOtherStatusEnable()) {
//                            //另一台车是否允许此台车执行
//                            if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
//                                continue;
//                            }
//                        }
//                        if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
//                                && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
//                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
//                            break;
//                        } else {
//                            TaskProtocol issued = new TaskProtocol(taskProtocol,true);
//                            write(issued);
//                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
//                            break;
//                        }
//                    }
//                }
//            } catch (Exception e) {
//                log.error("RGV任务下发线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
//                try{
//                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
//                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV任务下发线程异常"+e.getMessage());
//                } catch (Exception e2){
//                    log.error("e2:"+e2.getMessage());
//                }
////                e.printStackTrace();
//            }
//        }
//    }
    /**
     * 漫游
     */
    private void taskWalkIssued() {
        while (true) {
            try {
                // 休眠 1 秒
                Thread.sleep(100);
                if (!deviceDetection()) {
                    continue;
                }
                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                if (rgvTaskProtocol == null) {
                    initRgv();
                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                }
                if (rgvTaskProtocol.getAvoid() != 1) {
                    continue;
                }
                TaskProtocol issued = new TaskProtocol();
                issued.setTaskNo(32222L);
                issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
                issued.setTaskStatus(1);
                issued.setDirection(true);
                write(issued);
                rgvTaskProtocol.setAvoid(0);
                Thread.sleep(200);
                RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
            } catch (Exception e) {
                log.error("RGV行走任务下发线程异常!!!" + e.getMessage());
//                e.printStackTrace();
            }
        }
    }
//    private void taskWalkIssued() {
//        while (true) {
//            try {
//                // 休眠 1 秒
//                Thread.sleep(100);
//
//                if (!deviceDetection()) {
//                    continue;
//                }
//                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
//                if (rgvTaskProtocol == null) {
//                    initRgv();
//                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
//                }
//                if (rgvTaskProtocol.getAvoid() != 1) {
//                    continue;
//                }
//                TaskProtocol issued = new TaskProtocol();
//                issued.setTaskNo(32222L);
//                issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
//                issued.setTaskStatus(1);
//                issued.setDirection(true);
//                write(issued);
//                rgvTaskProtocol.setAvoid(0);
//
//                Thread.sleep(200);
//                RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
//
//            } catch (Exception e) {
//                log.error("RGV行走任务下发线程异常!!!" + e.getMessage());
////                e.printStackTrace();
//            }
//        }
//    }
    /**
     * 任务下发
     */
    private void taskIssued() {
        while (true) {
            RgvRunProtocol rgvRun = RgvRunCache.getRgvRun();
            if (!rgvRun.getRgvNo().equals(slave.getId())){
                continue;
            }
            try {
                Thread.sleep(50);
                rgvRun = RgvRunCache.getRgvRun();
//                System.out.println(JSON.toJSON(rgvRun));
                // 休眠 1 秒
                Thread.sleep(100);
                if (!deviceDetection()) {
                if (System.currentTimeMillis() - rgvRun.currentTimeMilliOld > 1000) {
                    rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                } else {
                    continue;
                }
                if (!deviceDetection()) {
                    rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                    rgvRun.setRgvNo(slave.getOtherId());
                    RgvRunCache.updateRgvStatus(rgvRun);
                    RgvErrCache.updateRgvErr(slave.getId(),errorRgv);
                    continue;
                }
                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                if (rgvProtocol == null || rgvTaskProtocol == null) {
@@ -303,10 +319,17 @@
                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
                }
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE) || (Math.abs(rgvProtocol.getRgvPos() - rgvProtocol.getRgvPosDestination())>100)){
                    rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                    rgvRun.setRgvNo(slave.getOtherId());
                    RgvRunCache.updateRgvStatus(rgvRun);
                    continue;
                }
                if (rgvProtocol.getLoaded() == -1){
                    rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                    rgvRun.setRgvNo(slave.getOtherId());
                    RgvRunCache.updateRgvStatus(rgvRun);
                    RgvErrCache.updateRgvErr(slave.getId(),"小车探物物状态异常");
                    continue;
                }
                if (rgvTaskProtocol.getAvoid() != 0) {
@@ -317,11 +340,16 @@
                        issued.setTaskStatus(1);
                        issued.setDirection(true);
                        write(issued);
                        RgvErrCache.updateRgvErr(slave.getId());
                        rgvTaskProtocol.setAvoid(0);
                        Thread.sleep(200);
//                        try{Thread.sleep(50L);} catch (Exception e){}
                        RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                    }
                    rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                    rgvRun.setRgvNo(slave.getOtherId());
                    RgvRunCache.updateRgvStatus(rgvRun);
                    continue;
                }
@@ -334,45 +362,34 @@
                            if (rgvOtherStatusEnable()) {
                                //另一台车是否允许此台车执行
                                if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                    continue;
                                    RgvErrCache.updateRgvErr(slave.getId(),errorRgv);
                                    break;
                                }
                            }
                            if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                    && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                RgvErrCache.updateRgvErr(slave.getId());
                                break;
                            } else {
                                TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                                write(issued);
                                RgvErrCache.updateRgvErr(slave.getId());
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                break;
                            }
                        }
                    }
                    RgvErrCache.updateRgvErr(slave.getId());
                } else {
                    for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                        if (taskProtocol.getIsRunning() == 1) {//准备下发
//                        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
                            if (taskProtocol.getTaskStatus()==3){
                                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
                                StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
                                if (staProtocol == null ) {
                                    continue;
                                }
                                // 判断是否满足入库条件
                                if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                        || staProtocol.getWorkNo() != 0
                                ){
                                    Thread.sleep(500);
//                                    System.out.println("<放货目标站>" + taskProtocol.getTargetPositionStaNo() + "<非自动或者有物或者存在工作号!!!>");
//                                continue;
                                }
                            }
                            //双车
                            if (rgvOtherStatusEnable()) {
                                //另一台车是否允许此台车执行
                                if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
                                    continue;
                                    RgvErrCache.updateRgvErr(slave.getId(),errorRgv);
                                    break;
                                }
                            }
                            if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
@@ -381,51 +398,66 @@
                                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
                                    StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
                                    if (staProtocol == null ) {
                                        continue;
                                        RgvErrCache.updateRgvErr(slave.getId(),"未查到小车作业站"+staProtocol.getStaNo()+"站点");
                                        break;
                                    }
                                    // 判断是否满足入库条件
                                    if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                            || staProtocol.getWorkNo() != 0
                                    ){
                                        Thread.sleep(500);
//                                    System.out.println("<放货目标站>" + taskProtocol.getTargetPositionStaNo() + "<非自动或者有物或者存在工作号!!!>");
                                        continue;
                                        RgvErrCache.updateRgvErr(slave.getId(),"小车等待"+staProtocol.getStaNo()+"站点就绪");
                                        break;
                                    }
                                } else if (taskProtocol.getTaskStatus()==2){
                                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
                                    StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
                                    if (staProtocol == null ) {
                                        RgvErrCache.updateRgvErr(slave.getId(),"未查到小车作业站"+staProtocol.getStaNo()+"站点");
                                        break;
                                    }
                                    // 判断是否满足入库条件
                                    if (!staProtocol.isAutoing() || !staProtocol.isLoading() || staProtocol.getWorkNo()==0
                                    ){
                                        RgvErrCache.updateRgvErr(slave.getId(),"小车等待"+staProtocol.getStaNo()+"站点就绪");
                                        break;
                                    }
                                }
                                TaskProtocol issued = new TaskProtocol(taskProtocol);
                                write(issued);
                                RgvErrCache.updateRgvErr(slave.getId());
                                taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                                break;
                            } else {
                                TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                                write(issued);
                                RgvErrCache.updateRgvErr(slave.getId());
                                break;
                            }
                        }
                    }
                }
                rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                rgvRun.setRgvNo(slave.getOtherId());
                RgvRunCache.updateRgvStatus(rgvRun);
            } catch (Exception e) {
                log.error("RGV任务下发线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                try{
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV任务下发线程异常"+e.getMessage());
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
//                    log.error("e2:"+e2.getMessage());
                }
                rgvRun = RgvRunCache.getRgvRun();
                rgvRun.currentTimeMilliOld = System.currentTimeMillis()+50;
                rgvRun.setRgvNo(slave.getOtherId());
                RgvRunCache.updateRgvStatus(rgvRun);
                continue;
//                e.printStackTrace();
            }
        }
    }
    public boolean deviceDetection() {
        RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
    public boolean isSignRgvTaskProtocol() {
        RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
        if (rgvProtocol == null || rgvTaskProtocol == null) {
            return false;
        }
        if (!rgvProtocol.getModeType().equals(RgvModeType.AUTO) || rgvProtocol.getRgvPos().equals(0L) || rgvTaskProtocol.getAvoid() == -1 || rgvProtocol.getRgvPosDestination() == 0L
                || (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE) && !rgvProtocol.getStatusType().equals(RgvStatusType.ROAM))) {
        if (rgvTaskProtocol == null) {
            return false;
        }
        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
@@ -435,7 +467,52 @@
            return false;
        }
        if (rgvProtocolOther.statusEnable) {
            if (rgvTaskProtocolOther == null) {
                return false;
            }
        }
        return true;
    }
    public boolean deviceDetection() {
        RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
        RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
        if (rgvProtocol == null || rgvTaskProtocol == null) {
            errorRgv = slave.getId()+"号小车连接失败";
            return false;
        }
        if (!rgvProtocol.getModeType().equals(RgvModeType.AUTO)) {
            errorRgv = slave.getId()+"号小车非自动";
            return false;
        }
        if (rgvProtocol.getRgvPos().equals(0L)
                || rgvTaskProtocol.getAvoid() == -1
                || rgvProtocol.getRgvPosDestination() == 0L) {
            errorRgv = slave.getId()+"号小车状态异常";
            return false;
        }
        if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE) && !rgvProtocol.getStatusType().equals(RgvStatusType.ROAM)) {
            errorRgv = slave.getId()+"号小车非空闲";
            return false;
        }
        if ( (Math.abs(rgvProtocol.getRgvPos() - rgvProtocol.getRgvPosDestination())>100) && !rgvProtocol.getStatusType().equals(RgvStatusType.ROAM)) {
            errorRgv = slave.getId()+"号小车存在运行目标值,需要复位!!!";
            return false;
        }
        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
        RgvTaskProtocol rgvTaskProtocolOther = RgvTaskCache.getRgvStatus(slave.getOtherId());
//        System.out.println("rgvTaskProtocol:"+slave.getId()+"sign:"+rgvTaskProtocol.isSignRun()+"/n"+
//                "rgvTaskProtocolOther:"+slave.getOtherId()+"sign:"+rgvTaskProtocolOther.isSignRun()+"/n");
        if (rgvProtocolOther == null) {
            errorRgv = slave.getOtherId()+"号小车连接失败";
            return false;
        }
        if (rgvProtocolOther.statusEnable) {
            if (!rgvProtocolOther.getModeType().equals(RgvModeType.AUTO) || rgvProtocolOther.getRgvPos().equals(0L) || rgvTaskProtocolOther.getAvoid() == -1 || rgvProtocolOther.getRgvPosDestination() == 0L) {
                errorRgv = slave.getOtherId()+"号小车状态异常";
                return false;
            }
        }
@@ -464,21 +541,71 @@
        Long[][] avoidRange = new TrackRangeUtils().avoidRange(slave, trackEntireLength, trackBenchmark, avoidDistance);
        if (new TrackRangeUtils().IsItSmall(slave)) {
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE)) {
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE) && (Math.abs(rgvProtocolOther.getRgvPos() - rgvProtocolOther.getRgvPosDestination())<50)) {
                if ((rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing())
                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    RgvThread rgvThreadOther = (RgvThread) SlaveConnection.get(SlaveType.Rgv, slave.getOtherId());
                    TaskProtocolCache taskProtocolCacheOther = rgvThreadOther.getTaskProtocolCache();
                    TaskProtocol nowPosRunTask = taskProtocolCacheOther.getNowPosRunTask(rgvProtocolOther.getRgvPos());
                    if (!Cools.isEmpty(nowPosRunTask)){
                        boolean signNowPosRun = true;
                        if (nowPosRunTask.getTaskStatus()==3){
                            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, nowPosRunTask.getTargetPositionStaNoPlcId());
                            StaProtocol staProtocol = devpThread.getStation().get(nowPosRunTask.getTargetPositionStaNo());
                            if (staProtocol == null ) {
                                signNowPosRun =false;
                            }
                            if (signNowPosRun){
                                // 判断是否满足入库条件
                                if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                ){
                                    if (targetPosition.equals(nowPosRunTask.getTargetPosition())){
                                        if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()-50) {
                                            long avoid = rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing() - avoidDistance - rgvProtocol.getCarBodyKunPeng();
                                            if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                                                log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                                                errorRgv = "RGV行走目标超出范围";
                                                try{
                                                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                                                } catch (Exception e2){
//                                                    log.error("e2:"+e2.getMessage());
                                                }
                                                return false;
                                            }
                                            long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
                                            if (avoidAbs<51){
                                                return false;
                                            }
                                            rgvTaskProtocol.setAvoid(1);
                                            rgvTaskProtocol.setAvoidingTheDestination(avoid);
                                            RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                                        }
                                        return false;
                                    }
                                    signNowPosRun =false;
                                }
                            }
                        }
                        if (signNowPosRun){
                            errorRgv = slave.getOtherId()+"号RGV等待作业...";
                            return false;
                        }
                    }
                    long avoid = targetPosition + rgvProtocol.getCarBodyKunPeng() + avoidDistance + rgvProtocolOther.getCarBodyJiaoMing();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        errorRgv = "RGV行走目标超出范围";
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
//                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
@@ -500,11 +627,13 @@
                    long avoid = targetPosition + rgvProtocol.getCarBodyKunPeng() + avoidDistance + rgvProtocolOther.getCarBodyJiaoMing();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        errorRgv = "RGV行走目标超出范围";
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
//                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
@@ -527,11 +656,13 @@
                        long avoid = rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing() - avoidDistance - rgvProtocol.getCarBodyKunPeng();
                        if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                            log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                            errorRgv = "RGV行走目标超出范围";
                            try{
                                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                            } catch (Exception e2){
                                log.error("e2:"+e2.getMessage());
//                                log.error("e2:"+e2.getMessage());
                            }
                            return false;
                        }
@@ -544,6 +675,8 @@
                        RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                        return false;
                    }
                    errorRgv = slave.getOtherId()+"号RGV影响,等待中...";
                    return false;
                }
            } else {
@@ -554,20 +687,71 @@
                }
            }
        } else {
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE)) {
            if (rgvProtocolOther.getStatusType().equals(RgvStatusType.IDLE)  && (Math.abs(rgvProtocolOther.getRgvPos() - rgvProtocolOther.getRgvPosDestination())<50)) {
                if ((targetPosition - rgvProtocol.getCarBodyJiaoMing())
                        - (rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng())
                        > avoidDistance - 50) {//无需避让
                    return true;
                } else {
                    RgvThread rgvThreadOther = (RgvThread) SlaveConnection.get(SlaveType.Rgv, slave.getOtherId());
                    TaskProtocolCache taskProtocolCacheOther = rgvThreadOther.getTaskProtocolCache();
                    TaskProtocol nowPosRunTask = taskProtocolCacheOther.getNowPosRunTask(rgvProtocolOther.getRgvPos());
                    if (!Cools.isEmpty(nowPosRunTask)){
                        boolean signNowPosRun = true;
                        if (nowPosRunTask.getTaskStatus()==3){
                            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, nowPosRunTask.getTargetPositionStaNoPlcId());
                            StaProtocol staProtocol = devpThread.getStation().get(nowPosRunTask.getTargetPositionStaNo());
                            if (staProtocol == null ) {
                                signNowPosRun =false;
                            }
                            if (signNowPosRun){
                                // 判断是否满足入库条件
                                if (!staProtocol.isAutoing() || staProtocol.isLoading()
                                ){
                                    if (targetPosition.equals(nowPosRunTask.getTargetPosition())){
                                        if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos() + 50) {
                                            long avoid = rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng() + avoidDistance + rgvProtocol.getCarBodyJiaoMing();
                                            if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                                                log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                                                errorRgv = "RGV行走目标超出范围";
                                                try{
                                                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                                    deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                                                } catch (Exception e2){
//                                                    log.error("e2:"+e2.getMessage());
                                                }
                                                return false;
                                            }
                                            long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
                                            if (avoidAbs<51){
                                                return false;
                                            }
                                            rgvTaskProtocol.setAvoid(1);
                                            rgvTaskProtocol.setAvoidingTheDestination(avoid);
                                            RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                                        }
                                        return false;
                                    }
                                    signNowPosRun =false;
                                }
                            }
                        }
                        if (signNowPosRun){
                            errorRgv = slave.getOtherId()+"号RGV等待作业...";
                            return false;
                        }
                    }
                    long avoid = targetPosition - rgvProtocol.getCarBodyJiaoMing() - avoidDistance - rgvProtocolOther.getCarBodyKunPeng();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        errorRgv = "RGV行走目标超出范围";
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
//                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
@@ -589,11 +773,13 @@
                    long avoid = targetPosition - rgvProtocol.getCarBodyJiaoMing() - avoidDistance - rgvProtocolOther.getCarBodyKunPeng();
                    if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                        log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                        errorRgv = "RGV行走目标超出范围";
                        try{
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
//                            log.error("e2:"+e2.getMessage());
                        }
                        return false;
                    }
@@ -617,11 +803,13 @@
                        long avoid = rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng() + avoidDistance + rgvProtocol.getCarBodyJiaoMing();
                        if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                            log.error("RGV行走超出范围!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                            errorRgv = "RGV行走目标超出范围";
                            try{
                                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV行走超出范围");
                            } catch (Exception e2){
                                log.error("e2:"+e2.getMessage());
//                                log.error("e2:"+e2.getMessage());
                            }
                            return false;
                        }
@@ -634,6 +822,7 @@
                        RgvTaskCache.updateRgvStatus(rgvTaskProtocol);
                        return false;
                    }
                    errorRgv = slave.getOtherId()+"号RGV等待中...";
                    return false;
                }
            } else {
@@ -644,6 +833,8 @@
                }
            }
        }
        errorRgv = slave.getOtherId()+"号RGV影响,等待中...";
        return false;
    }
@@ -680,7 +871,7 @@
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV异常"+e.getMessage());
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
//                log.error("e2:"+e2.getMessage());
            }
            rgvProtocol.setStatusEnable(true);
        }
@@ -718,7 +909,7 @@
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGVplc连接失败");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
//                log.error("e2:"+e2.getMessage());
            }
        }
        initRgv();
@@ -747,6 +938,7 @@
                if (rgvProtocol.getRgvPosDestination()==0L){
                    rgvProtocol.setRgvPosDestination(rgvProtocol.getRgvPos());
                }
                rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 8));
                rgvProtocol.setTaskNo((long)siemensNet.getByteTransform().TransInt32(result.Content, 10));
@@ -766,6 +958,7 @@
                rgvProtocol.setErr4(status[11]);
                rgvProtocol.setErr5(status[12]);
                rgvProtocol.setErr6(status[13]);
//                System.out.println("读线程"+ slave.getId()+"---"+JSON.toJSONString(rgvProtocol));
                OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId()));
//                // 工位1复位信号
@@ -779,6 +972,9 @@
//                        }
//                    }
//                }
                if (rgvProtocol.getAlarm$()!=0){
                    RgvErrCache.updateRgvErr(slave.getId(),rgvProtocol.getAlarmM());
                }
                try {
                    // 根据实时信息更新数据库
@@ -799,7 +995,7 @@
                            DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                            deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV plc数据库更新失败");
                        } catch (Exception e2){
                            log.error("e2:"+e2.getMessage());
//                            log.error("e2:"+e2.getMessage());
                        }
                    }
                } catch (Exception ignore) {
@@ -820,7 +1016,7 @@
                    DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                    deviceErrorService.addDeviceError("rgv", slave.getId(), "读取RGV plc状态信息失败");
                } catch (Exception e2){
                    log.error("e2:"+e2.getMessage());
//                    log.error("e2:"+e2.getMessage());
                }
            }
        } catch (Exception e) {
@@ -832,7 +1028,7 @@
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "读取RGV plc状态信息失败");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
//                log.error("e2:"+e2.getMessage());
            }
            initRgv();
        }
@@ -849,7 +1045,7 @@
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV写入命令为空");
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
//                log.error("e2:"+e2.getMessage());
            }
            return false;
        }
@@ -899,8 +1095,8 @@
        }
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
//            Thread.sleep(50);
//            this.readStatus();
            log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(taskProtocol));
            log.error("RGV 命令下发 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
@@ -908,7 +1104,7 @@
                DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class);
                deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV 命令下发"+JSON.toJSON(taskProtocol));
            } catch (Exception e2){
                log.error("e2:"+e2.getMessage());
//                log.error("e2:"+e2.getMessage());
            }
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(taskProtocol)));
            return true;
@@ -949,8 +1145,8 @@
        }
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
//            Thread.sleep(200);
//            this.readStatus();
            log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON("null"));
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON("null")));
            return true;