ROS1学习

ROS 是一个适用于机器人的开源操作系统。它提供了操作系统应有的服务,包括硬件抽象、底层设备控制、常用函数实现、进程间消息传递和包管理。它也提供用于获取、编译、编写和跨计算机运行代码所需的工具和库函数。

1. ROS工作空间

catkin是ROS的正式构建系统,并且是原始ROS构建系统rosbuild的继承者。catkin结合了CMake宏和Python脚本, 是组织代码的推荐方式,它使用更标准的 CMake 约定并提供更大的灵活性,特别是对于想要集成外部代码库或想要发布软件的人。

catkin工作空间用于存放ROS程序包,一个工作空间可存放多个程序包。

在安装好ROS之后,我们可以通过以下方式来创建一个工作空间,并编译这个工作空间:

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make

catkin_make命令是使用Catkin工作区工作的便利工具。 在您的工作区中第一次运行它,它将在“SRC”文件夹中创建一个cmakelist.txt链接。
在这里插入图片描述
查看当前目录, 有一个“build”和“devel”文件夹。 在“devel”文件夹中, 可以看到现在有几个 setup.*sh 文件。

source命令的目的是:刷新工作空间环境。

source devel/setup.bash 

要想保证工作空间已配置正确则需确保ROS_PACKAGE_PATH环境变量包含你的工作空间目录,采用以下命令查看

echo $ROS_PACKAGE_PATH 
/open/myros/catkin_ws/src:/opt/ros/noetic/share

至此,一个工作空间便创建起来了。

2. ROS 文件系统

ROS的新构建系统是“catkin”,而“rosbuild”是旧的ROS构建系统,被catkin取代。

catkin程序包Packages:程序包是是ROS代码的软件组织单元。程序包相当于一个project,一个程序包一般包含程序文件(src文件夹中的.cpp和.py文件)、编译描述文件(package.xml)和配置文件(CMakeList.txt)等。

编译描述文件package.xml 是包的编译描述文件 。 它用于定义包之间的依赖关系 ,并捕获有关 元信息包的版本、维护者​​、许可证等…

由于代码分布在许多 ROS 包中, 使用 命令行 ls 和 cd 可能不便,可以用 ROS 提供的工具。

2.1 rospack 获取有关包的信息

在这里插入图片描述
如, find返回路径

rospack find [package_name]

2.2 roscd 和 rosls

roscd 是 rosbash 套件的一部分 。 它允许将目录 ( cd ) 直接更改为包或堆栈。

liwete@liwete-OMEN-by-HP-Laptop:~$ roscd roscpp
liwete@liwete-OMEN-by-HP-Laptop:/opt/ros/noetic/share/roscpp$ 

roscd 也可以移动到包或堆栈的子目录。

liwete@liwete-OMEN-by-HP-Laptop:~$ roscd roscpp/cmake
liwete@liwete-OMEN-by-HP-Laptop:/opt/ros/noetic/share/roscpp/cmake$ 

roscd log 将带您到 ROS 存储日志文件的文件夹。

liwete@liwete-OMEN-by-HP-Laptop:~$ roscd log
No active roscore
liwete@liwete-OMEN-by-HP-Laptop:~/.ros/log/55e93524-1dde-11ec-9c40-8f5ed5766145$ ls
master.log  roslaunch-liwete-OMEN-by-HP-Laptop-16677.log  rosout-1-stdout.log  rosout.log

rosls 允许 直接在包中按名称而不是绝对路径进行ls

liwete@liwete-OMEN-by-HP-Laptop:~$ rosls roscpp_tutorials 
cmake  launch  package.xml  srv

ros支持TAB补全

3. 创建 ROS 包

一个catkin包,必须满足一些要求:

  • 包含符合catkin 的 package.xml 文件,该 package.xml 文件提供有关包的元信息。

  • 该包必须包含catkin 使用的 CMakeLists.txt 。

  • 每个包必须有自己的文件夹,这意味着没有嵌套的包,也没有共享同一目录的多个包。
    在这里插入图片描述

3.1创建

首先切换到 catkin 工作空间的src目录,使用 catkin_create_pkg 脚本创建一个名为“beginner_tutorials”的新包,它依赖于 std_msgs、roscpp 和 rospy。

