小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ROS关于编写节点时无法同时订阅和发布
-
我在编写ROS节点的时候发现冲突,不能同时订阅和发布了,但屏蔽pub之后能完美打印2了,这是为什么?
这时我的节点文件
def callback(msg):
pad_id = msg.data
if pad_id == 2:
rospy.loginfo('2')
pub.publish('two')if name == "main":
rospy.init_node('pad_to_led', anonymous=False)
sub = rospy.Subscriber('mission_pad_id', UInt8, callback)
pub = rospy.Publisher('mled', String, queue_size=10)
rospy.spin()
然后这是报错
Exception in thread Thread-9:
Traceback (most recent call last):
File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_UInt8.py", line 54, in serialize
buff.write(_get_struct_B().pack(_x))
struct.error: ubyte format requires 0 <= number <= 255During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 882, in publish
self.impl.publish(data)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 1066, in publish
serialize_message(b, self.seq, message)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py", line 152, in serialize_message
msg.serialize(b)
File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_UInt8.py", line 55, in serialize
except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(locals().get('_x', self)))))
File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 393, in _check_types
check_type(n, t, getattr(self, n))
File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 267, in check_type
raise SerializationError('field %s must be unsigned integer type' % field_name)
genpy.message.SerializationError: field data must be unsigned integer typeDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/timer.py", line 237, in run
self._callback(TimerEvent(last_expected, last_real, current_expected, current_real, last_duration))
File "rmtt_core.py", line 317, in rmtt_core_dev.scripts.rmtt_core.RoboMasterTelloTalent._drone_info_handler
File "rmtt_core.py", line 395, in rmtt_core_dev.scripts.rmtt_core.RoboMasterTelloTalent._drone_info_publisher
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 886, in publish
raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be unsigned integer type
查找了一群资料也不知道为什么,求大神帮助 -
@3053653379 注意格式哈
@3053653379 在 ROS关于编写节点时无法同时订阅和发布 中说:
rospy.exceptions.ROSSerializationException: field data must be unsigned integer type
看起来像是收到的数据类型有问题,贴下完成代码和操作,你这个看不太出缩进
@小鱼 在 提问前必看!一定要看!必须看一下! 中说:
问题一定要描述清楚,终端打印一定复制粘贴,方便回答者检索和引用(你可以在linux系统上打开浏览器进社区)
基本的Markdown语法一定要学习下,有的小伙伴图片代码一团糟
提问时一定要提供尽可能多的信息(系统版本,ROS版本,前后操作,终端日志),包括你的目的,比如你其实想装装某个库遇到问题,不要只说这个问题,因为可能有更好的替代方案
先搜索再提问,很多问题其实都有解决方案,确保你自己对自己的问题有一定了解再提问
尽量一句话说完,不要把社区当微信聊天一样用,每一个回复都尽量提供更多的的信息。