关于parrot 2.0 ardrone的NavData,如何添加延迟并构建实时图?

时间:2017-04-26 07:31:10

标签: javascript node.js graph drone ar.drone

我想问问题。我想延迟navData AR Drone 2.0上的navData读取。你能举例说明如何在我的Navdata读取中添加间隔吗?以及我需要做什么,如果我想构建实时图形,有人可以提供教程链接吗?

var arDrone = require('ar-drone'); 

var client = new arDrone.createClient();
client.on('navdata', function(d){

if(d.demo){
    console.log('Roll:' + d.demo.rotation.roll);
}
});

或此代码?

    var arDrone = require('ar-drone');
var droneClient = arDrone.createClient();
droneClient.config('general:navdata_demo', 'TRUE');

droneClient.on('navdata', function(navdata) {
try {
    console.log('test:' + navdata.demo.batteryPercentage);
}
catch(err) {
    console.log(err.message);
}
});

1 个答案:

答案 0 :(得分:0)

您无法真正更改导航数据的速度。如果您不想处理导航数据,则可以忽略导航数据。下面的代码每秒只处理一次navdata:

var lastNavDataMs = 0;

client.on('navdata', function(d) {
  var nowMs = new Date().getTime();
  if (nowMs - lastNavDataMs > 1000) {
    lastNavDataMs = nowMs;
    // Process navdata once per second.
    if (d.demo){
      console.log('pitch:' + d.demo.rotation.roll);
    }
  }
});

要显示传感器值的实时图表,您可以尝试使用Smoothie Charts库。它专门用于处理具有实时更新的图表和图形。他们的tutorial使它看起来很容易。下面,我尝试调整他们的教程来显示来自AR.Drone的navdata信息。我没有测试过它,但它可能是一个很好的起点。

首先,在HTML中,包含Smoothie脚本并创建一个canvas元素来保存图形:

<script type="text/javascript" src="smoothie.js"></script>
<canvas id="mycanvas" width="400" height="100"></canvas>

代码:

var arDrone = require('ar-drone'); 
var smoothie = new SmoothieChart();
smoothie.streamTo(document.getElementById("mycanvas"));

// Lines on the chart.
var rollLine = new TimeSeries();
var altitudeLine = new TimeSeries();

var client = new arDrone.createClient();
client.on('navdata', function(d) {
  if (d.demo) {
    // Add new measurements for roll and altitude.
    var time = new Date().getTime();
    rollLine.append(time, d.demo.rotation.roll);
    altitudeLine.append(time, d.demo.altitudeMeters);
  }
});

// Add the lines to our chart.
smoothie.addTimeSeries(rollLine);
smoothie.addTimeSeries(altitudeLine);