创建一个程序包命令:$catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

liwete@liwete-OMEN-by-HP-Laptop:/open/csdn/catkin_ws/src$ catkin_create_pkg begin_tutorials std_msgs rospy roscpp 
Created file begin_tutorials/package.xml
Created file begin_tutorials/CMakeLists.txt
Created folder begin_tutorials/include/begin_tutorials
Created folder begin_tutorials/src
Successfully created files in /open/csdn/catkin_ws/src/begin_tutorials. Please adjust the values in package.xml.

liwete@liwete-OMEN-by-HP-Laptop:/open/csdn/catkin_ws/src$ ls
begin_tutorials  CMakeLists.txt
liwete@liwete-OMEN-by-HP-Laptop:/open/csdn/catkin_ws/src$ cd begin_tutorials/
liwete@liwete-OMEN-by-HP-Laptop:/open/csdn/catkin_ws/src/begin_tutorials$ ls
CMakeLists.txt  include  package.xml  src

创建一个 begin_tutorials 文件夹,其中包含一个 package.xml 和一个 CMakeLists.txt ,其中部分填充了提供的信息。

3.2 构建一个 catkin 工作区并获取安装文件

编译这个程序包

catkin_make

在这里插入图片描述为了将工作区加入到ROS环境,需要刷新工作空间环境

source devel/setup.bash

3.3 包依赖

查看直接依赖

liwete@liwete-OMEN-by-HP-Laptop:/open/csdn/catkin_ws/src$ rospack depends1 begin_tutorials 
roscpp
rospy
std_msgs

包的这些依赖项存储在 package.xml 文件中:

roscd beginner_tutorials
cat package.xml

在这里插入图片描述
间接依赖:
在许多情况下,一个依赖项也会有它自己的依赖项。 例如, rospy 有其他依赖项。 rospack 可以递归地确定所有嵌套的依赖项。

$ rospack depends begin_tutorials 
cpp_common
rostime
roscpp_traits
roscpp_serialization
catkin
genmsg
genpy
message_runtime
gencpp
geneus
gennodejs
genlisp
message_generation
rosbuild
rosconsole
std_msgs
rosgraph_msgs
xmlrpcpp
roscpp
rosgraph
ros_environment
rospack
roslib
rospy

3.4 定制包

本教程的这一部分将逐行研究Catkin_Create_PKG生成的每个文件,这些文件的每个组件以及如何为您的包自定义它们。

自定义 package.xml

描述标签

<description>The beginner_tutorials package</description>

维护者标签(这是必需且重要的标记, 至少需要一名维护者,email 属性需要填)

<maintainer email="xxxxxxxx@qq.com">liming</maintainer>

许可证标签 (选择一个许可证并在此处填写。 一些常见的开源许可证是 BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1 和 LGPLv3。 本教程使用 BSD 许可证,因为其余的核心 ROS 组件已经使用它)

<license>BSD</license> 

依赖标签 (包的依赖关系被拆分为build_depend,buildtool_depend,exec_depend,test_depend。由于我们将 std_msgs 、 roscpp 和 rospy 作为参数传递给 catkin_create_pkg ,因此依赖项将如下所示。除了Catkin上的默认BuildTool_depend之外,我们所有列出的依赖项都被添加Build_Dependies。 在这种情况下,我们希望所有指定的依赖项都在构建和运行时可用,因此我们也会为每个exec_depend标记添加一个exec_depend标记 )

<buildtool_depend>catkin</buildtool_depend>
  
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>

<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>

4. Build包

catkin_make是一个命令行工具,为标准Catkin工作流提供了方便。 catkin_make将CMake和Make的调用结合在标准CMake工作流程中。

正常的Cmake流程:

# In a CMake project
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (optionally)

相比之下,catkin 项目可以在工作空间中一起构建。 在工作区中构建零到多个 catkin 包遵循以下流程:

# In a catkin workspace
$ catkin_make
$ catkin_make install  # (optionally)

上面的命令将构建在SRC文件夹中找到的任何Catkin项目。

如果源代码在不同的地方,调用方式如下:

# In a catkin workspace
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

进入工作区并构建我们的包:

