ROS学习笔记1——用ros输出helloworld

本文详细介绍了如何在VSCode中设置ROS工作空间,使用快捷键编译,创建并配置C++功能包,以及在CMakeLists.txt中进行编译和执行的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建ROS工作空间

mkdir -p xxx_ws/src(必须得有 src)
cd xxx_ws
catkin_make

二、启动vscode

进入工作空间,启动vscode

code .

三、快捷键编译ros

快捷键 ctrl + shift + B 调用编译,点击catkin_make:build后面的小齿轮

修改.vscode/tasks.json 文件

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "catkin_make:debug", //代表提示的描述性信息
            "type": "shell",  //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
            "command": "catkin_make",//这个是我们需要运行的命令
            "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"//可选always或者silence,代表是否输出信息
            },
            "problemMatcher": "$msCompile"
		}
	]
}

此时可以用ctrl + shift + B 测试能否成功编译。

四、创建ROS功能包

1、选定 src 右击 ---> create catkin package

2、设置包名:

helloworld

3、添加依赖:

roscpp rospy std_msgs    //一般来说都会添加这三个

五、C++代码实现

在功能包的src下创建cpp文件,注意cpp文件命名规范。

#include "ros/ros.h"

int main(int argc,char *argv[])
{
    //解决乱码问题
    setlocale(LC_ALL,"");
    ros::init(argc,argv,"helloworld");    //节点初始化
    ROS_INFO("hello world!,哈哈哈");    //输出日志
    
    return 0;
}

六、配置CMakeLists.txt文件

1、add_executable命令用于使用指定的源文件向项目(project)添加可执行文件

add_executable(节点名称   src/可执行cpp文件在功能包下的路径.cpp)

2、target_link_libraries命令用于指定链接给定目标和/或其依赖项时要使用的库或标志来自链接库目标的使用要求将被传播

target_link_libraries(节点名称  ${catkin_LIBRARIES})

七、编译执行

1、ctrl + shift + B 快捷键进行编译

2、可以在vscode中新建终端,目录会自动定位在该代码的工作空间下。

3、开启roscore

4、开一个新终端,刷新环境变量

source ./devel/setup.bash

5、执行代码

rosrun helloworld helloworld

### ROS2 C++ Topic Communication Learning Materials and Tutorials For those interested in exploring how to use topics for communication within the ROS2 framework using C++, several resources provide comprehensive guidance on this subject. The official documentation of ROS 2 offers an extensive tutorial that covers setting up publishers and subscribers with detailed explanations and code examples written in C++. This resource is invaluable as it not only explains the theory but also provides practical steps necessary to establish topic-based communications between nodes[^1]. Additionally, a popular choice among developers includes tutorials available from Robot Ignite Academy which presents step-by-step instructions alongside video demonstrations specifically tailored towards understanding message passing through topics via C++ interfaces in ROS2 environments. These sessions are designed to cater both beginners who need foundational knowledge about ROS concepts along with more advanced users looking into optimizing their applications' performance when dealing with real-time data exchange over networks. Another noteworthy mention goes out to Open Robotics’ own set of educational materials where one can find well-documented samples illustrating various aspects related to working with messages across different programming languages including C++; these documents often include best practices recommendations ensuring efficient utilization while adhering closely to community standards established around ROS ecosystem development processes. ```cpp // Example Code Snippet Demonstrating Basic Publisher Node Implementation Using rclcpp Library In C++ #include "rclcpp/rclcpp.hpp" using namespace std::chrono_literals; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); auto node = rclcpp::Node::make_shared("example_publisher"); auto publisher = node->create_publisher<std_msgs::msg::String>("topic", 10); auto msg = std_msgs::msg::String(); msg.data = "Hello, world!"; rclcpp::WallRate loop_rate(1s); // Publish every second while (rclcpp::ok()) { RCLCPP_INFO(node->get_logger(), "Publishing: '%s'", msg.data.c_str()); publisher->publish(msg); rclcpp::spin_some(node); loop_rate.sleep(); } rclcpp::shutdown(); } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值