【问题标题】:ros2 pub/sub custom message through ros2-web-bridge to client appros2 pub/sub 自定义消息通过 ros2-web-bridge 到客户端应用程序
【发布时间】:2021-08-13 10:53:31
【问题描述】:

我想通过 ros2-web-bridge 发布 ros2 自定义消息来响应应用程序。在反应中,我订阅了已发布的自定义消息。 ros2-web-bridge 在端口 ws://localhost:9090 上运行。我已经在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

【问题讨论】:

    标签: javascript ros2


    【解决方案1】:

    如果您已经在 React 端创建并订阅了一条自定义消息,您只需在此处包含消息标头,它将像 std_msg 一样工作。例如:如果您有包含自定义消息类型 my_msg 的包 custom_interface,您的代码将如下所示:

    // 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
    

    如果网络桥已经像你说的那样设置好了,那一切都会起作用。

    【讨论】:

    • 非常感谢 BTables 的回复。我在 .msg 文件中添加了“Header header”,但它不起作用,因为它是 python 代码。我认为它只适用于c++。您能否对此进行更详细的解释? github.com/Mulkijeetu/tutorial_interfaces.git。我已经给出了自定义消息界面的 git hub 链接。 Plaese帮助解决这个问题。在此先感谢 BTables。
    • 上面的python代码是在哪里构建的?为了让 python 节点能够导入自定义消息,它应该在同一个 ros2 工作区中。例如,在构建消息和 python 节点时,您应该能够运行单个命令,例如 colcon build --packages-select tutorial_interface <python_package_name>,然后获取安装目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2023-01-05
    • 1970-01-01
    • 2022-01-27
    • 2014-10-17
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多