$ cd ~/catkin_ws
$ catkin_make
Base path: /open/csdn/catkin_ws
Source space: /open/csdn/catkin_ws/src
Build space: /open/csdn/catkin_ws/build
Devel space: /open/csdn/catkin_ws/devel
Install space: /open/csdn/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/open/csdn/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /open/csdn/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /open/csdn/catkin_ws/devel;/opt/ros/noetic
-- This workspace overlays: /open/csdn/catkin_ws/devel;/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
-- Using empy: /usr/lib/python3/dist-packages/em.py
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /open/csdn/catkin_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") 
-- Using Python nosetests: /usr/bin/nosetests3
-- catkin 0.8.10
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - begin_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'begin_tutorials'
-- ==> add_subdirectory(begin_tutorials)
-- Configuring done
-- Generating done
-- Build files have been written to: /open/csdn/catkin_ws/build
####
#### Running command: "make -j8 -l8" in "/open/csdn/catkin_ws/build"
####

在 catkin 工作区中创建了几个文件夹,查看:

 ls
build  devel  src

build文件夹是builld空间以及调用CMake和Make来配置和构建软件包的默认位置。 devel文件夹是devel Space的默认位置,它是可执行文件和库在安装软件包之前的位置。

5. 理解ROS节点

基本概念:

Nodes:节点,一个节点即为一个可执行文件,它可以通过ROS与其它节点进行通信。
Messages:消息,消息是一种ROS数据类型,用于订阅或发布到一个话题。
Topics:话题,节点可以发布消息到话题,也可以订阅话题以接收消息。
Master:节点管理器,ROS名称服务 (比如帮助节点找到彼此)。
rosout:ROS中相当于stdout/stderr。 roscore: 主机+ rosout + 参数服务器 。

5.1 节点

ROS节点是程序包中的可执行文件,每一个节点都是ROS 包中的一个可执行文件, 可以通过ROS 客户端库实现节点与节点间的相互通讯。节点可以发布或接收一个话题,也可以提供或使用某种服务。

例如,机器人和遥控器就是两个节点。遥控器起到了下达指令的作用;机器人负责监听遥控器下达的指令,完成相应动作。

5.2 客户端库

ROS 客户端库允许使用不同编程语言编写的节点进行通信:

rospy = python 客户端库
roscpp = C++ 客户端库 

5.3 roscore

roscore 是你在使用 ROS 时应该运行的第一件事。

$ roscore
... logging to /home/liwete/.ros/log/20d204de-1e87-11ec-9c40-8f5ed5766145/roslaunch-liwete-OMEN-by-HP-Laptop-33293.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://liwete-OMEN-by-HP-Laptop:41601/
ros_comm version 1.15.11


SUMMARY
========

PARAMETERS
 * /rosdistro: noetic
 * /rosversion: 1.15.11

NODES

auto-starting new master
process[master]: started with pid [33315]
ROS_MASTER_URI=http://liwete-OMEN-by-HP-Laptop:11311/

setting /run_id to 20d204de-1e87-11ec-9c40-8f5ed5766145
process[rosout-1]: started with pid [33339]
started core service [/rosout]

如果 roscore 未初始化并发送有关缺少权限的消息,则使用以下命令递归更改该文件夹的所有权:

sudo chown -R <your_username> ~/.ros

5.4 使用节点

注意,roscore要一直开着

使用rosnode list查看运行的节点

$ rosnode list
/rosout

rosout 用于收集和记录节点的调试输出。

查看节点信息

$ rosnode info /rosout
--------------------------------------------------------------------------------
Node [/rosout]
Publications: 
 * /rosout_agg [rosgraph_msgs/Log]

Subscriptions: 
 * /rosout [unknown type]

Services: 
 * /rosout/get_loggers
 * /rosout/set_logger_level


contacting node http://liwete-OMEN-by-HP-Laptop:41321/ ...
Pid: 33339

启动节点:

rosrun 允许使用包名直接运行包内的节点(无需知道包路径)。

例如运行海龟节点

$ rosrun turtlesim turtlesim_node

可以看到
在这里插入图片描述
此时再查看节点,可以看到海龟节点也在运行:

