ROS与javascript入门教程-roslibjs-编写actionlib客户端
ROS与javascript入门教程-roslibjs-编写actionlib客户端
说明:
- 介绍如何利用roslibjs编写actionlib客户端
github:
参考代码:
新建名称为fibonacci.html
内容为:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://cdn.robotwebtools.org/EventEmitter2/current/eventemitter2.js"></script>
<script type="text/javascript" src="http://cdn.robotwebtools.org/roslibjs/current/roslib.js"></script>
<script type="text/javascript" type="text/javascript">
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});
var fibonacciClient = new ROSLIB.ActionClient({
ros : ros,
serverName : '/fibonacci',
actionName : 'actionlib_tutorials/FibonacciAction'
});
var goal = new ROSLIB.Goal({
actionClient : fibonacciClient,
goalMessage : {
order : 7
}
});
goal.on('feedback', function(feedback) {
console.log('Feedback: ' + feedback.sequence);
});
goal.on('result', function(result) {
console.log('Final Result: ' + result.sequence);
});
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.');
});
goal.send();
</script>
</head>
<body>
<h1>Fibonacci ActionClient Example</h1>
<p>Check the Web Console for output</p>
</body>
</html>
代码解释:
- 代码段:
var fibonacciClient = new ROSLIB.ActionClient({
ros : ros,
serverName : '/fibonacci',
actionName : 'actionlib_tutorials/FibonacciAction'
});
解释:
- ROSLIB.ActionClient 作为actionlib客户端,发送一个目标到actionlib服务器端并获取一个反馈。
- 更多ROSLIB.ActionClient文档参考:http://robotwebtools.org/jsdoc/roslibjs/current/ActionClient.html
代码段:
var goal = new ROSLIB.Goal({
actionClient : fibonacciClient,
goalMessage : {
order : 7
}
});
- 解释:
- ROSLIB.Goal是一个actionlib目标,用于发送到actionlib服务器端
- 更多ROSLIB.Goal文档参考:http://robotwebtools.org/jsdoc/roslibjs/current/Goal.html
- 代码段:
goal.on('feedback', function(feedback) {
console.log('Feedback: ' + feedback.sequence);
});
goal.on('result', function(result) {
console.log('Final Result: ' + result.sequence);
});
goal.send();
解释:
- 回调函数获取feedback和result的信息
运行:
- 服务器端,新终端,运行:
roslaunch rosbridge_server rosbridge_websocket.launch
- 服务器端,新终端,运行:
rosrun actionlib_tutorials fibonacci_server
- 直接拖动文件到chrome浏览器
file://simple.html
- 效果图:
参考:
- http://wiki.ros.org/roslibjs/Tutorials/ActionlibClient
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号