From ed6936545d20cc490145d2936cee4387be2afd53 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期一, 22 十二月 2025 19:04:34 +0800
Subject: [PATCH] 优化了导航预览模式
---
src/dikuai/daohangyulan.java | 67 +++++++++++++++++++++++----------
1 files changed, 46 insertions(+), 21 deletions(-)
diff --git a/src/dikuai/daohangyulan.java b/src/dikuai/daohangyulan.java
index edfc655..ec51972 100644
--- a/src/dikuai/daohangyulan.java
+++ b/src/dikuai/daohangyulan.java
@@ -363,43 +363,67 @@
return;
}
- Point2D.Double targetPoint = pathPoints.get(currentPathIndex + 1);
-
- // 璁$畻鍒扮洰鏍囩偣鐨勮窛绂�
- double dx = targetPoint.x - currentPos.x;
- double dy = targetPoint.y - currentPos.y;
- double distance = Math.hypot(dx, dy);
-
// 璁$畻姣忓抚绉诲姩鐨勮窛绂伙紙绫筹級
double moveDistance = currentSpeed * (TIMER_INTERVAL_MS / 1000.0);
- if (distance <= moveDistance) {
- // 鍒拌揪鐩爣鐐癸紝绉诲姩鍒颁笅涓�涓偣
- mower.setPosition(targetPoint.x, targetPoint.y);
- navigationTrack.add(new Point2D.Double(targetPoint.x, targetPoint.y));
- mapRenderer.addNavigationPreviewTrackPoint(targetPoint);
+ // 鍒拌揪闃堝�硷細褰撹窛绂诲皬浜庤繖涓�兼椂锛岃涓哄凡鍒拌揪鐩爣鐐癸紙0.05绫筹級
+ double arrivalThreshold = 0.05;
+
+ // 寰幆澶勭悊锛岀洿鍒扮Щ鍔ㄨ窛绂荤敤瀹屾垨鍒拌揪璺緞缁堢偣
+ double remainingDistance = moveDistance;
+
+ while (remainingDistance > 0.001 && currentPathIndex < pathPoints.size() - 1) {
+ Point2D.Double targetPoint = pathPoints.get(currentPathIndex + 1);
- currentPathIndex++;
+ // 璁$畻鍒扮洰鏍囩偣鐨勮窛绂�
+ double dx = targetPoint.x - currentPos.x;
+ double dy = targetPoint.y - currentPos.y;
+ double distance = Math.hypot(dx, dy);
- // 濡傛灉杩樻湁涓嬩竴涓偣锛岃绠楁柟鍚�
- if (currentPathIndex < pathPoints.size() - 1) {
- Point2D.Double nextPoint = pathPoints.get(currentPathIndex + 1);
- double heading = calculateHeading(targetPoint, nextPoint);
- setMowerHeading(heading);
+ // 濡傛灉璺濈灏忎簬鍒拌揪闃堝�硷紝璁や负宸插埌杈剧洰鏍囩偣锛岀Щ鍔ㄥ埌涓嬩竴涓偣
+ if (distance <= arrivalThreshold) {
+ // 鍒拌揪鐩爣鐐癸紝绉诲姩鍒扮簿纭綅缃�
+ mower.setPosition(targetPoint.x, targetPoint.y);
+ navigationTrack.add(new Point2D.Double(targetPoint.x, targetPoint.y));
+ mapRenderer.addNavigationPreviewTrackPoint(targetPoint);
+
+ currentPos = new Point2D.Double(targetPoint.x, targetPoint.y);
+ currentPathIndex++;
+
+ // 濡傛灉杩樻湁涓嬩竴涓偣锛屾洿鏂版柟鍚�
+ if (currentPathIndex < pathPoints.size() - 1) {
+ Point2D.Double nextPoint = pathPoints.get(currentPathIndex + 1);
+ double heading = calculateHeading(targetPoint, nextPoint);
+ setMowerHeading(heading);
+ }
+
+ // 缁х画澶勭悊鍓╀綑鐨勭Щ鍔ㄨ窛绂�
+ continue;
}
- } else {
+
// 鍚戠洰鏍囩偣绉诲姩
- // 鍏堟洿鏂版柟鍚戯紝纭繚杞﹀ご鏈濆悜鐩爣鐐�
+ // 璁$畻鏈瀹為檯绉诲姩鐨勮窛绂伙紙涓嶈秴杩囧墿浣欒窛绂诲拰鍒扮洰鏍囩偣鐨勮窛绂伙級
+ double actualMoveDistance = Math.min(remainingDistance, distance);
+
+ // 鏇存柊鏂瑰悜锛岀‘淇濊溅澶存湞鍚戠洰鏍囩偣
double heading = calculateHeading(currentPos, targetPoint);
setMowerHeading(heading);
- double ratio = moveDistance / distance;
+ // 璁$畻鏂颁綅缃�
+ double ratio = actualMoveDistance / distance;
double newX = currentPos.x + dx * ratio;
double newY = currentPos.y + dy * ratio;
mower.setPosition(newX, newY);
navigationTrack.add(new Point2D.Double(newX, newY));
mapRenderer.addNavigationPreviewTrackPoint(new Point2D.Double(newX, newY));
+
+ // 鏇存柊褰撳墠浣嶇疆鍜屽墿浣欒窛绂�
+ currentPos = new Point2D.Double(newX, newY);
+ remainingDistance -= actualMoveDistance;
+
+ // 濡傛灉宸茬粡鍒拌揪鐩爣鐐归檮杩戯紙璺濈灏忎簬闃堝�硷級锛屽湪涓嬫寰幆涓細澶勭悊
+ // 缁х画寰幆澶勭悊鍓╀綑鐨勭Щ鍔ㄨ窛绂�
}
// 鏇存柊閫熷害鏄剧ず鍒板湴鍥炬覆鏌撳櫒
@@ -548,3 +572,4 @@
}
+
--
Gitblit v1.10.0