go away spammer

Author Topic: HMC6343 compass code for Axon, I2C  (Read 9895 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
HMC6343 compass code for Axon, I2C
« on: June 30, 2009, 07:54:38 AM »
I got a HMC6343 Compass hooked up to my 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
« Last Edit: July 01, 2009, 11:01:40 AM by Admin »

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: HMC6343 compass code for Axon, I2C
« Reply #1 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.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: HMC6343 compass code for Axon, I2C
« Reply #2 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!

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: HMC6343 compass code for Axon, I2C
« Reply #3 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.
Check out the uBotino robot controller!

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: HMC6343 compass code for Axon, I2C
« Reply #4 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 . . .

Offline meysam1979

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: HMC6343 compass code for Axon, I2C
« Reply #5 on: July 20, 2011, 01:41:23 AM »
hi
could you send me full code of hmc6343 and atimega16.(codevision)
[email protected]
mersi ::)

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: HMC6343 compass code for Axon, I2C
« Reply #6 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/

Offline Jak24

  • Full Member
  • ***
  • Posts: 96
  • Helpful? 0
Re: HMC6343 compass code for Axon, I2C
« Reply #7 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

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: HMC6343 compass code for Axon, I2C
« Reply #8 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.

 


Get Your Ad Here

data_list