hybridgroup / cylon-i2c

Cylon.js drivers for i2c devices
http://cylonjs.com
Other
11 stars 21 forks source link

i2c connection problem with hmc 6352 compass module #17

Closed dongkibravo closed 9 years ago

dongkibravo commented 9 years ago

Currently I'm working on a drone navigation project using AR_Drone2 with Arduino Yun.

Basic setup to install cylon.js on Arduino Yun was based on this page with @edgarsilva 's help.

https://github.com/hybridgroup/cylon/issues/212#issuecomment-49395772

And now I'm trying to use the compass module (hmc6352) to get the heading of my drone but when i run a simple code

var Cylon = require('cylon');

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

device: {name: 'hmc6352', driver: 'hmc6352'},

work: function(my) { every((1).second(), function() { my.hmc6352.heading(function(data) { Logger.info("heading: " + data); }); }); }} ).start();

I get this error

/root/drone/node_modules/cylon/lib/utils.js:124 return target[method].apply(target, arguments); ^ TypeError: Cannot call method 'apply' of undefined at Adaptor.base.(anonymous function) as i2cConfig at Connection.base.(anonymous function) as i2cConfig at Hmc6352.start (/root/drone/node_modules/cylon-i2c/lib/hmc6352.js:26:19) at Device.start (/root/drone/node_modules/cylon/lib/device.js:60:22) at /root/drone/node_modules/cylon/node_modules/async/lib/async.js:568:25 at /root/drone/node_modules/cylon/node_modules/async/lib/async.js:118:13 at Array.forEach (native) at _each (/root/drone/node_modules/cylon/node_modules/async/lib/async.js:39:24) at Object.async.each (/root/drone/node_modules/cylon/node_modules/async/lib/async.js:117:9) at _parallel (/root/drone/node_modules/cylon/node_modules/async/lib/async.js:567:20)

What is wrong with this code? (I connected the compass module's sda scl pins to Arduino Yun's sda scl pins)

dongkibravo commented 9 years ago

@edgarsilva

So i've been trying to figure out this problem by debugging some js files in cylon lib folder.

https://docs.google.com/document/d/1PMi3lyb1PIpfdH3ThgH2Ftx7nCoRRxQDfEaPOpANfJ4/edit?usp=sharing

In the document, it shows some specific results from my debugging and I was just wondering if this could help you figuring out what's the problem with the program with not having the hardware.

I tried to connect hmc6352's scl and sda to pin 2, 3 on Yun and pin sda and scl on Yun.

dongkibravo commented 9 years ago

@edgarsilva

my friend and i figured out that target[method] gets grabbed from your device list (target is an array? or json list including whole bunch of information about my program). We are wondering what i2cConfig: [function] does since when i print out target[method].appy(targer, arguments)), the result is undefine

edgarsilva commented 9 years ago

Hi @dongkibravo

I'm working on this i2c issues right now, I'll update shortly.

PS: BTW yes, the info you posted here helps

edgarsilva commented 9 years ago

It seems there is an issue either on how you wired up the sensor to the arduino or with the sensor itself, do you have any other way of testing if it is working correctly?

I did a small fix in to of the repos, these are the branches and changes, I think this might fix the problem:

https://github.com/hybridgroup/cylon-firmata/commit/9ff319e7cd8fbb74e7ddf007544f64204396df88

https://github.com/hybridgroup/cylon-i2c/commit/b018f907245f09263b7f3bb3042dde6218067547

I recommend that you just modified the files in your node_modules/cylon-firmata and node_modules/cylon-i2c inside your project folder in the YUN, since it is just a couple of lines and that way you do not have to clone the branches and manually move everything over.

@dongkibravo I think there's a good chance this will fix the problem since it seems it was looking for those two calls to a function that does not exist. Let me know how it goes.

dongkibravo commented 9 years ago

How did you connect the compass?

edgarsilva commented 9 years ago

@dongkibravo http://cylonjs.com/documentation/drivers/hmc6352-compass/

See the image at the end, you actually do not need the pull-up resistors to connect it to the arduino, the arduino already has them wired up internally.

SDA should be connected to pin A4 on the arduino and SCL to pin A5.

Is your setup correct?

dongkibravo commented 9 years ago

A4 and a5 meaning analog pin 4 and 5?

edgarsilva commented 9 years ago

Yes, analog pin 4 is SDA and analog pin 5 is SCL

