From 393f5685448eab573f2c1f15f0725e49ffea8d4b Mon Sep 17 00:00:00 2001
From: darker <mr.darker@163.com>
Date: 星期四, 16 一月 2025 15:26:52 +0800
Subject: [PATCH] 1. 添加机械臂水平移动功能
---
SourceCode/Bond/x64/Debug/Res/Robot001.bmp | 0
SourceCode/Bond/x64/Debug/Res/Servo002.bmp | 0
SourceCode/Bond/x64/Debug/Res/Servo001.bmp | 0
SourceCode/Bond/Servo/ServoGraph.cpp | 18 +++++++++
SourceCode/Bond/Servo/ServoGraph.h | 2 +
SourceCode/Bond/Servo/ServoDlg.cpp | 69 +++++++++++++++++++++++++++++++++-
SourceCode/Bond/Servo/ServoDlg.h | 2 +
7 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index 7d7b5ad..e0156b3 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -9,12 +9,16 @@
#include "Common.h"
#include "Log.h"
#include "SecsTestDlg.h"
+#include <chrono>
+#include <thread>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
+// Image
+#define IMAGE_ROBOT 2
#define INDICATE_BONDER1 1
#define INDICATE_BONDER2 2
@@ -108,6 +112,7 @@
ON_UPDATE_COMMAND_UI(ID_MENU_WND_LOG, &CServoDlg::OnUpdateMenuWndLog)
ON_COMMAND(ID_MENU_HELP_ABOUT, &CServoDlg::OnMenuHelpAbout)
ON_WM_INITMENUPOPUP()
+ ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
@@ -156,6 +161,8 @@
strPath.Format(_T("%s\\res\\Servo001.bmp"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
m_pGraph->AddImage(1, (LPTSTR)(LPCTSTR)strPath, 0, 0);
+ strPath.Format(_T("%s\\res\\Robot001.bmp"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
+ m_pGraph->AddImage(IMAGE_ROBOT, (LPTSTR)(LPCTSTR)strPath, 170, 270);
// 添加指示器
// Bonder
@@ -204,10 +211,10 @@
// Robot
- m_pGraph->AddIndicateBox(INDICATE_ROBOT_ARM1, 620, 294, 48, RGB(22, 22, 22),
+ m_pGraph->AddIndicateBox(INDICATE_ROBOT_ARM1, 190, 294, 48, RGB(22, 22, 22),
RGB(255, 127, 39), RGB(0, 176, 80));
m_pGraph->SetBoxText(INDICATE_ROBOT_ARM1, "5", "Robot");
- m_pGraph->AddIndicateBox(INDICATE_ROBOT_ARM2, 673, 294, 48, RGB(22, 22, 22),
+ m_pGraph->AddIndicateBox(INDICATE_ROBOT_ARM2, 243, 294, 48, RGB(22, 22, 22),
RGB(255, 127, 39), RGB(0, 176, 80));
m_pGraph->SetBoxText(INDICATE_ROBOT_ARM2, "6", "Robot");
@@ -486,6 +493,55 @@
m_btnLog.Invalidate();
}
+void CServoDlg::UpdateRobotPosition(float percentage)
+{
+ // 限制百分比范围在 [0, 1] 之间
+ if (percentage < 0.0f) percentage = 0.0f;
+ if (percentage > 1.0f) percentage = 1.0f;
+
+ // 根据百分比计算目标 X 坐标
+ int startX = m_pGraph->GetImage(IMAGE_ROBOT)->x;
+ int endX = static_cast<int>(170 + percentage * (700 - 170));
+
+ int arm1Offset = 20; // 从图片到ARM1的偏移
+ int arm2Offset = 73; // 从图片到ARM2的偏移
+
+ // 计算移动所需的时间
+ int distance = abs(endX - startX);
+ int duration = static_cast<int>((distance / 100.0) * 1000);
+
+ auto startTime = std::chrono::steady_clock::now();
+ auto endTime = startTime + std::chrono::milliseconds(duration);
+
+ // 开始平滑移动
+ while (std::chrono::steady_clock::now() < endTime) {
+ auto currentTime = std::chrono::steady_clock::now();
+ float progress = std::chrono::duration<float, std::milli>(currentTime - startTime).count() / duration;
+ progress = min(progress, 1.0f);
+
+ // 根据进度计算当前位置
+ int currentX = static_cast<int>(startX + progress * (endX - startX));
+ m_pGraph->UpdateImageCoordinates(IMAGE_ROBOT, currentX, 270);
+ m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, currentX + arm1Offset, 294);
+ m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, currentX + arm2Offset, 294);
+
+ // 刷新界面
+ Invalidate();
+ UpdateWindow();
+
+ // 控制帧率约为 60 FPS
+ std::this_thread::sleep_for(std::chrono::milliseconds(16));
+ }
+
+ // 确保最后位置精确到目标位置
+ m_pGraph->UpdateImageCoordinates(IMAGE_ROBOT, endX, 270);
+ m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, endX + arm1Offset, 294);
+ m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, endX + arm2Offset, 294);
+
+ // 界面重绘
+ Invalidate();
+}
+
void CServoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
@@ -544,3 +600,12 @@
CDialogEx::OnMove(x, y);
}
+
+
+BOOL CServoDlg::OnEraseBkgnd(CDC* pDC)
+{
+ // TODO: 在此添加消息处理程序代码和/或调用默认值
+
+ //return CDialogEx::OnEraseBkgnd(pDC);
+ return TRUE;
+}
diff --git a/SourceCode/Bond/Servo/ServoDlg.h b/SourceCode/Bond/Servo/ServoDlg.h
index 3b1cd3b..f0a270e 100644
--- a/SourceCode/Bond/Servo/ServoDlg.h
+++ b/SourceCode/Bond/Servo/ServoDlg.h
@@ -19,6 +19,7 @@
private:
void Resize();
void UpdateLogBtn();
+ void UpdateRobotPosition(float percentage);
// 对话框数据
@@ -70,4 +71,5 @@
afx_msg void OnMenuFileExit();
afx_msg void OnUpdateMenuFileExit(CCmdUI* pCmdUI);
afx_msg void OnMenuHelpAbout();
+ afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
diff --git a/SourceCode/Bond/Servo/ServoGraph.cpp b/SourceCode/Bond/Servo/ServoGraph.cpp
index 718661c..32d4dfa 100644
--- a/SourceCode/Bond/Servo/ServoGraph.cpp
+++ b/SourceCode/Bond/Servo/ServoGraph.cpp
@@ -627,3 +627,21 @@
SendMessage(m_hWndTooltip, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&tti);
}
}
+
+void CServoGraph::UpdateImageCoordinates(int id, int newX, int newY)
+{
+ IMAGE* pImage = GetImage(id);
+ if (pImage != nullptr) {
+ pImage->x = newX;
+ pImage->y = newY;
+ }
+}
+
+void CServoGraph::UpdateIndicateBoxCoordinates(int id, int newX, int newY)
+{
+ INDICATEBOX* pIndicateBox = GetIndicateBox(id);
+ if (pIndicateBox != nullptr) {
+ pIndicateBox->x = newX;
+ pIndicateBox->y = newY;
+ }
+}
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/ServoGraph.h b/SourceCode/Bond/Servo/ServoGraph.h
index a5ca74f..445d676 100644
--- a/SourceCode/Bond/Servo/ServoGraph.h
+++ b/SourceCode/Bond/Servo/ServoGraph.h
@@ -166,6 +166,8 @@
BOOL GetIndicateBoxRect(int id, LPRECT lprcBox);
void SetBoxText(int id, const char* pszText, const char* pszTooltip);
HWND GetSafeWnd();
+ void UpdateImageCoordinates(int id, int newX, int newY);
+ void UpdateIndicateBoxCoordinates(int id, int newX, int newY);
private:
HWND m_hWnd;
diff --git a/SourceCode/Bond/x64/Debug/Res/Robot001.bmp b/SourceCode/Bond/x64/Debug/Res/Robot001.bmp
new file mode 100644
index 0000000..5247864
--- /dev/null
+++ b/SourceCode/Bond/x64/Debug/Res/Robot001.bmp
Binary files differ
diff --git a/SourceCode/Bond/x64/Debug/Res/Servo001.bmp b/SourceCode/Bond/x64/Debug/Res/Servo001.bmp
index 024cf3b..3eb0061 100644
--- a/SourceCode/Bond/x64/Debug/Res/Servo001.bmp
+++ b/SourceCode/Bond/x64/Debug/Res/Servo001.bmp
Binary files differ
diff --git a/SourceCode/Bond/x64/Debug/Res/Servo002.bmp b/SourceCode/Bond/x64/Debug/Res/Servo002.bmp
new file mode 100644
index 0000000..024cf3b
--- /dev/null
+++ b/SourceCode/Bond/x64/Debug/Res/Servo002.bmp
Binary files differ
--
Gitblit v1.9.3