SourceCode/Bond/Servo/ServoDlg.cpp
@@ -8,12 +8,18 @@
#include "afxdialogex.h"
#include "Common.h"
#include "Log.h"
#include "SecsTestDlg.h"
#include <chrono>
#include <thread>
#include <cmath>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Image
#define IMAGE_ROBOT            2
#define INDICATE_BONDER1      1
#define INDICATE_BONDER2      2
@@ -74,6 +80,7 @@
   m_crBkgnd = APPDLG_BACKGROUND_COLOR;
   m_hbrBkgnd = nullptr;
   m_bShowLogWnd = FALSE;
   m_bIsRobotMoving = FALSE;
   m_pLogDlg = nullptr;
}
@@ -107,6 +114,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()
@@ -155,6 +163,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
@@ -203,10 +213,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");
@@ -378,7 +388,8 @@
void CServoDlg::OnMenuFileSecsTest()
{
   AfxMessageBox("OnMenuFileSecsTest");
   CSecsTestDlg dlg;
   dlg.DoModal();
}
void CServoDlg::OnUpdateMenuFileSecsTest(CCmdUI* pCmdUI)
@@ -484,6 +495,93 @@
   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);
   // 开始移动,设置标记
   m_bIsRobotMoving = TRUE;
   // 开始平滑移动
   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();
   // 动画结束,设置标记
   m_bIsRobotMoving = FALSE;
}
void CServoDlg::RotateRobot(float angleInDegrees)
{
   return;
   // 将角度转换为弧度
   float angleInRadians = angleInDegrees * (std::acos(-1) / 180.0f);
   // 获取机器人图片的当前坐标和中心
   auto* pImage = m_pGraph->GetImage(IMAGE_ROBOT);
   if (!pImage) return;
   int cx = pImage->x + pImage->bmWidth / 2;  // 图片中心 X
   int cy = pImage->y + pImage->bmHeight / 2; // 图片中心 Y
   // m_pGraph->UpdateImageAngle(IMAGE_ROBOT, angleInDegrees);
   auto* pRobot1 = m_pGraph->GetIndicateBox(INDICATE_ROBOT_ARM1);
   auto* pRobot2 = m_pGraph->GetIndicateBox(INDICATE_ROBOT_ARM2);
   // 旋转指示框的坐标
   int newArmX1 = static_cast<int>(cx + (pRobot1->x - cx) * cos(angleInRadians) - (pRobot1->y - cy) * sin(angleInRadians));
   int newArmY1 = static_cast<int>(cy + (pRobot1->x - cx) * sin(angleInRadians) + (pRobot1->y - cy) * cos(angleInRadians));
   int newArmX2 = static_cast<int>(cx + (pRobot2->x - cx) * cos(angleInRadians) - (pRobot2->y - cy) * sin(angleInRadians));
   int newArmY2 = static_cast<int>(cy + (pRobot2->x - cx) * sin(angleInRadians) + (pRobot2->y - cy) * cos(angleInRadians));
   // 更新指示框的位置
   m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, newArmX1, newArmY1);
   m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, newArmX2, newArmY2);
   Invalidate();
}
void CServoDlg::OnSize(UINT nType, int cx, int cy)
{
   CDialogEx::OnSize(nType, cx, cy);
@@ -542,3 +640,15 @@
   CDialogEx::OnMove(x, y);
}
BOOL CServoDlg::OnEraseBkgnd(CDC* pDC)
{
   // TODO: 在此添加消息处理程序代码和/或调用默认值
   if (m_bIsRobotMoving) {
      // 禁止刷新背景,避免闪烁
      return TRUE;
   }
   return CDialogEx::OnEraseBkgnd(pDC);
}