5.1. Creating Packages#

This guide will walk you through creating packages for your nodes. It will also teach you how to use Jupyter notebooks to execute commands that are written for you.

5.1.1. Workspaces#

All packages exist within a workspace.

If you are using our docker container, a workspace has already been created for you at /home/ubuntu/learn_ros_ws!

If you are not in our docker container, create a folder where you would like your workspace to be located. Then, update the workspace_location variable below to match your folder location.

Note! You will see a similar block of code at the start of every tutorial notebook that initializes your workspace variables! Don’t forget to run it!

workspace_location = '/home/ubuntu/learn_ros_ws' # NOT your package location!
%cd $workspace_location

Now, we’re going to create a package to put inside the workspace. Create a folder inside your workspace called src. This is where all the source code for your package will go.

mkdir src

Inside your package, we’re going to initialize a default package using ament, ros2’s build system. Everytime you make a new package, you will set it up like this.

%cd src
!ros2 pkg create --build-type ament_python --node-name hello_node hello_package

Now, you’ve created a package called hello_package with a single node called hello_node. You can inspect the code in hello_package/hello_node.py.

def main():
    print('Hi from hello_package.')

It’s just a simple print statement! Now, we need to build our package. We can do that using ROS2’s build system, colcon.

Run the command below to build your package.

!colcon build --packages-select hello_package

And run the package:

%%bash --no
ros2 run hello_package hello_node

5.2. Review Quiz#