DCM Code ported from Arduino for FRDM-KL25Z
Dependents: minimu_data_capture minimu_data_capture
Fork of DCM_AHRS by
Diff: LSM303.h
- Revision:
- 1:3272ece36ce1
- Parent:
- 0:dc35364e2291
--- a/LSM303.h Thu Apr 12 13:47:23 2012 +0000 +++ b/LSM303.h Mon Apr 23 14:31:08 2012 +0000 @@ -1,144 +1,144 @@ -/* mbed LSM303 Library version 0beta1 - * Copyright (c) 2012 bengo - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - #ifndef LSM303_h #define LSM303_h #include "mbed.h" -#include <vector> + +// device types + +#define LSM303DLH_DEVICE 0 +#define LSM303DLM_DEVICE 1 +#define LSM303DLHC_DEVICE 2 +#define LSM303_DEVICE_AUTO 3 + +// SA0_A states + +#define LSM303_SA0_A_LOW 0 +#define LSM303_SA0_A_HIGH 1 +#define LSM303_SA0_A_AUTO 2 + +// register addresses + +#define LSM303_CTRL_REG1_A 0x20 +#define LSM303_CTRL_REG2_A 0x21 +#define LSM303_CTRL_REG3_A 0x22 +#define LSM303_CTRL_REG4_A 0x23 +#define LSM303_CTRL_REG5_A 0x24 +#define LSM303_CTRL_REG6_A 0x25 // DLHC only +#define LSM303_HP_FILTER_RESET_A 0x25 // DLH, DLM only +#define LSM303_REFERENCE_A 0x26 +#define LSM303_STATUS_REG_A 0x27 + +#define LSM303_OUT_X_L_A 0x28 +#define LSM303_OUT_X_H_A 0x29 +#define LSM303_OUT_Y_L_A 0x2A +#define LSM303_OUT_Y_H_A 0x2B +#define LSM303_OUT_Z_L_A 0x2C +#define LSM303_OUT_Z_H_A 0x2D + +#define LSM303_FIFO_CTRL_REG_A 0x2E // DLHC only +#define LSM303_FIFO_SRC_REG_A 0x2F // DLHC only + +#define LSM303_INT1_CFG_A 0x30 +#define LSM303_INT1_SRC_A 0x31 +#define LSM303_INT1_THS_A 0x32 +#define LSM303_INT1_DURATION_A 0x33 +#define LSM303_INT2_CFG_A 0x34 +#define LSM303_INT2_SRC_A 0x35 +#define LSM303_INT2_THS_A 0x36 +#define LSM303_INT2_DURATION_A 0x37 + +#define LSM303_CLICK_CFG_A 0x38 // DLHC only +#define LSM303_CLICK_SRC_A 0x39 // DLHC only +#define LSM303_CLICK_THS_A 0x3A // DLHC only +#define LSM303_TIME_LIMIT_A 0x3B // DLHC only +#define LSM303_TIME_LATENCY_A 0x3C // DLHC only +#define LSM303_TIME_WINDOW_A 0x3D // DLHC only + +#define LSM303_CRA_REG_M 0x00 +#define LSM303_CRB_REG_M 0x01 +#define LSM303_MR_REG_M 0x02 + +#define LSM303_OUT_X_H_M 0x03 +#define LSM303_OUT_X_L_M 0x04 +#define LSM303_OUT_Y_H_M -1 // The addresses of the Y and Z magnetometer output registers +#define LSM303_OUT_Y_L_M -2 // are reversed on the DLM and DLHC relative to the DLH. +#define LSM303_OUT_Z_H_M -3 // These four defines have dummy values so the library can +#define LSM303_OUT_Z_L_M -4 // determine the correct address based on the device type. + +#define LSM303_SR_REG_M 0x09 +#define LSM303_IRA_REG_M 0x0A +#define LSM303_IRB_REG_M 0x0B +#define LSM303_IRC_REG_M 0x0C + +#define LSM303_WHO_AM_I_M 0x0F // DLM only + +#define LSM303_TEMP_OUT_H_M 0x31 // DLHC only +#define LSM303_TEMP_OUT_L_M 0x32 // DLHC only + +#define LSM303DLH_OUT_Y_H_M 0x05 +#define LSM303DLH_OUT_Y_L_M 0x06 +#define LSM303DLH_OUT_Z_H_M 0x07 +#define LSM303DLH_OUT_Z_L_M 0x08 + +#define LSM303DLM_OUT_Z_H_M 0x05 +#define LSM303DLM_OUT_Z_L_M 0x06 +#define LSM303DLM_OUT_Y_H_M 0x07 +#define LSM303DLM_OUT_Y_L_M 0x08 + +#define LSM303DLHC_OUT_Z_H_M 0x05 +#define LSM303DLHC_OUT_Z_L_M 0x06 +#define LSM303DLHC_OUT_Y_H_M 0x07 +#define LSM303DLHC_OUT_Y_L_M 0x08 + +#define MAG_ADDRESS 0x3C +#define ACC_ADDRESS_SA0_A_LOW 0x30 +#define ACC_ADDRESS_SA0_A_HIGH 0x32 + +#define M_PI 3.14159265 + +typedef unsigned char byte; class LSM303 { - public: - - /** - * Create an LSM303 object connected to the specified I2C pins - * @param sda I2C SDA pin - * @param scl I2C SCL pin - */ - LSM303( PinName sda, PinName scl ); - LSM303( void ); - - /** - * Return status code of prevoius function call - */ - inline int getStatus( void ) { - return( _status ); - } - - /** - * Read specified accelerometer register content - * @param reg register address - */ - int accRegisterRead( int reg ); + typedef struct Plane { + float x, y, z; + } Plane; + + I2C i2c; + + Plane a; // accelerometer readings + Plane m; // magnetometer readings + Plane m_max; // maximum magnetometer values, used for calibration + Plane m_min; // minimum magnetometer values, used for calibration - /** - * Write to specified accelerometer register - * @param reg register address - * @parma data data to be written - */ - void accRegisterWrite( int reg, char data ); + LSM303(PinName, PinName); - /** - * Read specified magnetometer register content - * @param reg register address - */ - int magRegisterRead( int reg ); - - /** - * Write to specified magnetometer register - * @param reg register address - * @parma data data to be written - */ - void magRegisterWrite( int reg, char data ); - - /** - * Read accelerometer vector - */ - std::vector<short> accRead( void ); + void init(byte device = LSM303_DEVICE_AUTO, byte sa0_a = LSM303_SA0_A_AUTO); - /** - * Read acceleration - */ - std::vector<float> acceleration( void ); + void enableDefault(void); - /** - * Read magnetometer vector - */ - std::vector<short> magRead( void ); - - /** - * Read magnetic field vector - */ - std::vector<float> magneticField( void ); + void writeAccReg(byte reg, byte value); + int readAccReg(byte reg); + void writeMagReg(byte reg, byte value); + int readMagReg(int reg); - // Device registers addresses - static const int ACC_CTRL_REG1; - static const int ACC_CTRL_REG2; - static const int ACC_CTRL_REC3; - static const int ACC_CTRL_REG4; - static const int ACC_CTRL_REG5; - static const int ACC_HP_FILTER_RESET; - static const int ACC_REFERENCE; - static const int ACC_STATUS_REG; - static const int ACC_OUT_X_L; - static const int ACC_OUT_X_H; - static const int ACC_OUT_Y_L; - static const int ACC_OUT_Y_H; - static const int ACC_OUT_Z_L; - static const int ACC_OUT_Z_H; - static const int ACC_INT1_CFG; - static const int ACC_INT1_SOURCE; - static const int ACC_INT1_THS; - static const int ACC_INT1_DURATION; - static const int ACC_INT2_CFG; - static const int ACC_INT2_SOURCE; - static const int ACC_INT2_THS; - static const int ACC_INT2_DURATION; - static const int MAG_CRA_REG; - static const int MAG_CRB_REG; - static const int MAG_MR_REG; - static const int MAG_OUT_X_H; - static const int MAG_OUT_X_L; - static const int MAG_OUT_Y_H; - static const int MAG_OUT_Y_L; - static const int MAG_OUT_Z_H; - static const int MAG_OUT_Z_L; - static const int MAG_SR_REG; - static const int MAG_IRA_REG; - static const int MAG_IRB_REG; - static const int MAG_IRC_REG; - static const int MAG_WHO_AM_I; + void readAcc(void); + void readMag(void); + void read(void); + + int heading(void); + int heading(Plane from); + + // Plane functions + static void vector_cross(const Plane *a, const Plane *b, Plane *out); + static float vector_dot(const Plane *a,const Plane *b); + static void vector_normalize(Plane *a); private: + byte _device; // chip type (DLH, DLM, or DLHC) + byte acc_address; - int _status; - I2C _i2c; - int _SA0Pad; - char _bytes[7]; - - static const int ACC_ADDRESS; - static const int MAG_ADDRESS; - + byte detectSA0_A(void); }; -#endif // LSM303_h \ No newline at end of file +#endif \ No newline at end of file