@@ -333,48 +333,59 @@ async function createTurtleTf2Listener() {
333333 ) ;
334334
335335 // Timer to check for transforms and control turtle2
336- const timer = node . createTimer ( 1000 , async ( ) => {
337- if ( ! turtleSpawningServiceReady ) {
338- if ( await spawner . isServiceReady ( ) ) {
339- // Spawn turtle2
340- const request = {
341- name : 'turtle2' ,
342- x : 4.0 ,
343- y : 2.0 ,
344- theta : 0.0 ,
345- } ;
346-
347- try {
348- const response = await spawner . sendRequest ( request ) ;
349- if ( response . name === 'turtle2' ) {
350- turtleSpawned = true ;
351- turtleSpawningServiceReady = true ;
352- console . log ( 'Successfully spawned turtle2' ) ;
353-
354- if ( mainWindow ) {
355- mainWindow . webContents . send ( 'turtle-spawned' , {
336+ const timer = node . createTimer ( 1000 , ( ) => {
337+ // Wrap the async logic in a try-catch to handle promise rejections
338+ ( async ( ) => {
339+ try {
340+ if ( ! turtleSpawningServiceReady ) {
341+ try {
342+ if ( spawner . isServiceServerAvailable ( ) ) {
343+ // Spawn turtle2
344+ const request = {
356345 name : 'turtle2' ,
357346 x : 4.0 ,
358347 y : 2.0 ,
359348 theta : 0.0 ,
360- } ) ;
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+ }
361370 }
371+ } catch ( error ) {
372+ console . error ( 'Error checking service readiness:' , error ) ;
362373 }
363- } catch ( error ) {
364- console . error ( 'Failed to spawn turtle2:' , error ) ;
365374 }
366- }
367- }
368375
369- // Simple following logic (in real implementation, this would use TF lookup)
370- // For demo purposes, we'll simulate the transform lookup behavior
371- if ( turtleSpawned ) {
372- // This is a simplified version - in real TF2, we'd lookup transforms
373- // For the demo, we'll let the renderer handle the following logic
374- if ( mainWindow ) {
375- mainWindow . webContents . send ( 'request-turtle-follow' ) ;
376+ // Simple following logic (in real implementation, this would use TF lookup)
377+ // For demo purposes, we'll simulate the transform lookup behavior
378+ if ( turtleSpawned ) {
379+ // This is a simplified version - in real TF2, we'd lookup transforms
380+ // For the demo, we'll let the renderer handle the following logic
381+ if ( mainWindow ) {
382+ mainWindow . webContents . send ( 'request-turtle-follow' ) ;
383+ }
384+ }
385+ } catch ( error ) {
386+ console . error ( 'Timer callback error:' , error ) ;
376387 }
377- }
388+ } ) ( ) ;
378389 } ) ;
379390
380391 rclnodejs . spin ( node ) ;
0 commit comments