#
luxiaotao1123
2021-12-17 9acdc6158025f19c83d6f8d4144205b705625409
static/js/object/CrnTask.js
@@ -1,8 +1,20 @@
const xOffset = -1735;
const yOffset = -289;
const zOffset = -480;
const xOffset0 = -1735;
const yOffset0 = -108;
const zOffset0 = -480;
const xOffset1 = -1735;
const yOffset1 = -107;
const zOffset1 = -475;
// 堆垛机当前运行状态对象
function CrnTask(crnData) {
function CrnTask(crnData, object) {
    let that = this;
    // 访问公有属性,需要在变量名前加上this.
    that.crnNo = 0;
    that.run = false;
    // 上一数据
@@ -11,6 +23,7 @@
    that.preX = 0;
    that.preY = 0;
    that.preZ = 0;
    that.prePosition = null;
    that.preForkPos = -1;    // -1, "不在定位" 0, "货叉原位" 1, "货叉在左侧远" 2, "货叉在左侧" 3, "货叉在右侧"  4, "货叉在右侧远"
    // 当前数据
    that.bay = 1 ;
@@ -18,9 +31,15 @@
    that.x = 0;
    that.y = 0;
    that.z = 0;
    that.position = null;
    that.forkPos = -1;
    // 构造函数
    that.crnBody = null;
    that.crnLoad = null;
    that.crnFork = null;
    let curves = [];
    let init = function () {
        that.crnNo = crnData.crnNo;
        that.bay = crnData.bay;
@@ -28,21 +47,30 @@
        that.x = crnData.position.x;
        that.y = crnData.position.y;
        that.z = crnData.position.z;
        that.position = crnData.position;
        that.forkPos = crnData.forkPos;
        that.crnBody = getArrVal(object.objects, "name", that.crnNo + "-body");
        that.crnLoad = getArrVal(object.objects, "name", that.crnNo + "-load");
        that.crnFork = getArrVal(object.objects, "name", that.crnNo + "-fork");
    };
    init();
    // 对外函数
    that.modify = function (crnData) {
        if (that.run || that.crnNo === 0) {
            console.error(that.crnNo + "号堆垛机更新失败");
        if (that.run || that.crnNo === 0 || that.crnBody===null || that.crnLoad===null || that.crnFork==null) {
            // console.log(that.crnNo + "号堆垛机更新失败");
        } else {
            if (JSON.stringify(crnData.position) === JSON.stringify(that.position)) {
                return;
            }
            that.run = true;
            // 上一次
            that.preBay = that.bay;
            that.preLev = that.lev;
            that.preX = that.x;
            that.preY = that.y;
            that.preY = that.y;
            that.prePosition = JSON.parse(JSON.stringify(that.position));
            that.preForkPos = that.forkPos;
            // 当前
            that.bay = crnData.bay;
@@ -50,12 +78,118 @@
            that.x = crnData.position.x;
            that.y = crnData.position.y;
            that.z = crnData.position.z;
            that.position = crnData.position;
            that.forkPos = crnData.forkPos;
            // create Route ------------------------------------------------
            //  new Route();    // body
            // new Route();    // load
            that.run = true;
            console.log(that);
            // 堆垛机叉牙 伸叉
            if (that.prePosition.x !== that.position.x) {
                curves.push({
                    start: false,
                    progress: 0,
                    part: 'fork',
                    route: new Route([
                        {
                            x: that.prePosition.x,
                            y: that.crnFork.position.y,
                            z: that.crnFork.position.z
                        },
                        {
                            x: that.position.x,
                            y: that.crnFork.position.y,
                            z: that.crnFork.position.z
                        }
                    ])
                })
            }
            // 堆垛机载货台和叉牙 移动
            if (that.prePosition.z !== that.position.z || that.prePosition.y !== that.position.y) {
                curves.push({
                    start: false,
                    progress: 0,
                    part: 'load',
                    route: new Route([
                        {
                            x: that.crnLoad.position.x,
                            y: that.prePosition.y,
                            z: that.prePosition.z
                        },
                        {
                            x: that.crnLoad.position.x,
                            y: that.position.y,
                            z: that.position.z
                        }
                    ])
                })
            }
            // 堆垛机本体运动轨迹
            if (that.prePosition.z !== that.position.z && false) {
                curves.push({
                    start: false,
                    progress: 0,
                    part: 'body',
                    route: new Route([
                        {
                            x: that.crnBody.position.x,
                            y: that.crnBody.position.y,
                            z: that.prePosition.z
                        },
                        {
                            x: that.crnBody.position.x,
                            y: that.crnBody.position.y,
                            z: that.position.z
                        }
                    ])
                })
            }
            curves.map((value, index) => {
                console.log(value.part + " --> " + JSON.stringify(value.route.points))
            });
            curves[0].start = true;
        }
    }
    that.move = function () {
        if (curves.length > 0) {
            for (let i = 0; i < curves.length ; i++) {
                if (curves[i].start) {
                    curves[i].progress += 0.01;
                    if (curves[i].progress > 1.0) {
                        curves[i].start = false;
                        if (i !== curves.length - 1) {
                            curves[i+1].start = true;
                        } else {
                            curves = [];
                            that.run = false;
                        }
                    } else {
                        let point = curves[i].route.getPoint(curves[i].progress);
                        if ( point && point.x ) {
                            switch (curves[i].part) {
                                case 'body':
                                    that.crnBody.position.set(point.x, point.y, point.z);
                                    break
                                case 'load':
                                    that.crnBody.position.z = point.z + zOffset;
                                    that.crnLoad.position.set(point.x, point.y + yOffset0, point.z + zOffset0);
                                    that.crnFork.position.set(point.x, point.y + yOffset1, point.z + zOffset1);
                                    break
                                case 'fork':
                                    that.crnFork.position.set(point.x + xOffset1, point.y, point.z);
                                    break
                                default:
                                    break
                            }
                        }
                    }
                }
            }
        }
    }