Society of Robots - Robot Forum

Software => Software => Topic started by: Admin on June 30, 2009, 07:54:38 AM

Title: HMC6343 compass code for Axon, I2C
Post by: Admin on June 30, 2009, 07:54:38 AM
I got a HMC6343 Compass (http://www.sparkfun.com/commerce/product_info.php?products_id=8656) hooked up to my Axon (http://www.societyofrobots.com/axon/), using I2C. Here is some code I'd like to share . . . don't forget to include the I2C AVRlib library. ;D

Code: [Select]
//Read HMC6343 compass I2C data:
u08 TARGET_ADDR = 0x32;  //0x32 = HMC6343 compass slave write address
u08 command = 0x50;  //0x50 = post heading data, pitch data, and roll data
u08 in[6];  //6 bytes = MSB heading, LSB heading, MSB pitch, LSB pitch, MSB roll, LSB roll
i2cMasterSend(TARGET_ADDR, 1, &command);  //send 1 byte command to slave from master
delay_ms(10);
i2cMasterReceive(TARGET_ADDR, 6, &in[0]);  //read 6 byte data from slave to master

//process heading data:
compass = in[0];  //first byte is MSB heading
compass = compass << 8;  //shift MSB byte to top of int
compass = compass + in[1];  //add LSB byte (2nd byte) to bottom of int
compass = compass; // / 10;

//process pitch data:
pitchCompass = in[2];  //3rd byte is MSB pitch
pitchCompass = pitchCompass << 8;
pitchCompass = pitchCompass + in[3];  //4th byte is LSB pitch
pitchCompass = pitchCompass / 10;
if(pitchCompass > 6400)
pitchCompass = (pitchCompass - 6400 - 155);  //convert to negative values

//process roll data:
rollCompass = in[4];  //5th byte is MSB roll
rollCompass = rollCompass << 8;
rollCompass = rollCompass + in[5];  //6th byte is LSB roll
rollCompass = rollCompass / 10;
if(rollCompass > 6400)
rollCompass = (rollCompass - 6400 - 155);  //convert to negative values

Output of course is pitchCompass and rollCompass.

Also a comment on this compass . . . I'm using it on my robot fish. The fish uses a waterproof magnet switch . . . the magnet is weak, and 2-3 inches away from the compass, but still affects results by about +/- 13% (absolute value 27%). Hopefully that info helps someone!


edit: changed the % error, due to my own error :P
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Razor Concepts on June 30, 2009, 04:21:17 PM
Are the heading readings you are getting good? I've tried using a HMC6352 (no tilt compensation) and the readings are not proportional, but in between 360 and 0 degrees.
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Admin on June 30, 2009, 09:08:33 PM
Are the heading readings you are getting good? I've tried using a HMC6352 (no tilt compensation) and the readings are not proportional, but in between 360 and 0 degrees.

Hmmmm I haven't done any real testing of it yet to comment. But outside interference can definitely make it non-proportional!
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Ro-Bot-X on July 01, 2009, 06:21:04 AM
I have an old Lego compass and I have noticed that it needs a bit of time to settle if the robot was moving and the measurements are not reliable while in motion, even it it is a streight line. This sensor is similar with the Devantech CMPS03, I had the same problems with that one too. So, it may be a good idea to keep the robot still while checking the compass, then let it move. Why? because of magnetic interference from the motors.
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Admin on July 01, 2009, 11:03:52 AM
Why? because of magnetic interference from the motors.
Not to mention the power fluctuations in your system, and the 'moving' environment. What if your floor had a steel I-beam under it and you didn't know? etc.


note: I changed the reported error above, had a typo . . .
Title: Re: HMC6343 compass code for Axon, I2C
Post by: meysam1979 on July 20, 2011, 01:41:23 AM
hi
could you send me full code of hmc6343 and atimega16.(codevision)
[email protected]
mersi ::)
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Admin on July 20, 2011, 07:57:52 AM
I do not have CodeVision, code for it, nor did I write anything for the ATmega16. Sorry!

I recommend using WebbotLib for all of your robot programming needs:
http://webbot.org.uk/ (http://webbot.org.uk/)
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Jak24 on July 20, 2011, 01:26:44 PM
One question admin!

Does the HMC6343 give readings from 0-360 cause for me it skips to 0 after 278 :P
Did you have to calibrate it, if so, how did you do it, was it affective.
Thanks

Regards
Jak24
Title: Re: HMC6343 compass code for Axon, I2C
Post by: Admin on July 20, 2011, 06:07:57 PM
I had to calibrate it as various stuff in my lab distort the magnetic field. I just use excel to generate the transform for me.

It's been awhile so I could be wrong, but I remember it going to 360 when using WebbotLib.