• ROS机器人外网远程控制(一种基于Frp内网穿透,使用websocket/http通信方式)


    ROS机器人只能在内网访问?遇到这样一个需求,希望在公司访问控制到客户的机器人,需要获得摄像头图像以及远程控制机器人。想到采用的方案有以下几种:

    • 采用frp进行内网穿透
    • 采用蒲公英进行组网
    • 添加一张4G网卡使其具有公网IP

    那么对比这三种方案很明显我们不想花钱做这个事情。我们采用Frp搭建内网穿透尝试该方案是否可行。

    先决条件:

    • 需要一台公网服务器(没钱的可以去白嫖移动云一个月)
    • ROS机器人
    • frp软件(注意客户端和服务端版本需要保持一致)

    验证测试:
    因为机器人不方便测试,我采用在虚拟机里面安装一个ROS,然后跑起来小乌龟,当用公网服务器能控制小乌龟进行移动则证明实验成功。

    首先在公网服务器和虚拟机(机器人)中安装对应版本的frp
    https://github.com/fatedier/frp/releases
    在这里插入图片描述
    我这里下载的是0.44.0版本,由于虚拟机和服务器都是x86架构的,则两个都下frp_0.44.0_linux_amd64.tar.gz,如果机器人是nvidia系列arm板子,则需下载frp_0.44.0_linux_arm64.tar.gz,下载好分别解压在服务器和机器人上。
    frp服务端(服务器):

    
    tar -zxvf frp_0.44.0_linux_amd64.tar.gz
    mv frp_0.44.0_linux_amd64 frp
    cd frp
    #在frps.ini中配置
    vim frps.ini
    [common]
    bind_port = 7000  
    dashboard_port = 7500  #web dashboard仪表盘
    token = 12345678
    dashboard_user = admin
    dashboard_pwd = admin
    vhost_http_port = 6001  #为了让web图像正常传输
    #保存后执行
    ./frps -c frps.ini
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    frp客户端(机器人或者虚拟机):

    tar -zxvf frp_0.44.0_linux_amd64.tar.gz
    mv frp_0.44.0_linux_amd64 frp
    cd frp
    #在frpc.ini中配置
    [common]
    server_addr = 公网服务器IP
    token = 12345678
    server_port = 7000
    
    [ssh]
    type = tcp
    local_ip = 内网机器人IP
    local_port = 22
    remote_port = 6000
    
    [web]
    type = http
    local_ip = 内网机器人IP
    local_port = 80
    custom_domains = 公网服务器IP
    
    [ws]
    type = tcp
    local_ip = 内网机器人IP
    local_port = 9090
    remote_port = 9090
    
    #[camera]
    #type = http
    #local_ip = 内网机器人IP
    #local_port = 8080 
    #remote_port = 6003
    ## 如果要穿透多个web服务器则需要二级域名 只用ip不可以
    #custom_domains = 公网服务器IP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    接下来开启两个终端,打开小乌龟节点,运行rosbridge,这样才能用web进行控制。

    roslaunch rosbridge_server rosbridge_websocket.launch
    
    • 1

    在这里插入图片描述
    在自己的电脑编写如下测试html代码:

    DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js">script>
    <script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js">script>
    
    <script type="text/javascript" type="text/javascript">
      // Connecting to ROS
      var ros = new ROSLIB.Ros({
        url : 'ws://公网IP:9090'
      });
       
      var isconected=false;
    
      //判断是否连接成功并输出相应的提示消息到web控制台
      ros.on('connection', function() {
        isconected=true;
        console.log('Connected to websocket server.');
        subscribe();
      }); 
    
      ros.on('error', function(error) {
        isconected=false;
        console.log('Error connecting to websocket server: ', error);
      });
      
      ros.on('close', function() {
        isconected=false;
        console.log('Connection to websocket server closed.');
        unsubscribe();
      });
    
      // Publishing a Topic
      var cmdVel = new ROSLIB.Topic({
        ros : ros,
        name : 'turtle1/cmd_vel',
        messageType : 'geometry_msgs/Twist'
      });//创建一个topic,它的名字是'/cmd_vel',,消息类型是'geometry_msgs/Twist' 
    
      var twist = new ROSLIB.Message({
        linear : {
          x : 0.0,
          y : 0.0,
          z : 0.0
        },
        angular : {
          x : 0.0,
          y : 0.0,
          z : 0.0
        }
      });//创建一个message
        
      function control_move(direction){
        twist.linear.x = 0.0;
        twist.linear.y = 0;
        twist.linear.z = 0;
        twist.angular.x = 0;
        twist.angular.y = 0;
        twist.angular.z = 0.0;
        
        switch(direction){
          case 'up':
            twist.linear.x = 2.0;
            break;
          case 'down':
            twist.linear.x = -2.0;
          break;
          case 'left':
            twist.angular.z = 2.0;
          break;
          case 'right':
            twist.angular.z = -2.0;
          break;
        }
        cmdVel.publish(twist);//发布twist消息
      }
    
      var timer=null;    
      function buttonmove(){    
        var oUp=document.getElementById('up');
        var oDown=document.getElementById('down');
        var oLeft=document.getElementById('left');
        var oRight=document.getElementById('right');
             
        oUp.onmousedown=function ()
        {
            Move('up');        
        }
        oDown.onmousedown=function ()
        {
            Move('down');        
        }
         
        oLeft.onmousedown=function ()
        {
            Move('left');        
        }
         
        oRight.onmousedown=function ()
        {
            Move('right');       
        }
    
        oUp.onmouseup=oDown.onmouseup=oLeft.onmouseup=oRight.onmouseup=function ()
        {
            MouseUp ();
        }      
      }
    
    
      function keymove (event) {    
        event = event || window.event;/*||为或语句,当IE不能识别event时候,就执行window.event 赋值*/
        console.log(event.keyCode);
        switch (event.keyCode){/*keyCode:字母和数字键的键码值*/          
            /*65,87,68,83分别对应awds*/
            case 65:                 
                Move('left');
                break;
            case 87:              
                Move('up');
                break;
            case 68:              
                Move('right');
                break;
            case 83:
                Move('down');
                break;
            default:
                break;
        }
      }
    
      var MoveTime=20;
         
      function Move (f){
        clearInterval(timer);
          
        timer=setInterval(function (){          
          control_move(f)      
        },MoveTime);
      }        
           
      function MouseUp ()
      {
          clearInterval(timer);        
      }
      
      function KeyUp(event){
          MouseUp();
      }
      window.onload=function ()
      {  
            buttonmove();                
            document.onkeyup=KeyUp;
            document.onkeydown=keymove;           
      }
      
      // Subscribing to a Topic
      var listener = new ROSLIB.Topic({
        ros : ros,
        name : '/turtle1/pose',
        messageType : 'turtlesim/Pose'
      });//创建一个topic,它的名字是'/turtle1/pose',,消息类型是'turtlesim/Pose',用于接收乌龟位置信息
     
      var turtle_x=0.0;
      var turtle_y=0.0;
      
      function subscribe()//在连接成功后,控制div的位置,
      {
         listener.subscribe(function(message) {         
           turtle_x=message.x;
           turtle_y=message.y;
           document.getElementById("output").innerHTML = ('Received message on ' + listener.name +'  x: ' + message.x+" ,y: "+message.y);
         });
      } 
    
      function unsubscribe()//在断开连接后,取消订阅
      {
         listener.unsubscribe();    
      }
    
    script>
    head>
    <body>
    
      <input type="button" value="前行" id="up">
      <input type="button" value="后退" id="down">
      <input type="button" value="左转" id="left">
      <input type="button" value="右转" id="right">
    
      <p id = "output">p>
    
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196

    websocket连接成功后,就可以用公网服务器操控机器人运动了。
    在这里插入图片描述
    图像显示的话也是一样的,需要开启web_video_server服务,然后暴露http的8080口。
    如此,实现了公网地址远程操控内网机器人。

  • 相关阅读:
    Redis Sentinel工作原理
    会跳舞的网站引导页HTML源码
    PICO首届XR开发者挑战赛正式启动,助推行业迈入“VR+MR”新阶段
    Spring cloud alibaba实战
    DVWA-impossible代码审计
    高等数学(第七版)同济大学 习题1-9 个人解答
    LamdaUpdateWapper失效问题
    【微信小程序】小程序支持的css选择器、小程序自适应单位rpx简介
    Linux之ebpf(1)基础使用
    用pytorch实现神经网络
  • 原文地址:https://blog.csdn.net/qq_40695642/article/details/126819203