ROS2 入门控制命令(以海龟为例)和工作空间介绍

前言

命令方向特别注意空格,冒号后面加空格要形成习惯,否则经常报错。

1 打开海龟显示窗口

ros2 run turtlesim  turtlesim_node

终端输出显示

xucg@xucg-vm17:~$ ros2 run turtlesim  turtlesim_node 
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
[INFO] [1724157403.864968580] [turtlesim]: Starting turtlesim with node name /turtlesim
[INFO] [1724157403.871132454] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
[INFO] [1724158102.333855707] [turtlesim]: Spawning turtle [None] at x=[2.000000], y=[2.000000], theta=[0.200000]
[INFO] [1724159274.673713108] [turtlesim]: Spawning turtle [abc] at x=[2.000000], y=[2.000000], theta=[0.200000]
[WARN] [1724160334.641159560] [turtlesim]: Oh no! I hit the wall! (Clamping from [x=11.112075, y=4.070568])
[WARN] [1724160334.656553546] [turtlesim]: Oh no! I hit the wall! (Clamping from [x=11.115834, y=4.053306])
[WARN] [1724160334.686513557] [turtlesim]: Oh no! I hit the wall! (Clamping from [x=11.115834, y=4.036043])

2 启用键盘控制窗口

ros2 run turtlesim  turtle_teleop_key

终端输出显示

xucg@xucg-vm17:~$ ros2 run turtlesim  turtle_teleop_key 
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.
'Q' to quit.

3 海龟按圆形轨迹运动(圆周运动)

控制速度:x轴线性速度是2,z轴角速度是1.8

ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 1.8}}"

终端输出显示

xucg@xucg-vm17:~$ ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 1.8}}"
publisher: beginning loop
publishing #1: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=1.8))

publishing #2: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=1.8))

publishing #3: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=1.8))

4 产生新的海龟

新海龟出现的位置为 【x=2, y=2, theta=0.2】,名称为abc,不设置的话默认为None,后续不好控制

ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: 'abc'}"

终端显示输出

xucg@xucg-vm17:~$ ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: 'abc'}"
waiting for service to become available...
requester: making request: turtlesim.srv.Spawn_Request(x=2.0, y=2.0, theta=0.2, name='abc')

response:
turtlesim.srv.Spawn_Response(name='abc')

要想控制新海龟圆周运动的话,替换 turtle1 为 abc 即可

ros2 topic pub --rate 1 /abc/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 1.8}}"

在这里插入图片描述

5 记录并播放海龟运动轨迹

ros2 bag 功能

xucg@xucg-vm17:~$ ros2 bag 
usage: ros2 bag [-h] Call `ros2 bag <command> -h` for more detailed usage. ...

Various rosbag related sub-commands

options:
  -h, --help            show this help message and exit

Commands:
  convert  Given an input bag, write out a new bag with different settings
  info     Print information about a bag to the screen
  list     Print information about available plugins to the screen
  play     Play back ROS data from a bag
  record   Record ROS data to a bag
  reindex  Reconstruct metadata file for a bag

  Call `ros2 bag <command> -h` for more detailed usage.
xucg@xucg-vm17:~$ 

5.1 记录

ros2 bag record /turtle1/cmd_vel

5.2 播放

ros2 bag play rosbag2_2024_08_20-21_24_51/

终端输出显示

xucg@xucg-vm17:~ros2 bag record /turtle1/cmd_vel
[INFO] [1724160291.225165509] [rosbag2_recorder]: Press SPACE for pausing/resuming
[INFO] [1724160291.233668900] [rosbag2_storage]: Opened database 'rosbag2_2024_08_20-21_24_51/rosbag2_2024_08_20-21_24_51_0.db3' for READ_WRITE.
[INFO] [1724160291.240964579] [rosbag2_recorder]: Listening for topics...
[INFO] [1724160291.244260951] [rosbag2_recorder]: Subscribed to topic '/turtle1/cmd_vel'
[INFO] [1724160291.244561104] [rosbag2_recorder]: Recording...
[INFO] [1724160291.244647035] [rosbag2_recorder]: Event publisher thread: Starting
[INFO] [1724160291.245010608] [rosbag2_recorder]: All requested topics are subscribed. Stopping discovery...
[INFO] [1724160355.010195474] [rosbag2_cpp]: Writing remaining messages from cache to the bag. It may take a while
[INFO] [1724160355.010820387] [rosbag2_recorder]: Event publisher thread: Exiting
[INFO] [1724160355.011024280] [rosbag2_recorder]: Recording stopped

终端输出显示

xucg@xucg-vm17:~$ ros2 bag play rosbag2_2024_08_20-21_24_51/
[INFO] [1724160393.303341777] [rosbag2_storage]: Opened database 'rosbag2_2024_08_20-21_24_51/rosbag2_2024_08_20-21_24_51_0.db3' for READ_ONLY.
[INFO] [1724160393.303512838] [rosbag2_player]: Set rate to 1
[INFO] [1724160393.311010661] [rosbag2_player]: Adding keyboard callbacks.
[INFO] [1724160393.311181251] [rosbag2_player]: Press SPACE for Pause/Resume
[INFO] [1724160393.311263406] [rosbag2_player]: Press CURSOR_RIGHT for Play Next Message
[INFO] [1724160393.311321585] [rosbag2_player]: Press CURSOR_UP for Increase Rate 10%
[INFO] [1724160393.311378451] [rosbag2_player]: Press CURSOR_DOWN for Decrease Rate 10%
[INFO] [1724160393.312217527] [rosbag2_storage]: Opened database 'rosbag2_2024_08_20-21_24_51/rosbag2_2024_08_20-21_24_51_0.db3' for READ_ONLY.

