Ask Your Question
0

ROS2 custom message python import error

asked 2020-06-20 18:02:30 -0600

Malz gravatar image

updated 2020-06-20 18:13:07 -0600

Hi Deatails: ROS Env

ROS_VERSION=2
ROS_PYTHON_VERSION=3
ROS_LOCALHOST_ONLY=0
ROS_DISTRO=foxy

I created a custom message as follows in Custom.msg file

uint32 number

I have a publisher code with as follows

import rclpy
from rclpy.node import Node

from std_msgs.msg import String

from ros_nodes import Custom


class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(String, 'topic', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        #msg = String()
        msg = Custom()
        msg.number = self.i
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg.data)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

When i try to run i am getting following error

ImportError: cannot import name 'Custom' from 'ros_nodes'

This error is not python import error but specific to that python is not able to import custo.msg Am i doing something wrong. Can you guys share a sample project. As of now i am not using CMakeList or package.xml I don't think any of them are required. How do we use ros generate message to get the message imported. What is the right way to do with custom messages in python.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2020-06-22 06:13:01 -0600

ahcorde gravatar image

Hello,

can you provide more details about where did you locate the Custom.msg ? Did you create a specific package for building your custom messages?

You can follow this tutorial in the official documentation.

Regards

edit flag offensive delete link more

Comments

https://github.com/malapatiravi/ros2_... I was able to implement this. Please refer to the link above. Thanks.

Malz gravatar image Malz  ( 2020-07-05 23:17:03 -0600 )edit
1

answered 2020-06-23 11:46:12 -0600

nsprague gravatar image

It is currently not possible in ROS2 to create a custom message type in a Python package:

https://index.ros.org/doc/ros2/Tutori...

You need to put your message in a separate C++ package.

edit flag offensive delete link more
0

answered 2020-06-22 21:00:47 -0600

Malz gravatar image

Thank you i found a way to do that. Please see the repo here. https://github.com/malapatiravi/ros2_...

Thanks.

edit flag offensive delete link more

Comments

1

Please do not post link-only answers. If/when your repository disappears, your answer will lose all its value.

gvdhoorn gravatar image gvdhoorn  ( 2020-07-06 02:45:07 -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: 2020-06-20 18:02:30 -0600

Seen: 1,314 times

Last updated: Jun 23 '20