Diego 1: break through the limit of LAN and control your ROS robot within the scope of Internet

Posted by steeveherris on Fri, 19 Jun 2020 04:40:27 +0200

For more maker works, please pay attention to the author's website, yuandingniao, to collect creative and valuable maker works around the world
ROS robot knowledge, Diego robot
An online statistical process analysis tool SPC completed in spare time and SPC knowledge sharing website qdo

1. Overview

One limitation of ROS Robot system is that all robots must be in one LAN. Although robot web tools tool set can control ROS Robot through web browser, it still requires that Robot and browser must be in the same LAN. There are many applications that need to monitor robots across regions and networks. This paper introduces a way to break through the limitation of local area network and control robots through the web in the scope of Internet.

2. Principle

The principle of implementation is to use the robot web tools toolset to add a transit server, which is named Diego server in this paper.

2.1 basic principles of robot web tools

The robot web tools tool is centralized to realize the robot control through the web. It is realized through the two packages of rosbridge and roslibjs, and through websocket (how to install them, please search the network, which will not be introduced here). First, let's take a look at the architecture of the implementation

robot web tools provides a package of rossbridge, communicates with robot through topic/service, and encapsulates the corresponding topic/service into json message.
Rosslibjs is a rossbridge client implemented by java script. It communicates with rossbridge through websocket. Rosslibjs is responsible for the basic functions of publishing and subscribing messages on the web. The upper js package includes ross2djs and ross3djs, Keyboardtelepjs and other upper packages can realize functions such as map, navigation, 3d, keyboard control, etc. for specific functions, please refer to the official website http://http / / robotwebtools.org/ Most of the articles on the Internet are translated from official websites.

2.2 principle of Diego server

The principle of Diego server is simply to add a relay server, which is deployed on the Internet to realize remote control across LAN

Diego bridge is a pyton program deployed in the same LAN as robot and rossbridge, which implements rossbridge and Diego server

3. cmd_ web publishing of vel

Here is a basic implementation of CMD_ web publishing process of vel message
###3.1 web end implementation
The web-side implementation can be completely in accordance with the specifications of roslibjs. As long as you connect ros to the Diego server (or your own server), there are a lot of reference codes on the specific implementation network. This article uses the example code of the official website. The following is the code of js, which uses keyboardteleopjs. You can implement CMD through the keyboard_ Release of vel topic

<script type="text/javascript" type="text/javascript">
     * Setup all visualization elements when the page is loaded.
    function init() {
        // Connect to ROS.
        var ros = new ROSLIB.Ros({
            url: 'ws://diegoserver:5556/rosbridge'

        // Initialize the teleop.
        var teleop = new KEYBOARDTELEOP.Teleop({
            ros: ros,
            topic: '/cmd_vel'

        // Create a UI slider using JQuery UI.
            range: 'min',
            min: 0,
            max: 100,
            value: 90,
            slide: function (event, ui) {
                // Change the speed label.
                $('#speed-label').html('Speed: ' + ui.value + '%');
                // Scale the speed.
                teleop.scale = (ui.value / 100.0);

        // Set the initial speed .
        $('#speed-label').html('Speed: ' + ($('#speed-slider').slider('value')) + '%');
        teleop.scale = ($('#speed-slider').slider('value') / 100.0);

html code

                    <div id="speed-label"></div>
                    <div id="speed-slider"></div>

3.2 server side implementation

The server is developed by Flask and realizes websocket communication by using Flask socketio package. In essence, the server forwards the rossbridge package, which is very simple. The forwarding code is as follows:

from flask_socketio import send, emit, Namespace
import random
import json

class RosBridgeNamespace(Namespace):

    def __init__(self, socketio, namespace=None):
    def on_connect(self):
        print('############## some ros connect')
    def on_disconnect(self):

    def send_to_front(self,data):
    def send_to_rosbridge(self,data):
    def on_rosbridge(self, data):#response for rosbridge

    def on_operation(self,data):# response for web client
        print('**************** receive operation message from web roblibjs client')

3.3 implementation of rossbridge

The rossbridge implementation uses the rosslibpy package in the robot web tools to realize the communication with the rossbridge package, and uses the socketio to realize the communication with the Diego server. The code is as follows:

from __future__ import print_function
import roslibpy
import socketio
import time
import json

sio = socketio.Client()
sio.connect('wt://diegoserver:5556', namespaces=['/rosbridge'])

client = roslibpy.Ros(host='localhost', port=9090)

print('my sid is', sio.sid)

host = 'localhost'
port = 9090

class DiegoTalker():
    def __init__(self, topic, topic_type):

    def talk(self,data):
        talker = roslibpy.Topic(client, self.topic, self.topic_type)

class RosbridgeNamespace(socketio.ClientNamespace):
    def on_connect(self):

    def on_disconnect(self):

    def on_diegoserver(self, data):
        print('I received a message from the diego server')    
        if json.loads(data)['topic'] =='/cmd_vel' and json.loads(data)['op']=='publish':
            talker=DiegoTalker('/cmd_vel', 'geometry_msgs/Twist')


The principle of implementation is also very simple, receiving the CMD issued by the Diego server_ Vel message is forwarded to rosbridge

4. Operation

First run rosbridge
roslaunch rosbridge_server rosbridge_websocket.launch
Then run Diego Bridge
python3 diegobridge.py
After running, you can see that there is already a client connection in the output message of rosbridge

At this time, open the control interface of the Diego server, which can be controlled through the keyboard

This is how we use rostopic echo /cmd_vel can see the CMD published from the remote web client_ Vel message

Of course, you can publish or subscribe. The principle is the same. The following figure shows the effect of subscribing / map remotely through this method and displaying it on the web

Considering the stability of the network, remote control may have real-time problems. Of course, this depends on the actual application scenario. To realize a product application, the following problems should be considered:

  • Real time
  • The topic of ROS is high-frequency and published in real time. The server should increase the cache mechanism,
  • Security, based on Internet applications, should take full account of security, such as communication process encryption, this is very important, once the robot is hijacked by the network, there will be unexpected consequences.
  • Bandwidth issues, such as video, navigation data bandwidth requirements are very large
  • Other

Topics: JSON network Javascript Java