| | |
| | | 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; |
| | | |
| | | // 如果已经到达目标点附近(距离小于阈值),在下次循环中会处理 |
| | | // 继续循环处理剩余的移动距离 |
| | | } |
| | | |
| | | // 更新速度显示到地图渲染器 |
| | |
| | | } |
| | | |
| | | |
| | | |