hybridgroup / cylon-gpio

Cylon drivers for GPIO devices
http://cylonjs.com
Other
19 stars 14 forks source link

Infrared proximity sensor always returns distance as 0 #27

Closed nishendra closed 10 years ago

nishendra commented 10 years ago

There's a problem with the infrared proximity sensor driver where the distance value always returns 0. In the following example given here:

var Cylon = require('cylon');

Cylon.robot({
  connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },

  device: {
    name: 'sensor',
    driver: 'ir-range-sensor',
    pin: 0,
    model: 'gp2y0a41sk0f'
  },

  work: function(my) {
    every((1).seconds(), function(){
      var range = my.sensor.range();
      console.log('Range ===>', range);
    });
  }

}).start();

The "Range" always shows as 0. Going through the code on ir-range-sensor.js in cylon-gpio,

for (var range in this.rangeTable.rangeDistances){
    tmpRange = parseInt(range);
    if ((this.analogVal <= tmpRange) && (this.analogVal + 5 > tmpRange)){
      dist = this.rangeTable.rangeDistances[range].dist;
      break;
    }
  }

and logging "this.analogVal" in the console, I found that it is "undefined", hence the condition in the "if" statement never evaluates to "true" and therefore "dist" is always returned as 0.

deadprogram commented 10 years ago

Hi, @nishendra thank you for reporting this. I just pushed a commit that should correct your problem. Can you please try from the dev branch? Thank you!

deadprogram commented 10 years ago

Also, note there is evented way to do the same thing:

var Cylon = require('cylon');

Cylon.robot({
  connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },

  device: {
    name: 'sensor',
    driver: 'ir-range-sensor',
    pin: 0,
    model: 'gp2y0a41sk0f'
  },

  work: function(my) {
    my.sensor.on('range', function(data) {
       console.log('Range ===>', data);
    });
  }

}).start();
nishendra commented 10 years ago

Hi @deadprogram you are welcome! I tried using the dev branch and now the values are recorded correctly. Thank you!

edgarsilva commented 10 years ago

This has been fixed, closing the issue.