Shop OBEX P1 Docs P2 Docs Learn Events
HOW DO I USE THE Compass Module 3-Axis HMC5883L ON MY BS2 BOE BOT — Parallax Forums

HOW DO I USE THE Compass Module 3-Axis HMC5883L ON MY BS2 BOE BOT

atari132atari132 Posts: 1
edited 2014-04-12 09:17 in BASIC Stamp
I have recently purchased the Compass Module 3-Axis HMC5883L for my board of education with a BS2 microcontroller, and have been having trouble getting the thing to do anything at all. i have installed it using the guides on the website and
still find it troubling to get it to respond. if you could post some helpful codes or any advice at all i would greatly appreciate it.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-08-11 10:10
    As part of the list of links on the Parallax webstore page for the product, there are links to all sorts of documentation and sample code for the BS2. The links are below the Made In The USA banner.
  • PublisonPublison Posts: 12,366
    edited 2013-08-11 10:37
    Do you have a picture of your setup? That would help.

    Be aware that the BS2 code posted on the product web site that Mike linked to, is for a BS2P. The standard BS2 does not support I2C Command.


    The link on learn.parallax.com:

    http://learn.parallax.com/kickstart/29133

    works with a standard BS2.

    I have tried both on a BOE with a BS2 and BS2P, and both work as advertised.

    Again, a picture of the wiring would help.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-11 10:47
    Be aware the code outputs the xyz vector. Not a magnetic heading.

    To get a heading from the vector requires some additional math.

    Are you getting any output? If so, what? Were you expecting something different to happen?
  • MCAMCA Posts: 13
    edited 2013-08-12 04:40
    I'd be very interested to hear what output you get from this, atari! I am also using BS2 and this compass, and I'm getting values that make no sense at all.

    / Adam
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-12 06:37
    MCA wrote: »
    I'm getting values that make no sense at all.

    Such as . . .?

    I hope you read post #4.


    Edit: Nevermind. I found where you posted the numbers.

    I have my compass next to me. I'll plug it in, find my ATAN2 demo program and post some numbers today. I'll use a Propeller to interface with the sensor but I'll display the raw values and the computed heading.
  • PublisonPublison Posts: 12,366
    edited 2013-08-12 07:17
    On my BOE using the code from:

    http://learn.parallax.com/KickStart/29133

    I
    get +/- ~400 unit readings when rotating randomly in the three axis.

    Not sure why you started a new thread on the same subject. It could have saved some time if you just updated the old one.

    What's you wiring look like? (picture).
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-12 07:21
    Publison wrote: »
    Not sure why you started a new thread on the same subject. It could have saved some time if you just updated the old one.

    The other thread was by Adam. Atari123 started this one (and it's still the only post they have made).

    I had my Prop code for this sensor open in one my Prop Tool windows. I had opened yesterday to borrow a method from it. It was kind of strange to go look for a file to find it already open.
  • PublisonPublison Posts: 12,366
    edited 2013-08-12 07:32
    You are correct Duane....my bad....
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-12 07:46
    Here's the output with the sensor somewhat level and aligned with magnetic north.
    X = 229 , Y = 3   , Z =-508
    
    Course = 0.751   , tiltX = -65.73, tiltY = -89.662
    


    Here's the output with the sensor somewhat level and aligned with magnetic east.
    X = -1  , Y = 127 , Z =-493
    
    Course = 90.451  , tiltX = -90.116   ltY = -75.554
    


    Here's the output with the sensor somewhat level and aligned with magnetic south.
    X = -193,   = -5  , Z =-494
    
    Course = -178.516, tiltX = -111.3, tiltY = -90.580
    


    Here's the output with the sensor somewhat level and aligned with magnetic west.
    X = -5  , Y = -365, Z =-496
    
    Course = -90.785 , tiltX = -90.57, tiltY = -126.349
    


    Here's the output with the sensor originally aligned with magnetic north but then tilted to point straight up.
    X = -510, Y = 2   , Z =-154
    
    Course = 179.775 , tiltX = -163.19 tiltY = -89.621
    


    I used F32 to calculate the "Course" and "tilt" values. After converting the raw readings to floating point numbers, I used the following code to calculate the three values (calculated in same order as they are displayed).
    long[compassPtr][3] := F.FRound(F.FMul(F.Degrees(F.ATan2(fY, fX)), 1000.0))
      long[compassPtr][4] := F.FRound(F.FMul(F.Degrees(F.ATan2(fZ, fX)), 1000.0))
      long[compassPtr][5] := F.FRound(F.FMul(F.Degrees(F.ATan2(fZ, fY)), 1000.0))
    


    The results from the trig calculations were multiplied by 1000 before being changed back to integers. I used the 1000 multiplier to avoid rounding errors. These values don't really have as much precision as the numbers displayed would indicate.

    Again, this was done on a Propeller, I don't know how to do this with a Basic Stamp.
  • silviu.litasilviu.lita Posts: 13
    edited 2014-04-07 12:28
    I'm also having problems understanding how this compass module works.
    I'm using the BS2 sample and get the following readings:
    1. Axis system
    x
    |
    |____y

    x : -200, y: -300 when in the following

    2. I rotate the sesor to the left 90 deg so that the axis system becomes


    y
    |
    x____|

    I expect x: 300, y: -200 but I'm getting the totally unexpected values of x: -124, y:50

    Please tell me if I'm wrong in my expectation.

    Thanks,
    Silviu
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-07 13:01
    I'm also having problems understanding how this compass module works.
    I'm using the BS2 sample and get the following readings:
    1. Axis system
    x
    |
    |____y

    x : -200, y: -300 when in the following

    2. I rotate the sesor to the left 90 deg so that the axis system becomes


    y
    |
    x____|

    I expect x: 300, y: -200 but I'm getting the totally unexpected values of x: -124, y:50

    Please tell me if I'm wrong in my expectation.

    Thanks,
    Silviu

    The output from the sensor in a vector. You need to use the arc-tangent of the numbers to find the angle.

    I'm not sure how you'd do this with the BS2.

    When you say you rotated the sensor, do you mean like this?
          y
          |
          |
      x___|
    

    The forum software removes extra spaces unless you use code tags.

    If you did rotate it 90 degree counter clockwise, then the numbers you're getting back agree pretty closely to those expected (within about 10 degrees).

    I'm not sure how to help you with the BS2. I think it's possible to do trig with the BS2 but I don't know how.
  • silviu.litasilviu.lita Posts: 13
    edited 2014-04-10 00:24
    Thanks Duane,

    Yes, I meant rotating to the left like you depicted. Thanks for the "code" tip.
    Yes, I understand I should use ATAN(y/x) to know the angle (it's possible in BS2) but I should be ok with the x, y projections as well.
    If we use trigonometry, then the magnetic field angle to the x axis for the first position should be ATAN(-300/-200) = 56 deg.

    When rotating to the left 90 deg I'm to expect an angle of 90 + 56 = 146 deg.
    But the calculated angle is ATAN(50/-124) = -22 deg, far from my expectations.

    I guess either the sensor or the code I'm using is faulty or my expectation is wrong, the magnetic fields lines are far from being uniform (due to other sources?) in the region of space the sensor is placed..

    Silviu

    Duane Degn wrote: »
    The output from the sensor in a vector. You need to use the arc-tangent of the numbers to find the angle.

    I'm not sure how you'd do this with the BS2.

    When you say you rotated the sensor, do you mean like this?
          y
          |
          |
      x___|
    

    The forum software removes extra spaces unless you use code tags.

    If you did rotate it 90 degree counter clockwise, then the numbers you're getting back agree pretty closely to those expected (within about 10 degrees).

    I'm not sure how to help you with the BS2. I think it's possible to do trig with the BS2 but I don't know how.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-10 10:23
    I just noticed something about the Parallax HMC5883L board. It looks like the X-axis is pointing in the negative x direction. The pdf documentation shows the correct orientation of the x-axis. I think this could be a contributing factor in some of your confusion.

    Both of us copied this flipped x-axis in our earlier posts.
    the magnetic field angle to the x axis for the first position should be ATAN(-300/-200) = 56 deg.

    The problem with the ATAN function is it only returns a value between -90 degrees and +90 degress. The angle of the vector (-200,-300) is -124 degrees not 56 degrees.
    When rotating to the left 90 deg I'm to expect an angle of 90 + 56 = 146 deg.

    Since the sensor was rotated clockwise, the magnetic field was rotated counter clockwise with respect to the sensor. If the field initially read 90 degrees (aligned with y-axis), after the rotation the field would be aligned with the x-axis (the proper one) and should read 0 degrees.

    So now the expected angle should be -214 degrees (-124 - 90) but to keep the angles between -180 and +180 we'll add 360 to -214 which is 146 degrees.
    But the calculated angle is ATAN(50/-124) = -22 deg, far from my expectations.

    Again ATAN gives us the wrong quadrant. The angle of vector (-124, 50) is 158 degrees not -22 degrees.

    So now we were expecting 146 degree but got 158 degrees. Now we're just off by 12 degrees. I think that's progress.
    I guess either the sensor or the code I'm using is faulty or my expectation is wrong, the magnetic fields lines are far from being uniform (due to other sources?) in the region of space the sensor is placed..

    I think the code is probably working correctly but your other guesses are probably correct.

    When I played with the HMC2883L a few months ago, I ended up taking a bunch of readings and averaging them. The averaging really helped. Just be careful about averaging around the roll over area. If your readings are near -180 and +180 the average can be close to 0 which is very wrong.

    Rather than using ATAN, you want to find a way of using the ATAN2 function. The ATAN2 takes two parameters and will give an answer between -180 and +180 instead of ATAN's range of -90 and +90.

    Excel and other spreadsheet programs have an ATAN2 function. Just make sure you order your coordinates properly for whichever ATAN2 function you're using. Some ATAN2 functions want the x first others want the y first.
  • silviu.litasilviu.lita Posts: 13
    edited 2014-04-11 00:30
    Thanks Duane.
    I've tried again yesterday but simply I don't find a logical connection between the azimuth values I'm getting when rotating the sensor with 90 degrees. My expectation is to see the azimuth rotating with 90 degrees too.
    Could it be that the sensor x and y axis are not ortogonal? The reason I'm asking is that I expect the raw x and y readings to be swapped when I rotate the sensor (axis) with 90 degrees.

    Anyway I think I'm betting on a dead horse. My goal is to use this Compass for position control, that is, knowing when to stop when doing a left turn or right turn. That would be my close loop alternative to using encoders.
    But I guess due to accuracy issues, that woudn't go well anyway.

    Silviu
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-11 09:05
    TMy expectation is to see the azimuth rotating with 90 degrees too.
    Could it be that the sensor x and y axis are not ortogonal? The reason I'm asking is that I expect the raw x and y readings to be swapped when I rotate the sensor (axis) with 90 degrees.

    The compass data needs a lot of filtering. I'm sure your 78 degrees turn when expecting 90 degrees could be improved on with some filtering. I remember when I first started playing with my compass, it didn't seem like there was a correlation between the readings and the orientation of the compass. Averaging multiple readings greatly improved the results.

    Here's a link to a figure 8 attempt using a compass to indicate when to change turning directions. (The compass doesn't need to be so high. I just grabbed the first rod I could find to elevate the compass above the motors. Currently I use a rod about 6" long to elevate the compass.) I didn't use the compass to control the turn rate of the robot while making the circles of the figure 8, I just used the compass to know when to transition to the second loop. Using a compass for position feedback would be challenging but I think it is very possible. This is one of the things on my robotics todo list.

    While I'm pretty sure it's possible to use the compass as a way of determining if your robot is heading the correct direction, it certainly doesn't take the place of encoders. Still I think it would be possible to have reasonable control of a robot using a compass without encoders. Here's another figure 8 attempt but this time using my cheap bot. I didn't use encoders on this figure 8 and I think a compass could be used with this type of robot to help it navigate.

    I'm pretty sure it's within in the capability of a BS2 to navigate reasonable well with a compass. I think your task would be a bit easier (eventually) using a Propeller but either way, a compass assisted navigation program will not likely be easy to write. You'll need to have a way of averaging multiple readings, computing your actual turn rate and compare it to the turn rate you desire. The problem of going from -180 degree to +180 degrees as the robot turns through magnetic south is also something else you'll need to worry about.

    I don't know how far along your are in your robot education, but I'd certainly make sure you can have your robot travel in a figure 8 and other relatively simple tasks prior to trying to add a compass into the mix.
  • silviu.litasilviu.lita Posts: 13
    edited 2014-04-12 01:52
    Duane Degn wrote: »
    I'm pretty sure it's within in the capability of a BS2 to navigate reasonable well with a compass. I think your task would be a bit easier (eventually) using a Propeller but either way, a compass assisted navigation program will not likely be easy to write. You'll need to have a way of averaging multiple readings, computing your actual turn rate and compare it to the turn rate you desire. The problem of going from -180 degree to +180 degrees as the robot turns through magnetic south is also something else you'll need to worry about.

    I will insist a bit more, I will try multipe reading as well. Thanks for the support
    I don't know how far along your are in your robot education, but I'd certainly make sure you can have your robot travel in a figure 8 and other relatively simple tasks prior to trying to add a compass into the mix.

    I'm relatively new to robotics although I have experience with microcontrollers and many years on enterprise applications :)
    I've not tried yet 8 figues but my bot is currently able to navigate to some target relative to the starting point, avoid obstacles and, on sound command, navigate back to the starting point.
    For simplicity, I achive this by moving in straight lines and only taking 90 degrees turns. I always know my current heading (the starting point is 12 o'clock) so I know whether I move up,down,left or right comparing to the starting point.
    I think I will try the same pattern for the 8 figures.
    The problem is that the straight lines are not quite straight and the 90 deg turns are not really 90 deg, so I keep accumulating errors. That's due to the open loop design.
    I'm hoping that the compass can help me with the straight line and the 90 deg turns.

    Thanks.
    Silviu
  • GenetixGenetix Posts: 1,754
    edited 2014-04-12 09:17
    Compass modules are sensitive to magnetic fields so wiring in your house or electronics nearby can affect readings.
Sign In or Register to comment.