目录
- 前言
- 1 打开海龟显示窗口
- 2 启用键盘控制窗口
- 3 海龟按圆形轨迹运动(圆周运动)
- 4 产生新的海龟
- 5 记录并播放海龟运动轨迹
- ros2 bag 功能
- 5.1 记录
- 5.2 播放
- ROS的工作空间
- 6 创建功能包
- 7 编译功能包
前言
命令方向特别注意空格,冒号后面加空格要形成习惯,否则经常报错。
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.
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
6 创建功能包
功能包可以解耦代码间的依赖性,方便代码分享和互相协作
【命令范式】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)
7 编译功能包
cd ~/dev_ws/src
# 重编译
colcon build
source install/local setup.bash
把src代码重编译到install目录下,才可以使用ros2 run
命令进行终端运行。