ros中launch文件演示,start_turtle.launch,hello_vscode




避坑:注意 start_turtle.launch 文件和 demo_hello.py 文件的格式,格式错一点启动就报错


删除之前有问题的 ROS 包

rm -rf ~/catkin_ws

步骤一:创建 ROS 包

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_create_pkg hello_vscode rospy turtlesim

步骤二:创建 Launch 文件夹

cd ~/catkin_ws/src/hello_vscode
mkdir launch

步骤三:添加 Launch 文件

cd launch
touch start_turtle.launch

步骤四:编辑 Launch 文件内容

sudo nano start_turtle.launch

在编辑器中将以下内容粘贴到 `start_turtle.launch` 文件中:


<launch>
  <!-- 启动 demo_hello.py 节点 -->
  <node pkg="hello_vscode" type="demo_hello.py" name="hello" output="screen" />

  <!-- 启动 turtlesim_node 节点 -->
  <node pkg="turtlesim" type="turtlesim_node" name="t1"/>

  <!-- 启动 turtle_teleop_key 节点 -->
  <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
</launch>


保存并退出编辑器(在 nano 中按 Ctrl + X,然后按 Y 确认保存)

步骤五:创建 demo_hello.py 文件

cd ~/catkin_ws/src/hello_vscode
touch src/demo_hello.py

打开 demo_hello.py 文件

sudo nano src/demo_hello.py

在 demo_hello.py 文件内添加以下代码: 注意如果是ubuntu18把头部的#!/usr/bin/env python3   3去掉


#!/usr/bin/env python3

import rospy

if __name__ == '__main__':
    rospy.init_node('demo_hello')
    rospy.loginfo("Hello from demo_hello node!")
    rospy.spin()


确保 demo_hello.py 文件有可执行权限:

ls ~/catkin_ws/src/hello_vscode/src/demo_hello.py
chmod +x ~/catkin_ws/src/hello_vscode/src/demo_hello.py

步骤六:编译 ROS 包
cd ~/catkin_ws
catkin_make

步骤七:运行 Launch 文件

source devel/setup.bash
roslaunch hello_vscode start_turtle.launch

现在应该会启动包含在 `start_turtle.launch` 文件中定义的节点。



重新编译错误修复:
# 有时候,在重新编译 ROS 包之前,可能需要删除 build 和 devel 目录,然后重新运行 catkin_make。你可以尝试执行以下步骤:

cd ~/catkin_ws
rm -rf build devel
catkin_make

source /opt/ros/noetic/setup.bash
source ~/catkin_ws/devel/setup.bash
source devel/setup.bash
roslaunch hello_vscode start_turtle.launch


发表观点 / Comment

提示:本文章评论功能已关闭