Ask Your Question
0

Import python script to different ROS2 package

asked 2019-07-25 10:01:03 -0500

hect1995 gravatar image

I have created a package called custom_msg which has the following structure:

src
    custom_msg
        msg
            CustomMsg.msg
        constants
            msg_constants.py
        CMakeLists.txt
        package.xml

Where CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(custom_msg)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(custom_msg
  "msg/CustomMsg.msg"
  DEPENDENCIES builtin_interfaces
)

ament_package()

package.xml:

<?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>custom_msg</name>
  <version>0.7.6</version>
  <description>Custom messages for real-time communication between nodes of the truck.</description>

  <maintainer email="hector.esteban@man.de">Hector Esteban</maintainer>

  <license>Apache License 2.0</license>

  <author>Hector Esteban</author>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>rosidl_default_generators</buildtool_depend>

  <depend>rosidl_default_generators</depend>
  <depend>common_interfaces</depend>

  <exec_depend>rosidl_default_runtime</exec_depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

And msg_constants.py:

#! /usr/bin/env python
OK = 0
OBJECTIVE_NOT_REACHED = 1
PLANNING_EMPTY = 2

After doing:

colcon build --symlink-install
source install/local_setup.bash

When I import from another package msg_constants.py:

from custom_msg.constants import msg_constants as msg_type
from custom_msg.msg import CustomMsg

I can import CustomMsg but not msg_constants

[planning_prob_node-2]     from custom_msg.constants import msg_constants as msg_type
[planning_prob_node-2] ModuleNotFoundError: No module named 'custom_msg.constants'
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-07-25 13:52:56 -0500

tfoote gravatar image

Your problem is that you're not doing anything to install or configure your python file in the CMakeLists.txt to be useable from another package. There's lots of other questions relating to that such as: https://answers.ros.org/question/2987...

However in your case I would strongly recommend against trying to do that, and instead recommend that you put the constants into your .msg file so that as you've noticed they'll be easily available in python, but also in C++ and any other language for which a node is written. They will all share the constant values in all languages using the autogenerated code.

You can find documentation on constants at: http://wiki.ros.org/msg#Constants

edit flag offensive delete link more

Comments

But the problem is that I have 2 variables in the custom message:

int8 title
float32[4] coordinates

And what I want is to assign from the other Python script values to the title but with a name instead of the integer itself. So I do not want to create constants for the message, I want to assign values to the title with some codified words, like OK

hect1995 gravatar imagehect1995 ( 2019-07-25 14:11:03 -0500 )edit

That's exactly what the constants do. There are autogenerated constants defined in each language that let you reference them by name as you're doing in your python script.

>>> from sensor_msgs.msg import Range
>>> Range.ULTRASOUND
0
>>> Range.INFRARED
1
>>>

Come from https://github.com/ros2/common_interf...

tfoote gravatar imagetfoote ( 2019-07-25 14:25:42 -0500 )edit

thanks for the help

hect1995 gravatar imagehect1995 ( 2019-07-26 03:14:00 -0500 )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

1 follower

Stats

Asked: 2019-07-25 10:01:03 -0500

Seen: 65 times

Last updated: Jul 25