From e42e8364112e97d89eeaecd13f043dff42179949 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 27 五月 2025 17:19:02 +0800
Subject: [PATCH] 1.enum修改为enum class, 理顺CLoadPort各成员变量在调度中的判定作用;

---
 SourceCode/Bond/Servo/CMaster.cpp |  223 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 211 insertions(+), 12 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 7331415..3bd5d26 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -6,6 +6,15 @@
 namespace SERVO {
 	CMaster* g_pMaster = NULL;
 
+	unsigned __stdcall DispatchThreadFunction(LPVOID lpParam)
+	{
+		if (g_pMaster != NULL) {
+			return g_pMaster->DispatchProc();
+		}
+
+		return 0;
+	}
+
 	unsigned __stdcall ReadBitsThreadFunction(LPVOID lpParam)
 	{
 		if (g_pMaster != NULL) {
@@ -24,21 +33,26 @@
 
 	CMaster::CMaster()
 	{
-		m_listener = {nullptr, nullptr, nullptr, nullptr, nullptr};
+		m_listener = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
 		m_bDataModify = FALSE;
 		m_hEventReadBitsThreadExit[0] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
 		m_hEventReadBitsThreadExit[1] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
 		m_hReadBitsThreadHandle = nullptr;
 		m_nReadBitsThreadAddr = 0;
+		m_hDispatchEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+		m_hEventDispatchThreadExit[0] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+		m_hEventDispatchThreadExit[1] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+		m_hDispatchThreadHandle = nullptr;
+		m_nDispatchThreadAddr = 0;
+		m_ullStartTime = 0;
+		m_ullRunTime = 0;
+		m_state = MASTERSTATE::READY;
+		m_pActiveRobotTask = nullptr;
+		InitializeCriticalSection(&m_criticalSection);
 	}
 
 	CMaster::~CMaster()
 	{
-		for (auto item : m_listEquipment) {
-			delete item;
-		}
-		m_listEquipment.clear();
-
 		if (m_hEventReadBitsThreadExit[0] != nullptr) {
 			::CloseHandle(m_hEventReadBitsThreadExit[0]);
 			m_hEventReadBitsThreadExit[0] = nullptr;
@@ -48,15 +62,38 @@
 			::CloseHandle(m_hEventReadBitsThreadExit[1]);
 			m_hEventReadBitsThreadExit[1] = nullptr;
 		}
+
+		if (m_hDispatchEvent != nullptr) {
+			::CloseHandle(m_hDispatchEvent);
+			m_hDispatchEvent = nullptr;
+		}
+
+		if (m_hEventDispatchThreadExit[0] != nullptr) {
+			::CloseHandle(m_hEventDispatchThreadExit[0]);
+			m_hEventDispatchThreadExit[0] = nullptr;
+		}
+
+		if (m_hEventDispatchThreadExit[1] != nullptr) {
+			::CloseHandle(m_hEventDispatchThreadExit[1]);
+			m_hEventDispatchThreadExit[1] = nullptr;
+		}
+
+		DeleteCriticalSection(&m_criticalSection);
 	}
 
 	void CMaster::setListener(MasterListener listener)
 	{
+		m_listener.onMasterStateChanged = listener.onMasterStateChanged;
 		m_listener.onEqAlive = listener.onEqAlive;
 		m_listener.onEqCimStateChanged = listener.onEqCimStateChanged;
 		m_listener.onEqAlarm = listener.onEqAlarm;
 		m_listener.onEqVcrEventReport = listener.onEqVcrEventReport;
 		m_listener.onEqDataChanged = listener.onEqDataChanged;
+	}
+
+	CRobotTask* CMaster::getActiveRobotTask()
+	{
+		return m_pActiveRobotTask;
 	}
 
 	int CMaster::init()
@@ -100,6 +137,7 @@
 		CVacuumBake* pVacuumBake;
 		CAligner* pAligner;
 		CBakeCooling* pBakeCooling;
+		CMeasurement* pMeasurement;
 
 		pPort1 = addLoadPort(0);
 		pPort2 = addLoadPort(1);
@@ -115,6 +153,7 @@
 		pBonder1 = addBonder(0);
 		pBonder2 = addBonder(1);
 		pBakeCooling = addBakeCooling();
+		pMeasurement = addMeasurement();
 
 		ASSERT(pEfem);
 		ASSERT(pFliper);
@@ -123,6 +162,7 @@
 		ASSERT(pBonder1);
 		ASSERT(pBonder2);
 		ASSERT(pBakeCooling);
+		ASSERT(pMeasurement);
 
 		pEfem->setPort(0, pPort1);
 		pEfem->setPort(1, pPort1);
@@ -144,6 +184,7 @@
 		pBonder1->setArm(pArm);
 		pBonder2->setArm(pArm);
 		pBakeCooling->setArm(pArm);
+		pMeasurement->setArm(pArm);
 
 		connectEquipments();
 
@@ -160,6 +201,12 @@
 		SetTimer(NULL, 1, 250, (TIMERPROC)MasterTimerProc);
 
 
+		// 调度线程
+		m_hDispatchThreadHandle = (HANDLE)_beginthreadex(NULL, 0, SERVO::DispatchThreadFunction, this,
+			0, &m_nDispatchThreadAddr);
+
+
+		// 监控bit线程
 		m_hReadBitsThreadHandle = (HANDLE)_beginthreadex(NULL, 0, SERVO::ReadBitsThreadFunction, this,
 			0, &m_nReadBitsThreadAddr);
 
@@ -171,7 +218,9 @@
 	int CMaster::term()
 	{
 		SetEvent(m_hEventReadBitsThreadExit[0]);
+		SetEvent(m_hEventDispatchThreadExit[0]);
 		::WaitForSingleObject(m_hEventReadBitsThreadExit[1], INFINITE);
+		::WaitForSingleObject(m_hEventDispatchThreadExit[1], INFINITE);
 
 		LOGI("<Master>正在结束程序.");
 		for (auto item : m_listEquipment) {
@@ -180,7 +229,126 @@
 		saveCache();
 
 
+		for (auto item : m_listEquipment) {
+			delete item;
+		}
+		m_listEquipment.clear();
 
+
+		return 0;
+	}
+
+	int CMaster::start()
+	{
+		if (m_state != MASTERSTATE::READY) {
+			return -1;
+		}
+
+		setState(MASTERSTATE::STARTING);
+		m_ullStartTime = GetTickCount64();
+
+		return 0;
+	}
+
+	int CMaster::stop()
+	{
+		// 运行时间为累加结果,本次停止时刷新;
+		if (m_state != MASTERSTATE::RUNNING) {
+			return -1;
+		}
+
+		m_ullRunTime += (GetTickCount64() - m_ullStartTime);
+		setState(MASTERSTATE::STOPPING);
+
+		return 0;
+	}
+
+	ULONGLONG CMaster::getRunTime()
+	{
+		if (m_state == MASTERSTATE::RUNNING)
+			return m_ullRunTime + (GetTickCount64() - m_ullStartTime);
+		else
+			return m_ullRunTime;
+	}
+
+	MASTERSTATE CMaster::getState()
+	{
+		return m_state;
+	}
+
+	unsigned CMaster::DispatchProc()
+	{
+		while (1) {
+			// 待退出信号或时间到
+			HANDLE hEvents[] = { m_hEventDispatchThreadExit[0], m_hDispatchEvent };
+			int nRet = WaitForMultipleObjects(2, hEvents, FALSE, 1000);
+			if (nRet == WAIT_OBJECT_0) {
+				break;
+			}
+			
+
+			// 如果状态为STARTING,开始工作并切换到RUNNING状态
+			lock();
+			if (m_state == MASTERSTATE::STARTING) {
+				unlock();
+				Sleep(1000);
+				setState(MASTERSTATE::RUNNING);
+				continue;
+			}
+
+
+			// 处理完成当前事务后,切换到停止或就绪状态
+			else if (m_state == MASTERSTATE::STOPPING) {
+				unlock();
+				Sleep(1000);
+				setState(MASTERSTATE::READY);
+				continue;
+			}
+
+
+			// 调度逻辑处理
+			else if (m_state == MASTERSTATE::RUNNING) {
+				unlock();
+				LOGI("调度处理中...");
+				Sleep(1000);
+
+
+				// LoadPort -> Fliper(G2)
+
+
+				// LoadPort -> VacuumBake(G1)
+
+
+				// Fliper(G2) -> Aligner
+
+
+				// VacuumBake(G1) -> Aligner
+
+
+				// Aligner -> Bonder
+
+
+				// Bonder -> BakeCooling
+
+
+				// BakeCooling ->Measurement
+
+
+				// Measurement -> LoadPort
+
+
+
+
+
+			}
+			unlock();
+		}
+
+		SetEvent(m_hEventDispatchThreadExit[1]);
+
+
+		// _endthreadex(0);
+		TRACE("CMaster::DispatchProc 线程退出\n");
 		return 0;
 	}
 
@@ -304,8 +472,8 @@
 		CFliper* pEquipment = new CFliper();
 		pEquipment->setID(EQ_ID_FLIPER);
 		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
-		pEquipment->setName("Fliper");
-		pEquipment->setDescription("Fliper.");
+		pEquipment->setName("Fliper(G2)");
+		pEquipment->setDescription("Fliper(G2).");
 		pEquipment->setReadBitBlock(0x4000, 0x45ff);
 		pEquipment->setStation(0, 255);
 		addToEquipmentList(pEquipment);
@@ -321,8 +489,8 @@
 		CVacuumBake* pEquipment = new CVacuumBake();
 		pEquipment->setID(EQ_ID_VACUUMBAKE);
 		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
-		pEquipment->setName("VacuumBake");
-		pEquipment->setDescription("VacuumBake.");
+		pEquipment->setName("VacuumBake(G1)");
+		pEquipment->setDescription("VacuumBake(G1).");
 		pEquipment->setReadBitBlock(0x4000, 0x45ff);
 		pEquipment->setStation(0, 255);
 		addToEquipmentList(pEquipment);
@@ -444,6 +612,23 @@
 		return pEquipment;
 	}
 
+	CMeasurement* CMaster::addMeasurement()
+	{
+		CMeasurement* pEquipment = new CMeasurement();
+		pEquipment->setID(EQ_ID_MEASUREMENT);
+		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
+		pEquipment->setName("Measurement");
+		pEquipment->setDescription("Measurement.");
+		pEquipment->setReadBitBlock(0x6700, 0x6e00);
+		pEquipment->setStation(0, 255);
+		addToEquipmentList(pEquipment);
+
+		pEquipment->init();
+		LOGE("已添加“Measurement”.");
+
+		return pEquipment;
+	}
+
 	void CMaster::onTimer(UINT nTimerid)
 	{
 		for (auto item : m_listEquipment) {
@@ -496,6 +681,7 @@
 		CBonder* pBonder1 = (CBonder*)getEquipment(EQ_ID_Bonder1);
 		CBonder* pBonder2 = (CBonder*)getEquipment(EQ_ID_Bonder2);
 		CBakeCooling* pBakeCooling = (CBakeCooling*)getEquipment(EQ_ID_BAKE_COOLING);
+		CMeasurement* pMeasurement = (CMeasurement*)getEquipment(EQ_ID_MEASUREMENT);
 
 		nRet = pLoadPort1->getPin("Out1")->connectPin(pFliper->getPin("In1"));
 		if (nRet < 0) {
@@ -545,12 +731,17 @@
 			LOGE("连接Bonder2-BakeCooling失败");
 		}
 
-		nRet = pBakeCooling->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
+		nRet = pBakeCooling->getPin("Out")->connectPin(pMeasurement->getPin("In"));
 		if (nRet < 0) {
 			LOGE("连接BakeCooling-LoadPort3失败");
 		}
 
-		nRet = pBakeCooling->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
+		nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
+		if (nRet < 0) {
+			LOGE("连接BakeCooling-LoadPort3失败");
+		}
+
+		nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
 		if (nRet < 0) {
 			LOGE("连接BakeCooling-LoadPort4失败");
 		}
@@ -629,4 +820,12 @@
 			item->serialize(ar);
 		}
 	}
+
+	void CMaster::setState(MASTERSTATE state)
+	{
+		m_state = state;
+		if (m_listener.onMasterStateChanged != nullptr) {
+			m_listener.onMasterStateChanged(this, m_state);
+		}
+	}
 }

--
Gitblit v1.9.3