您无法真正更改 navdata 进入的速率。如果您不想处理它,可以忽略 navdata。下面的代码每秒只处理一次 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 库。它专门用于处理具有实时更新的图表和图形。他们的教程使它看起来很容易。下面,我尝试调整他们的教程以显示来自 AR.Drone 的导航数据信息。我还没有测试过,但这可能是一个很好的起点。
首先,在您的 HTML 中,包含 Smoothie 脚本并创建一个画布元素来保存图形:
<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);