Tools: One small experience to check the node of ros2
base 24.02 ubuntu jazzy ros2 check the version of ros2echo $ROS_DISTROwhich ros2 1 Ctrl + Alt + T open the first terminal 2 source /opt/ros/jazzy/setup.bash3 mkdir -p ~/ros2_ws/src cd ~/ros2_ws/src // create package in src workplace ,not in the root 4 creat python package ros2 pkg create --build-type ament_python my_py_pubsub --dependencies rclpy std_msgs 5 dive into the package dic cd ~/ros2_ws/src/my_py_pubsub 6creat talker and listenser code file cd ~/ros2_ws/src/my_py_pubsub/my_py_pubsubtouch talker.py listener.py 9 in the first terminal cd ~/ros2_wscolcon build --packages-select my_py_pubsubsource ~/ros2_ws/install/setup.bashros2 run my_py_pubsub listener 10 in the second terminal source /opt/ros/jazzy/setup.bashsource ~/ros2_ws/install/setup.bashros2 run my_py_pubsub talker explain:这个实验本质上做了什么? 在这个工作空间里创建了一个 Python 类型的 ROS 2 包 再通过 colcon build 让 ROS 2 识别这个包 所以,这不是“随便写了两个 Python 文件”,而是: 我们创建了一个最小的 ROS 2 Python 示例包,用来验证发布/订阅通信机制。 写 node → 放进 package → 放进 workspace → build → 用 ros2 run 运行 *workplace : ros2_ws/ *以后你可以在这个 workspace 里放很多个 ROS 2 包,比如: 自己写的包,通常都放在这里。所以常见的习惯就是: mkdir -p ~/ros2_ws/src src/ 下面的外层 my_py_pubsub/,就是你的 ROS 2 包目录。 它的名字就是包名,所以以后你运行的时候会用到这个名字: ros2 run my_py_pubsub talker package.xml 可以理解成 ROS 2 包的“身份证”或者“说明书”。 setup.py:告诉系统“这个包怎么装、怎么跑” setup.py 是 Python 包的安装配置文件。 entry_points={ 'console_scripts': [ 'talker = my_py_pubsub.talker:main', 'listener = my_py_pubsub.listener:main', ],} ros2 run my_py_pubsub talker my_py_pubsub/talker.py 13. talker.py 和 listener.py 分别干嘛? 这两个就是你真正写的 ROS 2 节点代码。 这两个文件合起来,就是 ROS 2 最小的通信实验。 14. build/、install/、log/ 是什么? 这三个目录通常不是你手动创建的,而是执行下面这个命令后自动生成的: 这里是编译过程中的中间文件。可以理解成构建缓存和临时产物。 这个目录非常重要,因为构建完成后你通常要执行: source ~/ros2_ws/install/setup.bash 这样系统才知道你自己写的包在哪里,才能用 ros2 run 找到它。 这里是构建日志。
如果编译失败,很多时候可以来这里查问题。 所以这三个目录里,初学阶段你最需要理解的是: install/ 很重要,因为它决定你的包能不能被 ROS 2 正确加载 Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse