news 2026/7/4 3:36:04

【ROS】 ros学习日记(1)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【ROS】 ros学习日记(1)

ros学习日记(1)

  • ros安装
  • 测试ros(小乌龟,启动!!)
    • 启动小乌龟并测试
    • 小乌龟不动错误排查
  • HelloWorld
    • 1.创建工作空间并初始化
    • 2.进入 src 创建 ros 包并添加依赖
    • 3.使用C++编写程序

ros的安装、测试和helloworld(学习的为赵虚左老师的教程 1.老师的文档 2.老师的视频)

ros安装

直接诶使用鱼香肉丝的ros一键安装
命令如下:

wgethttp://fishros.com/install-Ofishros&&.fishros

测试ros(小乌龟,启动!!)

启动小乌龟并测试

首先启动三个命令行(ctrl + alt + T)

1.命令行1键入:

` ``bash
roscor

2.命令行2键入以下命令(此时会弹出图形化界面) ```bash rosrun turtlesim turtlesim_node

可以输入tur后按Tab自动补全

3.命令行3键入以下命令(在3中可以通过上下左右控制2中乌龟的运动)

rosrun turtlesim turtle_teleop_key

最终结果如下所示:

小乌龟不动错误排查

1.光标是否停留在rosrun turtlesim turtle_teleop_key界面
2.输入法是否为英文

HelloWorld

先创建一个工作空间→再创建一个功能包→编辑源文件 →编辑配置文件→编译并执行

1.创建工作空间并初始化

mkdir-p自定义空间名称/srccd自定义空间名称 catkin_make

比如

mkdir-pdemo001_ws/srccddemo001_ws catkin_make

上述命令,首先会创建一个工作空间以及一个 src 子目录,然后再进入工作空间调用 catkin_make命令编译。

2.进入 src 创建 ros 包并添加依赖

cdsrc catkin_create_pkg 自定义ROS包名 roscpp rospy std_msgs

比如

cdsrc catkin_create_pkg helloworld roscpp rospy std_msg

上述命令,会在工作空间下生成一个功能包,该功能包依赖于 roscpp、rospy 与 std_msgs,其中roscpp是使用C++实现的库,而rospy则是使用python实现的库,std_msgs是标准消息库,创建ROS功能包时,一般都会依赖这三个库实现。
运行效果如下

can@volcano:~$mkdir-pdemo001_ws/src can@volcano:~$cddemo001_ws/ can@volcano:~/demo001_ws$ catkin_make Base path: /home/can/demo001_ws Source space: /home/can/demo001_ws/src Build space: /home/can/demo001_ws/build Devel space: /home/can/demo001_ws/devel Install space: /home/can/demo001_ws/install Creating symlink"/home/can/demo001_ws/src/CMakeLists.txt"pointing to"/opt/ros/noetic/share/catkin/cmake/toplevel.cmake"######## Running command: "cmake /home/can/demo001_ws/src -DCATKIN_DEVEL_PREFIX=/home/can/demo001_ws/devel -DCMAKE_INSTALL_PREFIX=/home/can/demo001_ws/install -G Unix Makefiles" in "/home/can/demo001_ws/build"####-- The C compiler identification is GNU9.4.0 -- The CXX compiler identification is GNU9.4.0 -- Checkforworking C compiler: /usr/bin/cc -- Checkforworking C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info -done-- Detecting C compile features -- Detecting C compile features -done-- Checkforworking CXX compiler: /usr/bin/c++ -- Checkforworking CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info -done-- Detecting CXX compile features -- Detecting CXX compile features -done-- Using CATKIN_DEVEL_PREFIX: /home/can/demo001_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3(found suitable version"3.8.10", minimum required is"3")-- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing()-- Using CATKIN_TEST_RESULTS_DIR: /home/can/demo001_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under'/usr/src/googletest':gtests will be built -- Found gmock sources under'/usr/src/googletest':gmock will be built -- Found PythonInterp: /usr/bin/python3(found version"3.8.10")-- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- Configuringdone-- Generatingdone-- Build files have been written to: /home/can/demo001_ws/build######## Running command: "make -j12 -l12" in "/home/can/demo001_ws/build"####can@volcano:~/demo001_ws$cdsrc can@volcano:~/demo001_ws/src$ catkin_create_pkg helloworld roscpp rospy std_msg Createdfilehelloworld/package.xml Createdfilehelloworld/CMakeLists.txt Created folder helloworld/include/helloworld Created folder helloworld/src Successfully created filesin/home/can/demo001_ws/src/helloworld. Please adjust the valuesinpackage.xml. can@volcano:~/demo001_ws/src$

