Accessing QTrobot speech interface with Python


The QTrobot is using ROS for easier access to basic robot functionalities with ROS publish/subscriber and Service/Client interfaces. Rospy is a pure Python client library for ROS. The rospy client API enables Python programmers to quickly interface with ROS Topics, Services, and Parameters.

We will use Python code to access QTrobot speech interface with rospy module.This interface implements QTrobot text to speech functionality which support many languages. The supported languages can be different for each robot, but for this example we will use American English (en-US). Speech interface is accessible with publish/subscribe and service/client methods. In this example we will use publish/subscribe method. You can also check "4.1 Speech Interface" chapter on Wiki ROS.

First we should import rospy, QTrobot interface module and data type.

Step 1

import sys
import rospy
from std_msgs.msg import String
from qt_robot_interface.srv import *

Next up we initialize ROS node and declare a name for it. To create a ROS publisher we need topic name, data type and size of our queue. Topic name for accessing QTrobot speech interface is "/qt_robot/speech/say" and data type is text "String".

Step 2

# ros node
rospy.init_node('python_qt_example')
# creating a ros publisher
speechSay_pub = rospy.Publisher('/qt_robot/speech/say', String, queue_size=10)

# waiting for connection establishment
wtime_begin = rospy.get_time()
while (speechSay_pub.get_num_connections() == 0) :
    rospy.loginfo("waiting for publisher connection")
    if rospy.get_time() - wtime_begin > 5.0:
        rospy.logerr("Timeout while waiting for publisher connection!")
        sys.exit()
    rospy.sleep(1)

To publish text to the QTrobot speech interface we need to call publish function of the ROS publisher(speechSay_pub) that we created.

Step 3

# publish a text message to TTS
speechSay_pub.publish("Hello!")

NOTICE

Default language is set in '/opt/ros/kinetic/share/qt_robot_interface/config/qtrobot-interface.yaml'. the default pitch is usually '140' and speed is '80'. When calling 'speech_config', leave pitch and speed parameters to '0' if you do not want to change them.

Get the full code in our github tutorial repository.


Last update: February 5, 2020