From 22f2984276ff75433ebeab877c96b48905826e6c Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期五, 06 二月 2026 16:51:25 +0800
Subject: [PATCH] 1.Slot防护

---
 SourceCode/Bond/Servo/CMaster.cpp |  219 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 193 insertions(+), 26 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 13146b8..492e813 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -63,6 +63,7 @@
 		m_ullRunTime = 0;
 		m_state = MASTERSTATE::READY;
 		m_curveMode = CurveMode::Production;
+		m_schedulingMode = SchedulingMode::Production;
 		m_pActiveRobotTask = nullptr;
 		m_nLastError = ER_CODE_NOERROR;
 		m_isCompareMapsBeforeProceeding = FALSE;
@@ -310,6 +311,17 @@
 	CurveMode CMaster::getCurveMode() const
 	{
 		return m_curveMode;
+	}
+
+	void CMaster::setSchedulingMode(SchedulingMode mode)
+	{
+		m_schedulingMode = mode;
+		LOGI("<Master>SchedulingMode=%s", mode == SchedulingMode::Production ? "Production" : "Tuning");
+	}
+
+	SchedulingMode CMaster::getSchedulingMode() const
+	{
+		return m_schedulingMode;
 	}
 
 	int CMaster::term()
@@ -746,14 +758,22 @@
 
 
 				// Measurement -> LoadPort
-				for (int s = 0; s < 4; s++) {
-					PortType pt = pLoadPorts[s]->getPortType();
-					if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
-						&& (pt == PortType::Unloading || pt == PortType::Both)
-						&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
-						m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
-						if (m_pActiveRobotTask != nullptr) {
-							goto PORT_PUT;
+				if (m_schedulingMode == SchedulingMode::Production) {
+					if (!rmd.armState[0]) {
+						m_pActiveRobotTask = createTransferTask_returnOrigin(pMeasurement, pLoadPorts);
+						CHECK_RUN_ACTIVE_ROBOT_TASK(m_pActiveRobotTask);
+					}
+				}
+				else {
+					for (int s = 0; s < 4; s++) {
+						PortType pt = pLoadPorts[s]->getPortType();
+						if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
+							&& (pt == PortType::Unloading || pt == PortType::Both)
+							&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
+							m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
+							if (m_pActiveRobotTask != nullptr) {
+								goto PORT_PUT;
+							}
 						}
 					}
 				}
@@ -1008,19 +1028,27 @@
 				// 缁勬暟闂ㄩ檺锛氣墺2 缁勬椂涓嶅啀浠� LP 涓婄墖锛岄伩鍏嶅爢绉紙涓庡崟鐗囦竴鑷达級
 				bool blockLoadFromLP = (nGlassGroup >= 2);
 
-				// 7) Measurement -> LoadPort锛堝浐瀹氾細G1 浼樺厛鍥� LP锛�
+				// 7) Measurement -> LoadPort
 				if (rmd.armState[0] || rmd.armState[1]) {
 					LOGD("Arm1 %s, Arm2 %s.",
 						rmd.armState[0] ? _T("涓嶅彲鐢�") : _T("鍙敤"),
 						rmd.armState[1] ? _T("涓嶅彲鐢�") : _T("鍙敤"));
 				}
-				for (int s = 0; s < 4; s++) {
-					PortType pt = pLoadPorts[s]->getPortType();
-					if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
-						&& (pt == PortType::Unloading || pt == PortType::Both)
-						&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
-						m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
-						if (m_pActiveRobotTask != nullptr) { goto BATCH_PORT_PUT; }
+				if (m_schedulingMode == SchedulingMode::Production) {
+					if (!rmd.armState[0]) {
+						m_pActiveRobotTask = createTransferTask_returnOrigin(pMeasurement, pLoadPorts);
+						CHECK_RUN_ACTIVE_ROBOT_TASK(m_pActiveRobotTask);
+					}
+				}
+				else {
+					for (int s = 0; s < 4; s++) {
+						PortType pt = pLoadPorts[s]->getPortType();
+						if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
+							&& (pt == PortType::Unloading || pt == PortType::Both)
+							&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
+							m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
+							if (m_pActiveRobotTask != nullptr) { goto BATCH_PORT_PUT; }
+						}
 					}
 				}
 
@@ -2449,11 +2477,19 @@
 
 		nRet = pLoadPort1->getPin("Out")->connectPin(pAligner->getPin("In1"));
 		if (nRet < 0) {
-			LOGE("杩炴帴LoadPort1-Fliper澶辫触");
+			LOGE("杩炴帴LoadPort1-Aligner澶辫触");
 		}
 		nRet = pLoadPort2->getPin("Out")->connectPin(pAligner->getPin("In2"));
 		if (nRet < 0) {
-			LOGE("杩炴帴LoadPort1-Fliper澶辫触");
+			LOGE("杩炴帴LoadPort2-Aligner澶辫触");
+		}
+		nRet = pLoadPort3->getPin("Out")->connectPin(pAligner->getPin("In3"));
+		if (nRet < 0) {
+			LOGE("杩炴帴LoadPort3-Aligner澶辫触");
+		}
+		nRet = pLoadPort4->getPin("Out")->connectPin(pAligner->getPin("In4"));
+		if (nRet < 0) {
+			LOGE("杩炴帴LoadPort4-Aligner澶辫触");
 		}
 
 		nRet = pAligner->getPin("Out1")->connectPin(pFliper->getPin("In"));
@@ -2498,14 +2534,29 @@
 			LOGE("杩炴帴BakeCooling-LoadPort3澶辫触");
 		}
 
