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/Servo/ServoDlg.cpp | 69 +++++++++++++++++++++++++++++++++-
1 files changed, 67 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;
+}
--
Gitblit v1.9.3