ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ValueError: data_class [Twist] is not a class

asked 2022-04-26 03:16:09 -0600

anonymous user

Anonymous

Hi, I am trying to get values from .yaml file for subscription and writing bag file. But i encounter with this error and i couldn't solve it. I guess I can't define my values as a class name in code.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import sys
import rospy 
import rosbag
from geometry_msgs.msg import Twist

filename='test.bag'
bag = rosbag.Bag(filename, 'w')


sensor_info=rospy.get_param("/bag/sensor_info") 
move_datatype=sensor_info[1]['datatype'] # Twist
move_topic=sensor_info[1]['topic_name'] # /cmd_vel

def move_callback(msg):
    x=msg

def main():
    global x
    rospy.init_node('rosbag_save', anonymous=True)
    rospy.Subscriber(move_topic,move_datatype(),move_callback)
    while not rospy.is_shutdown():
        bag.write(f'{move_topic}',x)

if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptException:
        pass

this is my code

bag:
  sensor_info:
    - {"topic_name": "/multi_scan", "datatype": "LaserScan"}
    - {"topic_name": "/cmd_vel", "datatype": "Twist"}

and this is my .yaml file

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-04-26 14:23:24 -0600

aarsh_t gravatar image

The error is correct. data_class Twist is not a class.

This is wrong,

rospy.Subscriber(move_topic,move_datatype(),move_callback)

This is the correct way to subscribe to any topic,

rospy.Subscriber('/cmd_vel', Twist, callback)

BUT

In your case, datatype: Twist is coming as string and you do not want that. try to use eval(). For an example

rospy.Subscriber(move_topic, eval(move_datatype), move_callback)
edit flag offensive delete link more

Comments

Yeah it really works. Thanks. I also tried "globals()[move_datatype]" and it works also too.

anonymous userAnonymous ( 2022-04-27 04:13:33 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-04-26 03:16:09 -0600

Seen: 311 times

Last updated: Apr 26 '22