ROS2D在Web端显示地图及定位

参考:How to display the robot location on top of the Viewer? · Issue #39 · RobotWebTools/ros2djs (github.com)

2023.9.28 机器人标志的角度,需要四元素转换为欧拉角计算;

注:1.js需要采用static方式引入,否则存在this指向问题,暂时未有其他解决方法;


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Simple ROS2D Map Example</title>
  <script src="http://static.robotwebtools.org/EaselJS/current/easeljs.min.js"></script>
  <script src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
  <script src="http://static.robotwebtools.org/roslibjs/current/roslib.js"></script>
  <script src="http://static.robotwebtools.org/ros2djs/current/ros2d.js"></script>
  <script type="text/javascript"
  src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
</head>

<body onload="init()" style="text-align: center;">
  <h3>Simple ROS2D Map Example</h3>
  <div id="map"></div>
  <script>
    function init() {
      var ros = new ROSLIB.Ros({
        url: 'ws://localhost:9090'
      });

      var viewer = new ROS2D.Viewer({
        divID: 'map',
        width: 800,
        height: 700
      });

      let gridClient = new ROS2D.OccupancyGridClient({
        ros: ros,
        rootObject: viewer.scene,
        continuous: true
      });

      gridClient.on('change', function () {
        viewer.scaleToDimensions(gridClient.currentGrid.width, gridClient.currentGrid.height);
        viewer.shift(gridClient.currentGrid.pose.position.x, gridClient.currentGrid.pose.position.y);
      });

      var robotMarker = new ROS2D.NavigationArrow({
        size: 0.5,
        strokeSize: 0.05,
        pulse: true,
        fillColor: createjs.Graphics.getRGB(255, 0, 0, 0.65)
      });

      var poseTopic = new ROSLIB.Topic({
        ros: ros,
        name: '/pose',
        messageType: 'geometry_msgs/PoseStamped'
      });
      poseTopic.subscribe(function (message) {
        robotMarker.x = message.pose.position.x;
        robotMarker.y = -message.pose.position.y;
        // 四元素转换欧拉角
        robotMarker.rotation = new THREE.Euler().setFromQuaternion(new THREE.Quaternion(
          message.pose.orientation.x,
          messge.pose.orientation.y,
          message.pose.orientation.z,
          message.pose.orientation.w,
        )).z * (-180) / Math.PI
      });
      gridClient.rootObject.addChild(robotMarker);
    }
  </script>
</body>

</html>

结果:红色三角表示定位点

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值