rosbridge_suite入门教程-虚拟机遥控控制
rosbridge_suite入门教程-虚拟机遥控控制
说明
- 介绍如何利用ros和Bootstrap实现虚拟机遥控控制
步骤:
安装gnix
新建index.html
内容如下:
<!doctype html>
<html lang="en">
<head>
<title>ROS + Bootstrap 4 demo</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body class="bg-light">
<!-- SPEED -->
<div class="row">
<div class="col-md-4"></div>
<div class=" col-md-4">
<label for="robot-speed">
<strong>Robot speed</strong>
</label>
<input type="range" min="15" max="80" class="custom-range" id="robot-speed">
</div>
<div class="col-md-4"></div>
</div>
<!-- VIDEO -->
<div class="row my-4">
<div class="col d-flex justify-content-center">
<img src="" class="p-1 bg-dark" alt="" id="video" />
</div>
</div>
<!-- JOYSTICK -->
<div class="row my-4">
<div class="col">
<div class="d-flex justify-content-center" style="width: 210px; height: 210px;">
<div id="joystick"></div>
</div>
</div>
</div>
<!-- INFO -->
<div class="row my-4">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="alert alert-success">
<h4 class="alert-heading">ROS + Bootstrap interface demo</h4>
<ul>
<li>set speed using a slider</li>
<li>use joystick or WASD keys on keyboard to move </li>
</ul>
</div>
</div>
<div class="col-md-2"></div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
<script src="https://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script src="https://static.robotwebtools.org/keyboardteleopjs/current/keyboardteleop.min.js"></script>
<script src="//yoannmoinet.github.io/nipplejs/javascripts/nipplejs.js"></script>
<script src="webui.js"></script>
</body>
</html>
- 新建webui.js
- 内容如下:
var twist;
var cmdVel;
var publishImmidiately = true;
var robot_IP;
var manager;
var teleop;
var ros;
function moveAction(linear, angular) {
if (linear !== undefined && angular !== undefined) {
twist.linear.x = linear;
twist.angular.z = angular;
} else {
twist.linear.x = 0;
twist.angular.z = 0;
}
cmdVel.publish(twist);
}
function initVelocityPublisher() {
// Init message with zero values.
twist = new ROSLIB.Message({
linear: {
x: 0,
y: 0,
z: 0
},
angular: {
x: 0,
y: 0,
z: 0
}
});
// Init topic object
cmdVel = new ROSLIB.Topic({
ros: ros,
name: '/cmd_vel',
messageType: 'geometry_msgs/Twist'
});
// Register publisher within ROS system
cmdVel.advertise();
}
function initTeleopKeyboard() {
// Use w, s, a, d keys to drive your robot
// Check if keyboard controller was aready created
if (teleop == null) {
// Initialize the teleop.
teleop = new KEYBOARDTELEOP.Teleop({
ros: ros,
topic: '/cmd_vel'
});
}
// Add event listener for slider moves
robotSpeedRange = document.getElementById("robot-speed");
robotSpeedRange.oninput = function () {
teleop.scale = robotSpeedRange.value / 100
}
}
function createJoystick() {
// Check if joystick was aready created
if (manager == null) {
joystickContainer = document.getElementById('joystick');
// joystck configuration, if you want to adjust joystick, refer to:
// https://yoannmoinet.github.io/nipplejs/
var options = {
zone: joystickContainer,
position: { left: 50 + '%', top: 105 + 'px' },
mode: 'static',
size: 200,
color: '#0066ff',
restJoystick: true
};
manager = nipplejs.create(options);
// event listener for joystick move
manager.on('move', function (evt, nipple) {
// nipplejs returns direction is screen coordiantes
// we need to rotate it, that dragging towards screen top will move robot forward
var direction = nipple.angle.degree - 90;
if (direction > 180) {
direction = -(450 - nipple.angle.degree);
}
// convert angles to radians and scale linear and angular speed
// adjust if youwant robot to drvie faster or slower
var lin = Math.cos(direction / 57.29) * nipple.distance * 0.005;
var ang = Math.sin(direction / 57.29) * nipple.distance * 0.05;
// nipplejs is triggering events when joystic moves each pixel
// we need delay between consecutive messege publications to
// prevent system from being flooded by messages
// events triggered earlier than 50ms after last publication will be dropped
if (publishImmidiately) {
publishImmidiately = false;
moveAction(lin, ang);
setTimeout(function () {
publishImmidiately = true;
}, 50);
}
});
// event litener for joystick release, always send stop message
manager.on('end', function () {
moveAction(0, 0);
});
}
}
window.onload = function () {
// determine robot address automatically
// robot_IP = location.hostname;
// set robot address statically
robot_IP = "10.0.0.11";
// // Init handle for rosbridge_websocket
ros = new ROSLIB.Ros({
url: "ws://" + robot_IP + ":9090"
});
initVelocityPublisher();
// get handle for video placeholder
video = document.getElementById('video');
// Populate video source
video.src = "http://" + robot_IP + ":8080/stream?topic=//usb_cam/image_raw&type=mjpeg&quality=80";
video.onload = function () {
// joystick and keyboard controls will be available only when video is correctly loaded
createJoystick();
initTeleopKeyboard();
};
}
两个文件放在一起,在放在nginx的目录下/var/www/html/
通过http://localhost,即可访问
小车端启动rosbridge_server
roslaunch rosbridge_server rosbridge_websocket.launch
- 再启动web_video_server
rosrun web_video_server web_video_server
- 再启动usb_cam,使用usb摄像头
roslaunch usb_cam usb_cam.launch
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号