自动化立体仓库 - WCS系统
Junjie
2023-12-08 13a7ee010e75bab8030c7c0be736d0ca02d966d7
src/main/java/com/zy/common/utils/ShuttleDispatchUtils.java
@@ -1,10 +1,15 @@
package com.zy.common.utils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasShuttle;
import com.zy.asrs.entity.WrkCharge;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.mapper.WrkChargeMapper;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.BasShuttleService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.NavigateNode;
@@ -12,11 +17,18 @@
import com.zy.common.service.CommonService;
import com.zy.core.News;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.ShuttleChargeType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.LiftSlave;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.model.protocol.LiftProtocol;
import com.zy.core.model.protocol.LiftStaProtocol;
import com.zy.core.model.protocol.NyShuttleProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.LiftThread;
import com.zy.core.thread.NyShuttleThread;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -46,6 +58,10 @@
     * 调度车辆-调度指定穿梭车
     */
    public boolean dispatchShuttle(Integer wrkNo, String locNo, Integer shuttleNo) {
        if (!checkCharge(shuttleNo, locNo)) {
            News.info("{}号小车,存在充电任务禁止小车移动任务调度", shuttleNo);
            return false;//存在充电任务暂停调度
        }
        return shuttleMoveGenerate(wrkNo, locNo, shuttleNo);
    }
@@ -53,6 +69,18 @@
     * 调度车辆
     */
    public boolean dispatchShuttle(Integer wrkNo, String locNo) {
        //检测目标库位组是否存在小车,如存在小车则直接指定该车
        WrkMast wrkMast = wrkMastMapper.selectByWorkNo(wrkNo);
        if (wrkMast != null) {
            String targetLocNo = wrkMast.getIoType() < 100 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();
            List<String> groupLoc = Utils.getGroupLoc(targetLocNo);
            Integer groupShuttleNo = Utils.checkGroupLocHasShuttle(groupLoc);
            if (groupShuttleNo != null) {
                //存在小车,直接调度该车
                return shuttleMoveGenerate(wrkNo, locNo, groupShuttleNo);
            }
        }
        ArrayList<NyShuttleThread> sameLev = new ArrayList<>();//相同楼层的穿梭车
        ArrayList<NyShuttleThread> diffLev = new ArrayList<>();//不同楼层的穿梭车
@@ -64,7 +92,16 @@
                continue;
            }
            if (checkChargeWrk(shuttle.getId())) {
                continue;//存在充电任务,过滤小车
            }
            if (!shuttleProtocol.isIdle()) {
                continue;//小车忙碌中
            }
            if (shuttleProtocol.getSuspendState() == 1) {
                //小车处于管制中
                continue;
            }
@@ -73,7 +110,13 @@
            if (currentLocNo.equals(locNo)) {
                //车辆当前位置已经是目标库位,调度该车
                shuttleMoveGenerate(wrkNo, locNo, shuttleProtocol.getShuttleNo().intValue());
                //给工作档绑定小车号
                WrkMast wrkMast1 = wrkMastMapper.selectByWorkNo(wrkNo);
                if (wrkMast1 != null) {
                    wrkMast1.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());
                    wrkMastMapper.updateById(wrkMast1);
                    return true;
                }
                break;
            }
@@ -96,7 +139,7 @@
                //当前穿梭车库位号
                String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                //当前穿梭车线程到目标地点距离
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, locNo, NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), Utils.getLev(currentLocNo)));//搜索空闲穿梭车,使用正常通道地图
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, locNo, NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), Utils.getLev(currentLocNo)), null);//搜索空闲穿梭车,使用正常通道地图
                if (currentShuttlePath == null) {
                    continue;
                }