3.使用C++编写程序

1.进入 ros 包的 src 目录编辑源文件

cd自定义的包


我这里是helloworld
所以

cdhelloworldcdsrc

新建一个cpp文档

touchhelloworld_c.cpp

然后在文档中输入以下代码

#include"ros/ros.h"intmain(intargc,char*argv[]){//执行 ros 节点初始化ros::init(argc,argv,"hello");//创建 ros 节点句柄(非必须)ros::NodeHandle n;//控制台输出 hello worldROS_INFO("hello world!");return0;}

2.编辑 ros 包下的 Cmakelist.txt文件

把下面两块的注释去掉并修改(一个是在136行,一个是149-151行)

136行的如下:

add_executable(步骤3的源文件名 src/步骤3的源文件名.cpp)

改为

add_executable(haha src/helloworld_c.cpp)

另一个如下:

target_link_libraries(步骤3的源文件名${catkin_LIBRARIES})

改为

target_link_libraries(haha${catkin_LIBRARIES})

3.进入工作空间目录并编译

cd自定义空间名称 catkin_make

4.执行
(新的终端)
先启动命令行1:

roscore

再启动命令行2:
(又一个新的终端)

cd工作空间source./devel/setup.bash rosrun 包名 C++节点

ps.节点就是第二步的时候我们修改的那两行的括号里第一个参量,这是一种映射关系,haha和src/helloworld_c.cpp的映射关系。
最终结果如下:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/4 3:34:53

swagger增强knife4j

1、官网文档 快速开始 | Knife4j 2、引入依赖 <dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.5.0</version> </dependency>3、配…

作者头像 李华
网站建设 2026/7/4 3:34:32

C++:拷贝构造函数

一、什么是拷贝构造函数&#xff1f; 拷贝构造函数是一种特殊的构造函数&#xff0c;它的参数是同类型对象的常量引用&#xff0c;即用已存在的一个变量初始化另一个同类型变量。 class Person { public:// 拷贝构造函数Person(const Person& other) {name other.name;age…

作者头像 李华
网站建设 2026/7/4 3:32:45

椭圆曲线 Diffie-Hellman 密钥交换解题思路

在密码学入门中&#xff0c;椭圆曲线密码&#xff08;ECC&#xff09;以其更短的密钥长度和更高的安全性备受青睐。而椭圆曲线 Diffie-Hellman&#xff08;ECDH&#xff09;则是 ECC 最经典的应用之一&#xff0c;它允许双方在不安全的信道上协商出一个共享秘密。最近在解决 Cr…

作者头像 李华
网站建设 2026/7/4 3:32:26

集团知识管理平台建设方案:74页PpT爆款干货全解析!

很多公司搞知识管理&#xff0c;看起来热闹&#xff0c;实际却是一堆文件躺在服务器里吃灰。业务部门找不到想要的方案&#xff0c;技术人员每次新人培训要翻几十个文件夹。知识库变成了“死库”&#xff0c;没人用&#xff0c;也没人维护。更头疼的是&#xff0c;领导问一声“…

作者头像 李华
网站建设 2026/7/4 3:31:56

【2026万字实录】从理论到实战:网络信息安全全景深度解析与避坑指南

【2026万字实录】从理论到实战&#xff1a;网络信息安全全景深度解析与避坑指南 &#x1f4cc; 核心导读 站在2026年的技术节点&#xff0c;网络安全已不再是“防火墙杀毒软件”的简单堆砌&#xff0c;而是演变为涵盖云原生、人工智能、数据治理与地缘合规的复杂巨系统。本文拒…

作者头像 李华
网站建设 2026/7/4 3:31:31

后 Django 时代:SQLAlchemy 2.0、Tortoise 与 Piccolo 三大异步 ORM 选型指南

参赛选手# SQLAlchemy 2.0&#xff1a;数据库界的工业母机# 如果说 Python 数据库领域有一座神庙&#xff0c;那供奉的一定是 SQLAlchemy。在经历了漫长的 1.x 时代后&#xff0c;2.0 版本的发布标志着它正式拥抱了强类型标注和原生异步。 SQLAlchemy 在 GitHub 上拥有 11.7…

作者头像 李华