ROS入门笔记

这篇博客介绍了ROS (Robot Operating System) 的安装过程,包括针对Ubuntu 18.04的ROSMelodicMorenia的详细步骤。接着,讲解了如何设置环境变量、创建ROS工作空间,并通过catkin_make进行编译。此外,还涵盖了ROS中的关键概念,如节点、消息、主题、服务和参数,以及使用rostopic、rosservice、rosparam等工具进行交互。最后,提到了rosbag用于记录和回放数据,以及roslaunch用于启动多个节点。

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

安装

ROS installation instructions
我安装 版本的是

ROS Melodic Morenia
Released May, 2018
LTS, supported until May, 2023
Recommended for Ubuntu 18.04

环境变量

  • source “some setup.*sh” files

source /opt/ros//setup.bash
source /opt/ros/melodic/setup.bash

设置每次打开终端,自动source设置文件

echo “source /opt/ros/melodic/setup.bash” >> ~/.bashrc
source ~/.bashrc

  • 查看环境变量

printenv | grep ROS

ROS_ETC_DIR=/opt/ros/melodic/etc/ros
ROS_ROOT=/opt/ros/melodic/share/ros
ROS_MASTER_URI=http://localhost:11311
ROS_VERSION=1
ROS_PYTHON_VERSION=2
ROS_PACKAGE_PATH=/opt/ros/melodic/share
ROSLISP_PACKAGE_DIRECTORIES=
ROS_DISTRO=melodic

Create a ROS Workspace

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
  • 工作空间的目录

build
devel. Inside the ‘devel’ folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment.
src : 里面有CmakeLists.txt

source devel/setup.bash

echo $ROS_PACKAGE_PATH
/home/lsy/catkin_ws/src:/opt/ros/melodic/share

Navigating the ROS Filesystem

  • Packages: Packages are the software organization unit of ROS code. Each package can contain libraries, executables, scripts, or other artifacts.

  • Manifests (package.xml): A manifest is a description of a package. It serves to define dependencies between packages and to capture meta information about the package like version, maintainer, license, etc…

rospack

rospack find [package_name]

roscd

roscd <package-or-stack>[/subdir]

rosls

rosls <package-or-stack>[/subdir]

Creating a ROS Package

cd ~/catkin_ws/src

catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

cd ~/catkin_ws
catkin_make

package dependencies

rospack depends1 beginner_tutorials 
rospack depends beginner_tutorials

包的目录结构

在这里插入图片描述

编译包

$ catkin_make
$ catkin_make install  # (optionally)

$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

The above commands will build any catkin projects found in the src folder.

节点

  • 节点: A node is an executable that uses ROS to communicate with other nodes.ROS nodes use a ROS client library to communicate with other nodes.
  • 消息:ROS data type used when subscribing or publishing to a topic.
  • 主题:odes can publish messages to a topic as well as subscribe to a topic to receive messages.
  • master:Name service for ROS (i.e. helps nodes find each other)
  • rosout:ROS equivalent of stdout/stderr
  • roscore: Master + rosout + parameter server (parameter server will be introduced later)

客户端库:

  • rospy = python client library

  • roscpp = c++ client library

  • roscore = ros+core : master (provides name service for ROS) + rosout (stdout/stderr) + parameter server (parameter server will be introduced later)

  • rosnode = ros+node : ROS tool to get information about a node.

  • rosrun = ros+run : runs a node from a given package.

主题

  • 先执行两个节点
rosrun [--prefix cmd] [--debug] PACKAGE EXECUTABLE [ARGS]
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
  • rqt_graph
rosrun rqt_graph rqt_graph
  • rostopic
lsy@lsy-GS65-Stealth-9SD:~/catkin_ws/src$ rostopic
rostopic is a command-line tool for printing information about ROS Topics.

Commands:
	rostopic bw	display bandwidth used by topic
	rostopic delay	display delay of topic from timestamp in header
	rostopic echo	print messages to screen
	rostopic find	find topics by type
	rostopic hz	display publishing rate of topic    
	rostopic info	print information about active topic
	rostopic list	list active topics
	rostopic pub	publish data to topic
	rostopic type	print topic or field type
  • rosmsg
lsy@lsy-GS65-Stealth-9SD:~/catkin_ws/src$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist
lsy@lsy-GS65-Stealth-9SD:~/catkin_ws/src$ rosmsg show geometry_msgs/Twist
geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