edgarsilva commented 9 years ago

@dongkibravo any progress?

dongkibravo commented 9 years ago

@edgarsilva so i finally got rid of all of the errors but now it's only saying reading i2c and not getting output inside

my.hmc6352.heading(function(data){ console.log("Debug"); console.log("heading " + data); });

why am i not going into that function

edgarsilva commented 9 years ago

@dongkibravo Could you pls paste all the code you are using for the example here?

Are you using the changes I pasted in the branch yesterday?

edgarsilva commented 9 years ago

Also paste a gist of output when you run your program .

edgarsilva commented 9 years ago

@dongkibravo any progress? anything I can help you with? if you can share the code you are running and an actual picture of the setup, how you are connecting everything, I might be able to help some more.

dongkibravo commented 9 years ago

@edgarsilva This is code that i'm using

var Cylon = require('cylon');

// define the robot var robot = Cylon.robot({ // change the port to the correct one for your Arduino connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyATH0' }, device: { name: 'hmc6352', driver: 'hmc6352'},

work: function(my) { every((1).second(), function(){ my.hmc6352.heading(function(data){ console.log("debug"); console.log("heading " + data); });

}); }}).start();

and this is the result

I, [2011-09-08T19:46:57.524Z] INFO -- : Initializing connections. I, [2011-09-08T19:46:57.585Z] INFO -- : Initializing connection 'arduino'. D, [2011-09-08T19:46:57.600Z] DEBUG -- : Loading adaptor 'firmata'. D, [2011-09-08T19:47:00.007Z] DEBUG -- : Registering Firmata adaptor for Robot 80994 D, [2011-09-08T19:47:00.013Z] DEBUG -- : Registering GPIO AnalogSensor driver for Robot 80994 D, [2011-09-08T19:47:00.040Z] DEBUG -- : Registering GPIO Button driver for Robot 80994 D, [2011-09-08T19:47:00.043Z] DEBUG -- : Registering GPIO ContinuousServo driver for Robot 80994 D, [2011-09-08T19:47:00.046Z] DEBUG -- : Registering GPIO LED driver for Robot 80994 D, [2011-09-08T19:47:00.049Z] DEBUG -- : Registering GPIO MakeyButton driver for Robot 80994 D, [2011-09-08T19:47:00.052Z] DEBUG -- : Registering GPIO Maxbotix driver for Robot 80994 D, [2011-09-08T19:47:00.055Z] DEBUG -- : Registering GPIO Motor driver for Robot 80994 D, [2011-09-08T19:47:00.057Z] DEBUG -- : Registering GPIO Servo driver for Robot 80994 D, [2011-09-08T19:47:00.060Z] DEBUG -- : Registering GPIO IR Range Sensor driver for Robot 80994 D, [2011-09-08T19:47:00.063Z] DEBUG -- : Registering GPIO DirectPin Driver for Robot 80994 D, [2011-09-08T19:47:00.068Z] DEBUG -- : Registering i2c BlinkM driver for Robot 80994 D, [2011-09-08T19:47:00.097Z] DEBUG -- : Registering i2c HMC6352 driver for Robot 80994 D, [2011-09-08T19:47:00.100Z] DEBUG -- : Registering i2c MPL115A2 driver for Robot 80994 D, [2011-09-08T19:47:00.104Z] DEBUG -- : Registering i2c BMP180 driver for Robot 80994 D, [2011-09-08T19:47:00.107Z] DEBUG -- : Registering i2c MPU6050 driver for Robot 80994 D, [2011-09-08T19:47:00.112Z] DEBUG -- : Registering i2c LCD driver for Robot 80994 I, [2011-09-08T19:47:00.125Z] INFO -- : Initializing devices. I, [2011-09-08T19:47:00.129Z] INFO -- : Initializing device 'hmc6352'. D, [2011-09-08T19:47:00.140Z] DEBUG -- : Loading driver 'hmc6352'. I, [2011-09-08T19:47:00.173Z] INFO -- : Starting connections. I, [2011-09-08T19:47:00.190Z] INFO -- : Connecting to 'arduino' on port /dev/ttyATH0. I, [2011-09-08T19:47:06.187Z] INFO -- : Starting devices. I, [2011-09-08T19:47:06.192Z] INFO -- : Starting device 'hmc6352'. I, [2011-09-08T19:47:06.197Z] INFO -- : Driver hmc6352 started. I, [2011-09-08T19:47:06.219Z] INFO -- : Working. Reading I2c Reading I2c Reading I2c Reading I2c Reading I2c Reading I2c

