0
$\begingroup$

Rosanswers logo

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.


Originally posted by Malz on ROS Answers with karma: 11 on 2020-06-20

Post score: 0

$\endgroup$

2 Answers 2

1
$\begingroup$

Rosanswers logo

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


Originally posted by ahcorde with karma: 421 on 2020-06-22

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by Malz on 2020-07-05:
https://github.com/malapatiravi/ros2_custom_msg I was able to implement this. Please refer to the link above. Thanks.

$\endgroup$
1
$\begingroup$

Rosanswers logo

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

https://index.ros.org/doc/ros2/Tutorials/Custom-ROS2-Interfaces/#id4

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


Originally posted by nsprague with karma: 228 on 2020-06-23

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.