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 428 forks source link

Problem with demo.altitude #91

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hello: I am new with the node-ar-node and I am trying to use the altitude section within the navdata to read what the current height of the drone is (I move the drone by hand). But I don't get anything back. What is wrong?

var arDrone = require('ar-drone')
  ,  arDroneConstants = require('ar-drone/lib/constants')
  ;

function navdata_option_mask(c){
    return 1 << c;
}
var navdata_options = (
    navdata_option_mask(arDroneConstants.options.DEMO)

);

var drone1 = new arDrone.createClient();
drone1.config('general:navdata_demo', false);
drone1.config('general:navdata_options', navdata_options);

drone1.on('navdata', function (d) {

        console.log(d.demo.altitude);

});

And returns :

TypeError: Cannot read property 'altitude' of undefined

Have I to change something in /lib/navdata/parseNavdata.js ?

Thank you

eschnou commented 10 years ago

It seems you are not having any demo data at all and my guess is because you have set navdata_demo to false. I typically write it this way (not sure about strings vs booleans).

drone1.config('general:navdata_demo', 'TRUE');

If this does not work, please log the complete result in 'd' and paste it.

ghost commented 10 years ago

I try with drone1.config('general:navdata_demo', 'TRUE'); and drone1.config('general:navdata_demo', true); and it returns the same TypeError: Cannot read property 'altitude' of undefined.

But if I put console.log(d.demo); or console.log(d); it prints the values correctly: demo

I try with other values like demo.batteryPercentage or visionDetect.nbDetected and it doesn't work ('undefined').

Thanks. Jon

eschnou commented 10 years ago

So, what do you actually receive in 'd' ? Can you try your code and share the result? Just put everything in a gist (https://gist.github.com/) ans share the link. Thanks!

drone1.on('navdata', function (d) {
       console.log(d);
});
ghost commented 10 years ago

There my code and the result: https://gist.github.com/jonlarra1/9913095

Thanks.

eschnou commented 10 years ago

So, it is all there, d.demo.altitude. What is wrong?

Note that it may happen, especially at the initial connection, that the demo data isn't there yet. So, it is good to check if demo is defined before looking for altitude. But since you logged it, it is there.

ghost commented 10 years ago

Yes, but my cuestion is why I can print, for example, d.droneState.flying without error but when I print or take like variable d.demo.altitude or d.visionDetect.nbDetect returns an error :

/home/gic/multi.js:20
    console.log(d.demo.altitude);
                      ^
TypeError: Cannot read property 'altitude' of undefined
    at Client.<anonymous> (/home/gic/multi.js:20:20)
    at Client.EventEmitter.emit (events.js:95:17)
    at Client._handleNavdata (/home/gic/node_modules/ar-drone/lib/Client.js:120:8)
    at UdpNavdataStream.EventEmitter.emit (events.js:95:17)
    at UdpNavdataStream._handleMessage (/home/gic/node_modules/ar-drone/lib/navdata/UdpNavdataStream.js:73:10)
    at Socket.EventEmitter.emit (events.js:98:17)
    at UDP.onMessage (dgram.js:423:8)

Sorry if I am explaining bad

eschnou commented 10 years ago

Well, that error means that demo isn't there (Cannot read property of undefined). Is it the same code?

ghost commented 10 years ago

Yes is the same code (https://gist.github.com/jonlarra1/9913095). I can't print d.demo.altitude but I can d.demo, I don't understand :(

ghost commented 10 years ago

Anyone know why this happens? Is possible that Demo navdata is wrong actived?

wiseman commented 10 years ago

Maybe try changing that gist so it reads

drone1.on('navdata', function(d) {
  console.log(d);
  if (d.demo) {
    console.log("DEMO:");
    console.log(d.demo);
    if (d.demo.altitude) {
      console.log("ALT");
      console.log(d.demo.altitude);
    }
  }
});

Does it print out d.demo.altitude every time? If it works, then you need to figure out what's different between that example and your code.

ghost commented 10 years ago

It works. Thanks!

wiseman commented 10 years ago

For the benefit of searchers etc, what was the problem?

ghost commented 10 years ago

The problem was that I didn't added : if (d.demo) { console.log(d.demo.altitude) } If I put only console.log(d.demo.altitude) it returned a error saying it couldn't read altitude.

wiseman commented 10 years ago

Closing as this has been resolved.

ghost commented 8 years ago

Great thread! solved a problem i was having for a while