edgarsilva commented 9 years ago

Seems like the sensor is either not working, or not connected properly can you share a pic of the setup of the sensor and how it is connected to the arduino?

dongkibravo commented 9 years ago

unnamed

scl is connected to pin a5 sda is connected to pin a4

dongkibravo commented 9 years ago

I confirmed that the chip works fine by using this code

include

void setup() { Serial.begin(57600); Serial1.begin(57600); Wire.begin();
}

void loop() { Wire.beginTransmission(0x21); Serial.write(Wire.write("A")); delay(100);
Wire.requestFrom(0x21, 2); //get the two data bytes, MSB and LSB byte MSB = Wire.read(); // Result will be in tenths of degrees (0 to 3599) byte LSB = Wire.read(); // Provided in binary format over two bytes." Wire.endTransmission(); // Compute result from the two bytes results float myres = ((MSB << 8) + LSB) / 10; Serial.print(myres); Serial.println(" degrees"); delay(3000); } in arduino

and got result like this

247.00 degrees 247.00 degrees 256.00 degrees 255.00 degrees 255.00 degrees 147.00 degrees 142.00 degrees

(ps the connected pins were digital 2,3)

edgarsilva commented 9 years ago

Are those accurate readings?

I can't make the connections to the chip very well on the pic, but does not seem to be hooked up like this?

http://bildr.org/2011/01/hmc6352/

dongkibravo commented 9 years ago

yes the reading was pretty accurate compare to my iphone compass and the connection is exactly like it on the website

edgarsilva commented 9 years ago

Here are more examples, from the picture it seems some cables are swaped.... but the picture might not be that accurate, since I cannot make out the last pin:

http://www.funnyrobotics.com/2011/03/arduino-with-hmc6352-digital-compass.html http://wiring.org.co/learning/libraries/hmc6352sparkfun.html

Check those examples and verify it works and it is 100% wired correctly if that is the case I can dig up some more in the code, try to debug the output.

I'll start checking the driver while you test only on the arduino side.

dongkibravo commented 9 years ago

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at EventEmitter.addListener (events.js:160:15) at EventEmitter.once (events.js:185:8) at Board.sendI2CReadRequest (/usr/lib/node_modules/firmata/lib/firmata.js:723:10) at Adaptor.i2cRead (/root/drone/node_modules/cylon-firmata/lib/firmata.js:95:14) at Connection.base.(anonymous function) as i2cRead at Hmc6352.heading (/root/drone/node_modules/cylon-i2c/lib/hmc6352.js:36:19) at Device.base.(anonymous function) as heading at null. (/root/drone/testa.js:11:17) at wrapper as _onTimeout at Timer.listOnTimeout as ontimeout

i got this error in the middle of running the program. And can we assume that Uno board and Yun board have the same pin diagrams?

edgarsilva commented 9 years ago

yes, if you upload a sketch to the uno with the sensor connected should work just fine on the arduino Yun.

edgarsilva commented 9 years ago

That error is happening because the firmata lib never hears back from the I2C device, so it requests to read from the I2C bus and waits for a response that never arrives, then it just keeps adding listeners every time you call heading and all of them are waiting for a response, causing the EventEmitter error, which makes me think the sensor is not being detected by the arduino board.

Let's log the heading function in the driver, could you replace the heading function with this one pls:

// ./node_modules/cylon-gpio/lib/hmc6352.js
Hmc6352.prototype.heading = function(callback) {
  var self = this;
  console.log('BEFORE I2C read:');
  this.connection.i2cRead(this.address, this.commandBytes('A'), 2, function(data) {
    console.log('DATA FROM CALLBACK:', data);
    callback(self.parseHeading(data));
  });
};

Then run it and share the output, BUT before that run the examples I pasted above just on the arduino to make sure the sensor is hooked up correctly and working as expected, you'll need to upload the sketches and then reupload the modified firmata file to the arduino.

deadprogram commented 9 years ago

I think this issue was addressed by the most recent release of cylon-i2c. Can you please give this a try?

edgarsilva commented 9 years ago

@dongkibravo did you ever got this to work? did you try the latest release? I will close the issue since it should be addressed by latest release.

If you ever got it to work please let us know, so this info can be useful to someone else, regards.