Basic python libraries to demonstrate creating modules in ROS packages.
This package was created using the following commands
cd ~/ros_workspaces/learning_ws/src
catkin_create_pkg basic_py_libs rospyThis package is to serve as a collection of libraries. These libraries could be used by nodes of this package or other packages. Traditionally, it is more efficient to package functionality as modules (or libraries) that you can include (or import) in your nodes.
Suggested order of traversal for the items in this package (specially for beginners)
| S. No. | Name | Link | Description |
|---|---|---|---|
| 1 | Simple Module | Modules > SimpleModule | A basic module for demonstrating modules |
| 2 | Node: Simple Module | Nodes > SimpleModuleNode | A node to demonstrate using the simple_module library |
| 3 | Basic Math Module | Modules > BasicMath | A basic math module, created to demonstrate sub-modules |
| 4 | Node: Basic Math Module | Node > BasicMathNode | A node to demonstrate using the basic_math library |
Modules defined in this package. As a convention, modules are put inside the src folder.
| Field | Value |
|---|---|
| Path | src/simple_module |
A module to only demonstrate how python modules can be created in packages.
Python code usually do not require building, but to make your created modules importable to other packages, they must be made available in the devel folder (in the workspace) and the variable PYTHONPATH must be managed. All this is handled using a build procedure. In brief, to expose your custom python module to other packages, do the following
-
Create a file called
setup.pyin your package's root folder. You can use the one here as a template (which is built upon the reference). Use the following instructions to create your own-
First import the basic modules that will be used to create an exportable package
from setuptools import setup from catkin_pkg.python_setup import generate_distutils_setup
-
Call the
generate_distutils_setupfunction to generate the setup dictionary (a dictionary that will later be passed as arguments to thesetupfunction). Include a list of packages in thepackagesargument and their path in thepackage_dirargumentsetup_args = generate_distutils_setup( packages=['simple_module'], package_dir={'': 'src'} )
-
Call the
setupfunction with argumentssetup(**setup_args)
-
-
In the
CMakeLists.txtfile, uncommentcatkin_python_setup(it's afterfind packagein the beginning, just beforeDeclare ROS messages, services and actionsheader)catkin_python_setup()
You could also add this immediately after (instead of un-commenting)
-
Run
catkin_makein the workspace directory to build your workspace.
After a successful build, you must see a folder devel/lib/python3/dist-packages/simple_module in your workspace. This is the module that can be imported by nodes (it is a wrapper around your actual module) and the generation of this is what is implied by installation of a Python library.
A sample node is written in this package. You may have to source your ROS workspace again, do it by running the following in the workspace directory
source ./devel/setup.bash| Field | Value |
|---|---|
| Path | src/basic_math |
A module made to demonstrate how submodules can be made and imported. The module's tree is as follows
basic_math
├── __init__.py
├── algebra
│ ├── __init__.py
│ └── real_number.py
└── imp_functions
├── __init__.py
└── factorial.py
Include the package name basic_math in the list of packages in generate_distutils_setup function of the setup.py. After this, the setup.py file must look like this
# Create dictionary for setup using package.xml
setup_args = generate_distutils_setup(
# A list of packages in the repository
packages=[
'simple_module', # In ./src/simple_module
'basic_math', # In ./src/basic_math (Note: You could include other modules)
],
# Directory in which packages are located (usually `src`)
package_dir={'': 'src'}
)Then, run catkin_make in the workspace directory. Just like in the case of simple_module library, you must now see a basic_math module under devel/lib/python3/dist-packages/ folder. This means that the library is successfully installed.
A sample node is written in this package. You may have to source the workspace after building this library.
Nodes created in this package. Most of them are for demonstrating uses of the modules in this package.
| Field | Value |
|---|---|
| Name | simple_module_node |
| File | scripts/simple_module_node.py |
This node simply includes the functionality of the simple_module python module in this package.
Add the function catkin_install_python in CMakeLists.txt (located under the install header).
catkin_install_python(PROGRAMS
scripts/simple_module_node.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)Then, run catkin_make in the workspace directory. To run this node, ensure that roscore is running first and then run
rosrun basic_py_libs simple_module_node.py| Field | Value |
|---|---|
| Name | basic_math_node |
| File | scripts/basic_math_node.py |
This node simply demonstrates how to access functionality of the basic_math python module.
Add the script script/basic_math_node.py to catkin_install_python function in CMakeLists.txt and run catkin_make in the workspace directory. To run this node, first run roscore and then run
rosrun basic_py_libs basic_math_node.py- How to install Python Modules through catkin