ROS2 ns3 Gazebo Demo Part 2 - nps-ros2/ns3_gazebo GitHub Wiki
This demo requires setup performed in ROS2 ns3 Gazebo Demo Part 1.
In part 2 we install Gazebo and then run the Gazebo Differential Drive demo shown at http://gazebosim.org/tutorials?tut=ros2_installing&cat=connect_ros. But here we use the differential drive robot to manipulate the antenna position of the talker robot in our talker/listener demo. We will move the differential drive demo robot to observe that the listener begins to fail listening to the talker at a distance of about 20 meters.
In this demo we use a robot which listens to Gazebo and updates the antenna position in ns-3. In ROS2 ns3 Gazebo Demo Part 3 we use a Gazebo plugin that updates the antenna position in ns-3 directly.
Setup
Setup for part 2 includes the following installation:
- Gazebo 9.
- A ns-3 Wifi Emulator program modified to interact with a ROS2 Node that listens to diff drive telemetry messages from Gazebo.
- Setting additional Global variables.
Once setup is complete we run the talker-listener demo over the Wifi emulator while manipulating the antenna position of the talker by issuing ROS2 Twist commands to the differential drive robot being modeled in Gazebo.
Install Gazebo
Install Gazebo 9 per http://gazebosim.org/tutorials?tut=install_ubuntu&cat=install:
curl -sSL http://get.gazebosim.org | sh
Maybe type sudo apt update; sudo apt upgrade
then retry if this fails.
Open Gazebo to verify that it comes up:
gazebo
Gazebo does not interface with ROS directly. Install packages so ROS2 Crystal can interface with Gazebo9:
sudo apt install ros-crystal-gazebo-ros-pkgs
We will use the Differential Drive Demo robot packaged in ros-crystal-gazebo-ros-pkgs
. Here are some details about the Differential Drive demo:
-
World file: https://github.com/ros-simulation/gazebo_ros_pkgs/blob/ros2/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world. Example construction: http://gazebosim.org/tutorials?tut=build_robot.
-
Source file: https://github.com/ros-simulation/gazebo_ros_pkgs/blob/ros2/gazebo_plugins/src/gazebo_ros_diff_drive.cpp and https://github.com/ros-simulation/gazebo_ros_pkgs/blob/ros2/gazebo_plugins/include/gazebo_plugins/gazebo_ros_diff_drive.hpp which makes
libgazebo_ros_diff_drive.so
. Source code example: http://gazebosim.org/tutorials/?tut=plugins_hello_world. -
Commands suggested in
gazebo_ros_diff_drive_demo.world
are:Gazebo ROS differential drive plugin demo Try sending commands: ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 1.0}}' -1 ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{angular: {z: 0.1}}' -1 Try listening to odometry: ros2 topic echo /demo/odom_demo Try listening to TF: ros2 run tf2_ros tf2_echo odom chassis ros2 run tf2_ros tf2_echo chassis right_wheel ros2 run tf2_ros tf2_echo chassis left_wheel
Please verify that your Gazebo installation works by starting Gazebo with the Differential Drive demo:
gazebo --verbose /opt/ros/crystal/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world
Try a differential drive robot Twist command at a command prompt to see that the robot moves:
ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 1.0}}' -1
Stop Gazebo.
diff_drive_ns3_ros2
Program
Build the The diff_drive_ns3_ros2
program has two parts:
- The ns-3 part that runs the network simulator which passes Wifi traffic between network namespaces.
- The ROS2 Node part that listens to odometry messages from the Diff Drive robot modeled in Gazebo and updates the ns-3 antenna location associated with network namespace
nns1
.
Because both the ns-3 part and the ROS2 Node parts block, one part is run from a thread. When exiting the diff_drive_ns3_ros2
program, you will need to press Ctrl-C twice to exit both parts.
ns-3 programs are natively built and run using the WAF build tool. ROS2 executables are natively built using the Colcon build tool and run using the ROS2 run tool. We chose to build the diff_drive_ns3_ros2
program the ROS2 way. As a result, we must define global variables so that our program can find the ns-3 components that were installed in ROS2 ns3 Gazebo Demo Part 1. Please add this to your .bashrc
file so that the ns-3 parts of diff_drive_ns3_ros2
can build and so that the ns-3 parts of diff_drive_ns3_ros2
can run:
# ns-3 compatibility
export LD_LIBRARY_PATH=~/repos/ns-3-allinone/ns-3.29/build/lib:$LD_LIBRARY_PATH
export PATH=$HOME/repos/ns-3-allinone/ns-3.29/build/src/fd-net-device:$HOME/repos/ns-3-allinone/ns-3.29/build/src/tap-bridge:$PATH
Start a new terminal so these ns-3 compatibility global variables are present in your command shell.
Now Build the diff_drive_ns3_ros2
program:
cd ~/gits/ns3_gazebo/ns3_gazebo_ws
colcon build
diff_drive_ns3_ros2
Run Source your ns3_gazebo_ws
workspace setup so you can launch the diff_drive_ns3_ros2
program from ROS2. You may want to put this in your .bashrc
file, too:
source ~/gits/ns3_gazebo/ns3_gazebo_ws/install/setup.bash
Start diff_drive_ns3_ros2
:
ros2 run diff_drive_ns3_ros2 diff_drive_ns3_ros2
When diff_drive_ns3_ros2
starts running, two things will be able to happen:
- The ns-3 part will engage to allow traffic to flow between network namespaces through the Wifi network simulator.
- The ROS2 node part will:
- Listen to diff drive telemetry messages from Gazebo.
- Display the diff drive
x
location. - Set the diff drive
x
location as the antenna location for the Wifi port associated with network namespacenns1
.
Start Gazebo
Start Gazebo with the diff drive demo world:
gazebo --verbose /opt/ros/crystal/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world
Sometimes Gazebo doesn't start because a previous run has not fully shut down. Try killall gzserver
.
If you like, you may prefer to start the diff drive demo world without the GUI:
gzserver --verbose /opt/ros/crystal/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world
Start the Talker Listener Demo
Start the Talker Listener Demo, if not still running from Part 1:
Open a window and start a bash process that uses network namespace nns1
:
sudo ip netns exec nns1 /bin/bash
and start a talker
ros2 run demo_nodes_cpp talker
Open a window for another network namespace:
sudo ip netns exec nns2 /bin/bash
and start a listener
ros2 run demo_nodes_cpp listener
Move the diff drive robot
Move the diff drive robot to move the antenna.
- The antenna starting location of
nns1
is x=0,y=0,z=0. - The antenna location of
nns2
is x=5,y=0,z=0. - Moving the diff_drive robot moves the antenna location of
nns1
. - The listener in the talker/listener demo will begin to fail at about 25 meters separation.
Open a command window from which to publish Twist messages in order to move the diff_drive robot.
Move positive x continuous:
ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 1.0}}' -1
Move negative x continuous:
ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: -1.0}}' -1
Note: these publish operations are not always received by the Gazebo Physics engine. You may need to repeat them to make them take hold. You may observe the x location in the window running diff_drive_ns3_ros2
.
Next
In ROS2 ns3 Gazebo Demo Part 3 we use a Gazebo plugin that updates the antenna position in ns-3 directly rather than using a ROS2 Node that listens to Gazebo to update the antenna position.