diff --git a/src/voice_to_text_node/voice_to_text_node/voice_to_text_node.py b/src/voice_to_text_node/voice_to_text_node/voice_to_text_node.py index eb33196..9c7a8cc 100755 --- a/src/voice_to_text_node/voice_to_text_node/voice_to_text_node.py +++ b/src/voice_to_text_node/voice_to_text_node/voice_to_text_node.py @@ -29,27 +29,31 @@ latest_result = {"text": ""} class VoicePublisher(ThreadedNode): def __init__(self): super().__init__(package_name) - self.string_pub = self.create_publisher(String, package_name + '/test_topic', 10) + self.string_pub = self.create_publisher(String, package_name + '/voice_to_text', 10) - self.prev_partial = "" - self.prev_text = "" + self.prev_partial = None + self.prev_text = None self.speech_thread = threading.Thread(target=self.speech_loop, daemon=True) self.speech_thread.start() def publish_if_changed(self, key, new_value): - if key == "partial" and new_value != self.prev_partial: - self.prev_partial = new_value - self._publish(new_value) - elif key == "text" and new_value != self.prev_text: - self.prev_text = new_value - self._publish(new_value) + if key == "partial": + if new_value and new_value != self.prev_partial: + self.prev_partial = new_value + self._publish("partial", new_value) + elif key == "text": + if new_value != self.prev_text: + self.prev_text = new_value + self._publish("text", new_value) - def _publish(self, data): + + def _publish(self, kind, value): msg = String() - msg.data = data + msg.data = json.dumps({"type": kind, "data": value}) self.string_pub.publish(msg) - self.get_logger().info(f'Published: {data}') + self.get_logger().info(f'Published: {msg.data}') + def speech_loop(self): model = Model(model_path) @@ -82,6 +86,7 @@ class VoicePublisher(ThreadedNode): + def main(): rclpy.init() node = VoicePublisher()