Ask Your Question
0

How to share common lib's in ROS workspace

asked 2017-11-20 04:22:03 -0600

Westranger gravatar image

updated 2017-11-21 02:37:08 -0600

I'm having several packages in my workspace and I'm currently at the point where a want to avoid code duplicity. At the moment I have package called myproject_common in which all msg and srv files are located. After building the workspace they are properly available in all my other packages. Now I want to my common .py files into the common packages and also make them available. After calling catkin_make the msg and srv classes are available under ./devel/lib/python2.7/dist-packages/ which is quiet nice because this makes them also available in my IDE without the need to import something manually. How can I accomplish this with self written common python files? What do I need to change in my CMakeLists.txt to get this working

edit:

I separated the msg / srv from the common modules, because the msg / srv stuff work fine so now my pkg with the shared modules is named myproject_common_lib

this is my directory structure

myproject_common_lib
├── CMakeLists.txt
├── package.xml
├── setup.py
└── src
    └── myproject_common_lib
        ├── coordinate.py
        ├── coordinate.pyc
        └── __init__.py

2 directories, 6 files

The content of my __init__.py looks like this

from coordinate import Coordinate

The content of my setup.py

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['myproject_common_lib'],
    package_dir={'': 'src'},
)

setup(**setup_args)

The content of my CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(myproject_common_lib)

find_package(catkin REQUIRED COMPONENTS
  rospy
)

catkin_python_setup()

catkin_package()

include_directories(
   $(catkin_INCLUDE_DIRS)
)

When I want to import the shared module I use the following import (which does not work)

from myproject_common_lib.coordinate import Coordinate

The content of my devel folder looks like this (short version)

devel
├── lib
│   └── python2.7
│       └── dist-packages
│           └── myproject_common_lib
│               ├── __init__.py
│               └── __init__.pyc
└── share
    ├── myproject_common_lib
    │   └── cmake
    │       ├── myproject_common_libConfig.cmake
    │       └── myproject_common_libConfig-version.cmake

I cannot see any difference to the posted tutorial. Also when watching this video I cannot see any difference :-(

edit 2:

This is what PyCharm sees

image description

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2017-11-20 06:44:14 -0600

rreignier gravatar image

You have to write Python modules (= libraries) and declare them, see this tutorial.

So your modules will be available from your other packages and you will be able to import them using:

import myproject_common.my_module
edit flag offensive delete link more

Comments

When I follow the tutorial it works for the script but it does not work for my modules. The import cannot be found. The error I get is Cannot find reference 'coordinate' in '__init__.py', which comes from PyCharm

Westranger gravatar imageWestranger ( 2017-11-20 10:26:22 -0600 )edit

Did you add an __init__.py file to the directory that contains your libraries/modules?

jayess gravatar imagejayess ( 2017-11-20 11:54:22 -0600 )edit

@jayess and @rreignier I updated my post with more information. Actually I cannot see any difference between my stuff and the provided tutorial

Westranger gravatar imageWestranger ( 2017-11-21 02:22:37 -0600 )edit

I found the error in the CMakeLists.txt the $(catkin_INCLUDE_DIRS) part must be written with {}

Westranger gravatar imageWestranger ( 2017-11-21 09:03:51 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-11-20 04:22:03 -0600

Seen: 126 times

Last updated: Nov 21 '17