$ rosnode list
/rosout
/turtlesim

ctrl-C 即可关闭退出。

ROS可以用 重映射参数 来更改节点的名称:

rosrun turtlesim turtlesim_node __name:=my_turtle 
rosnode list
/my_turtle
/rosout

出现了新的 /my_turtle 节点。 让我们使用另一个 rosnode 命令 ping 来测试它是否已启动:

$ rosnode ping my_turtle 
rosnode: node is [/my_turtle]
pinging /my_turtle with a timeout of 3.0s
xmlrpc reply from http://liwete-OMEN-by-HP-Laptop:36763/	time=0.411510ms
xmlrpc reply from http://liwete-OMEN-by-HP-Laptop:36763/	time=1.273632ms
xmlrpc reply from http://liwete-OMEN-by-HP-Laptop:36763/	time=1.128197ms
xmlrpc reply from http://liwete-OMEN-by-HP-Laptop:36763/	time=1.351357ms

6. 理解topic主题

首先保证有且仅有一个roscore正在运行。

启动海龟节点和控制器节点:

rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
Reading from keyboard
---------------------------
Use arrow keys to move the turtle. 'q' to quit.

选中控制器界面,即可利用方向键控制海龟运动。

turtlesim_node和turtle_teleop_key节点通过ROS主题相互通信。 turtle_teleop_key在某主题上发布消息,而Turtlesim订阅相同主题以接收消息。

6.1 rqt_graph

让我们使用rqt_graph,显示当前正在运行的节点和主题:

rosrun rqt_graph rqt_graph

rqt_graph会创建系统运行内容的动态图。

在这里插入图片描述
可以看到 turtlesim_node 和 turtle_teleop_key 节点在名为/turtle1/command_velocity的主题topic上进行通信。

6.2 rostopic

rostopic 工具可以获取有关ROS的主题的信息:

rostopic -h
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

Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

rostopic echo 显示发布在某个主题上的数据:

rostopic echo /turtle1/cmd_vel
linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 0.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: -2.0
---

现在让我们 看看 rqt_graph , 按左上角的刷新按钮以显示新节点。 如您所见 rostopic echo 现在也订阅 了turtle1/command_velocity 主题。
在这里插入图片描述
rostopic list 返回当前订阅和发布的所有主题的列表。

rostopic list -v

Published topics:
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
 * /rosout [rosgraph_msgs/Log] 4 publishers
 * /turtle1/pose [turtlesim/Pose] 1 publisher
 * /turtle1/color_sensor [turtlesim/Color] 1 publisher
 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher

Subscribed topics:
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers
 * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber

以上显示了要发布和订阅的主题及其类型的详细列表。

6.3 ROS消息

主题的通信是通过 在节点之间 发送 ROS 消息来实现的。 为了让发布者( turtle_teleop_key )和订阅者( turtlesim_node )进行通信,发布者和订阅者必须发送和接收相同类型 的消息。

这意味着主题类型由发布的消息类型定义, 可以使用rostopic类型确定在主题上发送的消息的类型。

$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist

主题类型为geometry_msgs/Twist,查看细节:

$ 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

可以看到,消息由线速度和角速度组成。

6.4 发布数据

rostopic pub 可以在当前广播的主题上发布数据:

例如:

$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
publishing and latching message for 3.0 seconds

rostopic pub表示向某个主题发布数据,-1表示发布一次即退出,/turtle1/cmd_vel表示主题名,geometry_msgs/Twist表示在主题发布的数据类型,--表示告诉option解析器以下参数不是option,如前所述,geometry_msgs/Twist msg 有两个向量:linear 和 angular。 每个向量包含三个浮点元素 。 在这种情况下, '[2.0, 0.0, 0.0]' 为线性值 x=2.0 、 y=0.0 和 z=0.0 ,而 '[0.0, 0.0, 1.8]' 为角度值 x=0.0 , y=0.0 和 z=1.8 。 这些参数实际上采用 YAML 语法。
在这里插入图片描述
前面的命令将向turtlesim发送一条消息,告诉它以2.0的线速度和1.8的角速度移动。

乌龟已经停止移动了; 这是因为海龟需要 1 Hz 的稳定命令流才能保持移动。 我们可以使用发布稳定的命令流 rostopic pub -r 命令

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