6 查看当前系统参数名称和参数值

# 参数名称
ros2 param list
# 参数值
ros2 param dump turtlesim
# 保存输出参数到yaml文件
ros2 param dump turtlesim >> turtlesim.yaml
# 加载参数配置(可直接修改yaml文件更新参数)
ros2 param load turtlesim turtlesim.yaml

终端输出显示

xucg@xucg-vm17:~$ ros2  param list
/service_adder_server:
  use_sim_time
/turtlesim:
  background_b #背景颜色值
  background_g
  background_r
  qos_overrides./parameter_events.publisher.depth
  qos_overrides./parameter_events.publisher.durability
  qos_overrides./parameter_events.publisher.history
  qos_overrides./parameter_events.publisher.reliability
  use_sim_time

xucg@xucg-vm17:~$ ros2 param dump turtlesim
/turtlesim:
  ros__parameters:
    background_b: 100
    background_g: 86
    background_r: 69
    qos_overrides:
      /parameter_events:
        publisher:
          depth: 1000
          durability: volatile
          history: keep_last
          reliability: reliable
    use_sim_time: false

xucg@xucg-vm17:~/dev_ws$ ros2 param load turtlesim turtlesim.yaml
Set parameter background_b successful
Set parameter background_g successful
Set parameter background_r successful
Set parameter qos_overrides./parameter_events.publisher.depth failed: parameter 'qos_overrides./parameter_events.publisher.depth' cannot be set because it is read-only
Set parameter qos_overrides./parameter_events.publisher.durability failed: parameter 'qos_overrides./parameter_events.publisher.durability' cannot be set because it is read-only
Set parameter qos_overrides./parameter_events.publisher.history failed: parameter 'qos_overrides./parameter_events.publisher.history' cannot be set because it is read-only
Set parameter qos_overrides./parameter_events.publisher.reliability failed: parameter 'qos_overrides./parameter_events.publisher.reliability' cannot be set because it is read-only
Set parameter use_sim_time successful

7 获取和设置系统背景参数

# 设置当前背景通道b值为100
ros2 param set turtlesim background_b 100
# 获取当前背景b通道数值
ros2 param get turtlesim background_b

终端输出显示

xucg@xucg-vm17:~$ ros2 param set turtlesim background_b 100
Set parameter successful
xucg@xucg-vm17:~$ ros2 param get turtlesim background_b
Integer value is: 100

在这里插入图片描述

ROS的工作空间

空间名称 dev_ws 随意更改,src、install、build、log 严格遵循

在这里插入图片描述

# 安装git包
sudo apt install git
# 创建工程文件夹,src存代码
mkdir -p ~/dev_ws/src
# 克隆教程代码
git clone https://gitee.com/guyuehome/ros2_21_tutorials.git
# 把代码移动到工程目录下
mv ros2_21_tutorials/ dev_ws/src
# 安装pip(python包管理工具)
sudo apt install python3-pip
# 安装ros依赖包(国内重开发,解决网络链接问题)
sudo pip install rosdepc
# 更新rosdepc环境和初始化
sudo rosdepc init && rosdepc update
# 检查代码依赖项(搜索src目录下所有包并检查依赖项,是否符合humble版本,否则更新)
rosdepc install -i --from-path src --rosdistro humble -y
# 下载编译器colcon
sudo apt install python3-colcon-ros
# 回到工作主目录
cd ~/dev_ws
# 编译工作空间
colcon build
# 把工作目录作为根目录,方便匹配环境变量,只影响当前终端
source install/local_setup.sh

把该命令复制到./bashrc文件中,新的根目录才可以在任意终端生效,方便后续代码运行
./bashrc文件在home目录下,属于隐藏显示,可以键盘按 Ctrl+H 进行显示查看。

source ~/dev_ws/install/local_setup.sh

1 创建功能包

功能包可以解耦代码间的依赖性,方便代码分享和互相协作

【命令范式】ros2 pkg create --build-type <build-type> <package_name>

cd ~/dev_ws/src 
# C++
ros2 pkg create --build-type ament_cmake learning_pkg_c
# Python 区别主要在ament_x
ros2 pkg create --build-type ament_python learning_pkg_python

CMakeLists.txt 编译的规则和过程顺序
package.xml 描述功能包基本信息(名称、版本、开发人人、许可证),环境依赖

setup.cfg
setup.py 功能包基本信息和程序起点描述(entry_points)

在这里插入图片描述

2 编译功能包

cd ~/dev_ws/src
# 重编译
colcon build
source install/local setup.bash

把src代码重编译到install目录下,才可以使用ros2 run命令进行终端运行。

猜你喜欢

转载自blog.csdn.net/weixin_42899627/article/details/141368128