Accessing QTrobot audio 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 audio interface with rospy module. This interface allows you to play a standard audio file (e.g. wav, mp3). The audio file can be given using its complete name (with the file extension) such as happy.mp3 or simply without the file extension: happy. In the second case, the player first looks for happy.mp3 to play and if it is not found it then tries with happy.wav and so on.


The default path for the audio files is '~/robot/data/audios/'

Audio interface is accessible with publish/subscribe and service/client methods. In this example we will use publish/subscribe method. You can also check "4.2 Audio 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 audio interface is "/qt_robot/audio/play" and data type is text "String" audio file name.

Step 2

# ros node
# creating a audio ros publisher
audioPlay_pub = rospy.Publisher('/qt_robot/audio/play', String, queue_size=10)

# waiting for connection establishment
wtime_begin = rospy.get_time()
while (audioPlay_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!")

To play audio file with the QTrobot audio interface we need to call publish function of the ROS publisher(audioPlay_pub) that we created with the audio file name.

Step 3

# publish audio file


To play the audio file from the default path, pass an empty string to filepath parameter.

Get the full code in our github tutorial repository.

Last update: February 5, 2020