Skip to content

Commit 802a97b

Browse files
committed
Fix send request issue
1 parent e2e98f5 commit 802a97b

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

electron_demo/turtle_tf2/main.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -337,41 +337,8 @@ async function createTurtleTf2Listener() {
337337
// Wrap the async logic in a try-catch to handle promise rejections
338338
(async () => {
339339
try {
340-
if (!turtleSpawningServiceReady) {
341-
try {
342-
if (spawner.isServiceServerAvailable()) {
343-
// Spawn turtle2
344-
const request = {
345-
name: 'turtle2',
346-
x: 4.0,
347-
y: 2.0,
348-
theta: 0.0,
349-
};
350-
351-
try {
352-
const response = await spawner.sendRequest(request);
353-
if (response.name === 'turtle2') {
354-
turtleSpawned = true;
355-
turtleSpawningServiceReady = true;
356-
console.log('Successfully spawned turtle2');
357-
358-
if (mainWindow) {
359-
mainWindow.webContents.send('turtle-spawned', {
360-
name: 'turtle2',
361-
x: 4.0,
362-
y: 2.0,
363-
theta: 0.0,
364-
});
365-
}
366-
}
367-
} catch (error) {
368-
console.error('Failed to spawn turtle2:', error);
369-
}
370-
}
371-
} catch (error) {
372-
console.error('Error checking service readiness:', error);
373-
}
374-
}
340+
// Don't automatically spawn turtle2 - let user control this via UI
341+
// This prevents service call errors if turtlesim is not running
375342

376343
// Simple following logic (in real implementation, this would use TF lookup)
377344
// For demo purposes, we'll simulate the transform lookup behavior
@@ -628,8 +595,56 @@ ipcMain.on('spawn-turtle-request', async (event, data) => {
628595
const { name, x, y, theta } = data;
629596

630597
if (turtleTf2Nodes.listener) {
631-
// Implementation would go here
632-
console.log(`Spawning turtle: ${name} at (${x}, ${y}, ${theta})`);
598+
try {
599+
// Get the spawn service client from the listener node
600+
const spawnerNode = turtleTf2Nodes.listener;
601+
const spawner = spawnerNode.createClient('turtlesim/srv/Spawn', 'spawn');
602+
603+
if (spawner.isServiceServerAvailable()) {
604+
const request = {
605+
x: x || 4.0,
606+
y: y || 2.0,
607+
theta: theta || 0.0,
608+
name: name || 'turtle2',
609+
};
610+
611+
console.log(`Attempting to spawn ${name} with request:`, request);
612+
const response = await spawner.sendRequest(request);
613+
console.log('Spawn response:', response);
614+
615+
if (response && response.name) {
616+
console.log(`Successfully spawned ${response.name}`);
617+
618+
if (mainWindow) {
619+
mainWindow.webContents.send('turtle-spawned', {
620+
name: response.name,
621+
x: x || 4.0,
622+
y: y || 2.0,
623+
theta: theta || 0.0,
624+
});
625+
}
626+
}
627+
} else {
628+
console.error(
629+
'Turtlesim spawn service not available. Make sure turtlesim_node is running.'
630+
);
631+
if (mainWindow) {
632+
mainWindow.webContents.send('spawn-error', {
633+
message:
634+
'Turtlesim service not available. Please start turtlesim_node first.',
635+
});
636+
}
637+
}
638+
} catch (error) {
639+
console.error(`Failed to spawn ${name}:`, error);
640+
if (mainWindow) {
641+
mainWindow.webContents.send('spawn-error', {
642+
message: `Failed to spawn ${name}: ${error.message}`,
643+
});
644+
}
645+
}
646+
} else {
647+
console.error('Listener node not initialized');
633648
}
634649
});
635650

0 commit comments

Comments
 (0)