From 62a4a8db6c05c98f180ae7f6429a9a232fce6c0c Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期四, 03 七月 2025 17:19:16 +0800
Subject: [PATCH] 1. 重构机器人动画逻辑,采用分帧动画机制,避免在 OnTimer 中阻塞主线程; 2. 动画过程中允许动态中断,确保设备状态变化时可即时响应; 3. 动画结束时自动校正位置与角度,确保最终状态准确一致;

---
 SourceCode/Bond/Servo/CPageGraph1.h   |   31 +++++++++
 SourceCode/Bond/Servo/CPageGraph1.cpp |  124 ++++++++++++++++++++++++++++++++++-------
 2 files changed, 131 insertions(+), 24 deletions(-)

diff --git a/SourceCode/Bond/Servo/CPageGraph1.cpp b/SourceCode/Bond/Servo/CPageGraph1.cpp
index 6378512..60f2ac4 100644
--- a/SourceCode/Bond/Servo/CPageGraph1.cpp
+++ b/SourceCode/Bond/Servo/CPageGraph1.cpp
@@ -41,6 +41,7 @@
 // 瀹氭椂鍣�
 #define TIMER_ID_DEVICE_STATUS      1   // 鐢ㄤ簬鍒濆鍖栬澶囩姸鎬�
 #define TIMER_ID_ROBOT_STATUS       2   // 鐢ㄤ簬鍛ㄦ湡鍒锋柊鏈哄櫒浜轰綅缃�/鑷傜姸鎬�
+#define TIMER_ID_ROBOT_ANIMATION	3	// 
 
 // CPageGraph1 瀵硅瘽妗�
 
@@ -49,17 +50,29 @@
 CPageGraph1::CPageGraph1(CWnd* pParent /*=nullptr*/)
 	: CDialogEx(IDD_PAGE_GRAPH1, pParent)
 {
-	m_pGraph = nullptr;
-	m_pObserver = nullptr;
-	m_bIsRobotMoving = FALSE;
-	m_crBkgnd = PAGE_GRPAH1_BACKGROUND_COLOR;
-	m_hbrBkgnd = nullptr;
-	m_lastRobotPosition = SERVO::ROBOT_POSITION::Port1;
-	m_lastArmState[0] = FALSE;
-	m_lastArmState[1] = FALSE;
+	// ===== 鍥惧舰鐣岄潰鐩稿叧鎴愬憳鍙橀噺鍒濆鍖� =====
+	m_pGraph = nullptr;                                 // 鍥惧舰缁樺浘瀵硅薄
+	m_pObserver = nullptr;                              // 瑙傚療鑰呭璞★紙鍙兘鏄簨浠惰瀵熻�咃級
+	m_crBkgnd = PAGE_GRPAH1_BACKGROUND_COLOR;           // 鑳屾櫙棰滆壊
+	m_hbrBkgnd = nullptr;                               // 鑳屾櫙鍒峰彞鏌�
 
-	m_arm1Offset = LoadArmOffset("ARM1");
-	m_arm2Offset = LoadArmOffset("ARM2");
+	// ===== 鏈哄櫒浜哄姩鐢荤姸鎬佸垵濮嬪寲 =====
+	m_bIsRobotMoving = FALSE;                           // 褰撳墠鏄惁姝e湪鍔ㄧ敾绉诲姩
+	m_nRobotMoveStartX = 0;                             // 鍔ㄧ敾璧峰 X 鍧愭爣
+	m_nRobotMoveEndX = 0;                               // 鍔ㄧ敾鐩爣 X 鍧愭爣
+	m_nRobotMoveSteps = 30;                             // 鍔ㄧ敾鎬绘鏁帮紙鍔ㄧ敾閫熷害鎺у埗锛�
+	m_nRobotMoveCurrentStep = 0;                        // 褰撳墠鍔ㄧ敾姝ユ暟
+	m_nRobotMoveStartAngle = 0.0f;                      // 鍔ㄧ敾璧峰瑙掑害
+	m_nRobotMoveEndAngle = 0.0f;                        // 鍔ㄧ敾鐩爣瑙掑害
+
+	// ===== 鏈哄櫒浜轰笂涓�娆$姸鎬佸垵濮嬪寲 =====
+	m_lastRobotPosition = SERVO::ROBOT_POSITION::Port1; // 涓婃鏈哄櫒浜轰綅缃紙榛樿 Port1锛�
+	m_lastArmState[0] = FALSE;                          // 涓婃鏈烘鑷�1 鐘舵�侊紙鏈崰鐢級
+	m_lastArmState[1] = FALSE;                          // 涓婃鏈烘鑷�2 鐘舵�侊紙鏈崰鐢級
+
+	// ===== 鏈烘鑷傜浉瀵瑰亸绉婚噺鍒濆鍖栵紙浠庨厤缃腑鍔犺浇锛� =====
+	m_arm1Offset = LoadArmOffset("ARM1");               // 鍔犺浇鏈烘鑷�1鍋忕Щ
+	m_arm2Offset = LoadArmOffset("ARM2");               // 鍔犺浇鏈烘鑷�2鍋忕Щ
 
 	//m_arm1Offset = { -30, -45 }; // ARM1 浠庝腑蹇冨悜宸�47, 鍚戜笂33
 	//m_arm2Offset = { 27, -45 };	 // ARM2 浠庝腑蹇冨悜鍙�10, 鍚戜笂33
@@ -494,6 +507,32 @@
 	m_lastRobotPosition = position;
 }
 
