2

I want to publish ros2 custom message through ros2-web-bridge to react app. In react i have subscribed to published custom message. ros2-web-bridge is running on port ws://localhost:9090. I have created the custom message interface on ros2.

   // this is client side web app subscribe method
   var example = new ROSLIB.Topic({
  ros: ros,
  name: "/sample",
  messageType: `tutorial_interfaces/msg/Num`,
});

// Subscribe a Topic
example.subscribe(function (message) {
  console.log("Subscribe data", message);
});


// This is ros2 publish python code
import rclpy
from rclpy.node import Node
from tutorial_interfaces.msg import Num    # CHANGE
class MinimalPublisher(Node):

def __init__(self):
    super().__init__('minimal_publisher')
    self.publisher_ = self.create_publisher(Num, 'sample', 10)     # CHANGE
    timer_period = 0.5
    self.timer = self.create_timer(timer_period, self.timer_callback)
    self.i = 0

// This is ros2 published class and method
def timer_callback(self):
    msg = Num()                                           # CHANGE
    msg.num = self.i                                      # CHANGE
    self.publisher_.publish(msg)
    self.get_logger().info('Publishing: "%d"' % msg.num)  # CHANGE
    self.i += 1

1 Answers1

1

If you already have a custom message created and subscribed to on the React side all you have to do here is include the message header and it'll work just like a std_msg. Ex: if you have package custom_interface that includes a custom message type my_msg your code would look something like this:

# This is ros2 publish python code
import rclpy
from rclpy.node import Node
from custom_interface.msg import my_msg
class MinimalPublisher(Node):
    
    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(my_msg, 'sample', 10)
        timer_period = 0.5
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0
    
    # This is ros2 published class and method
    def timer_callback(self):
        msg = my_msg()
        my_msg.custom_field = self.i
        self.publisher_.publish(my_msg)
        self.get_logger().info('Publishing: "%d"' % my_msg.custom_field)
        self.i += 1

If the web-bridge is already setup like you say, that will all work.

sgvd
  • 3,819
  • 18
  • 31
BTables
  • 4,413
  • 2
  • 11
  • 30
  • Thank you so much for reply BTables. I have added 'Header header' inside .msg file but it is not working because of it is python code. I think it will work only for c++. Can you please explain more detailed about this?. https://github.com/Mulkijeetu/tutorial_interfaces.git. I have given my git hub link of custom message interface. Plaese help to solve this problem. Thank you in advance BTables. – Jithesh kumar Aug 13 '21 at 15:45
  • Where is the above python code being built? For the python node to be able to import the custom message it should be in the same ros2 workspace. For example, when building both the message and python node you should be able to run a single command such as `colcon build --packages-select tutorial_interface ` and then source the install directory. – BTables Aug 13 '21 at 16:40