@@ -104,37 +147,51 @@
                if (currentAllDistance < recentAllDistance) {
                    //如果当前楼层的车路径更小,则更新最近穿梭车
                    recentShuttle = shuttleThread;
                    recentAllDistance = currentAllDistance;
                }
            }
        }else {
            //同一楼层,没有空闲穿梭车,只能从其他楼层调度
            //寻找离任务最近的穿梭车
            for (NyShuttleThread shuttleThread : diffLev) {
            //寻找离任务楼层最近的穿梭车(不考虑跨楼层小车移动距离)
                //当前穿梭车库位号
                String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                int currentLev = Utils.getLev(currentLocNo);
                List<WrkMast> wrkMasts1 = wrkMastService.selectNoShuttleWrkByLev(currentLev);//判断当前穿梭车楼层是否有待分配车辆的任务,如果有则不分配这辆车
                if (wrkMasts1.size() > 0) {
                    //存在其他任务,跳过这辆车
                    continue;
            //获取任务
            WrkMast wrkMast1 = wrkMastMapper.selectByWorkNo(wrkNo);
            if (wrkMast1 != null) {
                String targetLoc = wrkMast1.getIoType() < 100 ? wrkMast1.getLocNo() : wrkMast1.getSourceLocNo();
                int lev = Utils.getLev(targetLoc);//目标楼层
                //检测目标楼层车数量是否小于允许的最大数量
                boolean checkDispatchMaxNum = checkDispatchMaxNum(lev);
                if (!checkDispatchMaxNum) {
                    News.info("{}任务,{}层,已经达到当前楼层调度车辆最大值", wrkMast1.getWrkNo(), lev);
                    return false;
                }
                //当前穿梭车线程到当前车子所在楼层的提升机口距离
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, Utils.levToOutInStaLocNo(currentLev), NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), currentLev));//搜索空闲穿梭车,使用正常通道地图
                if (currentShuttlePath == null) {
                    continue;
                }
                int recentValue = 99999;//最小差值
                for (NyShuttleThread shuttleThread : diffLev) {
                    //当前穿梭车库位号
                    String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                    int currentLev = Utils.getLev(currentLocNo);
                    List<WrkMast> wrkMasts1 = wrkMastService.selectNoShuttleWrkByLev(currentLev);//判断当前穿梭车楼层是否有待分配车辆的任务,如果有则不分配这辆车
                    if (wrkMasts1.size() > 0) {
                        //存在其他任务,跳过这辆车
                        continue;
                    }
                Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
                if (currentAllDistance < recentAllDistance) {
                    //如果当前楼层的车路径更小,则更新最近穿梭车
                    recentShuttle = shuttleThread;
                    //ABS(目标楼层 - 当前楼层) 得到差距,取最小差值
                    int currentValue = Math.abs(lev - currentLev);
                    if (currentValue < recentValue) {
                        //如果当前楼层的车路径更小,则更新最近穿梭车
                        recentShuttle = shuttleThread;
                        recentValue = currentValue;
                    }
                }
            }
        }
        if (recentShuttle == null) {//没有搜索到可用穿梭车
            News.info("{}目标库位没有搜索到可用穿梭车", locNo);
            return false;
        }
