Using nps ros2 examples - nps-ros2/nps-ros2-examples GitHub Wiki

The examples here are based on https://github.com/ros2/examples. These examples demonstrate how to download and modify the demos and examples available at https://github.com/ros2/demos and https://github.com/ros2/examples. You may use these examples as a starting point for creating your ROS2 projects.

Preparation

These examples expect a ROS2 environment setup as shown in Installing-the-ROS2-Environment. If you get the chance, please also run the talker/listener demo as shown there.

Download nps-ros2-examples

These examples extend examples provided in this repository. To install this repository: create a working directory:

mkdir ~/gits
cd ~/gits

then clone this repository from GitHub. Clone either by:

git clone https://github.com/nps-ros2/nps-ros2-examples.git

or if you are set up to use RSA keys:

git clone [email protected]:nps-ros2/nps-ros2-examples.git

C++ Example

Build and Run the C++ Talker/Listener

Now that the setup, above, is in place, build, set up to run, and then run the C++ talker/listener demo from this repository:

cd ~/gits/nps-ros2-examples/nps_examples_cpp
colcon build
source ./install/local_setup.bash
ros2 run nps_examples_cpp talker

and in another terminal:

cd ~/gits/nps-ros2-examples/nps_examples_cpp
source ./install/local_setup.bash
ros2 run nps_examples_cpp listener

Notes:

  • You can put source ./install/local_setup.bash into your .bashrc file as:

    source ~/gits/nps-ros2-examples/nps_examples_cpp/install/local_setup.bash
    

    so that you don't have to keep typing it.

  • Please do not modify these examples in place. Instead, please copy them to your own work area.

Build your own C++ Talker Listener Package

Here we copy the nps_examples_cpp package to another directory and rename it. Copy nps_examples_cpp to your own directory, for example to my_custom_examples_cpp:

cd ~/gits/nps-ros2-examples
cp -r nps_examples_cpp my_custom_examples_cpp

For C++, Colcon uses CMake. Change your package name from nps_examples_cpp to my_custom_examples_cpp in the Colcon and CMake files:

  • Change line <name>nps_examples_cpp</name> in file package.xml.

  • Change line project(nps_examples_cpp) in file CMakeLists.txt.

    This way when you use the ros2 run package_name executable_name command and type ros2 run my_custom_examples_cpp talker ROS2 will use the executable from your package my_custome_examples_cpp.

Put in your functionality. For example change text Hello World: in src/topics/talker.cpp to My Hello World: .

Now build, set up to run, and then run your custom C++ talker/listener:

cd ~/gits/nps-ros2-examples/my_custom_examples_cpp
colcon build
source ./install/local_setup.bash
ros2 run my_custom_examples_cpp talker

and in another terminal:

cd ~/gits/nps-ros2-examples/my_custom_examples_cpp
source ./install/local_setup.bash
ros2 run my_custom_examples_cpp listener

Python Example

Build and Run the Python Talker/Listener

Build, set up to run, and then run the Python talker/listener demo from this repository:

cd ~/gits/nps-ros2-examples/nps_examples_py
colcon build
source ./install/local_setup.bash
ros2 run nps_examples_py talker

and in another terminal:

cd ~/gits/nps-ros2-examples/nps_examples_py
source ./install/local_setup.bash
ros2 run nps_examples_py listener

Notes:

  • If you like, put source ~/gits/nps-ros2-examples/nps_examples_cpp/install/local_setup.bash in your .bashrc file so you don't have to keep typing it.
  • Please do not modify these examples in place. Instead, please copy them to your own work area.

Build your own Python Talker Listener Package

Copy the nps_examples_py package to another directory and rename it. Copy nps_examples_py to your own directory, for example to my_custom_examples_py:

cd ~/gits/nps-ros2-examples
cp -r nps_examples_py my_custom_examples_py
cd my_custom_examples_py

For Python builds, Colcon internally uses setup from the Python distutils package rather than using CMake as was done for C++. As a result, setup requirements will be a bit different. Change your package name from nps_examples_py to my_custom_examples_py in Colcon and Python build tool files as follows:

  • Change line <name>nps_examples_py</name> in file package.xml.
  • Change the two paths in file setup.cfg.
  • Change the package name and also the talker and listener entry points in setup.py.
  • Rename package directory nps_examples_py.
  • Rename the empty marker file under the resource directory.
  • Do not remove the two empty __init__.py files.

Put in your functionality. For example change text Hello World: in topics/talker.py to My Python Hello World: .

Now build, set up to run, and then run your custom Python talker/listener:

cd ~/gits/nps-ros2-examples/my_custom_examples_py
colcon build
source ./install/local_setup.bash
ros2 run my_custom_examples_py talker

and in another terminal:

cd ~/gits/nps-ros2-examples/my_custom_examples_py
source ./install/local_setup.bash
ros2 run my_custom_examples_py listener

Tips

  • When you run colcon build from ~/gits/nps-ros2-examples/my_custom_examples_py it creates script ~/gits/nps-ros2-examples/my_custom_examples_py/install/local_setup.bash which defines global variables so that ROS2 can find your package and its executable files. You may want to add this to your .bashrc file so that your package may be visible when you open a command window:

    source ~/gits/nps-ros2-examples/my_custom_examples_py/install/local_setup.bash
    
  • During the evolution of your work you may create programs that you later decide to rename or remove. Colcon will not delete programs that it previously built. To completely clean up after Colcon you may delete the build, install, and log directories that it creates:

    cd ~/gits/nps-ros2-examples/my_custom_examples_py
    rm -rf build install log
    

Working with Other ROS2 Examples

The examples here specifically target running your own workspace using ROS2 Crystal on Ubuntu 18. If you clone ROS2 examples or demos from https://github.com/ros2, be sure to select the Crystal branch and not the default Master branch, which is incompatible. To select the Crytal branch type git checkout crystal after cloning. For example clone ROS2's demos and make them compatible with Crystal as follows:

mkdir ~/gits
cd ~/gits
git clone https://github.com/ros2/demos.git
cd demos
git checkout crystal
⚠️ **GitHub.com Fallback** ⚠️