-		nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
-		if (nRet < 0) {
-			LOGE("杩炴帴BakeCooling-LoadPort3澶辫触");
-		}
+		if (m_schedulingMode == SchedulingMode::Production) {
+			// 鐢熶骇妯″紡锛氭祴閲忚緭鍑哄洖鍒� G1 鍘熶綅锛堥粯璁� Port1 / Port3锛�
+			nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort1->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort1澶辫触");
+			}
 
-		nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
-		if (nRet < 0) {
-			LOGE("杩炴帴BakeCooling-LoadPort4澶辫触");
+			nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort3->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort3澶辫触");
+			}
+		}
+		else {
+			// 璋冩満妯″紡锛氱淮鎸佸師杩炴帴锛圤ut1->Port3, Out2->Port4锛�
+			nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort3澶辫触");
+			}
+
+			nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort4澶辫触");
+			}
 		}
 	}
 
@@ -2735,6 +2786,57 @@
 		return pTask;
 	}
 
+	CRobotTask* CMaster::createTransferTask_returnOrigin(CEquipment* pEqSrc, CLoadPort** pPorts)
+	{
+		if (!pEqSrc->IsEnabled()) {
+			return nullptr;
+		}
+
+		CSlot* pSrcSlot = pEqSrc->getProcessedSlot(MaterialsType::G1, m_bJobMode);
+		if (pSrcSlot == nullptr) {
+			return nullptr;
+		}
+
+		CGlass* pGlass = (CGlass*)pSrcSlot->getContext();
+		if (pGlass == nullptr) {
+			return nullptr;
+		}
+
+		int port = 0, slot = 0;
+		pGlass->getOrginPort(port, slot);
+		if (port < 0 || port >= 4 || slot < 0 || slot >= SLOT_MAX) {
+			return nullptr;
+		}
+
+		CLoadPort* pPort = pPorts[port];
+		if (pPort == nullptr || !pPort->isEnable()) {
+			return nullptr;
+		}
+		PortType pt = pPort->getPortType();
+		if (!(pt == PortType::Unloading || pt == PortType::Both)) {
+			return nullptr;
+		}
+		if (pPort->getPortStatus() != PORT_INUSE) {
+			return nullptr;
+		}
+
+		CSlot* pTarSlot = pPort->getSlot(slot);
+		if (pTarSlot == nullptr) {
+			return nullptr;
+		}
+		if (!pTarSlot->isEnable() || pTarSlot->isLock() || pTarSlot->getContext() != nullptr) {
+			return nullptr;
+		}
+
+		CRobotTask* pTask = new CRobotTask();
+		pTask->setContext(pSrcSlot->getContext());
+		pTask->setEFEM((CEFEM*)getEquipment(EQ_ID_EFEM));
+		taskSeqNo = pTask->setRobotTransferParam(taskSeqNo, 1, pSrcSlot->getPosition(),
+			pTarSlot->getPosition(), pSrcSlot->getNo(), pTarSlot->getNo());
+
+		return pTask;
+	}
+
 	CRobotTask* CMaster::createTransferTask_continuous_transfer(CEquipment* pSrcEq, int nSrcSlot,
 		CEquipment* pTarEq, int nTarSlot, int armNo/* = 1*/)
 	{
@@ -2844,6 +2946,19 @@
 		pPort->localSetCessetteType(type);
 	}
 
