Your robot’s “Hello world”#
🏁 Goal of this tutorial
By the end of this tutorial, you will know how to get your robot to say out loud a message.
Pre-requisites#
You should first have completed 🏁 Getting started.
The code#
1import rclpy
2from rclpy.node import Node
3from rclpy.action import ActionClient
4from tts_msgs.action import TTS
5
6
7class SayClient(Node):
8 def __init__(self):
9 super().__init__('say_client')
10
11 self._tts = ActionClient(self, TTS, '/tts_engine/tts')
12 while not self._tts.wait_for_server(timeout_sec=1.0):
13 self.get_logger().info("waiting for tts_engine server")
14
15 def say(self, msg):
16 self.get_logger().info("Saying: %s" % msg)
17 tts_goal = TTS.Goal()
18 tts_goal.locale = "en_US"
19 tts_goal.input = msg
20 self._tts.send_goal_async(tts_goal)
21
22
23def main():
24 rclpy.init()
25 say_node = SayClient()
26 say_node.say("Hello world!")
27
28
29if __name__ == '__main__':
30 main()
The code explained#
We first import the different libraries required in our code.
import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient
from tts_msgs.action import TTS
rclpy
: The core ROS 2 Python client library.Node
: A base class for creating ROS 2 nodes, representing a single process communicating with the ROS 2 network.ActionClient
: A class for interacting with ROS 2 action servers. Actions are used for long-running tasks that provide feedback and results.TTS
: A custom action definition (from the tts_msgs package) used to communicate with the TTS engine.
We next define the class SayClient
, which is the one in charge of
making the robot speak:
class SayClient(Node):
def __init__(self):
super().__init__('say_client')
self._tts = ActionClient(self, TTS, '/tts_engine/tts')
while not self._tts.wait_for_server(timeout_sec=1.0):
self.get_logger().info("waiting for tts_engine server")
The constructor initializes a ROS 2 node named say_client
. Next, it
creates an action client for the TTS engine. It will send requests to the action
server listening on the /tts_engine/tts
topic. We then ensure the action
server is available before sending requests using the wait_for_server
method. The loop waits with a 1-second timeout and logs a message during each
wait.
The say method handles the task of sending a message to the TTS server.
def say(self, msg):
self.get_logger().info("Saying: %s" % msg)
tts_goal = TTS.Goal()
tts_goal.input = msg
tts_goal.locale = "en_US"
self._tts.send_goal_async(tts_goal)
A first log message is provided indicating the message to be sent. Next, we create the goal to be sent to the server.
TTS.Goal()
: Represents a goal message for the TTS action.input
: The actual text to be spoken.locale
: Specifies the language/locale (e.g., in the script U.S. English).
Finally, we send the goal to the server asynchronously, allowing the node to continue running without waiting for the action to complete.
The main function creates the node and sends the request to say “Hello world!”
def main():
rclpy.init()
say_node = SayClient()
say_node.say("Hello world!")
Run it on your robot#
To run the script, copy it first to your robot:
scp hello_world_robot.py pal@<robot>-0n:.
Note
Replace <robot>-0c
with your robot’s hostname. For instance, tiago-pro-10c
.
The default password is pal
.
Connect to your robot over ssh
(same password, pal
by default):
ssh pal@<robot>-0c
Run the script:
python3 hello_world_robot.py
The robot should say out loud: “Hello world!”
Note
Check that the robot’s playback volume is high enough. You can check it from eg the Web User Interface.
Next steps#
You can continue with the next ‘Beginners’ tutorial: [‼️ROS1] Creating a simple multi-modal interaction