在这里插入图片描述
-r 1 表示以 1 Hz 的速率发布命令。

刷新rqt_graph, rostopic pub 节点正在与 rostopic echo 节点通信:
在这里插入图片描述
查看我们的turtlesim发布的数据rostopic echo /turtle1/pose
在这里插入图片描述
查看数据发布频率rostopic hz /turtle1/pose
在这里插入图片描述
现在我们可以看出,turtlesim 正在以 60 Hz 的速率发布有关我们的海龟的数据。

6.5 rqt_plot

rqt_plot显示在主题上发布的数据的滚动时间图。 在这里我们将使用rqt_plot绘制在 /turtle1/pose 主题上发布的数据。

启动rqt_plot

rosrun rqt_plot rqt_plot

在左上角文本框分别输入/turtle1/pose/x 和/turtle1/pose/y,然后点击加号,即可。

在这里插入图片描述

7. ROS 服务和参数

服务是节点相互通信的另一种方式。 服务允许节点发送请求 并接收响应 。

7.1 rosservice

rosservice 可以通过服务轻松地附加到 ROS 的客户端/服务框架。 rosservice 有很多可以在服务上使用的命令:

rosservice list 打印有关活动服务的信息 
rosservice call 使用提供的参数调用服务 
rosservice type 打印服务类型 
rosservice find 按服务类型查找服务 
rosservice uri 打印服务 ROSRPC uri 

rosservice list

$ rosservice list
/clear
/kill
/reset
/rosout/get_loggers
/rosout/set_logger_level
/rostopic_14737_1632820543894/get_loggers
/rostopic_14737_1632820543894/set_logger_level
/rqt_gui_py_node_12599/get_loggers
/rqt_gui_py_node_12599/set_logger_level
/spawn
/teleop_turtle/get_loggers
/teleop_turtle/set_logger_level
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/get_loggers
/turtlesim/set_logger_level

List命令向我们展示Turtlesim节点提供的服务:reset, clear, spawn, kill, turtle1/set_pen, /turtle1/teleport_absolute, /turtle1/teleport_relative, turtlesim/get_loggers, turtlesim/set_logger_level.
还有两个与单独的rosout节点相关的服务:/rosout/get_loggers , /rosout/set_logger_level。

rosservice type 查看服务类型

$ rosservice type /clear
std_srvs/Empty

该服务是空的,这意味着在进行服务调用时它不接受任何参数(即,在发出请求时不发送数据,在接收时不接收数据响应)。

rosservice call 服务调用

$ rosservice call /clear

在这里插入图片描述
turtlesim_node背景被清除了。

$ rosservice type /spawn | rossrv show
float32 x
float32 y
float32 theta
string name
---
string name

这项服务让我们在给定的位置和方向产生新的乌龟。 名称字段是可选的。

$ rosservice call /spawn 2 2 0.2 ""
name: "turtle2"

在这里插入图片描述

7.2 rosparam

rosparam允许您存储和操作ROS参数服务器上的数据。 参数服务器可以存储整数,浮点,布尔值,词典和列表。 rosparam使用YAML标记语言进行语法。 在简单的情况下,yaml看起来非常自然:1是一个整数,1.0是一个浮点数,一个是一个字符串,true是一个布尔,[1,2,3]是整数列表,{a:b,c: d}是字典。

rosparam有许多可用于参数的命令,如下所示:

rosparam set 设置参数 
rosparam get 获取参数 
rosparam load 从文件中加载参数 
rosparam dump 转储参数到文件 
rosparam delete 删除参数 
rosparam list 列出参数名称 

rosparam list

$ rosparam list
/rosdistro
/roslaunch/uris/host_liwete_omen_by_hp_laptop__38301
/rosversion
/run_id
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r

可以看到turtlesim节点在param server上有3个参数用于背景颜色

rosparam set and rosparam get

$ rosparam set /turtlesim/background_r 150
$ rosservice call /clear

在这里插入图片描述
背景变了。

rosparam get获取参数

$ rosparam get /turtlesim/background_g 
86

使用 rosparam get / 来向我们展示整个 Parameter Server 的内容

