自动化立体仓库 - WCS系统
#
Junjie
2023-10-24 925b834a3341dbe9f486964a7ad5d1affd7a404d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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.BasDevp;
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.BasDevpService;
import com.zy.asrs.service.BasShuttleService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.enums.NavigationMapType;
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;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 四向穿梭车调度工具
 */
@Service
public class ShuttleDispatchUtils {
 
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkMastMapper wrkMastMapper;
    @Autowired
    private WrkChargeMapper wrkChargeMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private BasDevpService basDevpService;
 
    /**
     * 调度车辆-调度指定穿梭车
     */
    public boolean dispatchShuttle(Integer wrkNo, String locNo, Integer shuttleNo) {
        return shuttleMoveGenerate(wrkNo, locNo, shuttleNo);
    }
 
    /**
     * 调度车辆
     */
    public boolean dispatchShuttle(Integer wrkNo, String locNo) {
        //检测目标库位组是否存在小车,如存在小车则直接指定该车
        List<String> groupLoc = Utils.getGroupLoc(locNo);
        Integer groupShuttleNo = Utils.checkGroupLocHasShuttle(groupLoc);
        if (groupShuttleNo != null) {
            //存在小车,直接调度该车
            return shuttleMoveGenerate(wrkNo, locNo, groupShuttleNo);
        }
 
        ArrayList<NyShuttleThread> sameLev = new ArrayList<>();//相同楼层的穿梭车
        ArrayList<NyShuttleThread> diffLev = new ArrayList<>();//不同楼层的穿梭车
 
        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;
            }
 
            if (!shuttleProtocol.isIdle()) {
                continue;
            }
 
            int currentLev = shuttleProtocol.getPoint().getZ();//小车当前层高
            String currentLocNo = shuttleProtocol.getCurrentLocNo();//小车当前库位号
 
