3.4.3系统信息获取与发布
-
fishros@fishros-linux:~/chapt3/topic_practice_ws$ ros2 run status_publisher sys_status_pub
Traceback (most recent call last):
File "/home/fishros/chapt3/topic_practice_ws/install/status_publisher/lib/status_publisher/sys_status_pub", line 33, in <module>
sys.exit(load_entry_point('status-publisher==0.0.0', 'console_scripts', 'sys_status_pub')())
File "/home/fishros/chapt3/topic_practice_ws/install/status_publisher/lib/python3.10/site-packages/status_publisher/sys_status_pub.py", line 39, in main
rclpy.spin(node)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/init.py", line 226, in spin
executor.spin_once()
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 739, in spin_once
self._spin_once_impl(timeout_sec)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 736, in _spin_once_impl
raise handler.exception()
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/task.py", line 239, in call
self._handler.send(None)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 437, in handler
await call_coroutine(entity, arg)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 351, in _execute_timer
await await_or_execute(tmr.callback)
File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 107, in await_or_execute
return callback(*args)
File "/home/fishros/chapt3/topic_practice_ws/install/status_publisher/lib/python3.10/site-packages/status_publisher/sys_status_pub.py", line 28, in timer_callback
msg.memory_total = memory_info.total
File "/home/fishros/chapt3/topic_practice_ws/install/status_interfaces/local/lib/python3.10/dist-packages/status_interfaces/msg/_system_status.py", line 227, in memory_total
assert
AssertionError: The 'memory_total' field must be of type 'float'
[ros2run]: Process exited with failure 1 -
这个错误信息表明在发布系统状态信息时遇到了一个问题,具体是在设置
memory_total
字段的值时发生了断言错误(AssertionError
)。错误信息指出'memory_total' field must be of type 'float'
,这意味着memory_total
字段的值必须是浮点数类型,但是在你的代码中可能传递了一个非浮点数的值。错误发生在
status_publisher
包的sys_status_pub.py
文件的第 28 行,在timer_callback
函数中。这个函数尝试从某个地方获取内存信息(可能是通过memory_info.total
),并将这个值赋给消息对象的memory_total
字段。要解决这个问题,你可以按照以下步骤操作:
-
检查数据类型:确保
memory_info.total
的值是浮点数。如果memory_info.total
是整数或其他类型,你需要将其转换为浮点数。例如,可以使用float(memory_info.total)
来确保它是浮点数。 -
修改代码:在
sys_status_pub.py
文件的第 28 行,修改代码如下:msg.memory_total = float(memory_info.total)
如果
memory_info.total
已经是浮点数,这行代码不会改变其值,但如果它是整数或其他类型,这将会进行类型转换。 -
重新编译并运行:在做了上述修改后,重新编译你的 ROS 2 包(如果之前已经编译过),然后再次运行你的节点来查看问题是否解决。
-
调试:如果问题仍然存在,检查
memory_info
对象的来源和它是如何被赋值的。确保在赋值给msg.memory_total
之前,memory_info.total
的值是正确的类型。
通过这些步骤,你应该能够解决这个类型不匹配的问题,并使你的 ROS 2 节点能够正确发布系统状态信息。
以上为生成回答,仅供参考~
-