Shop OBEX P1 Docs P2 Docs Learn Events
Mysterious(?) output from compass HMC5883L — Parallax Forums

Mysterious(?) output from compass HMC5883L

MCAMCA Posts: 13
edited 2013-11-07 00:33 in Accessories
I’ve connected a HMC5883L compass to my BS2 using the setup and program code found at the of the kickstart-page, http://learn.parallax.com/KickStart/29133.

Typically I get the following output using default settings, and although the values are varying about +-10 between readings, they do not seem
to change with rotation of the compass:
X = -20
Y = -4
Z = 10

These values do not make any sense to me, they are far too low to be useable. So I suspect either there is either something
I do not understand (likely, since I'm new to this) or there is something wrong with either the code or the compass itself. I tried
maxing the gain in the compass, but still get low values (although sligthly higher). Then I made the compass do the self test. This gave
reasonable results, not the exact ones indicated in the compass data, but not way off ( http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf ).

For Gain 5 I got positive/negative self test:
X = -434
Y = -405
Z = -4096
X = 412
Y = 396
Z = 237

To me this sets off some new questions: How come there is an overflow on negative when not on positive? Do the outputs indicate
the compass is working normally or not (there was no Z overflow when I tried gain 7, the value then was only z = -94!)?

And, my original question remains, how come I get so low values when trying to measure in continuos mode and why don't they
seem to correspond at all to rotation in any axis??

Would be very grateful for any help sorting this out, and/or if someone has tried the BS2 kick-start code for this and and can tell
me their results.

BR / Adam

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-14 15:21
    The HMC5883L doesn't output a heading. It outputs the magnetic field vector components. It requires some additional math to turn the raw values into headings.

    IIRC, you want to take the arctangent of the x and y values to compute the heading.

    I have code for the Propeller to do this but not for the Basic Stamp.
    PUB GetRaw | fX, fY, fZ  ' Get raw data from continous output
    
    
      StartI2c
      Send(READ_DATA)
      x := ((Receive(true) << 8) | Receive(true))
      z := ((Receive(true) << 8) | Receive(true))
      y := ((Receive(true) << 8) | Receive(false))
      StopI2c
      ~~x
      ~~z
      ~~y
      'x := x   ' this doesn't make sense to me.    
      'z := z 
      'y := y 
      long[compassPtr] := x
      long[compassPtr][1] := y
      long[compassPtr][2] := z
      fX := F.FFloat(x)
      fY := F.FFloat(y)
      fZ := F.FFloat(z)
      
      long[compassPtr][3] := F.FRound(F.FMul(F.Degrees(F.ATan2(fY, fX)), fPseudoMultiplier))
      long[compassPtr][4] := F.FRound(F.FMul(F.Degrees(F.ATan2(fZ, fX)), fPseudoMultiplier))
      long[compassPtr][5] := F.FRound(F.FMul(F.Degrees(F.ATan2(fZ, fY)), fPseudoMultiplier))
      
    
    
    

    The variable identified by "long[compassPtr][3]" holds the heading. The code uses this notation to make it easier for the parent object to access the results.

    Some of the above code I wrote and some I copied (including the part that didn't make sense to me).

    I don't know how you go about preforming trig calculations the Basic Stamp.
  • MCAMCA Posts: 13
    edited 2013-07-16 14:01
    Duane, thanks for the reply. I had completely missed the data was in vector form... :lol: That surely is
    fundamental to know, but it does not help to sort out the strangenessof the output. Here I go again:

    The quantities of the vector elements are still to small to give a decent resolution. Well, today I used an ordinary compass to check directions to see if the vector
    element signs (+/-) would at least change correctly when turning it. As you can see from the following, the output is the same no matter
    how i turn the damn thing (the direction headings is the actual heading that x+ on the digital compass is pointing and I sampled 4 measurements x,y,z per direction):

    North
    x = -18
    y = 3
    z = 12

    x = -16
    y = -1
    z = 11

    x = -13
    y = -3
    z = 12

    x = -13
    y = -5
    z = 13

    West
    x = -28
    y = 3
    z = 9

    x = -15
    y = -1
    z = 10

    x = -11
    y = 0
    z = 10

    x = -13
    y = -3
    z = 11

    South
    x = -23
    y = 1
    z = 11

    x = -20
    y = -1
    z = 13

    x = -19
    y = 2
    z = 9

    x = -11
    y = -2
    z = 8

    Any ideas, anyone??

    BR, Adam
  • MCAMCA Posts: 13
    edited 2013-10-30 13:50
    I got the company where I bought it (lawicel-shop.se) to send me a new one as it seemed to be defective; and the happy news is now it's working perfectly with the new one!

    BR Adam
  • Mark_TMark_T Posts: 1,981
    edited 2013-11-06 07:39
    Don't expect a MEMS compass to be anything like as accurate as a good mechanical compass, they just are not.
  • MCAMCA Posts: 13
    edited 2013-11-07 00:33
    Mark_T wrote: »
    Don't expect a MEMS compass to be anything like as accurate as a good mechanical compass, they just are not.

    Ok, but low accuracy is something I can live with in this case. It's s a lot better then nothing at all which is what I had before...
Sign In or Register to comment.