            if (currentLocNo.equals(locNo)) {
                //车辆当前位置已经是目标库位,调度该车
                //给工作档绑定小车号
                WrkMast wrkMast1 = wrkMastMapper.selectByWorkNo(wrkNo);
                if (wrkMast1 != null) {
                    wrkMast1.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());
                    wrkMastMapper.updateById(wrkMast1);
                    return true;
                }
                break;
            }
 
            if (currentLev == Utils.getLev(locNo)) {
                //工作档楼层相同的穿梭车
                sameLev.add(shuttleThread);
            }else {
                //工作档不同楼层的穿梭车
                diffLev.add(shuttleThread);
            }
 
        }
 
        Integer recentAllDistance = 9999999;
        NyShuttleThread recentShuttle = null;//当前距离最近的四向穿梭车线程
        if (sameLev.size() > 0) {
            //同一楼层有空闲穿梭车,则只在工作档楼层寻找
            //寻找离任务最近的穿梭车
            for (NyShuttleThread shuttleThread : sameLev) {
                //当前穿梭车库位号
                String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                //当前穿梭车线程到目标地点距离
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, locNo, NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), Utils.getLev(currentLocNo)));//搜索空闲穿梭车,使用正常通道地图
                if (currentShuttlePath == null) {
                    continue;
                }
                Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
                if (currentAllDistance < recentAllDistance) {
                    //如果当前楼层的车路径更小,则更新最近穿梭车
                    recentShuttle = shuttleThread;
                    recentAllDistance = currentAllDistance;
                }
            }
        }else {
            //同一楼层,没有空闲穿梭车,只能从其他楼层调度
            //寻找离任务楼层最近的穿梭车(不考虑跨楼层小车移动距离)
 
            //获取任务
            WrkMast wrkMast1 = wrkMastMapper.selectByWorkNo(wrkNo);
 
            if (wrkMast1 != null) {
                String locNO=wrkMast1.getIoType()<100?wrkMast1.getLocNo():wrkMast1.getSourceLocNo();
                int lev = Utils.getLev(locNO);//目标楼层
 
                //检测目标楼层车数量是否小于允许的最大数量
                boolean checkDispatchMaxNum = checkDispatchMaxNum(lev);
                if (!checkDispatchMaxNum) {
                    return false;
                }
 
                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;
                    }
 
                    //ABS(目标楼层 - 当前楼层) 得到差距,取最小差值
                    int currentValue = Math.abs(lev - currentLev);
                    if (currentValue < recentValue) {
                        //如果当前楼层的车路径更小,则更新最近穿梭车
                        recentShuttle = shuttleThread;
                        recentValue = currentValue;
                    }
                }
            }
        }
 
        if (recentShuttle == null) {//没有搜索到可用穿梭车
            return false;
        }
 
        //搜索到可用穿梭车,调度该车
        return shuttleMoveGenerate(wrkNo, locNo, recentShuttle.getSlave().getId());
    }
 
    /**
     * 小车迁移任务生成
     */
    @Transactional
    public boolean shuttleMoveGenerate(Integer wrkNo, String locNo, Integer shuttleNo) {
        Date now = new Date();
        //获取四向穿梭车线程
        NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return false;
        }
        NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
        if (shuttleProtocol == null) {
            return false;
        }
 
        //小车处于空闲状态
        if (!shuttleProtocol.isIdleNoCharge()) {
            return false;
        }
 
        //判断穿梭车是否存在未完成的小车移库任务
        WrkMast hasMoveWorking = wrkMastMapper.selectShuttleHasMoveWorking(shuttleNo);
        if (hasMoveWorking != null) {//小车存在移库任务,等待执行完成后再生成新的任务
            return false;
        }
 
        //判断是否有其他任务正在使用穿梭车
        WrkMast wrkMast2 = wrkMastMapper.selectShuttleWorking(shuttleNo);
        if (wrkMast2 != null) {//小车存在其他工作档任务,等待执行完成后再生成新的任务
            return false;
        }
 
        //判断是否有充电任务正在使用穿梭车
        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;
            }
        }
 
        Integer sourceStaNo = null;//小车换层源站点
        Integer staNo = null;//小车换层目标站点
        if (Utils.getLev(locNo) != shuttleProtocol.getPoint().getZ()) {
            //目标库位和小车库位处于不同一楼层,需要通过提升机调度
            //获取穿梭车最近且空闲的提升机输送站点
            LiftStaProtocol liftSta = this.getRecentLiftSta(shuttleNo, Utils.getLev(locNo));
            if (liftSta == null) {
                return false;//没有可用且空闲的输送站点
            }
            sourceStaNo = liftSta.getStaNo();//源站点
            //提升机号*100+目标楼层=目标站点
            staNo = liftSta.getLiftNo() * 100 + Utils.getLev(locNo);//目标站
        }
 
        // 获取工作号
        int workNo = commonService.getWorkNo(0);
        // 保存工作档
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(101L); // 工作状态:101.移动到近点等待迁出
        wrkMast.setIoType(200); // 入出库状态: 200.小车移库
        wrkMast.setIoPri(20D);
        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");
        wrkMast.setAppeTime(now);
        wrkMast.setModiTime(now);
        int res = wrkMastMapper.insert(wrkMast);
        if (res == 0) {
            News.error("小车迁移 --- 保存工作档失败! 穿梭车号:" + shuttleNo);
            throw new CoolException("保存工作档失败");
        }
 
        //给工作档绑定小车号
        WrkMast wrkMast1 = wrkMastMapper.selectByWorkNo(wrkNo);
        if (wrkMast1 != null) {
            wrkMast1.setShuttleNo(shuttleNo);
            wrkMastMapper.updateById(wrkMast1);
        }
 
        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)));//使用正常通道地图
            if (currentShuttlePath == null) {
                continue;
            }
            Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
            if (currentAllDistance < recentAllDistance) {
                //如果当前楼层的车路径更小,则更新最近站点
                recentSta = liftStaProtocol;
                recentAllDistance = currentAllDistance;
            }
        }
 
        return recentSta;
    }
 
}