HOME
BLOG
TAGS
ROS Bridge
Apr 01 2023

       ROS有很多开源的软件包、工具包,但是资料实在有限,只能自己摸索使用。最近有ROS与非ROS系统通信和ROS数据可视化的需求,于是用到了ROS Bridge这一工具。

通信架构

  • 服务端 通过rosbridge_server运行已架设好应用层API的完整服务器
  • 客户端 通过roslib的API编写相应非ROS程序实现与服务器的网络连接和数据通信


文件结构

  • 服务端
rosbridge
├── rosbridge_server
|   ├── rosbridge_websocket
|   |   ...
|   ├── rosbridge_tcp
|   |   ...
|   ├── rosbridge_udp
|   |   ...
|
└── ...
  
  • 客户端
roslibjs
  或
roslibpy

       理论上,你可以用roslibjsroslibpy客户端与webtcpudp服务端任意对接,但是建议考虑语言生态。



roslibjs与websocket

服务端

       首先,需要预先搭建好ROS环境并下载一些软件包。启动ROS各节点,让各Topic发布出来,然后启动web服务器节点:

roslaunch rosbridge_server rosbridge_websocket

       获取IP与端口号,本机默认是ws://0.0.0.0:9090 ,若客户端不在本机上就要使用其IPv4:

ifconfig

       这样服务端就算架设完毕了,客户端可以通过各种API与该服务端交互。但是注意,两端仅这样连接需要在同一局域网下才能通信,真正远程实用还需要内网穿透或把服务端挂在公共域名上。

客户端

       搭建网页客户端要先下载roslibjs,解压后文件夹内打开有Node.js环境的终端:

npm install

一般会报错,按提示输指令修复。

没有科学上网的话修复也会错,可以用国内淘宝的源:
cnpm install
以及 修复指令

       配好环境后在example文件夹里放自己写的.html文件即可简单使用,下面是自己当时整的简陋可视化IMU输出的客户端测试代码:

  • imu.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
  <script src="../build/roslib.js"></script>
  <link rel="stylesheet" href="imu.css">
  <script>
  // Connecting to ROS
  var ros = new ROSLIB.Ros();

  // If there is an error on the backend, an 'error' emit will be emitted.
  ros.on('error', function(error) {
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('connected').style.display = 'none';
    document.getElementById('closed').style.display = 'none';
    document.getElementById('error').style.display = 'inline';
    console.log(error);
  });

  // Find out exactly when we made a connection.
  ros.on('connection', function() {
    console.log('Connection made!');
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('error').style.display = 'none';
    document.getElementById('closed').style.display = 'none';
    document.getElementById('connected').style.display = 'inline';
  });

  ros.on('close', function() {
    console.log('Connection closed.');
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('connected').style.display = 'none';
    document.getElementById('closed').style.display = 'inline';
  });

  // Create a connection to the rosbridge WebSocket server.
  ros.connect('ws://xxx.xxx.xxx.xx:9090');


  //Subscribing to a Topic, use 'rostopic' to confirm your information below.
  var listener = new ROSLIB.Topic({
    ros : ros,
    name : '/raw_imu',
    messageType : 'sensor_msgs/Imu'
  });

  // Then we add a callback to be called every time a message is published on this topic.
  listener.subscribe(function(message) {
  
    console.log('Received message on ' + listener.name + ': ' + message.angular_velocity.x +', '+ message.angular_velocity.y+': '+message.angular_velocity.z);
    
    var Msg=document.getElementById("message");
    Msg.innerHTML='Received message on ' + listener.name + ': ' + message.angular_velocity.x +', '+ message.angular_velocity.y+': '+message.angular_velocity.z;
    
    // If desired, we can unsubscribe from the topic as well.
    //listener.unsubscribe();
  });
  </script>
</head>

<body>
  <h1>Simple roslib Example by Radioactive</h1>
  <p>Open your own nodes.</p>
    Run the following commands in the terminal then refresh this page. Check the JavaScript
  <p>console for the output.</p>
  <ol>
    <li><tt>roslaunch rosbridge_server rosbridge_websocket</tt></li>
    <li><tt></tt></li>
  </ol>
  <div id="statusIndicator">
    <p id="connecting">
      Connecting to rosbridge...
    </p>
    <p id="connected">
      Connected
    </p>
    <p id="error" >
      Error in the backend!
    </p>
    <p id="closed">
      Connection closed.
    </p>
  </div>
  <div>
    <h2>Car message</h2>
    <p id="message"></p>
  </div>
  </body>
</html>

  • imu.css
#connected{
    color:#00D600; 
    display:none;
}
#error{
    color:#FF0000; 
    display:none;
}
#closed{
    display: none;
}
li{
    list-style: none;
}
#message{
    padding: 10px;
}

       然后就可以用浏览器打开服务器地址对应端口看到页面了,其他API等内容见官方文档



roslibpy与tcpsocket

服务端

       同上,需要预先搭建好ROS环境并下载一些软件包。启动ROS各节点,让各Topic发布出来,然后启动tcp服务器节点:

roslaunch rosbridge_server rosbridge_tcp

       获取IP与端口号,本机默认是ws://0.0.0.0:9090 ,若客户端不在本机上就要使用其IPv4:

ifconfig

       这样服务端就算架设完毕了,客户端可以通过各种API与该服务端交互。

客户端

       当时因为没找到roslibpy的资料,我只能自己摸索API使用。准备写这一部分时发现有官方文档,还有人做了汉化。Python的API用起来就很简单了,需要啥就看汉化文档就彳亍了。