My attempt to made a more useful lib. You can get the accelerator and magnetometer.

Fork of LSM303DLH by Michael Shimniok

Committer:
salco
Date:
Sun Aug 06 00:43:57 2017 +0000
Revision:
3:9b4ff901b5c9
Parent:
2:aea5caec809c
Child:
4:4f2ed3f8726c
lsm303dlh acceleration now work, but need clean up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 1:48d83c63d1d9 1 #include "mbed.h"
shimniok 1:48d83c63d1d9 2 #include "vector.h"
salco 3:9b4ff901b5c9 3 #include "AnsiGraphicalConsole.h"
shimniok 1:48d83c63d1d9 4
shimniok 1:48d83c63d1d9 5 #ifndef M_PI
shimniok 1:48d83c63d1d9 6 #define M_PI 3.14159265358979323846
shimniok 1:48d83c63d1d9 7 #endif
shimniok 1:48d83c63d1d9 8
shimniok 1:48d83c63d1d9 9 /** Tilt-compensated compass interface Library for the STMicro LSM303DLH 3-axis magnetometer, 3-axis acceleromter
shimniok 0:de767f4959ef 10 *
shimniok 0:de767f4959ef 11 * Michael Shimniok http://bot-thoughts.com
shimniok 0:de767f4959ef 12 *
shimniok 0:de767f4959ef 13 * Based on test program by @tosihisa and
shimniok 0:de767f4959ef 14 *
shimniok 0:de767f4959ef 15 * Pololu sample library for LSM303DLH breakout by ryantm:
shimniok 0:de767f4959ef 16 *
shimniok 0:de767f4959ef 17 * Copyright (c) 2011 Pololu Corporation. For more information, see
shimniok 0:de767f4959ef 18 *
shimniok 0:de767f4959ef 19 * http://www.pololu.com/
shimniok 0:de767f4959ef 20 * http://forum.pololu.com/
shimniok 0:de767f4959ef 21 *
shimniok 0:de767f4959ef 22 * Permission is hereby granted, free of charge, to any person
shimniok 0:de767f4959ef 23 * obtaining a copy of this software and associated documentation
shimniok 0:de767f4959ef 24 * files (the "Software"), to deal in the Software without
shimniok 0:de767f4959ef 25 * restriction, including without limitation the rights to use,
shimniok 0:de767f4959ef 26 * copy, modify, merge, publish, distribute, sublicense, and/or sell
shimniok 0:de767f4959ef 27 * copies of the Software, and to permit persons to whom the
shimniok 0:de767f4959ef 28 * Software is furnished to do so, subject to the following
shimniok 0:de767f4959ef 29 * conditions:
shimniok 0:de767f4959ef 30 *
shimniok 0:de767f4959ef 31 * The above copyright notice and this permission notice shall be
shimniok 0:de767f4959ef 32 * included in all copies or substantial portions of the Software.
shimniok 0:de767f4959ef 33 *
shimniok 0:de767f4959ef 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shimniok 0:de767f4959ef 35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shimniok 0:de767f4959ef 36 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shimniok 0:de767f4959ef 37 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shimniok 0:de767f4959ef 38 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shimniok 0:de767f4959ef 39 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shimniok 0:de767f4959ef 40 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shimniok 0:de767f4959ef 41 * OTHER DEALINGS IN THE SOFTWARE.
shimniok 0:de767f4959ef 42 *
shimniok 0:de767f4959ef 43 * @code
shimniok 0:de767f4959ef 44 * #include "mbed.h"
shimniok 0:de767f4959ef 45 * #include "LSM303DLH.h"
shimniok 0:de767f4959ef 46 *
shimniok 0:de767f4959ef 47 * Serial debug(USBTX,USBRX);
shimniok 0:de767f4959ef 48 * LSM303DLH compass(p28, p27);
shimniok 0:de767f4959ef 49 *
shimniok 0:de767f4959ef 50 * int main() {
shimniok 0:de767f4959ef 51 * float hdg;
shimniok 0:de767f4959ef 52 * debug.format(8,Serial::None,1);
shimniok 0:de767f4959ef 53 * debug.baud(115200);
shimniok 0:de767f4959ef 54 * debug.printf("LSM303DLH Test\x0d\x0a");
shimniok 0:de767f4959ef 55 * compass.setOffset(29.50, -0.50, 4.00); // example calibration
shimniok 0:de767f4959ef 56 * compass.setScale(1.00, 1.03, 1.21); // example calibration
shimniok 0:de767f4959ef 57 * while(1) {
shimniok 0:de767f4959ef 58 * hdg = compass.heading();
shimniok 0:de767f4959ef 59 * debug.printf("Heading: %.2f\n", hdg);
shimniok 0:de767f4959ef 60 * wait(0.1);
shimniok 0:de767f4959ef 61 * }
shimniok 0:de767f4959ef 62 * }
shimniok 1:48d83c63d1d9 63 * @endcode
shimniok 0:de767f4959ef 64 */
shimniok 0:de767f4959ef 65 class LSM303DLH {
shimniok 0:de767f4959ef 66 public:
shimniok 0:de767f4959ef 67 /** Create a new interface for an LSM303DLH
shimniok 0:de767f4959ef 68 *
shimniok 0:de767f4959ef 69 * @param sda is the pin for the I2C SDA line
shimniok 0:de767f4959ef 70 * @param scl is the pin for the I2C SCL line
shimniok 0:de767f4959ef 71 */
shimniok 0:de767f4959ef 72 LSM303DLH(PinName sda, PinName scl);
salco 3:9b4ff901b5c9 73 LSM303DLH(I2C* ptrI2C);
shimniok 0:de767f4959ef 74 /** sets the x, y, and z offset corrections for hard iron calibration
shimniok 0:de767f4959ef 75 *
shimniok 0:de767f4959ef 76 * Calibration details here:
shimniok 0:de767f4959ef 77 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 78 *
shimniok 0:de767f4959ef 79 * If you gather raw magnetometer data and find, for example, x is offset
shimniok 0:de767f4959ef 80 * by hard iron by -20 then pass +20 to this member function to correct
shimniok 0:de767f4959ef 81 * for hard iron.
shimniok 0:de767f4959ef 82 *
shimniok 0:de767f4959ef 83 * @param x is the offset correction for the x axis
shimniok 0:de767f4959ef 84 * @param y is the offset correction for the y axis
shimniok 0:de767f4959ef 85 * @param z is the offset correction for the z axis
shimniok 0:de767f4959ef 86 */
shimniok 0:de767f4959ef 87 void setOffset(float x, float y, float z);
shimniok 0:de767f4959ef 88
shimniok 0:de767f4959ef 89 /** sets the scale factor for the x, y, and z axes
shimniok 0:de767f4959ef 90 *
shimniok 0:de767f4959ef 91 * Calibratio details here:
shimniok 0:de767f4959ef 92 * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/
shimniok 0:de767f4959ef 93 *
shimniok 0:de767f4959ef 94 * Sensitivity of the three axes is never perfectly identical and this
shimniok 0:de767f4959ef 95 * function can help to correct differences in sensitivity. You're
shimniok 0:de767f4959ef 96 * supplying a multipler such that x, y and z will be normalized to the
shimniok 0:de767f4959ef 97 * same max/min values
shimniok 0:de767f4959ef 98 */
shimniok 0:de767f4959ef 99 void setScale(float x, float y, float z);
shimniok 0:de767f4959ef 100
shimniok 0:de767f4959ef 101 /** read the raw accelerometer and compass values
shimniok 0:de767f4959ef 102 *
shimniok 0:de767f4959ef 103 * @param a is the accelerometer 3d vector, written by the function
shimniok 0:de767f4959ef 104 * @param m is the magnetometer 3d vector, written by the function
shimniok 0:de767f4959ef 105 */
salco 3:9b4ff901b5c9 106 bool read(vector &a, vector &m);
shimniok 0:de767f4959ef 107
shimniok 0:de767f4959ef 108 /** returns the magnetic heading with respect to the y axis
shimniok 0:de767f4959ef 109 *
shimniok 0:de767f4959ef 110 */
shimniok 0:de767f4959ef 111 float heading(void);
shimniok 0:de767f4959ef 112
shimniok 0:de767f4959ef 113 /** returns the heading with respect to the specified vector
shimniok 0:de767f4959ef 114 *
shimniok 0:de767f4959ef 115 */
shimniok 0:de767f4959ef 116 float heading(vector from);
shimniok 0:de767f4959ef 117
shimniok 2:aea5caec809c 118 /** sets the I2C bus frequency
shimniok 2:aea5caec809c 119 *
shimniok 2:aea5caec809c 120 * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000)
shimniok 2:aea5caec809c 121 */
shimniok 2:aea5caec809c 122 void frequency(int hz);
shimniok 2:aea5caec809c 123
shimniok 0:de767f4959ef 124 private:
salco 3:9b4ff901b5c9 125 I2C* m_ptr_I2C;//_compass;
shimniok 0:de767f4959ef 126 float _offset_x;
shimniok 0:de767f4959ef 127 float _offset_y;
shimniok 0:de767f4959ef 128 float _offset_z;
shimniok 0:de767f4959ef 129 float _scale_x;
shimniok 0:de767f4959ef 130 float _scale_y;
shimniok 0:de767f4959ef 131 float _scale_z;
shimniok 1:48d83c63d1d9 132 long _filt_ax;
shimniok 1:48d83c63d1d9 133 long _filt_ay;
shimniok 1:48d83c63d1d9 134 long _filt_az;
shimniok 1:48d83c63d1d9 135
salco 3:9b4ff901b5c9 136 void init(void) ;
shimniok 0:de767f4959ef 137 bool write_reg(int addr_i2c,int addr_reg, char v);
shimniok 0:de767f4959ef 138 bool read_reg(int addr_i2c,int addr_reg, char *v);
shimniok 0:de767f4959ef 139 bool read_reg_short(int addr_i2c,int addr_reg, short *v);
shimniok 0:de767f4959ef 140 };