< >
Home » ROS与JS入门教程 » ROS与javascript入门教程-roslibjs-URDF解析器

ROS与javascript入门教程-roslibjs-URDF解析器

ROS与javascript入门教程-roslibjs-URDF解析器

说明:

  • 介绍如何获取机器人模型并利用roslib解析

github:

步骤:

  • 新建urdf.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">
  var ros = new ROSLIB.Ros({
    url : 'ws://localhost:9090'
  });

  var param = new ROSLIB.Param({
    ros : ros,
    name : 'robot_description'
  });

  param.get(function(param) {
    var urdfModel = new ROSLIB.UrdfModel({
      string : param
    });
    console.log(urdfModel);
  });

  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.');
  });
</script>
</head>

<body>
  <h1>URDF Parsing Example</h1>
  <p>Check the JavaScript console for the output.</p>
</body>
</html>

代码解析:

  • 代码段:
  var param = new ROSLIB.Param({
    ros : ros,
    name : 'robot_description'
  });
  • 解释:

    • 机器人的URDF模型放在参数robot_description,通过参数服务器获取参数robot_description
    • robot_description如果存在,可以提取到URDF的XML内容.
  • 代码段:

  param.get(function(param) {
    var urdfModel = new ROSLIB.UrdfModel({
      string : param
    });
    console.log(urdfModel);
  });
  • 解释:
    • 解析urdf模型,并可以访问不同的link mesh等内容
    • 更多ROSLIB.UrdfModel参考文档:http://robotwebtools.org/jsdoc/roslibjs/current/UrdfModel.html

运行:

  • 安装ros-kinetic-pr2-common

  • 服务器,新终端,运行rosbridge

roslaunch rosbridge_server rosbridge_websocket.launch
  • 服务器,新终端, 运行模型
roslaunch pr2_description upload_pr2.launch
  • chrome浏览器访问urdf.html

参考:

  • http://wiki.ros.org/roslibjs/Tutorials/UrdfParsing

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

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


标签: ros与javascript入门教程