0
点赞
收藏
分享

微信扫一扫

Ventus(承影):基于RISC V的开源GPGPU

上善若水山西太原 2024-04-21 阅读 6
ubuntulinux

ROS的第一个项目:输出Hello World

我们将学习如何创建一个简单的ROS(Robot Operating System)项目,该项目将在终端中输出"Hello World"。我们将使用Python语言进行编程。
在这里插入图片描述

环境准备

首先,确保你的计算机已经安装了ROS。如果还没有安装,可以参考ROS官方文档进行安装。

创建ROS包

打开终端(请确保已经创建了ros工作空间,不会请点击看以前的文章),导航到你的catkin工作区的src目录下,然后使用以下命令创建一个名为hello_world的ROS包:

cd ~/catkin_ws/src
# 这个路径是你自己ros项目文件,注意替换自己的路径    catkin_ws是我的ros项目名字
catkin_create_pkg hello_world std_msgs rospy roscpp

这将在src目录下创建一个名为hello_world的新目录,其中包含一些文件和目录。

创建Python脚本

hello_world包中,我们需要创建一个Python脚本来发布消息。首先,我们需要在hello_world目录中创建一个名为scripts的新目录:

cd ~/catkin_ws/src/hello_world
mkdir scripts
# 这个路径是你自己ros项目文件,注意替换自己的路径    catkin_ws是我的ros项目名字

然后,在scripts目录中创建一个名为hello_world.py的新文件,并添加以下内容:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

记得给hello_world.py添加可执行权限:

chmod +x hello_world.py

编译包

在终端输入

cd ~/Desktop/ros_1/ #切换到自己的ros项目根目录
# 这个路径是你自己ros项目路径,注意替换自己的路径
catkin_make
source ~/Desktop/ros_1/devel/setup.bash
# 这个路径是你自己ros项目下的devel目录下setup.bash文件,注意替换自己的路径

运行节点

现在,我们可以运行hello_world节点了。首先,需要启动ROS核心:

roscore

然后,在新的终端窗口中,运行我们的hello_world节点:

rosrun hello_world hello_world.py

你应该能在终端中看到"Hello World"的消息。
在这里插入图片描述

举报

相关推荐

0 条评论