我正在尝试从.yaml文件中获取值,以便在ROS中订阅和编写包文件。但是我遇到了这个错误,我无法解决它。我想我无法在代码中将我的值定义为类名。
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
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这是我的密码
bag:
sensor_info:
- {"topic_name": "/multi_scan", "datatype": "LaserScan"}
- {"topic_name": "/cmd_vel", "datatype": "Twist"}这是我的.yaml文件
发布于 2022-04-26 15:20:49
您将得到此错误,因为当您从服务器读取rosparam时,它将被存储为一个字符串;您不能将该字符串传递给订阅服务器。相反,您应该在读取对象类型后将其转换为对象类型。有几种方法可以做到这一点,但是使用globals()可能是最简单的。本质上,这将返回全局符号表中所有内容的字典,从该字典中您可以使用一个键(一个字符串)来获取值(类类型)。可以这样做,例如:
sensor_info = rospy.get_param("/bag/sensor_info")
move_datatype_string = sensor_info[1]['datatype'] # Twist
move_topic = sensor_info[1]['topic_name'] # /cmd_vel
move_datatype = globals()[move_datatype_string]https://stackoverflow.com/questions/72011576
复制相似问题