Quiero publicar un mensaje personalizado de ros2 a través de ros2-web-bridge para reaccionar a la aplicación. En reaccionar me he suscrito al mensaje personalizado publicado. ros2-web-bridge se ejecuta en el puerto ws://localhost:9090. Creé la interfaz de mensajes personalizados en 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 += 1Si ya tiene un mensaje personalizado creado y suscrito en el lado de React, todo lo que tiene que hacer aquí es incluir el encabezado del mensaje y funcionará como un std_msg . Ejemplo: si tiene un paquete custom_interface que incluye un mensaje personalizado, escriba my_msg , su código se vería así:
// 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 += 1Si el puente web ya está configurado como usted dice, todo funcionará.