3-Axis Magnetometer with 3-Axis Gimbals Experiment
Duane Degn
Posts: 10,588
As I was experimenting with a HMC5883L sensor, I was curious about using it to detect ones orientation in 3D space.
After writing some code to compute the heading and how much tilt is in the magnetic field, I made a contraption using four small servos with some Polymorph.
I previously posted a photo of this contraption in the thread about servo attachments.
I kept hoping I could get the thing to work well enough to make a video of it in action but I finally gave up on trying to get it to work well and just decided to make a video of it attempting to work.
The two bottom servos use the same signal line. Using the servos stacked like this, allows the two 180 degree servos to provide a full 360 degree range of motion. I was really pleased with how easy it was to get 360 degree motion from these servos.
The Earth's magnetic field is almost vertical here in Idaho. It's off from vertical by only 19 degrees. The vertically aligned field plays havoc on the sensor's ability to find a heading.
Here's a video with the z-axis aligned with the field (this was part of my calibration process).
You can see when the field is perpendicular to the board, the measured heading jumps around.
I'll save a couple of extra posts in case I want to add to this project.
After writing some code to compute the heading and how much tilt is in the magnetic field, I made a contraption using four small servos with some Polymorph.
I previously posted a photo of this contraption in the thread about servo attachments.
I kept hoping I could get the thing to work well enough to make a video of it in action but I finally gave up on trying to get it to work well and just decided to make a video of it attempting to work.
The two bottom servos use the same signal line. Using the servos stacked like this, allows the two 180 degree servos to provide a full 360 degree range of motion. I was really pleased with how easy it was to get 360 degree motion from these servos.
The Earth's magnetic field is almost vertical here in Idaho. It's off from vertical by only 19 degrees. The vertically aligned field plays havoc on the sensor's ability to find a heading.
Here's a video with the z-axis aligned with the field (this was part of my calibration process).
You can see when the field is perpendicular to the board, the measured heading jumps around.
I'll save a couple of extra posts in case I want to add to this project.
Comments
That's it. Now you all can tell me how cool this is.
(Photo from Robotroom.com)
I'm hoping to add position control to may encoder with PWM code. Hopefully this would allow me to treat motors with encoders as a servo.
The larger motors should allow a larger structure with hopefully enough distance between motors and sensor to allow the sensor to pick up the Earth's magnetic field better than the motor's magnetic field.
I'm interested because I'm wanting to do a 3D mouse. So for this the pitch yaw, and tilt are calculated as well as X, Y, and Z in space.
I Gordon, I think your HMC8553L demo code is somewhere buried in the program used to control the servos.
I don't think one needs an accelerometer to have a tilt compensated compass. As long as the initial tilt can be determined then the current tilt can be calculated.
The program starts out will all the servos centered and takes an initial reading. It tried to move the servos so the sensor returns to the original orientation. The heading is used to calculate how much the two yaw servos should move. The pitch servo moves the sensor so the tilt around the y axis is returned match the original tilt in this direction. The same thing happens with the roll servo. It tries to match the original x-axis tilt.
Unfortunately my 3D math skills are pretty rusty. I'm attempting to relearn how to use quaternions so I can get things like this to work correctly.
BTW, I'm willing to post the code, it's just so horribly ugly right now, I'll wait until I'm asked for it before posting it.
IOW, a user might lift the mouse off the table, position it in their arm, then press a "track" button, where all movement in 3D space is then followed. Release the button, and the tracking is frozen. The mouse can be placed back on the table, but the position is recorded and locked.
I do have a tilt compensated version of this, or similar 3-axis, compass but it's not a Parallax part. I'd like to keep in the Parallax family. (Parallax does have a 3-axis accelerometer that could be added in, but then I'd have to code the compensation, and all I can say is "ugg." I can add and subtract, but fancy math isn't my bag, baby.)
I've bee trying to imagine orientation where a 3-axis magnetometer wouldn't be able to accurately determine it's orientation and I can think of several such orientations.
Whenever one of the axes is aligned with the Earth's magnetic field, it loses its ability to determine the rotational position of the sensor in relation to the magnetic field. As I tried to show in the second video, when the z-axis is aligned with the field so the x and y components are zero (or near zero) the Propeller is unable to compute an accurate heading since there isn't a x and y component of the field to measure. This lack of ability to monitor rotation will also occur if the x or y axes are aligned with the field.
So now a agree that some additional sensor is needed.
I love Parallax as much (probably more so) than the next guy, but I also love the inexpensive parts you can get from ebay. Yesterday I received four little PCBs with HMC8552L sensors on them. These cost $1.45 each shipped! I don't understand how this is humanly possible but I love that it is.
Besides the HMC5883L sensors, I purchased some MPU6050 sensors (these cost $3.42) which have a 3-axis accelerometer and a 3-axis gyro. Someone has written some amazingly good code to use these sensors with an Arduino. It's on my todo list to translate this code to the Propeller (Spin and PASM) for now, I plan to use one of the inexpensive Arduino pro mini boards to communicate with the sensor and have the Propeller receive the filtered position data via UART (I'm stealing this idea from Fernand). The Arduino code does all the hard work of sensor fusion and just spits out yaw, pitch and roll angles. It almost seems magical the way it works so well (black magic since it's coming out of an Arduino).
I plan to test this contraption with the MPU6050 sometime soon. I'm afraid with the new sensors, I won't have anything to blame except for my math, when the servos don't move as expected.
I used to be pretty good with 3D rotations back when I was experimenting with animation (as I showed in my search for an avatar thread). Some of the discussions I've read on the forum (mainly from SLRM) has made me think I need to use quarternions in order to prevent gimbal lock situations. I'm pretty sure the MPU6050, and the Arduino code that goes with it, will output quarternions just fine. I just need to figure out what to do with a quarternion once it's in front of me (I presently have no idea how to use quaternions).
In any case, the part I have is a perfectly good one, produced by Gerry Coe over at Devantech, and I've highlighted it in many written pieces, though it's somewhat hard to find in the US. But as this is a Propeller-based project I'm working on for publication, ideally I'd like to stay with Parallax product.
I'm surprised there aren't more compass boards with tilt compensation. I know it makes for a more expensive product (either a compass with a built-in accelerometer or an outboard accelerometer and microcontroller to do the math). Or maybe someone knows they don't sell. SparkFun's has been out of stock for a while, with no backordering allowed. Pololu has a nice sale for theirs, and at $12. Not Chinese cheap, but cheap still. Jan and the others at Pololu are pretty good with support.
http://www.pololu.com/product/2124