+void CPageGraph1::StartRobotMoveToPosition(SERVO::ROBOT_POSITION position)
+{
+	auto it = g_positionMap.find(position);
+	if (it == g_positionMap.end()) {
+		TRACE("Invalid robot position: %d\n", static_cast<int>(position));
+		return;
+	}
+
+	const RobotPositionMapping& mapping = it->second;
+
+	auto* pImage = m_pGraph->GetImage(IMAGE_ROBOT);
+	if (!pImage) return;
+
+	m_nRobotMoveStartX = pImage->x;
+	m_nRobotMoveEndX = static_cast<int>(170 + mapping.percentage * (700 - 170));
+
+	m_nRobotMoveStartAngle = pImage->angle;     // 璧峰瑙掑害锛堝綋鍓嶈搴︼級
+	m_nRobotMoveEndAngle = mapping.angle;       // 鐩爣瑙掑害
+
+	m_nRobotMoveCurrentStep = 0;
+	m_targetRobotPosition = position;
+	m_bIsRobotMoving = TRUE;
+
+	SetTimer(TIMER_ID_ROBOT_ANIMATION, 16, nullptr);
+}
+
 POINT CPageGraph1::LoadArmOffset(const std::string& armName)
 {
 	std::string iniPath = GetConfigPath();
@@ -521,37 +560,37 @@
 
 	// 绉诲姩鍒版寚瀹氫綅缃� (娴嬭瘯浣跨敤)
 	if (pGraphNmhdr->dwData == INDICATE_LPORT1) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Port1);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Port1);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_LPORT2) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Port2);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Port2);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_LPORT3) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Port3);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Port3);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_LPORT4) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Port4);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Port4);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_ALIGNER) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Aligner);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Aligner);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_FLIPER) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Fliper);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Fliper);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_BONDER1) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Bonder1);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Bonder1);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_BONDER2) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Bonder2);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Bonder2);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_VACUUM_BAKE) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Bake);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Bake);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_BAKE_COOLING) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Cooling);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Cooling);
 	}
 	else if (pGraphNmhdr->dwData == INDICATE_MEASUREMENT) {
-		MoveRobotToPosition(SERVO::ROBOT_POSITION::Measurement);
+		StartRobotMoveToPosition(SERVO::ROBOT_POSITION::Measurement);
 	}
 	
 	*pResult = 0;
@@ -636,9 +675,50 @@
 
 		// 浣嶇疆淇℃伅鐘舵�佹樉绀�
 		if (robotData.position != m_lastRobotPosition) {
-			MoveRobotToPosition(robotData.position);
+			StartRobotMoveToPosition(robotData.position);
 		}
 	}
+	else if (nIDEvent == TIMER_ID_ROBOT_ANIMATION) {
+		if (!m_bIsRobotMoving) {
+			KillTimer(TIMER_ID_ROBOT_ANIMATION);
+			return;
+		}
+
+		m_nRobotMoveCurrentStep++;
+
+		float progress = static_cast<float>(m_nRobotMoveCurrentStep) / m_nRobotMoveSteps;
+		if (progress >= 1.0f) {
+			progress = 1.0f;
+			m_bIsRobotMoving = FALSE;
+			KillTimer(TIMER_ID_ROBOT_ANIMATION);
+			m_lastRobotPosition = m_targetRobotPosition;
+		}
+
+		// 骞虫粦璁$畻浣嶇疆
+		int currentX = static_cast<int>(m_nRobotMoveStartX + progress * (m_nRobotMoveEndX - m_nRobotMoveStartX));
+		auto* pImage = m_pGraph->GetImage(IMAGE_ROBOT);
+		if (!pImage) return;
+
+		int cx = currentX + pImage->bmWidth / 2;
+		int y = 270;
+		int cy = y + pImage->bmHeight / 2;
+
+		// 骞虫粦璁$畻瑙掑害
+		float currentAngle = m_nRobotMoveStartAngle + progress * (m_nRobotMoveEndAngle - m_nRobotMoveStartAngle);
+		float radians = currentAngle * 3.1415926f / 180.0f;
+
+		int rotatedX1 = static_cast<int>(cos(radians) * m_arm1Offset.x - sin(radians) * m_arm1Offset.y);
+		int rotatedY1 = static_cast<int>(sin(radians) * m_arm1Offset.x + cos(radians) * m_arm1Offset.y);
+		int rotatedX2 = static_cast<int>(cos(radians) * m_arm2Offset.x - sin(radians) * m_arm2Offset.y);
+		int rotatedY2 = static_cast<int>(sin(radians) * m_arm2Offset.x + cos(radians) * m_arm2Offset.y);
+
+		m_pGraph->UpdateImageCoordinates(IMAGE_ROBOT, currentX, y);
+		m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, cx + rotatedX1, cy + rotatedY1);
+		m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, cx + rotatedX2, cy + rotatedY2);
+		m_pGraph->UpdateImageAngle(IMAGE_ROBOT, currentAngle);
+
+		Invalidate();
+	}
 
 	CDialogEx::OnTimer(nIDEvent);
 }
