ProjectKarman / avionics-firmware

[DEPRECATED] Firmware for the rocket avionics
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Write Magnetometer Driver #13

Open gregwallan opened 8 years ago

gregwallan commented 8 years ago

Honeywell HMC5883L-TR

ADKaster commented 8 years ago

Progress started on branch magnetometer

ADKaster commented 8 years ago

If someone could look at the datasheet for this

http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf

And tell me where I'm missing how to convert the hex values for the data registers (X, Y, and Z) into decimal values, I'd be really grateful. Because as far as I know, it doesn't specify what exactly the vales of -2048 to 2047 actually correspond to.

So while knowing that we're pointed at, for example, (1568, 200, -987) is great, I have no idea what to do with that information to convert it into something meaningful, such as a heading.

MatanSilver commented 8 years ago

Well the conversion itself is just converting from base 16 to base 10, no? And then it looks like the three headings are just vectors of magnetic field strength in each direction, which you can convert using some trig to a theta angle for the XY plane, a phi angle for the XY/Z direction, and a magnitude. Please correct me if I am wrong haha.

--Matan Silver

On Sun, Feb 14, 2016 at 6:38 PM, RamboRambowski notifications@github.com wrote:

If someone could look at the datasheet for this

http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf

And tell me where I'm missing how to convert the hex values for the data registers (X, Y, and Z) into decimal values, I'd be really grateful. Because as far as I know, it doesn't specify what exactly the vales of -2048 to 2047 actually correspond to.

So while knowing that we're pointed at, for example, (1568, 200, -987) is great, I have no idea what to do with that information to convert it into something meaningful, such as a heading.

— Reply to this email directly or view it on GitHub https://github.com/ProjectKarman/avionics-firmware/issues/13#issuecomment-184006749 .

MatanSilver commented 8 years ago

Actually the calculations for heading might be a bit more complicated, since it is the rocket that is moving and the magnetic field that is stable. So the vector of the sum of the three vectors that they give is the vector of the magnetic field (North to South I think) relative to the rocket. So if the magnetic field vector is 90 degrees clockwise of the rocket, the rocket is 90 degrees counterclockwise of the magnetic field (so going East to West). This gets a little fiddly in 3 dimensions though, I'm not sure how it would go from there.

--Matan Silver

On Sun, Feb 14, 2016 at 7:00 PM, Matan Silver matansilver@gmail.com wrote:

Well the conversion itself is just converting from base 16 to base 10, no? And then it looks like the three headings are just vectors of magnetic field strength in each direction, which you can convert using some trig to a theta angle for the XY plane, a phi angle for the XY/Z direction, and a magnitude. Please correct me if I am wrong haha.

--Matan Silver

On Sun, Feb 14, 2016 at 6:38 PM, RamboRambowski notifications@github.com wrote:

If someone could look at the datasheet for this

http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf

And tell me where I'm missing how to convert the hex values for the data registers (X, Y, and Z) into decimal values, I'd be really grateful. Because as far as I know, it doesn't specify what exactly the vales of -2048 to 2047 actually correspond to.

So while knowing that we're pointed at, for example, (1568, 200, -987) is great, I have no idea what to do with that information to convert it into something meaningful, such as a heading.

— Reply to this email directly or view it on GitHub https://github.com/ProjectKarman/avionics-firmware/issues/13#issuecomment-184006749 .

ADKaster commented 8 years ago

So the answer is ask someone who knows how magnetometers work how to use them? Calculating theta, phi and a magnitude doesn't sound very hard, the hard part is figuring out what they mean as far as where we are and where we're pointing.

I might throw an email to one of the software engineers at my last co-op and see if he can give me some intuition as far as this stuff is concerned.

MatanSilver commented 8 years ago

Yes that sounds like a good plan, I'm sure they would give a much more detailed answer. Maybe contact the manufacturer too? There might be some details about what those values mean that only they would know.

ADKaster commented 8 years ago

Basically what we're doing is creating a homebrew Inertial measurement unit, right? Which means we're going to have our magnetometer and 3-axis accelerometer inside a gyroscope setup. And when we combine the measurements from both of these devices we'll be able to get our change in position, in the form of xyz coordinates, and our heading, as either yaw/pitch/roll or quaternions.

So from an architectural standpoint, should calculating our heading be the job of the driver, or the task? It would be really easy to just say okay, if you want to read the magnetometer, here you go have fun with these hex values. But if we wanted it to be on the driver level, we'd have to do those calculations before sending our heading back to the task for it to make decisions with.

Would it make sense to have a task dedicated specifically to the IMU?

MatanSilver commented 8 years ago

I was under the impression we didn't need an IMU because GPS just gives the location--that way we don't have to worry about the inherent inaccuracies of integrating acceleration to get velocity, and velocity to get position (where errors will accumulate very quickly, especially at such high accelerations). From what I understand it really doesn't make sense to try to make our own IMU when we already have two redundant GPS modules. I think the magnetometer driver might only be needed to keep track of heading for the sake of logging data and making sure things are in order, but I might be wrong

ADKaster commented 8 years ago

I got an email back from the software engineer I messaged, and this is what he said:

Okay I see what you want. With the magnetometer you can measure the X, Y and Z field strengths. The X, Y and Z strengths in ECR coordinates can be found by running the world magnetic model, wmm. Just google them they have free software in C for doing this calculation. For starters you cannot get the absolute orientation from the magnotometer. For example you can rotate your magnetometer about the measured field an nothing changes, just like accelerometers you cannot get yaw because rotations about the z arises doesn't affect the readings. But some things you can do with the magnotomete are calculate roll. For example if the rocket is flying mostly straight up, the z axis, the earth magnetic field is mostly parallel to the ground. The the orientation of the field in the X, Y plane, atan2( y, x ) gives the roll of the rocket with respect to magnetic north. This is often done to compensate for drift in the roll rate sensors. Because magnetometers are very hard to calibrate they are mostly used to track changes in roll and not so much absolute values.

So the World Magnetic Model is here https://www.ngdc.noaa.gov/geomag/WMM/soft.shtml

It would give us the magnetic field strength of the earth's magnetic field based on where we are (i.e lat/lon/alt). So using the magnetometer we could calculate the earth's magnetic field based on our gps coordinates, And do math on the earth's field compared to our readings to give us what our roll is.

He also told me this:

The direction of the magnetic field is a slowly changing function of position, you can probably get away with just a constant or a linear interpolation. Percompute at the beginning and end of your expected trajectory and see how constant it is.

which seems like a reasonable solution. We'd just have to change that for every test flight/launch.