< >
Home » ROS与JS入门教程 » ROS与javascript入门教程-keyboardteleopjs-键盘控制

ROS与javascript入门教程-keyboardteleopjs-键盘控制

ROS与javascript入门教程-keyboardteleopjs-键盘控制

说明:

  • 介绍通过keyboardteleopjs来进行键盘控制

步骤:

  • 新建keyboard.html
  • 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>

<script type="text/javascript" src="http://cdn.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script type="text/javascript" src="http://cdn.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
<script type="text/javascript" src="http://cdn.robotwebtools.org/keyboardteleopjs/current/keyboardteleop.min.js"></script>

<script type="text/javascript">
  /**
   * Setup all GUI elements when the page is loaded.
   */
  function init() {
    // Connecting to ROS.
    var ros = new ROSLIB.Ros({
      url : 'ws://rail-sim2.cs.wpi.edu:9090'
    });

    // Initialize the teleop.
    var teleop = new KEYBOARDTELEOP.Teleop({
      ros : ros,
      topic : '/base_controller/command'
    });

    // Create a UI slider using JQuery UI.
    $('#speed-slider').slider({
      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);
  }
</script>
</head>

<body onload="init()">
  <div id="speed-label"></div>
  <div id="speed-slider"></div>
</body>
</html>

代码解析:

  • 代码段:
var teleop = new KEYBOARDTELEOP.Teleop({
  ros : ros,
  topic : '/base_controller/command'
});
  • 解释:

    • 创建KEYBOARDTELEOP.Teleop对象,发布对应的话题
  • 代码段:

$('#speed-slider').slider({
  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);
  }
});
  • 解释:
    • 显示键盘速度命令
    • 使用jQuery的ui slider组件

运行:

  • 需要pr2_simulator 和rosbridge_server包

  • 服务器,新终端,运行

roslaunch pr2_gazebo pr2_empty_world.launch
  • 服务器,新终端,运行
roslaunch rosbridge_server rosbridge_websocket.launch
  • 浏览器访问keyboard.html
  • 通过 W, A, S, 和D 键盘来控制

参考:

  • http://wiki.ros.org/keyboardteleopjs/Tutorials/CreatingABasicTeleopWidgetWithSpeedControl

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros与javascript入门教程