diff --git a/SourceCode/Bond/Servo/CPageGraph1.h b/SourceCode/Bond/Servo/CPageGraph1.h
index f891489..ffd2aeb 100644
--- a/SourceCode/Bond/Servo/CPageGraph1.h
+++ b/SourceCode/Bond/Servo/CPageGraph1.h
@@ -34,6 +34,7 @@
 	void RotateRobot(float angleInDegrees);
 	void BindEquipmentToGraph();
 	void MoveRobotToPosition(SERVO::ROBOT_POSITION position);
+	void StartRobotMoveToPosition(SERVO::ROBOT_POSITION position);
 	POINT LoadArmOffset(const std::string& armName);
 	void SaveArmOffset(const std::string& armName, const POINT& pt);
 
@@ -43,10 +44,36 @@
 	BOOL m_bIsRobotMoving;
 	COLORREF m_crBkgnd;
 	HBRUSH m_hbrBkgnd;
+
+	// ===== 鏈哄櫒浜哄姩鐢荤浉鍏虫垚鍛樺彉閲� =====
+
+	// 鍔ㄧ敾鐩爣浣嶇疆锛堟満鍣ㄤ汉鐩爣浣嶇疆鏋氫妇锛�
+	SERVO::ROBOT_POSITION m_targetRobotPosition;
+
+	// 鍔ㄧ敾璧峰鍜岀洰鏍� X 鍧愭爣锛堟按骞充綅绉伙級
+	int m_nRobotMoveStartX;
+	int m_nRobotMoveEndX;
+
+	// 鍔ㄧ敾姝ユ暟鎺у埗锛堟�绘鏁� & 褰撳墠姝ワ級
+	int m_nRobotMoveSteps;          // 鍔ㄧ敾鎬绘鏁帮紙鎺у埗鍔ㄧ敾閫熷害锛�
+	int m_nRobotMoveCurrentStep;    // 褰撳墠鍔ㄧ敾姝ユ暟杩涘害
+
+	// 鍔ㄧ敾璧峰鍜岀洰鏍囪搴︼紙鏃嬭浆瑙掑害锛屽崟浣嶏細搴︼級
+	float m_nRobotMoveStartAngle;   // 璧峰瑙掑害
+	float m_nRobotMoveEndAngle;     // 鐩爣瑙掑害锛堟棆杞搴︼級
+
+	// ===== 鏈哄櫒浜虹姸鎬佺浉鍏虫垚鍛樺彉閲� =====
+
+	// 涓婁竴娆″凡瀹屾垚鐨勪綅缃紙鐢ㄤ簬鍒ゆ柇鏄惁闇�瑕佺Щ鍔級
 	SERVO::ROBOT_POSITION m_lastRobotPosition;
+
+	// 涓婁竴娆″凡鏇存柊鐨勬満鍣ㄤ汉 ARM 鍗犵敤鐘舵�侊紙涓や釜鏈烘鑷傦級
 	BOOL m_lastArmState[2];
-	POINT m_arm1Offset; // ARM1 浠庝腑蹇冨悜宸�47, 鍚戜笂33
-	POINT m_arm2Offset; // ARM2 浠庝腑蹇冨悜鍙�10, 鍚戜笂33
+
+	// 涓や釜鏈烘鑷傜浉瀵逛簬鏈哄櫒浜轰腑蹇冪殑鍋忕Щ锛堝儚绱犲潗鏍囷級
+	POINT m_arm1Offset;
+	POINT m_arm2Offset;
+
 
 // 瀵硅瘽妗嗘暟鎹�
 #ifdef AFX_DESIGN_TIME

--
Gitblit v1.9.3