osparam get / 
rosdistro: 'noetic

  '
roslaunch:
  uris:
    host_liwete_omen_by_hp_laptop__38301: http://liwete-OMEN-by-HP-Laptop:38301/
rosversion: '1.15.11

  '
run_id: 0c471ff2-2000-11ec-90eb-b98384596bd9
turtlesim:
  background_b: 255
  background_g: 86
  background_r: 150

rosparam dump将参数写入文件

$ rosparam dump params.yaml

rosparam load可以将这些 yaml 文件加载到新的命名空间中:

rosparam load params.yaml copy_turtle

8. rqt_console 和 roslaunch

8.1 rqt_console

rqt_console 附加到 ROS 的日志框架以显示节点的输出。 rqt_logger_level 允许我们在节点运行时更改节点的详细级别(DEBUG、WARN、INFO 和 ERROR)。

在我们启动turtlesim之前, 在两个新终端中 启动 rqt_console 和 rqt_logger_level :

rosrun rqt_console rqt_console
rosrun rqt_logger_level rqt_logger_level

在这里插入图片描述
在这里插入图片描述通过刷新 rqt_logger_level 窗口中的节点并选择Warn来将记录器级别更改为Warn,如下所示:
在这里插入图片描述

现在让我们将乌龟跑到墙上,看看我们的 rqt_console 中显示了什么:
在这里插入图片描述出现了大量Warn。

日志级别按以下顺序排列优先级:

Fatal
Error
Warn
Info
Debug

Fatal 的优先级最高, 优先级 Debug 的 最低。 通过设置记录器级别,您将获得该优先级或更高级别的所有消息。 例如,通过将级别设置为 Warn ,您将获得所有 Warn 、 Error 和 Fatal 日志消息。

8.2 roslaunch

roslaunch 可以启动 启动文件中定义的节点。

打开路径,创建启动文件夹

$ source devel/setup.bash
$ roscd begin_tutorials
$ mkdir launch
$ cd launch

存储启动文件的目录不一定必须命名为 launch 。 事实上,您甚至不需要将它们存储在目录中。 roslaunch 命令会自动查看传递的包并检测可用的启动文件。 但是,这被认为是一种很好的做法。

创建一个名为 turtlemimic.launch 的启动文件,

gedit turtlemimic.launch 

并粘贴以下内容:

<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>
<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>

启动两个group,他们分别带有名为sim的海龟节点的两个命名空间turtlesim1和turtlesim2,这允许我们在没有名称冲突的情况下启动两个模拟器。

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

Here we start the mimic node with the topics input and output renamed to turtlesim1 and turtlesim2. This renaming will cause turtlesim2 to mimic turtlesim1.

我们将mimic节点的主题输入和输出,重映射为turtlesim1和turtlesim2,这种重映射将导致Turtlesim2模仿Turtlesim1。

 </launch>

关闭启动文件的 xml 标记。

启动启动文件:

roslaunch beginner_tutorials turtlemimic.launch

在这里插入图片描述两个turtlesims将启动并在 新终端中, 发送 rostopic 命令:

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

在这里插入图片描述
即使发布命令仅发送到turtlesim1,您也会看到两个turtlesim 开始移动。

使用 rqt_graph 来更好地了解我们的启动文件做了什么:

在这里插入图片描述
可以看到 mimic 节点将海龟1 的消息 发布给了海龟2

9. rosed编辑文件

rosed 允许使用包名称直接编辑包中的文件,而不必键入包的完整路径。

例如:

rosed roscpp Logger.msg 

还可以使用tab补全

$ rosed roscpp 
Empty.srv                   roscpp.cmake
genmsg_cpp.py               roscppConfig.cmake
gensrv_cpp.py               roscppConfig-version.cmake
GetLoggers.srv              roscpp-msg-extras.cmake
Logger.msg                  roscpp-msg-paths.cmake
msg_gen.py                  SetLoggerLevel.srv
package.xml                 

rosed 的默认编辑器是 vim 。 对初学者更友好的编辑器 nano 包含在默认的 Ubuntu 安装中。 您可以通过编辑 ~/.bashrc 文件来使用它export EDITOR='nano -w'

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值