Skip to content

Instantly share code, notes, and snippets.

@awesomebytes
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save awesomebytes/362f5a31a846952068c0 to your computer and use it in GitHub Desktop.

Select an option

Save awesomebytes/362f5a31a846952068c0 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 6/16/15
@author: Sammy Pfeiffer
try_get_topic_class.py contains a little test
demonstrating that get_topic_class from rostopic
does not give the original name of a topic given a
topic string of a subfield
"""
import rospy
from rostopic import get_topic_class
from geometry_msgs.msg import PoseStamped
TESTING_TOPIC = '/posestamped'
if __name__ == '__main__':
rospy.init_node('test_get_topic_class', anonymous=True)
# First we need a publisher that publishes some class that has subfields
# I like PoseStamped, as it has a header and also some subfields
ps_pub = rospy.Publisher(TESTING_TOPIC, PoseStamped, queue_size=1, latch=True)
# We need it publishing, so lets publish, and check if get_topic_class correctly
# gives us the topic name as it's docstring states:
# Get the topic message class
# :returns: message class for topic, real topic
# name, and function for evaluating message objects into the subtopic
# Publish a message
ps_pub.publish(PoseStamped())
# Wait for some time to let the universe get comfy
rospy.sleep(0.1)
# Let's check the "real topic", after the print line you can see the output given
_, real_topic, _ = get_topic_class(TESTING_TOPIC)
print "get_topic_class says real_topic of '" + TESTING_TOPIC + "' is: '" + real_topic + "'"
# get_topic_class says real_topic of '/posestamped' is: '/posestamped'
SUBFIELD_HEADER = TESTING_TOPIC + '/header'
_, real_topic, _ = get_topic_class(SUBFIELD_HEADER)
print "get_topic_class says real_topic of '" + SUBFIELD_HEADER + "' is: '" + real_topic + "'"
# get_topic_class says real_topic of '/posestamped/header' is: '/posestamped/header'
SUBFIELD_POSE = TESTING_TOPIC + '/pose'
_, real_topic, _ = get_topic_class(SUBFIELD_POSE)
print "get_topic_class says real_topic of '" + SUBFIELD_POSE + "' is: '" + real_topic + "'"
# get_topic_class says real_topic of '/posestamped/pose' is: '/posestamped/pose'
SUBFIELD_POSE_POSITION = TESTING_TOPIC + '/pose/position'
_, real_topic, _ = get_topic_class(SUBFIELD_POSE_POSITION)
print "get_topic_class says real_topic of '" + SUBFIELD_POSE_POSITION + "' is: '" + real_topic + "'"
# get_topic_class says real_topic of '/posestamped/pose/position' is: '/posestamped/pose'
SUBFIELD_POSE_POSITION_X = TESTING_TOPIC + '/pose/position/x'
_, real_topic, _ = get_topic_class(SUBFIELD_POSE_POSITION_X)
print "get_topic_class says real_topic of '" + SUBFIELD_POSE_POSITION_X + "' is: '" + real_topic + "'"
# get_topic_class says real_topic of '/posestamped/pose/position/x' is: '/posestamped/pose'
# Leaving the process open for debugging purposes
rospy.spin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment