felixge / node-ar-drone

A node.js client for controlling Parrot AR Drone 2.0 quad-copters.
http://nodecopter.com/
MIT License
1.76k stars 427 forks source link

How to add delay to navdata read, and make real time graph? #147

Closed ivejun closed 7 years ago

ivejun commented 7 years ago

how to add delay for navdata read? because its too fast. what can i use if i want build real time graph?

here is my Code.

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

var client = new arDrone.createClient(); client.on('navdata', function(d){ if(d.demo){ console.log('pitch:' + d.demo.rotation.roll); } });`

Or here i can use another Code:

`var arDrone = require('ar-drone'); var droneClient = arDrone.createClient(); droneClient.config('general:navdata_demo', 'FALSE'); // get back all data the copter can send

droneClient.on('navdata', function(navdata) {

try { console.log('test:' + navdata.demo.velocity.x); } catch(err) { console.log(err.message); } });`

YunJinHan commented 7 years ago

i think if you want to just read slow navdata data , using setInterval / setTimeout function

for example

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

or

setInterval(function(){ droneClient.on('navdata', function(navdata) { try { console.log('test:' + navdata.demo.velocity.x); } catch(err) { console.log(err.message); } }); setTimeout(function(){ droneClient.removeListener('navdata',function(){ }); },1000); },1000);

the above code prints navdata data every 1 second.

may be? it's work

wiseman commented 7 years ago

Just ignore any navdata you don't want to process.

ivejun commented 7 years ago

your suggest about set interval didn't work at all. its still read nav data too fast

wiseman commented 7 years ago

You can't really change the rate at which navdata comes in. You can just ignore navdata if you don't want to process it. The code below only processes navdata once per second:

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);
    }
  }
});