Montag, 10. Januar 2011

Attaching an I2C compass to the AR.Drone

I've recently attached a magnetic compass to the drone using the i2c bus. Let me start by saying that a good compass is not exactly cheap, I paid about 25€ for it and I've seen it being sold for over 100€ so it's an expensive toy. Also, from what I've gathered, it's really hard to find a good position for it on the drone seeing as all the motors have a strong magnetic field. Right now, my drone isn't exactly flyable but I will share my experience with the additional sensor nonetheless, even though I have not found an ideal spot to place it at yet.

The compass chip I've attached is an HMC6352 (datasheet, link to breakout board supplier) by Honeywell mounted on a breakout board (very necessary if you are not a soldering pro). I've attached VCC and ground to the two pins carrying those current marked 10 and 11 in my previous post about the mainboard layout. The two i2c wires were attached to what I labeled C and D in said post. To be exact, I chose to attach the sensor to the i2c bus solder joints in the middle of the board, not next to the i2c eeprom; it was really easy to solder the cables on, no protective varnish of any kind. My warranty is probably void now, though. If you don't want to risk voiding your warranty I suggest glueing something onto those pins also I can not imagine that being a really solid connection. In a worst case scenario it's probably easiest if you swap the mainboard out for a replacement part and send the device in for warranty after that ;-)

The sensor is not attached to the same bus the firmware is using for the ground camera, an eeprom and another device I have yet to identify. I have not done excessive testing put I am able to poll the sensor while the firmware is operating so that seems to be a good sign. I slapped together a very small test program to retrieve the current heading from the sensor:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <sys/time.h>
  7. #include "i2c-dev.h"
  8. #define ADDRESS 0x21
  9. int fd;
  10. int main(int argc, char *argv[]) {
  11.   fd = open( "/dev/i2c-0", O_RDWR );
  12.   if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 )
  13.   {
  14.     fprintf( stderr, "Failed to set slave address: %m\n" );
  15.     return 2;
  16.   }
  17.   if( i2c_smbus_write_byte( fd, 'A' ) < 0 )
  18.     fprintf( stderr, "Failed to write 'A' to I2C device: %m\n" );
  19.   usleep(10);
  20.   unsigned result = i2c_smbus_read_word_data(fd, 0);
  21.   unsigned upper = result >> 8 & 0x00FF;
  22.   unsigned lower = result & 0x00FF;
  23.   printf("%u\n", (lower << 8) | upper);
  24.   return 0;
  25. }
So the sensor is attached and working. I will update this as soon as I figure out a good location for the sensor on the drone :-)

compass (arm binary)


  1. Where are you running this code? It's not running on the ar.drone is it?

  2. Can you flight on GPS only mode? And be able to control the drone remotely? Thanks

  3. i'm trying to run this code but:
    1. you can post the i2c-dev.h file?
    2. the address 21? what is? you don't write the address of sensor?