# ros2-demo **Repository Path**: shardcn/ros2-demo ## Basic Information - **Project Name**: ros2-demo - **Description**: No description available - **Primary Language**: Python - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-06 - **Last Updated**: 2025-08-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 环境: python 3.11 ## 安装 以下是mac本地安装ros的步骤。 ```shell # 创建专用的ROS2环境 conda create -n ros2_env python=3.11 conda activate ros2_env #conda deactivate #conda remove -n ros2_env # 配置conda频道 conda config --env --add channels conda-forge conda config --env --add channels robostack-staging conda config --env --remove channels defaults 步骤 3:安装 ROS2 Humble # 安装ROS2 Humble核心包 conda install ros-humble-desktop # 安装开发工具(可选但推荐) conda install compilers cmake pkg-config make ninja colcon-common-extensions # 重新激活环境确保所有配置生效 conda deactivate conda activate ros2_env 步骤 4:验证安装 # 测试ROS2基本功能 ros2 --help # 运行turtlesim验证图形界面 ros2 run turtlesim turtlesim_node # 在另一个终端(同样激活ros2_env)中控制turtle ros2 run turtlesim turtle_teleop_key ``` ## Python rclpy 使用示例 安装完成后,你可以直接使用 Python rclpy 库。以下是一个简单的发布者和订阅者示例: ``` #!/usr/bin/env python3 import rclpy from rclpy.node import Node from std_msgs.msg import String class SimplePublisher(Node): def __init__(self): super().__init__('simple_publisher') self.publisher_ = self.create_publisher(String, 'hello_topic', 10) self.timer = self.create_timer(1.0, self.timer_callback) self.counter = 0 def timer_callback(self): msg = String() msg.data = f'Hello from Mac M4! Message {self.counter}' self.publisher_.publish(msg) self.get_logger().info(f'Publishing: {msg.data}') self.counter += 1 def main(args=None): rclpy.init(args=args) node = SimplePublisher() rclpy.spin(node) node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` ## 优势与限制 ✅ 主要优势 - 性能最佳,无 Docker 开销 - 安装简单,维护方便 - 支持 739/1912 个 ROS2 包在 ARM64 上 - 完美支持 rviz2 和其他 GUI 工具 - 与系统 Python 环境隔离 ⚠️ 注意事项 - 部分高级包暂不支持 ARM64 - Gazebo 集成可能需要额外配置 - colcon build 需要特殊 Python 路径配置 - 某些第三方插件兼容性有限