BS2 & HMC5883L Code
sylveeski
Posts: 17
Hi,
I am trying to calibrate an HMC5883L compass with my BS2. I uploaded the sample Kickstarter code and am getting a constant unchanging output. No matter which way I tilt the compass, my debug window shows:
X= - 4096
Y= - 4096
Z = - 4096
I don't know what it could be. The only wiring I did was connecting the Vin, Gnd, SDA & SDL so I don't know what it could be.
Any ideas or suggestions?
Thanks!
I am trying to calibrate an HMC5883L compass with my BS2. I uploaded the sample Kickstarter code and am getting a constant unchanging output. No matter which way I tilt the compass, my debug window shows:
X= - 4096
Y= - 4096
Z = - 4096
I don't know what it could be. The only wiring I did was connecting the Vin, Gnd, SDA & SDL so I don't know what it could be.
Any ideas or suggestions?
Thanks!
Comments
To assist Forum members to formulate an answer, we'll need you to:
1 - post the code you're using
2 - post a schematic or image of the connections between the BS2 and compass
The Compass Module is also very sensitive to magnetic fields such as electrical wiring in the wall. If you still get the same reading try moving it away from anything electrical or electronic.
This is what it looks like connected to my basic stamp:
And the code is here:
http://learn.parallax.com/KickStart/29133
I tried using longer wires to get the compass farther away from my breadboard. I walked around my whole house with it trying to get it to say something else but the readings now are always -1,-1,-1Sylvee
' {$STAMP BS2}
' {$PBASIC 2.5}
SDA PIN 0 ' SDA of gyro to pin P0
SCL PIN 1 ' SCL of gyro to pin P1
WRITE_Data CON $3C ' Requests Write operation
READ_Data CON $3D ' Requests Read operation
MODE CON $02 ' Mode setting register
X_MSB CON $03 ' X MSB data output register
X VAR Word
Y VAR Word
Z VAR Word
rawl VAR Word
rawh VAR Word
' Variables for I2C communications
I2C_DATA VAR Byte
I2C_LSB VAR Bit
I2C_REG VAR Byte
I2C_VAL VAR Byte
PAUSE 100 ' Power up delay
I2C_REG = MODE ' Set operating mode to continuous
I2C_VAL = $0
GOSUB I2C_Write_Reg
DO
GOSUB GetRawReading ' Get raw Compass reading
DEBUG HOME, "X = ",11, SDEC x, CR ' Print values
DEBUG "Y = ",11, SDEC y, CR
DEBUG "Z = ",11, SDEC z, CR
DEBUG CR
LOOP
GetRawReading:
PAUSE 400 ' Wait for new data
' Send request to X MSB register
GOSUB I2C_Start
I2C_DATA = WRITE_Data
GOSUB I2C_Write
I2C_DATA = X_MSB
GOSUB I2C_Write
GOSUB I2C_Stop
'Get data from register (6 bytes total, 2 bytes per axis)
GOSUB I2C_Start
I2C_DATA = READ_Data
GOSUB I2C_Write
' Get X
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
X = (rawH << 8) | rawL
' Get Z
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
Z = (rawH << 8) | rawL
' Get Y
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_NACK
Y = (rawH << 8) | rawL
GOSUB I2C_Stop
RETURN
'
I2C functions
' Set I2C_REG & I2C_VAL before calling this
I2C_Write_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
I2C_DATA = I2C_VAL
GOSUB I2C_Write
GOSUB I2C_Stop
RETURN
' Set I2C_REG before calling this, I2C_DATA will have result
I2C_Read_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
GOSUB I2C_Stop
GOSUB I2C_Start
I2C_DATA = READ_DATA
GOSUB I2C_Write
GOSUB I2C_Read
GOSUB I2C_NACK
GOSUB I2C_Stop
RETURN
I2C_Start:
LOW SDA
LOW SCL
RETURN
I2C_Stop:
LOW SDA
INPUT SCL
INPUT SDA
RETURN
I2C_ACK:
LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
RETURN
I2C_NACK:
INPUT SDA
INPUT SCL
LOW SCL
RETURN
I2C_Read:
SHIFTIN SDA, SCL, MSBPRE, [I2C_DATA]
RETURN
I2C_Write:
I2C_LSB = I2C_DATA.BIT0
I2C_DATA = I2C_DATA / 2
SHIFTOUT SDA, SCL, MSBFIRST, [I2C_DATA\7]
IF I2C_LSB THEN INPUT SDA ELSE LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
INPUT SCL
LOW SCL
RETURN
I did those things and here's a picture of what my wiring looks like now.
I also went to Fry's and bought another compass and it's doing the exact same thing. The readings are always X=-4096, Y=-4096, Z=-4096.
Any ideas anyone? I really need this to work so I can use the compass for my project.
Thanks,
Sylvee
Are you sure your battery is good?
What about changing the IO pins you're using? Is is possible the Stamp is damaged?
One last thing. Move the compass to a different area of the breadboard and use different wires (it's worked for me more than once).
I appreciate your help!
That sure looks like it should work.
Are you sure your battery is good?
Good call, Duane! Maybe that should be our #1 question for anyone using a board that can take 9 volt batteries.
So basically, I don't care about the z-axis, but want to use x and y to determine a degree between 0 and 360. I tried using the ATN command in basic stamp using the X and Y raw values, but that doesn't seem to correlate with anything.
Some info: The x and y raw values I am getting are between ~ -250 to +250
I want to be able to pick up this whole contraption and have the compass give the same degree readings at different points along the lazy Susan. Should I add a magnet or something onto the contraption so it is calibrated to that? If it's calibrated to the Earth's magnetic field, I will have to re-orient the lazy Susan everytime I move it.
I read the application notes for the HNC5883L, but am still not fully understanding which equations to use to calibrate the compass. For your info, I'm in the Cupertino, CA so do I use the known Earth coordinates of this location when calibrating?
Any suggestions would be greatly appreciated!
Sylvee
If that's not possible, that's fine. I can make sure that I always set the contraption the same way and align it facing earth north. I can use my iphone compass or something to make sure it's facing the right way.
But I would prefer for it to just be calibrated to the contraption instead of earth.... What do you think?
In post 17 you first state that you want the "contraption" to sense where a point on the turntable is pointing (at). Then further on you state "I want to be able to pick up this whole contraption and have the compass give the same degree readings at different points along the lazy Susan."
To me, the second statement sounds like you want the compass to report the same readings no matter which way it is pointing. If that is correct, what is the compass for?
Many years ago I helped a friend move a boat he purchased from Florida to Ohio. When I got on board the owner told the magnetic compass was not working. It always pointed somewhere North-East. One quick look at his console told me why. He had installed a weatherproof loudspeaker about an inch from the compass! That compass and the big magnet on the speaker had a very intimate relationship.
The point is, placing a magnet on the lazy Susan which also holds the compass will perform the same job as my friend's compass.
I want the compass to sense where the arrow is pointing along the turntable. Then, eventually I will blow the fans to move the arrow to a desired location along the circle. Does this make sense?
+1.
I was just going to post that solution.
There are many examples of simple 1st and 2nd order systems that can be combined together in actuator-plant-sensor combinations in order to obtain fourth order (or higher) systems. Note that for mechanical motion, position dynamics are generally 2nd order and speed control is generally 1st order. A 4th order system can be built by having a second order actuator control a 2nd order plant, or perhaps controlling a 2nd order plant with a first order sensor and actuator. You will be shown videos of previous projects in order to get a sense of typical systems for this assignment. Example systems include: positioning a cart (a 2nd order mass) along a beam in order to balance the beam (a 2nd order inertia); positioning a cart to balance an inverted pendulum (another 2nd order and unstable plant); positioning a motor in order to tilt a beam and therefore position a ball on that beam; speed controlling fans to rotate a lazy susan platform to a specific position and using a slow compass to determine position; and so on. Note that you are responsible for 4 orders of control; if you buy a hobby servo that already has built-in position control (such that you don't need to design/tune it), then you do not get credit for implementing those two orders - I will only give you cridt for 1 order for such an actuator. Each project will be reviewed by the instructor to assess the true level of student-implemented system order. There are many other great ideas, so brainstorm with the instructor and your partner.
So what do you think he meant by usage of a compass then?
The first post sounded as you where using the compass in a normal operation.
What is a slow compass?
No suggestions here. You are using the device in a situation against from it's intended use.