3.4.3系统信息获取与发布ros2 run status_publisher sys_status_pub报错ModuleNotFoundError: No module named 'status_interfaces.status_interfaces_s__rosidl_typesupport_c'
-
colcon build不报错,但是ros2 run status_publisher sys_status_pub就报错,报错内容为
(base) jack@jack-HP-ZBook-Power-15-6-inch-G10-Mobile-Workstation-PC:~/ros2/learning/chapter3/topic_practice_ws$ ros2 run status_publisher sys_status_pub [INFO] [1738679500.556775450] [sys_status_pub]: sys_status_pub, start! Traceback (most recent call last): File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/import_type_support_impl.py", line 46, in import_type_support return importlib.import_module(module_name, package=pkg_name) File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked ModuleNotFoundError: No module named 'status_interfaces.status_interfaces_s__rosidl_typesupport_c' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/jack/ros2/learning/chapter3/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/jack/ros2/learning/chapter3/topic_practice_ws/install/status_publisher/lib/python3.10/site-packages/status_publisher/sys_status_pub.py", line 31, in main node = SysStatusPub("sys_status_pub") File "/home/jack/ros2/learning/chapter3/topic_practice_ws/install/status_publisher/lib/python3.10/site-packages/status_publisher/sys_status_pub.py", line 11, in __init__ self.sys_status_publisher_ = self.create_publisher(SystemStatus, "sys_status", 10) # 创建发布者 File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/node.py", line 1290, in create_publisher check_is_valid_msg_type(msg_type) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/type_support.py", line 35, in check_is_valid_msg_type check_for_type_support(msg_type) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/type_support.py", line 29, in check_for_type_support msg_or_srv_type.__class__.__import_type_support__() File "/home/jack/ros2/learning/chapter3/topic_practice_ws/install/status_interfaces/lib/python3.12/site-packages/status_interfaces/msg/_system_status.py", line 31, in __import_type_support__ module = import_type_support('status_interfaces') File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/import_type_support_impl.py", line 48, in import_type_support raise UnsupportedTypeSupport(pkg_name) rosidl_generator_py.import_type_support_impl.UnsupportedTypeSupport: Could not import 'rosidl_typesupport_c' for package 'status_interfaces' [ros2run]: Process exited with failure 1
sys_status_pub.py的代码的内容为
import rclpy from status_interfaces.msg import SystemStatus from rclpy.node import Node import psutil import platform class SysStatusPub(Node): def __init__(self, node_name): super().__init__(node_name) self.get_logger().info(f"{node_name}, start!") self.sys_status_publisher_ = self.create_publisher(SystemStatus, "sys_status", 10) # 创建发布者 self.create_timer(1, self.timer_callback) def timer_callback(self): msg = SystemStatus() msg.stamp = self.get_clock().now().to_msg() # 获取当前时间 msg.host_name = platform.node() # 主机名 msg.cpu_percent = psutil.cpu_percent() # CPU使用率 msg.mem_percent = psutil.virtual_memory().percent # 内存使用率 msg.mem_total = psutil.virtual_memory().total / 1024 / 1024 # 内存使用率 msg.mem_available = psutil.virtual_memory().available / 1024 / 1024 # 内存使用率 msg.net_sent = psutil.net_io_counters().bytes_sent / 1024 / 1024 # 网络发送量,字节B(/1024/1024) -> MB msg.net_recv = psutil.net_io_counters().bytes_recv / 1024 / 1024 # 网络接收量,字节B(/1024/1024) -> MB self.sys_status_publisher_.publish(msg) # 发布消息 self.get_logger().info(f"发布了{str(msg)}") def main(): rclpy.init() node = SysStatusPub("sys_status_pub") rclpy.spin(node) rclpy.shutdown()
工程结构为
尝试很多方法没能解决,求助!!!
-
从你提供的错误信息来看,主要问题是 ROS 2 无法找到名为
status_interfaces.status_interfaces_s__rosidl_typesupport_c
的模块。这通常是因为消息类型支持(type support)没有正确生成或者没有正确安装。以下是一些可能的解决步骤:-
确保消息接口和类型支持正确生成:
- 首先,确保你的
status_interfaces
包中的.idl
文件已经正确描述了消息类型。 - 使用
colcon build
命令重新构建你的工作区。在构建过程中,ROS 2 会生成 C++ 和 Python 的类型支持代码。
- 首先,确保你的
-
检查
setup.py
和package.xml
:- 确保
status_interfaces
包的setup.py
文件中包含了正确的rosidl_generate_interfaces
调用,这通常看起来像这样:from setuptools import setup setup( # 其他参数... packages=find_packages(exclude=['test']), package_dir={'': 'src'}, generate_ros_interface_targets=True, depend_ency_links=[ # 依赖链接(如果有) ], install_requires=[ # 依赖包 ], entry_points={ # 入口点(如果有) }, include_package_data=True, zip_safe=False, maintainer='your_name', maintainer_email='your_email@example.com', description='TODO: Package description', license='TODO: License declaration', tests_require=['pytest'], classifiers=[ 'Programming Language :: Python', 'License :: OSI Approved :: Apache Software License', 'Operating System :: OS Independent', ], python_requires='>=3.6', )
- 确保
package.xml
中包含了所有必要的依赖项和构建工具。
- 确保
-
清理和重建:
- 有时候,旧的构建文件可能会导致问题。尝试清理你的工作区:
rm -rf build/ install/ log/ colcon build --packages-up-to status_interfaces
- 确保
status_interfaces
包被成功构建。
- 有时候,旧的构建文件可能会导致问题。尝试清理你的工作区:
-
检查 ROS 2 环境:
- 确保你的
source
命令正确指向了你的 ROS 2 安装和工作区:source /opt/ros/humble/setup.bash source ~/ros2/learning/chapter3/topic_practice_ws/install/setup.bash
- 确保你的
-
运行节点:
- 在执行
ros2 run status_publisher sys_status_pub
之前,确保所有的依赖和环境都已正确设置。
- 在执行
如果上述步骤都不能解决问题,可能需要更详细地检查
status_interfaces
包的内容,特别是.idl
文件和相关的 CMake 配置。确保所有必要的类型支持都被正确生成和安装。
以上为生成回答,仅供参考~
-