Refactor PathRecorder to utilize server-side pathfinding for waypoint navigation
This commit is contained in:
@@ -13,9 +13,6 @@ const PathRecorder = ({ turtles, selectedTurtle, apiUrl }) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [playingBack, setPlayingBack] = useState(false);
|
||||
|
||||
const moveForward = useTurtleStore((state) => state.moveForward);
|
||||
const moveUp = useTurtleStore((state) => state.moveUp);
|
||||
const moveDown = useTurtleStore((state) => state.moveDown);
|
||||
const setTurtleState = useTurtleStore((state) => state.setTurtleState);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -153,44 +150,23 @@ const PathRecorder = ({ turtles, selectedTurtle, apiUrl }) => {
|
||||
}
|
||||
|
||||
setPlayingBack(true);
|
||||
showMessage(`Playing back ${waypoints.length} waypoints...`, 'info');
|
||||
showMessage(`Playing back ${waypoints.length} waypoints via server pathfinding...`, 'info');
|
||||
|
||||
const turtleId = selectedTurtle.turtleID;
|
||||
|
||||
// Determine movement commands between consecutive waypoints
|
||||
for (let i = 1; i < waypoints.length; i++) {
|
||||
const prev = waypoints[i - 1];
|
||||
const curr = waypoints[i];
|
||||
// Use server-side pathfinding to navigate to each waypoint sequentially
|
||||
for (let i = 0; i < waypoints.length; i++) {
|
||||
const wp = waypoints[i];
|
||||
|
||||
const dx = curr.x - prev.x;
|
||||
const dy = curr.y - prev.y;
|
||||
const dz = curr.z - prev.z;
|
||||
|
||||
// Vertical movement
|
||||
if (dy > 0) {
|
||||
for (let j = 0; j < dy; j++) sendCommand(turtleId, 'up');
|
||||
} else if (dy < 0) {
|
||||
for (let j = 0; j < Math.abs(dy); j++) sendCommand(turtleId, 'down');
|
||||
}
|
||||
|
||||
// Horizontal movement - send as forward movements with turns
|
||||
if (dx > 0) {
|
||||
for (let j = 0; j < dx; j++) sendCommand(turtleId, 'forward');
|
||||
} else if (dx < 0) {
|
||||
for (let j = 0; j < Math.abs(dx); j++) sendCommand(turtleId, 'forward');
|
||||
}
|
||||
if (dz > 0) {
|
||||
for (let j = 0; j < dz; j++) sendCommand(turtleId, 'forward');
|
||||
} else if (dz < 0) {
|
||||
for (let j = 0; j < Math.abs(dz); j++) sendCommand(turtleId, 'forward');
|
||||
}
|
||||
|
||||
// Small delay between waypoint groups to avoid flooding
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
// Navigate to each waypoint using the server's moving state
|
||||
await setTurtleState(turtleId, 'moving', { target: { x: wp.x, y: wp.y, z: wp.z } });
|
||||
|
||||
// Wait for turtle to arrive (poll position or just add delay)
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
|
||||
setPlayingBack(false);
|
||||
showMessage('Playback complete! Commands queued for turtle.', 'success');
|
||||
showMessage('Playback complete! Turtle navigating via server.', 'success');
|
||||
};
|
||||
|
||||
const showMessage = (text, type) => {
|
||||
|
||||
Reference in New Issue
Block a user