HOW DO I USE THE Compass Module 3-Axis HMC5883L ON MY BS2 BOE BOT
atari132
Posts: 1
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.
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
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.
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?
/ Adam
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.
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).
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.
Here's the output with the sensor somewhat level and aligned with magnetic east.
Here's the output with the sensor somewhat level and aligned with magnetic south.
Here's the output with the sensor somewhat level and aligned with magnetic west.
Here's the output with the sensor originally aligned with magnetic north but then tilted to point straight up.
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).
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.
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?
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.
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
Both of us copied this flipped x-axis in our earlier posts.
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.
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.
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 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.
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
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.
I will insist a bit more, I will try multipe reading as well. Thanks for the support
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