Skip to content

Commit 17fdd18

Browse files
committed
Update the node to run async
1 parent c9d7d9b commit 17fdd18

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

coffee_ws/src/coffee_voice_agent/coffee_voice_agent/voice_agent_node.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,38 @@ def start_voice_agent(self):
126126
# Create mock context for integration
127127
self.mock_ctx = MockLiveKitContext()
128128

129-
# Start the voice agent task
130-
self.agent_task = asyncio.create_task(self.run_voice_agent())
129+
# Create a one-time timer to start the async task after ROS2 is fully initialized
130+
self.startup_timer = self.create_timer(0.1, self.start_voice_agent_task)
131131

132-
self.get_logger().info("Voice agent task started")
132+
self.get_logger().info("Voice agent startup scheduled")
133+
134+
def start_voice_agent_task(self):
135+
"""Timer callback to start the voice agent async task"""
136+
137+
# Cancel the timer after first run
138+
self.startup_timer.cancel()
139+
140+
# Start the voice agent using asyncio.ensure_future which works better with executors
141+
try:
142+
self.agent_task = asyncio.ensure_future(self.run_voice_agent())
143+
self.get_logger().info("Voice agent task started with ensure_future")
144+
except Exception as e:
145+
self.get_logger().error(f"Failed to start voice agent task: {e}")
146+
# Try alternative approach
147+
try:
148+
import concurrent.futures
149+
executor = concurrent.futures.ThreadPoolExecutor()
150+
self.agent_task = executor.submit(self._run_voice_agent_sync)
151+
self.get_logger().info("Voice agent started in thread executor")
152+
except Exception as e2:
153+
self.get_logger().error(f"Failed to start voice agent in thread: {e2}")
154+
155+
def _run_voice_agent_sync(self):
156+
"""Synchronous wrapper for running the voice agent"""
157+
try:
158+
asyncio.run(self.run_voice_agent())
159+
except Exception as e:
160+
self.get_logger().error(f"Voice agent sync wrapper error: {e}")
133161

134162
async def run_voice_agent(self):
135163
"""Run the original voice agent with ROS2 integration hooks"""

0 commit comments

Comments
 (0)