@@ -146,39 +203,53 @@
     * 小车迁移任务生成
     */
    @Transactional
    private boolean shuttleMoveGenerate(Integer wrkNo, String locNo, Integer shuttleNo) {
    public boolean shuttleMoveGenerate(Integer wrkNo, String locNo, Integer shuttleNo) {
        Date now = new Date();
        //获取四向穿梭车线程
        NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            News.info("{}号小车,线程不存在", shuttleNo);
            return false;
        }
        NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
        if (shuttleProtocol == null) {
            News.info("{}号小车,线程不存在", shuttleNo);
            return false;
        }
        //小车处于空闲状态
        if (!shuttleProtocol.isIdleNoCharge()) {
            News.info("{}号小车,忙碌中", shuttleNo);
            return false;
        }
        //判断穿梭车是否存在未完成的小车移库任务
        WrkMast hasMoveWorking = wrkMastMapper.selectShuttleHasMoveWorking(shuttleNo);
        if (hasMoveWorking != null) {//小车存在移库任务,等待执行完成后再生成新的任务
            News.info("{}号小车,存在移动任务,等待执行完成后再生成新的任务", shuttleNo);
            return false;
        }
        //判断是否有其他任务正在使用穿梭车
        WrkMast wrkMast2 = wrkMastMapper.selectShuttleWorking(shuttleNo);
        if (wrkMast2 != null) {//小车存在其他工作档任务,等待执行完成后再生成新的任务
            News.info("{}号小车,存在其他工作档任务,等待执行完成再生成新的任务", shuttleNo);
            return false;
        }
        //判断是否有充电任务正在使用穿梭车
        WrkCharge wrkCharge = wrkChargeMapper.selectWorking(shuttleNo);
        if (wrkCharge != null) {//小车存在充电任务,等待执行完成后再生成新的任务
            return false;
        Integer sourceStaNo = null;//小车换层源站点
        Integer staNo = null;//小车换层目标站点
        if (Utils.getLev(locNo) != shuttleProtocol.getPoint().getZ()) {
            //目标库位和小车库位处于不同一楼层,需要通过提升机调度
            //获取穿梭车最近且空闲的提升机输送站点
            LiftStaProtocol liftSta = this.getRecentLiftSta(shuttleNo, Utils.getLev(locNo));
            if (liftSta == null) {
                News.info("{}号小车,{}目标库位,没有可用空闲输送站点", shuttleNo, locNo);
                return false;//没有可用且空闲的输送站点
            }
            sourceStaNo = liftSta.getStaNo();//源站点
            //提升机号*100+目标楼层=目标站点
            staNo = liftSta.getLiftNo() * 100 + Utils.getLev(locNo);//目标站
        }
        // 获取工作号
@@ -193,6 +264,8 @@
        wrkMast.setShuttleNo(shuttleNo);//穿梭车号
        wrkMast.setSourceLocNo(shuttleProtocol.getCurrentLocNo()); // 源库位 => 小车当前库位号
        wrkMast.setLocNo(locNo); // 目标库位
        wrkMast.setSourceStaNo(sourceStaNo);//源站
        wrkMast.setStaNo(staNo);//目标站
        wrkMast.setPicking("N"); // 拣料
        wrkMast.setExitMk("N"); // 退出
        wrkMast.setLinkMis("N");
@@ -214,4 +287,223 @@
        return true;
    }
    /**
     * 检测目标楼层车数量是否小于允许的最大数量
     * true: 小于最大数量  false: 大于或等于最大数量
     */
    public boolean checkDispatchMaxNum(Integer lev) {
        BasShuttleService basShuttleService = SpringUtils.getBean(BasShuttleService.class);
        ConfigService configService = SpringUtils.getBean(ConfigService.class);
        EntityWrapper<Config> wrapper = new EntityWrapper<>();
        wrapper.eq("code", "dispatchShuttleMaxNum");
        Config config = configService.selectOne(wrapper);
        if (config == null) {
            return false;
        }
        int levCount = 0;//目标楼层车辆数量
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
                continue;
            }
            NyShuttleProtocol.NyShuttlePointClass point = null;
            if (shuttleProtocol.getPoint() == null) {
                BasShuttle basShuttle = basShuttleService.selectById(shuttle.getId());//小车如果没有数据,从数据库取数据
                if (basShuttle == null || basShuttle.getPoint() == null) {
                    continue;
                }
                point = JSON.parseObject(basShuttle.getPoint(), NyShuttleProtocol.NyShuttlePointClass.class);
            }else {
                point = shuttleProtocol.getPoint();
            }
            if (point.getZ().equals(lev)) {
                levCount++;//目标楼层有车,数量增加
            }
        }
        //搜索是否存在前往目标楼层的小车移动工作档
        for (WrkMast wrkMast : wrkMastMapper.selectShuttleMoveWrk()) {
            if (wrkMast.getSourceLocNo() == null || wrkMast.getLocNo() == null) {
                continue;
            }
            int sourceLev = Utils.getLev(wrkMast.getSourceLocNo());//工作档源楼层
            int targetLev = Utils.getLev(wrkMast.getLocNo());//工作档目标楼层
            if (sourceLev == lev) {
                continue;//工作档楼层和目标楼层相同,跳过
            }
            if (targetLev == lev) {
                levCount++;//工作档目标楼层和实际楼层相同,数量增加
                continue;
            }
        }
        return levCount < Integer.parseInt(config.getValue());
    }
    /**
     * 获取穿梭车最近且空闲的提升机输送站点
     */
    public LiftStaProtocol getRecentLiftSta(Integer shuttleNo, Integer targetLev) {
        //获取四向穿梭车线程
        NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return null;
        }
        NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
        if (shuttleProtocol == null) {
            return null;
        }
        //获取小车同一楼层的站点
        ArrayList<LiftStaProtocol> list = new ArrayList<>();
        int lev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//小车楼层
        for (LiftSlave slave : slaveProperties.getLift()) {
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, slave.getId());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
            if (!liftProtocol.isIdle()) {
                continue;
            }
            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(slave.getId(), lev);
            if (liftStaProtocol == null) {
                continue;
            }
            //判断目标楼层站点是否无托盘
            LiftStaProtocol targetLiftStaProtocol = NyLiftUtils.getLiftStaByLev(slave.getId(), targetLev);
            if (targetLiftStaProtocol == null) {
                continue;
            }
            if (targetLiftStaProtocol.getHasTray()) {
                continue;//有托盘跳过
            }
            list.add(liftStaProtocol);
        }
        if (list.isEmpty()) {
            return null;
        }
        String currentLocNo = shuttleProtocol.getCurrentLocNo();//小车位置
        Integer recentAllDistance = 9999999;
        LiftStaProtocol recentSta = null;//最近站点
        //搜索距离小车最近的站点
        for (LiftStaProtocol liftStaProtocol : list) {
            Integer staNo = liftStaProtocol.getStaNo();//站点号
            String locNo = liftStaProtocol.getLocNo();//站点库位号
            //当前穿梭车线程到目标地点距离
            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, locNo, NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleNo, Utils.getLev(currentLocNo)), null);//使用正常通道地图
            if (currentShuttlePath == null) {
                continue;
            }
            Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
            if (currentAllDistance < recentAllDistance) {
                //如果当前楼层的车路径更小,则更新最近站点
                recentSta = liftStaProtocol;
                recentAllDistance = currentAllDistance;
            }
        }
        return recentSta;
    }
    /**
     * 检测是否穿梭车是否有充电任务
     */
    public boolean checkChargeWrk(int shuttleNo) {
        //判断是否有充电任务正在使用穿梭车
        WrkCharge wrkCharge = wrkChargeMapper.selectWorking(shuttleNo);
        if (wrkCharge != null) {
            return true;//有充电任务
        }
        return false;//无充电任务
    }
    /**
     * 检测穿梭车是否有充电任务,目标点是否为充电桩位置
     * @param shuttleNo 小车号
     * @param locNo 目标位置
     */
    public boolean checkCharge(int shuttleNo, String locNo) {
        //判断是否有充电任务正在使用穿梭车
        WrkCharge wrkCharge = wrkChargeMapper.selectWorking(shuttleNo);
        if (wrkCharge != null) {//小车存在充电任务,等待执行完成后再生成新的任务
            //判断目标点是否为充电桩,如果是去充电则放行
            boolean toCharge = false;//去充电目标
            for (ShuttleChargeType chargeType : ShuttleChargeType.values()) {
                if (chargeType.locNo.equals(locNo)) {
                    toCharge = true;//去充电桩
                    break;
                }
            }
            if (wrkCharge.getWrkSts() == 53) {
                toCharge = true;//充电结束,允许生成移库任务
            }
            if (!toCharge) {
                //不是去充电桩且存在充电任务,禁止生成新的移动任务
                return false;
            }
        }
        return true;//无充电任务放行 || 去充电桩中放行
    }
    /**
     * 获取楼层可用小车数量
     */
    public int getShuttleCountByLev(int lev) {
        int count = 0;
        for (ShuttleSlave slave : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId());
            if (shuttleThread == null) {
                continue;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
                continue;
            }
            if (checkChargeWrk(slave.getId())) {
                continue;//存在充电任务,过滤小车
            }
            if (shuttleProtocol.getWorkingMode() == 0) {
                continue;//手动状态
            }
            if (shuttleProtocol.getSuspendState() == 1) {
                //小车处于管制中
                continue;
            }
            if (shuttleProtocol.getPoint().getZ() == lev) {
                //同一楼层可用小车
                count++;
                continue;
            }
        }
        return count;
    }
}