12/18/24
Intro
https://articulatedrobotics.xyz/tutorials/ready-for-ros/packages/
Create a Package
- Set up workspace
mkdir -p ~/training_ws/src
cd ~/training_ws9/7/25
Create an empty Python package
ROS2 Python packages
my_package/ βββ package.xml # File containing meta-information about the package βββ resource/ β βββ my_package # Marker file for the package. βββ setup.cfg # Required when a package has executables, so `ros2 run` can locate them. βββ setup.py # Contains instructions for how to install the package. βββ my_package/ # A directory with the same name as your package, used by ROS 2 tools to locate your package; it # contains `__init__.py`.
- Example: Create a Package
Create an empty CMake package
ROS2 CMake Packages
my_package/ βββ CMakeLists.txt # Describes how to build the code within the package βββ include/ β βββ my_package/ # Directory containing the public headers for the package βββ package.xml # File containing meta-information about the package βββ src/ # Directory containing the source code for the package
- Create an empty package using CMake
ros2 pkg create --build-type ament_cmake my_package- Open the workspace in VS Code
- Since weβre using python-only code, we can delete the
includeandsrcfolders which are used for C++ code
- Since weβre using python-only code, we can delete the
cd ..
code .
launch
- Create a
launchfolder and add atalker.launch.pyfile
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription({
Node(
package='demo_nodes_cpp',
executable='talker'
)
})- Add a
listener.launch.pyfile
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='demo_nodes_py',
executable='listener'
)
])- This will launch the
talkerandlistenernodes from the demo_nodes_cpp package:
demo_nodes_cpp/
βββ CMakeLists.txt
βββ package.xml
βββ src/
β βββ talker.cpp
β βββ listener.cpp
βββ launch/
βββ demo_nodes_cpp_launch.py
CMakeLists.txt
- Update CMakeLists to tell
colconhow to build the files- Add the following lines just before
ament_package()
- Add the following lines just before
install(DIRECTORY launch
DESTINATION share/${PROJECT_NAME}
)package.xml
- Update
package.xmlto include additional information for ROS andcolconto recognize and manage the package, including dependencies (e.g. thedemo_nodes_cppanddemo_nodes_pypackages)- We use
<exec_depend>because these dependencies are needed for execution. - Alternatively, we could use
<build_depend>if the dependencies are required only for building or<depend>for both building and executing.
- We use
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>my_package</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="email@gmail.com">Name</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<exec_depend>demo_nodes_cpp</exec_depend>
<exec_depend>demo_nodes_py</exec_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>- Additionally, itβs good practice to update other fields in the
package.xmlsuch as:name: The name of the package.email: The maintainerβs contact email.description: A brief description of the package.license: The license under which the package is distributed.
Build the Package
- Build the package from the workspace directory
- This option uses symlinks instead of copies, so you donβt need to rebuild when tweaking certain files
colcon build --symlink-installPush the package to GitHub
- Create a repository on GitHub
- Initialize git (from inside your local
my_packagedirectory)
echo "# my_package" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:<github_username>/<repo_name>.git
git push -u origin mainPull the package onto the Pi
- Create a
robot_wsworkspace on the other machine (e.g. Raspberry Pi 4B) and cd into it- Make sure SSH authentication is set up
mkdir -p ~/robot_ws/src
cd ~/robot_ws- Clone the repo into the
srcfolder
git clone git@github.com:<github_username>/<repo_name>.gitcdback into the root of the workspace for both machines and run
colcon build --symlink-installTest the package
cdinto the workspace directory and source the workspace (on both machines)
source install/setup.bashNormally you would have separate terminals for building and running the package
- Run the publisher node on the dev machine
ros2 launch my_package talker.launch.py- Run the publisher node on the pi
ros2 launch my_package listener.launch.py




