Skip to content

Extra ‐ Your own ROS Pkg

Riccardo Polvara edited this page Dec 8, 2025 · 2 revisions

Task 0 - Your own code repository

This guide outlines the steps required to set up your module repository and initialise a new ROS 2 package within it.

  1. GitHub Repository Setup

    • Ensure you have a GitHub account at https://github.com/.
    • Generate a new repository from the University of Lincoln template:
      • Navigate to the module's code template: https://github.com/UoL-SoCS/cmp9767-ws.
      • Click the 'Use this template' button.
      • Choose your account as the owner and provide a descriptive Repository name (e.g., cmp9767-your-name-module).
  2. Local Setup and Cloning

    • Open your terminal and navigate to your chosen development directory (e.g., cd ~).

    • Clone your newly created repository to your local machine:

      # IMPORTANT: Replace 'YourUsername/your-new-repo-name.git' with your actual repository URL
      git clone [https://github.com/YourUsername/your-new-repo-name.git](https://github.com/YourUsername/your-new-repo-name.git)
      ### ```
      
  3. Create the ROS 2 Package

    The cloned repository contains the top-level src folder, which serves as the source space for your Colcon workspace. You will create your new package inside this folder.

    • Navigate into the source directory of your local repository:

      cd your-new-repo-name/src
    • Use the ros2 pkg create command to generate the package skeleton. You must specify the build type (ament_cmake for C++ or ament_python for Python) and any dependencies.

      • For a Python Package:
        # Replace 'my_ros2_package' with your chosen package name
        ros2 pkg create --build-type ament_python my_ros2_package --dependencies rclpy std_msgs
  4. Build and Source (Colcon)

    These steps compile your new code and make it available in your ROS 2 environment using the Colcon build tool.

    • Return to the root directory of your Colcon workspace:

      cd ..
    • Build the entire workspace using Colcon:

      colcon build
    • Source the setup file to register the new packages in your current terminal session:

      source install/setup.bash
    • Tip: You should ensure your main ROS 2 distribution's setup file is sourced first (e.g., source /opt/ros/humble/setup.bash), followed by the workspace-specific install/setup.bash command, for everything to work properly!

  5. Any Python script you are going to write yourself needs to be put within the my_ros2_package/my_ros2_package folder of your ROS package (you should find here a __init__.py file that makes this folder a Python module), and you need to specify in the setup.py the name of the script you want to install and make visible.

    # Inside setup.py
    # ...
    entry_points={
        'console_scripts': [
            # 'executable_name = package_name.module_file_name:main_function'
            'talker = my_ros2_package.my_node:main',
        ],
    },
    # ...

    You can also have a look at this file for inspiration.

  6. Remember to push online any changes you introduce to the local copy of your code, especially if working on a shared lab machine. You can find more information on how to use Git, here.

Clone this wiki locally