Hassan Elahi / LSM303D

Dependents:   LSM303D_SPI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM303D.h Source File

LSM303D.h

00001 /** LSM303D Interface Library
00002  *
00003  * Written by Eric Coyle
00004  *
00005  * Based on Michael Shimniok's LSM303DLH library which is based on
00006  * test program by @tosihisa and 
00007  * Pololu sample library for LSM303DLH breakout by ryantm:
00008  *
00009  * Copyright (c) 2011 Pololu Corporation. For more information, see
00010  *
00011  * http://www.pololu.com/
00012  * http://forum.pololu.com/
00013  *
00014  * Permission is hereby granted, free of charge, to any person
00015  * obtaining a copy of this software and associated documentation
00016  * files (the "Software"), to deal in the Software without
00017  * restriction, including without limitation the rights to use,
00018  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00019  * copies of the Software, and to permit persons to whom the
00020  * Software is furnished to do so, subject to the following
00021  * conditions:
00022  *
00023  * The above copyright notice and this permission notice shall be
00024  * included in all copies or substantial portions of the Software.
00025  * 
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00028  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00030  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00031  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00032  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00033  * OTHER DEALINGS IN THE SOFTWARE.
00034  */
00035 #ifndef MBED_LSM303D_H
00036 #define MBED_LSM303D_H
00037  
00038 #include "mbed.h"
00039 #include "vector.h"
00040  
00041 #ifndef M_PI
00042 #define M_PI 3.14159265358979323846
00043 #endif
00044 
00045 #define FILTER_SHIFT 6      // used in filtering acceleromter readings
00046 
00047 #define XAXIS 0x00
00048 #define YAXIS 0x01
00049 #define ZAXIS 0x02
00050 
00051  
00052 class LSM303D {
00053 public:
00054     LSM303D(SPI &spi, PinName CS);
00055     int whoami();
00056     
00057     //Initializes all accelerometer and magnetometer
00058     //Sets values to defaults
00059     int initialize();
00060     
00061     //Returns raw 16-bit magnetometer values for a given axis
00062     int magnitometer(int axis);
00063     
00064     //Returns raw 16-bit magnetometer values for a given axis
00065     int accelerometer(int axis);
00066     
00067     /** sets the x, y, and z offset corrections for hard iron calibration
00068      * 
00069      * Calibration details here:
00070      *  http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
00071      *
00072      * If you gather raw magnetometer data and find, for example, x is offset
00073      * by hard iron by -20 then pass +20 to this member function to correct
00074      * for hard iron.
00075      *
00076      * @param x is the offset correction for the x axis
00077      * @param y is the offset correction for the y axis
00078      * @param z is the offset correction for the z axis
00079     */
00080     void setOffset(float x, float y, float z);
00081         
00082     /** sets the scale factor for the x, y, and z axes
00083      *
00084      * Calibratio details here:
00085      *  http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
00086      *
00087      * Sensitivity of the three axes is never perfectly identical and this
00088      * function can help to correct differences in sensitivity.  You're
00089      * supplying a multipler such that x, y and z will be normalized to the
00090      * same max/min values
00091     */
00092     void setScale(float x, float y, float z);
00093     
00094     /** read the raw accelerometer and compass values
00095      *
00096      *@param a is the accelerometer 3d vector, written by the function
00097      *@param m is the magnetometer 3d vector, written by the function
00098     **/
00099     void read(vector &a, vector &m);
00100     
00101     
00102     //returns the magnetic heading with respect to the y axis
00103     float heading(void);
00104         
00105     // returns the heading with respect to the specified vector
00106     float heading(vector from);
00107     
00108     union _data {
00109       short int raw;
00110       char b[2];
00111     } compass;
00112     
00113 private:  
00114     DigitalOut cs;
00115     SPI &mspi;
00116     float _offset_x;
00117     float _offset_y;
00118     float _offset_z;
00119     float _scale_x;
00120     float _scale_y;
00121     float _scale_z;
00122     long _filt_ax;
00123     long _filt_ay;
00124     long _filt_az;
00125 };
00126  
00127 #endif