+	void CMaster::applySchedulingModePortMapping()
+	{
+		// 鐢熶骇妯″紡锛氬浐瀹� Port1/Port3 涓� G1锛孭ort2/Port4 涓� G2锛圙4 鏈畾涔夛紝鎸� G2 澶勭悊锛�
+		if (m_schedulingMode != SchedulingMode::Production) {
+			return;
+		}
+
+		setPortCassetteType(0, SERVO::CassetteType::G1);
+		setPortCassetteType(1, SERVO::CassetteType::G2);
+		setPortCassetteType(2, SERVO::CassetteType::G1);
+		setPortCassetteType(3, SERVO::CassetteType::G2);
+	}
+
 	void CMaster::setPortEnable(unsigned int index, BOOL bEnable)
 	{
 		ASSERT(index < 4);
@@ -2907,7 +3022,54 @@
 
 		static int pid[] = { EQ_ID_LOADPORT1, EQ_ID_LOADPORT2, EQ_ID_LOADPORT3, EQ_ID_LOADPORT4};
 		CLoadPort* pPort = (CLoadPort*)getEquipment(pid[port]);
-		pPort->sendCassetteCtrlCmd(CCC_PROCESS_START, nullptr, 0, 0, 0, nullptr, nullptr);
+		if (pPort == nullptr) return -1;
+
+		short jobExistence[12] = { 0 };
+		short slotProcess = 0;
+		short jobCount = 0; // 0 = Process All Glass
+		bool anyScheduled = false;
+
+		// Prefer hardware scan map for job existence (first 16 slots).
+		const short scanMap = pPort->getScanCassetteMap();
+		if (scanMap != 0) {
+			jobExistence[0] = scanMap;
+		}
+
+		const int maxSlots = 12 * 16;
+		const int totalSlots = (SLOT_MAX < maxSlots) ? SLOT_MAX : maxSlots;
+		for (int slot = 1; slot <= totalSlots; ++slot) {
+			CGlass* pGlass = pPort->getGlassFromSlot(slot);
+			if (pGlass == nullptr) continue;
+
+			const int wordIndex = (slot - 1) / 16;
+			const int bitIndex = (slot - 1) % 16;
+			jobExistence[wordIndex] = (short)(jobExistence[wordIndex] | (1 << bitIndex));
+
+			if (slot <= 16 && pGlass->isScheduledForProcessing()) {
+				slotProcess = (short)(slotProcess | (1 << bitIndex));
+				anyScheduled = true;
+			}
+		}
+
+		if (!anyScheduled) {
+			slotProcess = jobExistence[0];
+		}
+
+		bool hasExistence = false;
+		for (short w : jobExistence) {
+			if (w != 0) { hasExistence = true; break; }
+		}
+		const int portStatus = pPort->getPortStatus();
+		if (!hasExistence) {
+			LOGE("ProcessStart blocked (ProceedWithCarrier): no JobExistence map (port=%u, portStatus=%d, scanMap=%d, cassetteId=%s).",
+				port + 1, portStatus, scanMap, pPort->getCassetteId().c_str());
+			return -2;
+		}
+		if (portStatus != PORT_INUSE) {
+			LOGW("ProcessStart warning (ProceedWithCarrier): port status is %d (expected INUSE).", portStatus);
+		}
+
+		pPort->sendCassetteCtrlCmd(CCC_PROCESS_START, jobExistence, 12, slotProcess, jobCount, nullptr, nullptr);
 		return 0;
 	}
 
@@ -3495,6 +3657,11 @@
 		}
 		m_pControlJob->abort(description);
 
+		// 鍏堜笂鎶ヤ竴娆$姸鎬佸彉鍖栵紙渚夸簬 PrJobAbort 瑙﹀彂锛�
+		if (m_listener.onControlJobChanged) {
+			notifyControlJobChanged();
+		}
+
 
 		// 閲婃斁Job鐩稿叧
 		for (auto item : m_processJobs) {

--
Gitblit v1.9.3