Post date: Jun 10, 2012 12:19:8 PM
We can measure the complete orientation of a static (or quasi-static) object by using a combination of a 3-axis accelerometer and a 3-axis magnetic sensor (also called a magnetometer).
Triple Axis Accelerometer - MMA7260Q SPI output ($34.95) from Sparkfun
MicroMag 3-Axis Magnetometer 0-3.3 V analog output ($54.95) from Sparkfun
MicroMag
MMA7260Q
A PIC16F88 microcontroller is used to interface with the two sensors, MicroMag and MMA7260Q. Micromag gives a digital measurement reading of a 3-axis magnetic field through a SPI interface. MMA7260 gives a 3-axis analog acceleration reading in the range of 0-3.3 volt. Both MicroMag and MMA7260 requires a 3.3 volt for power supply and all interface signals. Fortunately, the PIC16F88 can operate using a wide voltage range of 2.0-5.0 volt, thus we chose a supply voltage of 3.3 volt for the PIC16F88 to allow direct signal connection to the two sensors. LM1117, a surface-mounted 3.3-volt regulator, is used to provide the 3.3 volt supply to the circuit. The PIC16F88 sends out measurements to a computer through a serial port (RS232) by using MAX232 to convert the voltage level to a RS232 standard.
The PIC16F88 uses a 10-bit hardware module to perform analog to digital conversion (ADC) on the MMA7260Q output signals (3 analog outputs for x, y and z axes). The sensitivity of the sensor can be selected from the choice of 200, 300, 600 or 800 mV/g using pin GS1 and GS2. In our case, we chose the sensitivity to be 800 mV/g by connecting both GS1 and GS2 pins to the ground. The reading from the ADC is in the range of 0-1023, where the reading of 1024 corresponding to 3.3 volt. The SPI interface with MicroMag is implemented by the software. We don't use the hardware module on PIC16F88 for SPI because the SDO pin for SPI module is used as a RX pin for serial module. To read measurement from MicroMag, 8-bit command must be initially sent to specify axis to be read and division ratio for the output. Then 16-bit data in two's complement format (range -32,768 to 32,767) can be read as a measurement. The choice of division ratio of 2048, as in our case, results in the output sensitivity of MicroMag of 0.03 uT/count. The program for PIC16F88 is written for Hi-Tech PICC compiler. The program gives continuous text stream output through serial port in the following format: c0:xxxx c1:xxxx c2: ... c5:xxxx \rc0:xxxx ... where xxxx is the 4-digit hex number of the measurement. The numbers after c0: c1: and c2: are the measurement from MicroMag in x, y and z axis respectively. The numbers after c3: c4: and c5: are measurement from MMA7260Q in x, y and z axis respectively. After the program functionality is successfully tested on the project board, a PCB is designed and made. The sensor output can be checked using Hyperterminal. The output should look something like this. The source code for Hi-Tech PICC compiler is available here: 3DOF_Tilt_Sensor.zip
On the computer side, the program is written in C++ (Win32 console application) to read measurement from serial port and display it using OpenGL. The raw measurement from MicroMag and MMA7260Q indicates the vector direction of the magnetic field and gravity field respectively. The measurement is low-pass filtered in order to remove high frequency disturbance (for example, from shaking). The raw measurement then must be translated into a set of consecutive rotation angles in which OpenGL can understand. The mathematic derivation of the rotation angles can be found in the file: 3dof_tilt_zxz.pdf Some screenshots of the program: