Dynamically get topic type for rosbag
Hello,
I want to create a service, which records multiple different topics into a rosbag
-file.
The problem is, that I need the data type of the topic that is going to be recorded to subscribe to the topic.
I start the service with the command line
rosservice call /start_data_recorder "topics: ['scan','map','imu/data']
to collect the three topics.
def on_start_recording(self, msg):
# Initialize new Rosbag
PATH = "../catkin_ws/src/robolab_base/data_recorder/recordings"
self.bag = rosbag.Bag(f'{PATH}/Rec.bag', 'w')
for topic in msg.topics:
# Initialize new datawriter class
subscriber = DataWriter(topic, self.bag, self.shutdown_recording)
# Collect topic data
data_type = rostopic.get_topic_type("/" + topic, blocking = False)[0]
Sub = self._ros_api.Subscriber(topic, ros_msg_type, subscriber.callback)
self.subscribers.append(Sub)
return StartDataRecordingResponse()
with my DataWriter class writing the data to the bag file
and a service (StartDataRecording.srv
)
However, I get an error message, when initialising the Subscriber Sub
:
Error processing request: data_class [sensor_msgs/LaserScan] is not a class
I suppose that data_type
is not a valid output, but I am not sure how to proceed from here to get each topic recorded with the rosbag.
Edit:
I fixed it, by using rostopic.get_topic_class()
instead of rostopic.get_topic_type()
which provided the type of data needed for the Subscriber.
We don't know your (other) requirements, but perhaps osrf/nodelet_rosbag can already do this.
Since I have to integrate a given API, I hoped there would be a direct way to read the topic type.