From 92c572e50893a93a810bbe6adb6d3cdbfc225073 Mon Sep 17 00:00:00 2001
From: lsh <lsh@163.com>
Date: 星期二, 20 五月 2025 14:17:33 +0800
Subject: [PATCH] *
---
src/main/java/com/zy/core/thread/RgvThread.java | 271 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 247 insertions(+), 24 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/RgvThread.java b/src/main/java/com/zy/core/thread/RgvThread.java
index 0258e1d..365d9b9 100644
--- a/src/main/java/com/zy/core/thread/RgvThread.java
+++ b/src/main/java/com/zy/core/thread/RgvThread.java
@@ -14,6 +14,7 @@
import com.zy.asrs.service.BasRgvService;
import com.zy.asrs.utils.RouteUtils;
import com.zy.asrs.utils.TrackRangeUtils;
+import com.zy.core.DevpThread;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.*;
import com.zy.core.enums.RgvModeType;
@@ -25,6 +26,7 @@
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 lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -48,11 +50,11 @@
// private RgvProtocol rgvProtocol;
private TaskProtocolCache taskProtocolCache = new TaskProtocolCache();
// # 杞ㄩ亾鎬婚暱
- private Long trackEntireLength = 100L;
+ private Long trackEntireLength = 224000L;
//# 杞ㄩ亾鍩哄噯鐐�
- private Long trackBenchmark = 100L;
+ private Long trackBenchmark = 1L;
// # 閬胯璺濈
- private Long avoidDistance = 100L;
+ private Long avoidDistance = 5000L;
/**
* 宸ヤ綅1澶嶄綅淇″彿
@@ -63,6 +65,9 @@
*/
private boolean resetFlag2 = false;
+ private boolean connectRgv = false;
+ private boolean delRgvTask = false;
+
public RgvThread(RgvSlave slave) {
this.slave = slave;
}
@@ -70,20 +75,48 @@
@Override
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
- boolean connect = this.connect();
- if (connect) {
+ connectRgv = this.connect();
+ while(!connectRgv){
+ try {
+ connectRgv = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
- // 鍚姩璇绘暟鎹嚎绋�
- new Thread(this::readStatusRgv).start();
+ }
+ }
- // 鍚姩浠诲姟涓嬪彂绾跨▼
- new Thread(this::taskIssued).start();
+ // 鍚姩璇绘暟鎹嚎绋�
+ new Thread(this::rgvConnect).start();
- // 鍚姩婕父绾跨▼
- new Thread(this::taskWalkIssued).start();
+ new Thread(this::readStatusRgv).start();
- // 鍚姩浠诲姟瀹屾垚绾跨▼
- new Thread(this::taskComplete).start();
+ // 鍚姩浠诲姟涓嬪彂绾跨▼
+ new Thread(this::taskIssued).start();
+
+ // 鍚姩婕父绾跨▼
+ new Thread(this::taskWalkIssued).start();
+ new Thread(this::taskWalkIssued2).start();
+
+ // 鍚姩浠诲姟瀹屾垚绾跨▼
+ new Thread(this::taskComplete).start();
+ }
+
+ private void rgvConnect() {
+ while (true) {
+ try {
+ if(!connectRgv){
+ try {
+ connectRgv = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
+
+ }
+ }
+ } catch (Exception e) {
+ log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
+ initRgv();
+// e.printStackTrace();
+ }
}
}
@@ -110,11 +143,16 @@
private void taskComplete() {
while (true) {
try {
+ if (delRgvTask){
+ writeDelRgvTask();
+ delRgvTask = false;
+ continue;
+ }
Thread.sleep(100);
OperateResultExOne<byte[]> result = siemensNet.Read("DB100.12", (short) 1);
boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 0, 1);
if (status[0]){
- OperateResult result4 = siemensNet.Write("DB100.12.0", true);
+ OperateResult result4 = siemensNet.Write("DB100.12.0", false);
}
} catch (Exception e) {
log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
@@ -124,6 +162,63 @@
}
+ }
+
+ /**
+ * 浠诲姟涓嬪彂
+ */
+ 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鍙栬揣浠诲姟涓嬪彂绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
+// e.printStackTrace();
+ }
+ }
}
/**
@@ -149,6 +244,8 @@
TaskProtocol issued = new TaskProtocol();
issued.setTaskNo(32222L);
issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
+ issued.setTaskStatus(1);
+ issued.setDirection(true);
write(issued);
rgvTaskProtocol.setAvoid(0);
@@ -186,11 +283,29 @@
if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
continue;
}
+ if (rgvProtocol.getLoaded() == -1){
+ continue;
+ }
List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getTakeOrPutTaskProtocol(rgvProtocol.getLoaded());
for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
if (taskProtocol.getIsRunning() == 1) {//鍑嗗涓嬪彂
- RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
+// 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()) {
//鍙︿竴鍙拌溅鏄惁鍏佽姝ゅ彴杞︽墽琛�
@@ -198,12 +313,32 @@
continue;
}
}
- Thread.sleep(100);
- TaskProtocol issued = new TaskProtocol(taskProtocol);
- write(issued);
- taskProtocol.setIsRunning(4);
- taskProtocolCache.updateTaskProtocol(taskProtocol);
- break;
+ if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
+ && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
+ 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;
+ }
+ }
+ TaskProtocol issued = new TaskProtocol(taskProtocol);
+ write(issued);
+ taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
+ break;
+ } else {
+ TaskProtocol issued = new TaskProtocol(taskProtocol,true);
+ write(issued);
+ break;
+ }
}
}
} catch (Exception e) {
@@ -272,6 +407,10 @@
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
return false;
}
+ long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+ if (avoidAbs<51){
+ return true;
+ }
rgvTaskProtocolOther.setAvoid(1);
rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -288,6 +427,10 @@
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
return false;
}
+ long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+ if (avoidAbs<51){
+ return true;
+ }
rgvTaskProtocolOther.setAvoid(1);
rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -299,10 +442,14 @@
> avoidDistance) {//鏃犻渶閬胯
return true;
} else {
- if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()) {
+ if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()-50) {
long avoid = rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing() - avoidDistance - rgvProtocol.getCarBodyKunPeng();
if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
+ return false;
+ }
+ long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
+ if (avoidAbs<51){
return false;
}
rgvTaskProtocol.setAvoid(1);
@@ -311,6 +458,12 @@
return false;
}
return false;
+ }
+ } else {
+ if ((rgvProtocolOther.getRgvPosDestinationOrPos(true) - rgvProtocolOther.getCarBodyJiaoMing())
+ - (targetPosition + rgvProtocol.getCarBodyKunPeng())
+ > avoidDistance) {//鏃犻渶閬胯
+ return true;
}
}
} else {
@@ -324,6 +477,10 @@
if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
return false;
+ }
+ long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+ if (avoidAbs<51){
+ return true;
}
rgvTaskProtocolOther.setAvoid(1);
rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
@@ -341,6 +498,10 @@
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
return false;
}
+ long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+ if (avoidAbs<51){
+ return true;
+ }
rgvTaskProtocolOther.setAvoid(1);
rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -353,10 +514,14 @@
> avoidDistance) {//鏃犻渶閬胯
return true;
} else {
- if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos()) {
+ if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos() + 50) {
long avoid = rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng() + avoidDistance + rgvProtocol.getCarBodyJiaoMing();
if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯� 鑱旂郴绠$悊鍛橈紒锛侊紒");
+ return false;
+ }
+ long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
+ if (avoidAbs<51){
return false;
}
rgvTaskProtocol.setAvoid(1);
@@ -365,6 +530,12 @@
return false;
}
return false;
+ }
+ } else {
+ if ((rgvProtocolOther.getRgvPosDestinationOrPos(false) - rgvProtocolOther.getCarBodyJiaoMing())
+ - (targetPosition + rgvProtocol.getCarBodyKunPeng())
+ > avoidDistance) {//鏃犻渶閬胯
+ return true;
}
}
}
@@ -449,8 +620,10 @@
RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
if (rgvProtocol == null) {
rgvProtocol = new RgvProtocol();
- rgvProtocol.setRgvNo(slave.getId());
}
+ rgvProtocol.setRgvNo(slave.getId());
+ rgvProtocol.setCarBodyJiaoMing(slave.getCarBodyJiaoMing());
+ rgvProtocol.setCarBodyKunPeng(slave.getCarBodyKunPeng());
rgvProtocol.setRgvPos((long)siemensNet.getByteTransform().TransInt32(result.Content, 0));
rgvProtocol.setRgvPosDestination((long)siemensNet.getByteTransform().TransInt32(result.Content, 4));
@@ -509,6 +682,7 @@
} else {
initRgv();
+ connectRgv = false;
// OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戣鍙朢GV plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
log.error("璇诲彇RGV plc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
}
@@ -531,6 +705,8 @@
// convertRow(command);
// taskProtocol.setRgvNo(slave.getId());
Long[] array = new Long[11];
+ OperateResult result41 = siemensNet.Write("DB100.12.0", false);
+
// array[0] = taskProtocol.getAckFinish1();
// array[1] = taskProtocol.getTaskNo();
// array[2] = taskProtocol.getTaskMode();
@@ -584,6 +760,53 @@
}
}
+ private boolean writeDelRgvTask() throws InterruptedException {
+//
+ OperateResult result = siemensNet.Write("DB100.12.0", false);
+ OperateResult result0 = siemensNet.Write("DB100.0", (int) 0);
+ OperateResult result1 = siemensNet.Write("DB100.4", (short) 0);
+ OperateResult result2 = siemensNet.Write("DB100.6", (int) 0);
+ OperateResult result3 = siemensNet.Write("DB100.10", (short) 0); // 鎵ц鏂瑰悜锛堥潰鏈濊建閬� 瀹氫綅鍊煎乏灏忓彸澶э級 true:宸� false:鍙�
+
+ try {
+ // 鏃ュ織璁板綍
+ BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
+ BasRgvOpt basRgvOpt = new BasRgvOpt(
+ 0,
+ 0,
+ slave.getId(),
+ new Date(),
+ String.valueOf(0),
+ null,
+ null,
+ null,
+ result.IsSuccess ? 1 : 0,
+ null,
+ new Date(),
+ null
+ );
+ bean.insert(basRgvOpt);
+ } catch (Exception ignore) {
+ }
+
+ if (result != null && result.IsSuccess) {
+ 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;
+ } else {
+ OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戝啓鍏GV plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ log.error("鍐欏叆RGV plc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ return false;
+ }
+ }
+
+
+ public void setDelRgvTask() {
+ delRgvTask = true;
+ }
+
@Override
public void close() {
siemensNet.ConnectClose();
--
Gitblit v1.9.1