Skip to content

Instantly share code, notes, and snippets.

@PierrickKoch
Created June 18, 2012 11:24
Show Gist options
  • Select an option

  • Save PierrickKoch/2947946 to your computer and use it in GitHub Desktop.

Select an option

Save PierrickKoch/2947946 to your computer and use it in GitHub Desktop.
ros wander js (soluce)
<!doctype html>
<html>
<head>
<title>wander rosjs</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="ros.js"></script>
<script type="text/javascript">
(function () { // [onload]
Array.prototype.sum = function (begin, end) {
var end = typeof end !== 'undefined' ? end : this.length,
inc = typeof begin !== 'undefined' ? begin : 0, sum = 0;
for (; inc < end; sum += this[inc++]) {}
return sum;
}
function logdebug(msg) {
document.getElementById('debug').innerHTML += msg + '\n';
}
function wander(ranges) {
var mid = Math.floor(ranges.length / 2),
cmd = {linear: {x:0, y:0, z:0},
angular: {x:0, y:0, z:0}},
halt = false;
// halt if an object is less than 2m in a 30deg angle
for (i = mid-15; i < mid+15; i++) {
if (ranges[i] < 2) {
halt = true;
break;
}
}
if (halt) {
// we go to the highest-range side scanned
if (ranges.sum(0, mid) > ranges.sum(mid)) {
cmd.angular.z = -1;
} else {
cmd.angular.z = 1;
}
} else {
cmd.linear.x = 1;
}
return cmd;
}
/* rosrun mjpeg_server mjpeg_server */
function updateImage() {
var image = document.getElementById('image');
image.onload = function() {
window.setTimeout(updateImage, 50);
};
image.src = 'http://localhost:8080/stream?topic=/beego/camera/image&' +
Number(new Date);
}
var dowander = true;
var connection = new ros.Connection("ws://localhost:9090");
connection.setOnClose(function (e) {
logdebug("connection closed");
});
connection.setOnError(function (e) {
logdebug("error! " + e);
});
connection.setOnOpen(function (e) {
logdebug("connected to ROS");
/* rosbridge requires that you first make a call to /rosjs/subscribe
with the name of the topic and the desired minimum delay between
messages in milliseconds. */
connection.callService('/rosjs/subscribe','["/beego/scan",100]', function (rsp) {
logdebug('rosjs subscribe to scan: ' + rsp);
});
/* handle the laser scan message */
connection.addHandler('/beego/scan', function (scan) {
/* wander, obstacle avoidance */
if (dowander) {
var cmd = wander(scan.ranges);
connection.publish('/beego/velocity', 'geometry_msgs/Twist', cmd);
}
});
/* start image display */
updateImage();
});
/* https://developer.mozilla.org/en/DOM/event/UIEvent/KeyEvent#Constants */
function handleKey(code, down) {
var cmd = {linear: {x:0, y:0, z:0},
angular: {x:0, y:0, z:0}},
scale = (down == true) ? 1 : 0,
move = false;
switch (code) {
case 32:
// spacebar
if (!down) {
move = true;
dowander = !dowander;
}
case 37:
// left
move = true;
cmd.angular.z = 1 * scale;
break;
case 38:
// up
move = true;
cmd.linear.x = .5 * scale;
break;
case 39:
// right
move = true;
cmd.angular.z = -1 * scale;
break;
case 40:
// down
move = true;
cmd.linear.x = -.5 * scale;
break;
}
if (move) {
connection.publish('/beego/velocity', 'geometry_msgs/Twist', cmd);
}
}
document.addEventListener('keydown', function (e) {
handleKey(e.keyCode, true);
}, true);
document.addEventListener('keyup', function (e) {
handleKey(e.keyCode, false);
}, true);
})(); // [end onload]
</script>
</head>
<body>
<section>
<h1>wander rosjs</h1>
<em>hit [escape] to close the connection</em>
</section>
<section>
<h2>legend</h2>
<p>[spacebar] to stop/restart wandering,<br/>
arrows ← ↑ → ↓ to move</p>
</section>
<section>
<h2>image</h2>
<img id="image"/>
</section>
<section>
<h2>debug</h2>
<pre><div id="debug"></div></pre>
</section>
</body>
</hmtl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment