ros2+gazebo仿真官方示例

Setting up a robot simulation (Gazebo)

Goal: Launch a Simulation with Gazebo and ROS 2

Tutorial level: Advanced

Time: 20 minutes

Contents

  • Prerequisites

  • Tasks

    • 1 Launch the simulation

    • 2 Configuring ROS 2

    • 3 Visualizing lidar data in ROS 2

  • Summary

Prerequisites

First of all you should install ROS 2 and Gazebo. You have two options:

  • Install from deb packages. To check which versions are available from deb packages please check this table.

  • Compile from sources:

    • ROS 2 install instructions

    • Gazebo install instructions

Tasks

1 Launch the simulation

In this demo you are going to simulate a simple diff drive robot in Gazebo. You are going to use one of the worlds defined in the Gazebo examples called visualize_lidar.sdf. To run this example you should execute the following command in a terminal:

Linux

ign gazebo -v 4 -r visualize_lidar.sdf

ros2+gazebo仿真官方示例_第1张图片

When the simulation is running you can check the topics provided by Gazebo with the ign command line tool:

Linux

ign topic -l
这个ing是因为gazebo在Ubuntu里面那个软件就是ign开头的叫做ignxxxx gazebo

Which should show:

/clock
/gazebo/resource_paths
/gui/camera/pose
/gui/record_video/stats
/model/vehicle_blue/odometry
/model/vehicle_blue/tf
/stats
/world/visualize_lidar_world/clock
/world/visualize_lidar_world/dynamic_pose/info
/world/visualize_lidar_world/pose/info
/world/visualize_lidar_world/scene/deletion
/world/visualize_lidar_world/scene/info
/world/visualize_lidar_world/state
/world/visualize_lidar_world/stats

Since you have not launched an ROS 2 nodes yet, the output from ros2 topic list should be free of any robot topics:

Linux

ros2 topic list

Which should show:

/parameter_events
/rosout

2 Configuring ROS 2

To be able to communicate our simulation with ROS 2 you need to use a package

called ros_gz_bridge. This package provides a network bridge which enables the exchange of messages between ROS 2 and Gazebo Transport. You can install this package by typing:

这里需要下载一个ros_gz_bridge功能包,用来接受ros2文件的cmd_vel发出的消息,然后转化为gazebo内机器人识别的命令,因该是个虚拟的驱动包

Linux

sudo apt-get install ros-humble-ros-ign-bridge

At this point you are ready to launch a bridge from ROS to Gazebo. In particular you are going to create a bridge for the topic /model/vehicle_blue/cmd_vel:

Linux

source /opt/ros/humble/setup.bash
ros2 run ros_gz_bridge parameter_bridge /model/vehicle_blue/cmd_vel@geometry_msgs/msg/Twist]ignition.msgs.Twists

上面的2行需要合并一行;        

其中第二行的命令格式:

ros2 run ros_gz_bridge parameter_bridge /TOPIC@ROS_MSG@GZ_MSG

 不过好像其中第二个@既可以是@也可以使用 ']'这个括号代替也可以正常执行

具体解释:

The ROS message type is followed by an @[, or ] symbol where:

  • @ is a bidirectional bridge.
  • [ is a bridge from Gazebo to ROS.
  • ] is a bridge from ROS to Gazebo.

如果第二个@的位置使用符号@,那么代表这是一个双向传递的通道。测试常用

如果第二个@的位置使用符号[,表示数据从gazebo向ROS2传递

如果第二个@的位置使用符号】,表示数据从ros2向gazebo传递,这里使用了],表示信息(在这里也就是命令)单向从ros2传递到gazebo。

For more details about the ros_gz_bridge please check this README .

Once the bridge is running the robot is able to follow your motor commands. There are two options:

  • Send a command to the topic using ros2 topic pub

Linux

ros2 topic pub /model/vehicle_blue/cmd_vel geometry_msgs/Twist "linear: { x: 0.1 }"

 这个格式可能是:ros2 topic pub 节点名称 消息类型   ”具体消息内容“

这个命令可真好用没有安装任何的包也可以伪装成那个包发送消息

  • teleop_twist_keyboard package. This node takes keypresses from the keyboard and publishes them as Twist messages. You can install it typing:

Linux

sudo apt-get install ros-humble-teleop-twist-keyboard

The default topic where teleop_twist_keyboard is publishing Twist messages is /cmd_vel but you can remap this topic to make use of the topic used in the bridge:

Linux

source /opt/ros/humble/setup.bash
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r /cmd_vel:=/model/vehicle_blue/cmd_vel

Which will show:

This node takes keypresses from the keyboard and publishes them
as Twist messages. It works best with a US keyboard layout.
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .

For Holonomic mode (strafing), hold down the shift key:
---------------------------
   U    I    O
   J    K    L
   M    <    >

t : up (+z)
b : down (-z)

anything else : stop

q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%

CTRL-C to quit

currently:      speed 0.5       turn 1.0

3 Visualizing lidar data in ROS 2

The diff drive robot has a lidar. To send the data generated by Gazebo to ROS 2, you need to launch another bridge. In the case the data from the lidar is provided in the Gazebo Transport topic /lidar2, which you are going to remap in the bridge. This topic will be available under the topic /lidar_scan:

Linux

source /opt/ros/humble/setup.bash
ros2 run ros_gz_bridge parameter_bridge /lidar2@sensor_msgs/msg/LaserScan[ignition.msgs.LaserScan --ros-args -r /lidar2:=/laser_scan

To visualize the data from the lidar in ROS 2 you can use Rviz2:

Linux

source /opt/ros/humble/setup.bash
rviz2

Then you need to configure the fixed frame:

../../../../_images/fixed_frame.png

And then click in the button “Add” to include a display to visualize the lidar:

ros2+gazebo仿真官方示例_第2张图片

Now you should see the data from the lidar in Rviz2:

ros2+gazebo仿真官方示例_第3张图片

Summary

In this tutorial, you launched a robot simulation with Gazebo, launched bridges with actuators and sensors, visualized data from a sensor, and moved a diff drive robot.

你可能感兴趣的:(机器人,ros2,gazebo)