Accessing QTrobot Emotion 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 emotion interface with rospy module. This interface allows you to change the QTrobot facial emotions such as 'QT/happy', 'QT/sad', etc.
The complete list of emotion files can be found in '~/robot/data/emotions/'.
Emotion interface is accessible with publish/subscribe and service/client methods. In this example we will use publish/subscribe method. You can also check "4.3 Emotion Interface" chapter on Wiki ROS.
First we should import rospy, QTrobot interface module and data type.
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 emotion interface is "/qt_robot/emotion/show" and data type is text "String" emotion name.
# ros node rospy.init_node('python_qt_example') # creating a emotion ros publisher emotionShow_pub = rospy.Publisher('/qt_robot/emotion/show', String, queue_size=10) # waiting for connection establishment wtime_begin = rospy.get_time() while (emotionShow_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 show emotion with the QTrobot emotion interface we need to call publish function of the ROS publisher(emotionShow_pub) that we created with the emotion file name.
# publish emotion to QTrobot emotionShow_pub.publish("QT/happy")
Get the full code in our github tutorial repository.