主动发布主题

rostopic pub [topic] [msg_type] [args]

rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

Services and Parameters

节点可以通过服务来通信。Services allow nodes to send a request and receive a response.

  • rosservice
rosservice list         print information about active services
rosservice call         call the service with the provided args
rosservice type         print service type
rosservice find         find services by service type
rosservice uri          print service ROSRPC uri

rosservice type [service]

rosservice call [service] [args]
  • rosparam
    rosparam allows you to store and manipulate data on the ROS Parameter Server.
rosparam set            set parameter
rosparam get            get parameter
rosparam load           load parameters from file
rosparam dump           dump parameters to file
rosparam delete         delete parameter
rosparam list           list parameter names

rqt_console and roslaunch

using rqt_console and rqt_logger_level for debugging and roslaunch for starting many nodes at once.

  • roslaunch
    自动在包的目录下寻找对应的launch文件。oslaunch command automatically looks into the passed package and detects available launch files.
roslaunch beginner_tutorials turtlemimic.launch

lsy@lsy-GS65-Stealth-9SD:~/catkin_ws/src/beginner_tutorials$ rosrun turtlesim 
draw_square        mimic              turtlesim_node     turtle_teleop_key

一次性启动多个节点

<launch>

  <group ns="turtlesim1">
    <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
  </group>

  <group ns="turtlesim2">
    <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
  </group>

  <node pkg="turtlesim" name="mimic" type="mimic">
    <remap from="input" to="turtlesim1/turtle1"/>
    <remap from="output" to="turtlesim2/turtle1"/>
  </node>

</launch>

rosed

不用输入完整目录

rosed [package_name] [filename]

msg 和 srv 文件

  • msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.

每行一个类型

  Header header
  string child_frame_id
  geometry_msgs/PoseWithCovariance pose
  geometry_msgs/TwistWithCovariance twist
  • srv: an srv file describes a service. It is composed of two parts: a request and a response.
    分请求、响应2部分
int64 A
int64 B
---
int64 Sum

既使简单的功能,生成的文件页特别复杂

Publisher and Subscriber

固定流程。
CmakLists.txt

Writing a Simple Service and Client (C++)

还行吧。约束很多

Recording and playing back data

rosbag record -a
rosbag info 2022-03-20-19-10-22.bag 
rosbag play 2022-03-20-19-10-22.bag 
rosbag play -r 2  2022-03-20-19-10-22.bag 

Reading messages from a bag file

  • rostopic
  • ros_readbagfile:更快

Getting started with roswtf

古月居ROS入门21课笔记主要涵盖了ROS的安装和基本使用方法,以下是我的总结: 1. 安装ROS:首先要安装ROS操作系统,根据教程可以选择安装适合自己系统的版本,如ROS Kinetic或ROS Melodic等。 2. 创建工作空间:使用catkin工具创建ROS工作空间,可以将所有的ROS相关文件放在这个空间中。 3. 创建包:在工作空间内创建一个ROS包,这个包是ROS项目的基本单元。可以使用catkin_create_pkg命令来创建包,并指定包的依赖项。 4. 编写节点代码:节点是ROS中最基本的执行单元,可以通过编写节点来实现各种功能。可以使用C++或Python编写节点代码。 5. 编译代码:使用catkin_make命令编译ROS代码,编译后会生成可执行文件。编译成功后,可以在工作空间中的bin目录下找到生成的可执行文件。 6. 运行节点:使用rosrun命令来运行编译好的节点。节点运行后会执行相应的功能。 7. 使用消息通信:ROS中的节点通过发布和订阅消息来进行通信。可以编写发布节点和订阅节点代码,通过话题的方式进行消息传递。 8. 使用服务通信:除了消息通信,ROS还提供了服务通信的机制。可以编写服务端和客户端代码,进行服务调用和响应。 9. 使用参数服务器:ROS提供了参数服务器来存储和共享参数。可以将特定的参数存储在参数服务器上,并在节点中进行访问和修改。 10. 使用RViz可视化:RViz是ROS环境下的可视化工具,可以将机器人模型和传感器数据等进行可视化展示,方便调试和分析。 通过学习古月居ROS入门21课,我对ROS的基本使用方法和常用功能有了初步了解。在实践中,我将进一步深入学习和应用ROS,提高自己在机器人开发和控制方面的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值