ROS Bridge Simple Keyboard

From wikidb
Jump to: navigation, search

Overview

This is a rudimentary proof-of-concept test. Key presses are sometimes missed, the twist commands has too long an interval causing control latencies, and the coding style is poor. The code started with the Robot Webtools keyboardtelepjs/examples/keyboardteleop.html and diverged quickly from there. In the end it doesn't use the keyboardteleopjs library at all. See references below.

Run

In separate terminals.

 roscore
 rosrun phidgets motor_control_hc
 roslaunch rosbridge_server rosbridge_websocket.launch

In browser

 localhost/~eepp/keyboard_test.html

roslauanch Script

 roslaunch floor_hugger teleop_bridge.launch

ROS_Bridge_RobotWebTools_Keyboard_Teleop#roslaunch_Script

Debug

Motor Test

Enter the following to make sure the motors turn.

 rostopic pub -1 /cmd_vel geometry_msgs/Twist '[5,0,0]' '[0,0,0]'

Message Listen

 rostopic echo /cmd_vel

Also

 rostopic list
 rosnode list

References

JavaScript Test Page


<!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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src="http://cdn.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script src="http://cdn.robotwebtools.org/roslibjs/current/roslib.js"></script>

<script>
  // ------ GLOBALS ------

  // ------ Websocket ------

  // Connect to ROS.
  var ros = new ROSLIB.Ros
  ({
    url : 'ws://localhost:9090'
  });

  ros.on('connection', function() {
    console.log('Connected to websocket server.');
  });

  ros.on('error', function(error) 
  {
    console.log('Error connecting to websocket server: ', error);
  });

  ros.on('close', function() 
  {
    console.log('Connection to websocket server closed.');
  });

  // ------ move ------
  // linearx: -100 to 100 precent
  // rotatez: -100 to 100 percent

  function move(linearx, rotatez)
  {
    // Create the velocity command
    var cmdVel = new ROSLIB.Topic
    ({
      ros : ros,
      name : '/cmd_vel',
      messageType : 'geometry_msgs/Twist'
    });

    // Create the twist message
    var twist = new ROSLIB.Message
    ({
      linear : 
      {
        x : linearx / 20,
        y : 0.0,
        z : 0.0
      },
      angular : 
      {
        x : 0.0,
        y : 0.0,
        z : rotatez / 20
      }
    });

    // Publishing the twist message
    cmdVel.publish(twist);
  }

  // ------ init ------
  // Setup the slider when the page is loaded. 

  // Create the slide
  function init() 
  {
    $("#target").keypress( function(e) {
      console.log("keypress" + " " + String.fromCharCode(e.which));
      if (e.charCode == 87 || e.charCode == 119) {move ( 100,   0);} // w is forward
      if (e.charCode == 83 || e.charCode == 115) {move (-100,   0);} // s is back
      if (e.charCode == 68 || e.charCode == 100) {move (   0, 100);} // d is right
      if (e.charCode == 65 || e.charCode ==  97) {move (   0,-100);} // a is left
    });

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

    // Set the initial speed.
    $('#speed-label').html('Speed: ' + ($('#speed-slider').slider('value')) + '%');
    move ($('#speed-slider').slider('value'), 0);

    // clear the input field
    $('#target').val('');
  }

</script>
</head>

<body onload="init()">
  <h1>Simple Keyboard Teleop Example</h1>
  <p>Run the following commands in the terminal then refresh this page. Check the JavaScript
    console for the output.</p>
  <ol>
    <li><tt>roscore</tt></li>
    <li><tt>rosrun phidgets motor_control_hc</tt></li>
    <li><tt>roslaunch rosbridge_server rosbridge_websocket.launch</tt></li>
    <li>Use the slider to set motor linear x speed. Use 'w', 'a', 's', and 'd'
             control direction</li>
  </ol>
  <form>
    <fieldset>
      <input id="target" type="text" value="Hello there">
    </fieldset>
  </form>
  <div id="speed-label"></div>
  <div id="speed-slider"></div>
</body>
</html>