reading from 6 sensors and packaging the data

Dependents:   LSM303D_SPI

Committer:
hassan_elahi
Date:
Mon Mar 23 19:18:57 2015 +0000
Revision:
2:171076b97de0
Parent:
0:f186dd92c836
asfdsd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DrCoyle 0:f186dd92c836 1 /** LSM303D Interface Library
DrCoyle 0:f186dd92c836 2 *
DrCoyle 0:f186dd92c836 3 * Written by Eric Coyle
DrCoyle 0:f186dd92c836 4 *
DrCoyle 0:f186dd92c836 5 * Based on Michael Shimniok's LSM303DLH library which is based on
DrCoyle 0:f186dd92c836 6 * test program by @tosihisa and
DrCoyle 0:f186dd92c836 7 * Pololu sample library for LSM303DLH breakout by ryantm:
DrCoyle 0:f186dd92c836 8 *
DrCoyle 0:f186dd92c836 9 * Copyright (c) 2011 Pololu Corporation. For more information, see
DrCoyle 0:f186dd92c836 10 *
DrCoyle 0:f186dd92c836 11 * http://www.pololu.com/
DrCoyle 0:f186dd92c836 12 * http://forum.pololu.com/
DrCoyle 0:f186dd92c836 13 *
DrCoyle 0:f186dd92c836 14 * Permission is hereby granted, free of charge, to any person
DrCoyle 0:f186dd92c836 15 * obtaining a copy of this software and associated documentation
DrCoyle 0:f186dd92c836 16 * files (the "Software"), to deal in the Software without
DrCoyle 0:f186dd92c836 17 * restriction, including without limitation the rights to use,
DrCoyle 0:f186dd92c836 18 * copy, modify, merge, publish, distribute, sublicense, and/or sell
DrCoyle 0:f186dd92c836 19 * copies of the Software, and to permit persons to whom the
DrCoyle 0:f186dd92c836 20 * Software is furnished to do so, subject to the following
DrCoyle 0:f186dd92c836 21 * conditions:
DrCoyle 0:f186dd92c836 22 *
DrCoyle 0:f186dd92c836 23 * The above copyright notice and this permission notice shall be
DrCoyle 0:f186dd92c836 24 * included in all copies or substantial portions of the Software.
DrCoyle 0:f186dd92c836 25 *
DrCoyle 0:f186dd92c836 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
DrCoyle 0:f186dd92c836 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
DrCoyle 0:f186dd92c836 28 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DrCoyle 0:f186dd92c836 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
DrCoyle 0:f186dd92c836 30 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
DrCoyle 0:f186dd92c836 31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
DrCoyle 0:f186dd92c836 32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
DrCoyle 0:f186dd92c836 33 * OTHER DEALINGS IN THE SOFTWARE.
DrCoyle 0:f186dd92c836 34 */
DrCoyle 0:f186dd92c836 35 #ifndef MBED_LSM303D_H
DrCoyle 0:f186dd92c836 36 #define MBED_LSM303D_H
DrCoyle 0:f186dd92c836 37
DrCoyle 0:f186dd92c836 38 #include "mbed.h"
DrCoyle 0:f186dd92c836 39 #include "vector.h"
DrCoyle 0:f186dd92c836 40
DrCoyle 0:f186dd92c836 41 #ifndef M_PI
DrCoyle 0:f186dd92c836 42 #define M_PI 3.14159265358979323846
DrCoyle 0:f186dd92c836 43 #endif
DrCoyle 0:f186dd92c836 44
DrCoyle 0:f186dd92c836 45 #define FILTER_SHIFT 6 // used in filtering acceleromter readings
DrCoyle 0:f186dd92c836 46
DrCoyle 0:f186dd92c836 47 #define XAXIS 0x00
DrCoyle 0:f186dd92c836 48 #define YAXIS 0x01
DrCoyle 0:f186dd92c836 49 #define ZAXIS 0x02
DrCoyle 0:f186dd92c836 50
DrCoyle 0:f186dd92c836 51
DrCoyle 0:f186dd92c836 52 class LSM303D {
DrCoyle 0:f186dd92c836 53 public:
DrCoyle 0:f186dd92c836 54 LSM303D(SPI &spi, PinName CS);
DrCoyle 0:f186dd92c836 55 int whoami();
DrCoyle 0:f186dd92c836 56
DrCoyle 0:f186dd92c836 57 //Initializes all accelerometer and magnetometer
DrCoyle 0:f186dd92c836 58 //Sets values to defaults
DrCoyle 0:f186dd92c836 59 int initialize();
DrCoyle 0:f186dd92c836 60
DrCoyle 0:f186dd92c836 61 //Returns raw 16-bit magnetometer values for a given axis
DrCoyle 0:f186dd92c836 62 int magnitometer(int axis);
DrCoyle 0:f186dd92c836 63
DrCoyle 0:f186dd92c836 64 //Returns raw 16-bit magnetometer values for a given axis
DrCoyle 0:f186dd92c836 65 int accelerometer(int axis);
DrCoyle 0:f186dd92c836 66
DrCoyle 0:f186dd92c836 67 /** sets the x, y, and z offset corrections for hard iron calibration
DrCoyle 0:f186dd92c836 68 *
DrCoyle 0:f186dd92c836 69 * Calibration details here:
DrCoyle 0:f186dd92c836 70 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
DrCoyle 0:f186dd92c836 71 *
DrCoyle 0:f186dd92c836 72 * If you gather raw magnetometer data and find, for example, x is offset
DrCoyle 0:f186dd92c836 73 * by hard iron by -20 then pass +20 to this member function to correct
DrCoyle 0:f186dd92c836 74 * for hard iron.
DrCoyle 0:f186dd92c836 75 *
DrCoyle 0:f186dd92c836 76 * @param x is the offset correction for the x axis
DrCoyle 0:f186dd92c836 77 * @param y is the offset correction for the y axis
DrCoyle 0:f186dd92c836 78 * @param z is the offset correction for the z axis
DrCoyle 0:f186dd92c836 79 */
DrCoyle 0:f186dd92c836 80 void setOffset(float x, float y, float z);
DrCoyle 0:f186dd92c836 81
DrCoyle 0:f186dd92c836 82 /** sets the scale factor for the x, y, and z axes
DrCoyle 0:f186dd92c836 83 *
DrCoyle 0:f186dd92c836 84 * Calibratio details here:
DrCoyle 0:f186dd92c836 85 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
DrCoyle 0:f186dd92c836 86 *
DrCoyle 0:f186dd92c836 87 * Sensitivity of the three axes is never perfectly identical and this
DrCoyle 0:f186dd92c836 88 * function can help to correct differences in sensitivity. You're
DrCoyle 0:f186dd92c836 89 * supplying a multipler such that x, y and z will be normalized to the
DrCoyle 0:f186dd92c836 90 * same max/min values
DrCoyle 0:f186dd92c836 91 */
DrCoyle 0:f186dd92c836 92 void setScale(float x, float y, float z);
DrCoyle 0:f186dd92c836 93
DrCoyle 0:f186dd92c836 94 /** read the raw accelerometer and compass values
DrCoyle 0:f186dd92c836 95 *
DrCoyle 0:f186dd92c836 96 *@param a is the accelerometer 3d vector, written by the function
DrCoyle 0:f186dd92c836 97 *@param m is the magnetometer 3d vector, written by the function
DrCoyle 0:f186dd92c836 98 **/
DrCoyle 0:f186dd92c836 99 void read(vector &a, vector &m);
DrCoyle 0:f186dd92c836 100
DrCoyle 0:f186dd92c836 101
DrCoyle 0:f186dd92c836 102 //returns the magnetic heading with respect to the y axis
DrCoyle 0:f186dd92c836 103 float heading(void);
DrCoyle 0:f186dd92c836 104
DrCoyle 0:f186dd92c836 105 // returns the heading with respect to the specified vector
DrCoyle 0:f186dd92c836 106 float heading(vector from);
DrCoyle 0:f186dd92c836 107
DrCoyle 0:f186dd92c836 108 union _data {
DrCoyle 0:f186dd92c836 109 short int raw;
DrCoyle 0:f186dd92c836 110 char b[2];
DrCoyle 0:f186dd92c836 111 } compass;
DrCoyle 0:f186dd92c836 112
DrCoyle 0:f186dd92c836 113 private:
DrCoyle 0:f186dd92c836 114 DigitalOut cs;
DrCoyle 0:f186dd92c836 115 SPI &mspi;
DrCoyle 0:f186dd92c836 116 float _offset_x;
DrCoyle 0:f186dd92c836 117 float _offset_y;
DrCoyle 0:f186dd92c836 118 float _offset_z;
DrCoyle 0:f186dd92c836 119 float _scale_x;
DrCoyle 0:f186dd92c836 120 float _scale_y;
DrCoyle 0:f186dd92c836 121 float _scale_z;
DrCoyle 0:f186dd92c836 122 long _filt_ax;
DrCoyle 0:f186dd92c836 123 long _filt_ay;
DrCoyle 0:f186dd92c836 124 long _filt_az;
DrCoyle 0:f186dd92c836 125 };
DrCoyle 0:f186dd92c836 126
DrCoyle 0:f186dd92c836 127 #endif