#
luxiaotao1123
5 天以前 9b79c0073a7108cd05ed49806a4affb45e9f1629
zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MapService.java
@@ -1,10 +1,13 @@
package com.zy.acs.manager.core.service;
import com.alibaba.fastjson.JSON;
import com.zy.acs.common.enums.AgvDirectionType;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.core.constant.MapDataConstant;
import com.zy.acs.manager.core.domain.DirectionDto;
import com.zy.acs.manager.core.domain.SortCodeDto;
import com.zy.acs.manager.core.domain.UnlockPathTask;
import com.zy.acs.manager.core.domain.type.CodeDirectionType;
import com.zy.acs.manager.core.service.astart.*;
import com.zy.acs.manager.core.service.astart.domain.AStarNavigateNode;
import com.zy.acs.manager.core.service.astart.domain.DynamicNode;
@@ -12,6 +15,8 @@
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.Loc;
import com.zy.acs.manager.manager.entity.Segment;
import com.zy.acs.manager.manager.entity.Sta;
import com.zy.acs.manager.manager.enums.CodeSpinType;
import com.zy.acs.manager.manager.service.ActionService;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.system.service.ConfigService;
@@ -34,6 +39,7 @@
@Component("mapService")
public class MapService {
    private static final double EPS = 1e-7;
    private final static int[][] DIRECTIONS = {{0,1},{0,-1},{-1,0},{1,0}};
    @Value("${floyd.enable}")
@@ -123,6 +129,18 @@
        }).collect(Collectors.toList());
    }
    public static Double mapToNearest(Double angle) {
        if (null == angle) {
            return null;
        }
        angle = (angle + 360) % 360;
        double remainder = angle % 45;
        if (remainder < 22.5) {
            return angle - remainder;
        } else {
            return angle + (45 - remainder);
        }
    }
    // 角度计算
    public Double calculateDirection(Code startCode, Code endCode, int angleOffsetVal) {
@@ -134,7 +152,7 @@
        double deltaX = x1 - x0;
        double deltaY = y1 - y0;
        double angle = -Math.atan2(deltaX, deltaY);
        double angle = Math.atan2(deltaX, deltaY);
        angle = Math.toDegrees(angle) + angleOffsetVal;
        angle = (angle + 360) % 360; // 将角度转换为正值
@@ -142,7 +160,7 @@
    }
    // 坐标货架阈值 todo:luxiaotao
    public AgvDirectionType calculateAgvWorkDirection(Loc loc, Code code) {
    public AgvDirectionType calculateAgvWorkDirectionByShelf(Loc loc, Code code) {
        Integer compDirect = loc.getCompDirect();
        AgvDirectionType agvDirectionType = null;
        if (compDirect == 0) {
@@ -154,27 +172,24 @@
        return agvDirectionType;
    }
    public Double getStaAngle(Sta sta, Double workDirection) {
        if (null == sta) {
            return null;
        }
        if (Cools.isEmpty(sta.getAngle())) {
            return workDirection;
        }
        return Double.parseDouble(sta.getAngle());
    }
    public Double calculateAgvWorkDirectionByStation(Double staWorkDirection, Double lastDirection) {
        return Math.abs(staWorkDirection - lastDirection) + 90.0D;
    }
    public double calculateDistance(double x1, double y1, double x2, double y2) {
        double deltaX = x2 - x1;
        double deltaY = y2 - y1;
        return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    }
    public DirectionType calcDirectionType(int[] originIdx, int[] oppositeIdx) {
        if (oppositeIdx[0] < originIdx[0] && oppositeIdx[1] == originIdx[1]) {
            return DirectionType.TOP;
        }
        if (oppositeIdx[0] > originIdx[0] && oppositeIdx[1] == originIdx[1]) {
            return DirectionType.BOTTOM;
        }
        if (oppositeIdx[0] == originIdx[0] && oppositeIdx[1] > originIdx[1]) {
            return DirectionType.RIGHT;
        }
        if (oppositeIdx[0] == originIdx[0] && oppositeIdx[1] < originIdx[1]) {
            return DirectionType.LEFT;
        }
        return DirectionType.NONE;
    }
    public void lockPath(Integer lev, List<String> pathList, String agvNo) {
@@ -183,8 +198,46 @@
    }
    public void unlockPath(String agvNo, String codeData) {
        if (Cools.isEmpty(agvNo, codeData)) {
            return;
        }
        try {
            unlockTaskQueue.offer(new UnlockPathTask(agvNo, codeData), 5, TimeUnit.SECONDS);
//            Integer lev = null;
//
//            String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(null);
//            int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, codeData);
//
//
//            DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev);
//
//            DynamicNode dynamicNode = dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]];
//
//
//            int serial = dynamicNode.getSerial();
//
//            List<int[]> resetCodeIdxList = new ArrayList<>();
//
//            for (int i = 0; i < dynamicMatrix.length; i++) {
//                for (int j = 0; j < dynamicMatrix[i].length; j++) {
//
////                    if (i == codeMatrixIdx[0] && j == codeMatrixIdx[1]) { continue; }
//
//                    DynamicNode node = dynamicMatrix[i][j];
//                    if (node.getVehicle().equals(agvNo)) {
//                        if (node.getSerial() < serial) {
//                            resetCodeIdxList.add(new int[] {i, j});
//                        }
//                    }
//                }
//            }
//
//            if (!Cools.isEmpty(resetCodeIdxList)) {
//
//                mapDataDispatcher.clearDynamicMatrixByCodeList(lev, resetCodeIdxList);
//            }
        } catch (InterruptedException e) {
            log.error("unlockTaskQueue", e);
        }
@@ -210,6 +263,8 @@
    // v1 BFS ------------------------------------------------------------------------------
    public List<NavigateNode> getWaveScopeByCode(Integer lev, String code, Double radiusLen) {
        double radiusLenSquared = Math.pow(radiusLen, 2);
        String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev);
        Double[][][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev);
@@ -222,25 +277,25 @@
        includeList.add(originNode);
        existNodes.add(originNode);
        this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
        this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
        return includeList;
    }
    public void spreadWaveNode(NavigateNode originNode, NavigateNode currNode
            , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLen
            , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLenSquared
            , List<NavigateNode> includeList, Set<NavigateNode> existNodes) {
        int x = currNode.getX();
        int y = currNode.getY();
        this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
        this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
    }
    public void extendNeighborNodes(NavigateNode originNode, NavigateNode nextNode
            , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLen
            , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLenSquared
            , List<NavigateNode> includeList, Set<NavigateNode> existNodes) {
        int x = nextNode.getX();
@@ -259,14 +314,14 @@
        Double[] o1Cda = cdaMatrix[originNode.getX()][originNode.getY()];
        Double[] o2Cda = cdaMatrix[x][y];
        if (Math.pow(o1Cda[0] - o2Cda[0], 2) + Math.pow(o1Cda[1] - o2Cda[1], 2) <= Math.pow(radiusLen, 2)) {
        if (Math.pow(o1Cda[0] - o2Cda[0], 2) + Math.pow(o1Cda[1] - o2Cda[1], 2) <= radiusLenSquared) {
            nextNode.setCodeData(codeMatrix[x][y]);
            if (!nextNode.getCodeData().equals(CodeNodeType.NONE.val)) {
                includeList.add(nextNode);
            }
            this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes);
            this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes);
        }
    }
@@ -364,6 +419,115 @@
        return turnMatrixNode == TurnNodeType.TURN.val;
    }
    public CodeSpinType spinDirection(Code code) {
        if (Cools.isEmpty(code)) {
            return CodeSpinType.NA;
        }
        return CodeSpinType.of(code.getSpin());
    }
    public static void main(String[] args) {
        List<DirectionDto> directionDtoList = DirectionDto.initCodeDirections();
        System.out.println(JSON.toJSONString(directionDtoList));
    }
    public CodeSpinType calcSpinDirection(Code code, Double currDir, Double nextDir) {
        if (Cools.isEmpty(code, currDir, nextDir)) {
            return CodeSpinType.NA;
        }
        List<Double> disableAngleList = new ArrayList<>();
        List<DirectionDto> directionDtoList = DirectionDto.initCodeDirections();
        for (DirectionDto dto : directionDtoList) {
            if (null != dto.getEnabled() && !dto.getEnabled()) {
                disableAngleList.add(dto.getAngle());
            }
        }
        if (Cools.isEmpty(disableAngleList)) {
            return CodeSpinType.NA;
        }
        double curr = norm360(currDir);
        double next = norm360(nextDir);
        // 如果几乎相等,认为不需要旋转
        if (almostEqual(curr, next)) {
            return CodeSpinType.NA;
        }
        double cwDelta = cwDelta(curr, next);     // (0, 360)
        double ccwDelta = 360.0 - cwDelta;  // (0, 360)
        boolean cwBlocked = false;
        boolean ccwBlocked = false;
        for (Double disableAngle : disableAngleList) {
            if (disableAngle == null) continue;
            double disable = norm360(disableAngle);
            if (isOnArcCW(curr, next, disable)) {
                cwBlocked = true;
            }
            if (isOnArcCCW(curr, next, disable)) {
                ccwBlocked = true;
            }
        }
        if (cwBlocked && !ccwBlocked) {
            return CodeSpinType.CCW;
        }
        if (!cwBlocked && ccwBlocked) {
            return CodeSpinType.CW;
        }
        return CodeSpinType.NA;
    }
    private static double norm360(double a) {
        double x = a % 360.0;
        if (x < 0) x += 360.0;
        // 360 归一化为 0
        if (almostEqual(x, 360.0)) return 0.0;
        return x;
    }
    private static boolean almostEqual(double a, double b) {
        return Math.abs(a - b) <= EPS;
    }
    /**
     * 顺时针距离:从 start 到 end,沿 CW 方向走多少度,范围 (0, 360],这里不会返回 0(除非你 curr==next 已提前处理)
     */
    private static double cwDelta(double start, double end) {
        double d = end - start;
        if (d < 0) d += 360.0;
        if (almostEqual(d, 0.0)) return 0.0;
        return d;
    }
    /**
     * 判断 angle 是否落在从 start -> end 的顺时针弧段上(不含端点)。
     * 即:沿 CW 从 start 走 cwDelta(start,end) 的路径上是否“经过”angle。
     */
    private static boolean isOnArcCW(double start, double end, double angle) {
        double total = cwDelta(start, end);
        // 弧长为 0 表示无旋转(已提前处理),这里直接 false
        if (almostEqual(total, 0.0)) return false;
        double a = cwDelta(start, angle);
        // 不包含端点:a 在 (0, total) 之间才算经过
        return a > EPS && a < total - EPS;
    }
    /**
     * 判断 angle 是否落在从 start -> end 的逆时针弧段上(不含端点)。
     * 逆时针等价于:顺时针从 end -> start 的弧段。
     */
    private static boolean isOnArcCCW(double start, double end, double angle) {
        // 逆时针 start->end == 顺时针 end->start
        return isOnArcCW(end, start, angle);
    }
    /**
     * That vehicle is walking if the dynamic node count > 1
     */