LSM303DLHC 3D accelerometer, 3D magnetometer and thermometer

Dependents:   MAPLEminiA MAPLEminiA

LSM303DLHC is 3D accelerometer and 3D magnetometer module

このチップのライブラリは既にいくつかあるみたいですが、どれも方位取得に特化していて加速度計だけのデータを得ることが面倒(しかもI2Cアドレスが間違ってたりする)なのと内蔵の温度計の値を得ることが出来なかったので、新たに作ってみました。
方位の計算部分は既存のライブラリの物を流用しています。

Committer:
jk1lot
Date:
Sun Jun 12 14:37:01 2016 +0000
Revision:
0:e5bf52560a0c
first published version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jk1lot 0:e5bf52560a0c 1 #ifndef LSM303DLHC_H
jk1lot 0:e5bf52560a0c 2 #define LSM303DLHC_H
jk1lot 0:e5bf52560a0c 3
jk1lot 0:e5bf52560a0c 4 #include "mbed.h"
jk1lot 0:e5bf52560a0c 5
jk1lot 0:e5bf52560a0c 6 //! LSM303DLHC 3D accelerometer and 3D magnetometer
jk1lot 0:e5bf52560a0c 7 class LSM303DLHC {
jk1lot 0:e5bf52560a0c 8 typedef struct vector {float x, y, z;} vector;
jk1lot 0:e5bf52560a0c 9 static void vector_cross(const vector *a, const vector *b, vector *out);
jk1lot 0:e5bf52560a0c 10 static float vector_dot(const vector *a,const vector *b);
jk1lot 0:e5bf52560a0c 11 static void vector_normalize(vector *a);
jk1lot 0:e5bf52560a0c 12 public:
jk1lot 0:e5bf52560a0c 13 //! @param obj pointer to I2C object
jk1lot 0:e5bf52560a0c 14 LSM303DLHC(I2C *obj);
jk1lot 0:e5bf52560a0c 15 //! measure accelerometer value
jk1lot 0:e5bf52560a0c 16 void getAccel(void) {read(ACCEL_SAD, OUT_A, _accel, 6);}
jk1lot 0:e5bf52560a0c 17 //! extract X-axis value from mesured accelerometer
jk1lot 0:e5bf52560a0c 18 int16_t accelX(void) {return (_accel[1]<<8)|_accel[0];}
jk1lot 0:e5bf52560a0c 19 //! extract Y-axis value from mesured accelerometer
jk1lot 0:e5bf52560a0c 20 int16_t accelY(void) {return (_accel[3]<<8)|_accel[2];}
jk1lot 0:e5bf52560a0c 21 //! extract Z-axis value from mesured accelerometer
jk1lot 0:e5bf52560a0c 22 int16_t accelZ(void) {return (_accel[5]<<8)|_accel[4];}
jk1lot 0:e5bf52560a0c 23
jk1lot 0:e5bf52560a0c 24 //! mesure magnetometer value
jk1lot 0:e5bf52560a0c 25 void getMagnet(void) {read(MAGNET_SAD, OUT_M, _magnet, 6);}
jk1lot 0:e5bf52560a0c 26 //accelと magnetでは Highと Lowの順序が逆になってる変な仕様
jk1lot 0:e5bf52560a0c 27 //しかもMagnetは X,Z,Y の順番
jk1lot 0:e5bf52560a0c 28 //! extract X-axis value from mesured magnetometer
jk1lot 0:e5bf52560a0c 29 int16_t magnetX(void) {return (_magnet[0]<<8)|_magnet[1];}
jk1lot 0:e5bf52560a0c 30 //! extract Z-axis value from mesured magnetometer
jk1lot 0:e5bf52560a0c 31 int16_t magnetZ(void) {return (_magnet[2]<<8)|_magnet[3];}
jk1lot 0:e5bf52560a0c 32 //! extract Y-axis value from mesured magnetometer
jk1lot 0:e5bf52560a0c 33 int16_t magnetY(void) {return (_magnet[4]<<8)|_magnet[5];}
jk1lot 0:e5bf52560a0c 34
jk1lot 0:e5bf52560a0c 35 float heading(vector from);
jk1lot 0:e5bf52560a0c 36 //! @return orientation(direction) value, expressed in degrees(0~360)
jk1lot 0:e5bf52560a0c 37 float orientation(void);
jk1lot 0:e5bf52560a0c 38 //! @return Temperature value, expressed in degrees Celsius
jk1lot 0:e5bf52560a0c 39 float temperature(void);
jk1lot 0:e5bf52560a0c 40 protected:
jk1lot 0:e5bf52560a0c 41 //! write 1byte
jk1lot 0:e5bf52560a0c 42 //! @param sad I2C address
jk1lot 0:e5bf52560a0c 43 //! @param reg register address
jk1lot 0:e5bf52560a0c 44 //! @param data data
jk1lot 0:e5bf52560a0c 45 void write(char sad, char reg, char data);
jk1lot 0:e5bf52560a0c 46 //! read data
jk1lot 0:e5bf52560a0c 47 //! @param sad I2C address
jk1lot 0:e5bf52560a0c 48 //! @param reg register address start from here and auto incriment
jk1lot 0:e5bf52560a0c 49 //! @param data pointer to data storage
jk1lot 0:e5bf52560a0c 50 //! @param length how many bytes to read
jk1lot 0:e5bf52560a0c 51 void read(char sad, char reg, char *data, int length=1);
jk1lot 0:e5bf52560a0c 52
jk1lot 0:e5bf52560a0c 53 I2C *i2c;
jk1lot 0:e5bf52560a0c 54 static const int8_t ACCEL_SAD = 0x32;
jk1lot 0:e5bf52560a0c 55 static const int8_t MAGNET_SAD = 0x3C;
jk1lot 0:e5bf52560a0c 56 enum accel_regs {
jk1lot 0:e5bf52560a0c 57 CTRL_REG1_A=0x20,
jk1lot 0:e5bf52560a0c 58 CTRL_REG2_A=0x21,
jk1lot 0:e5bf52560a0c 59 CTRL_REG3_A=0x22,
jk1lot 0:e5bf52560a0c 60 CTRL_REG4_A=0x23,
jk1lot 0:e5bf52560a0c 61 CTRL_REG5_A=0x24,
jk1lot 0:e5bf52560a0c 62 CTRL_REG6_A=0x25,
jk1lot 0:e5bf52560a0c 63 REFERENCE_A=0x26,
jk1lot 0:e5bf52560a0c 64 STATUS_REG_A=0x27,
jk1lot 0:e5bf52560a0c 65 OUT_A=0x28, //6bytes
jk1lot 0:e5bf52560a0c 66 FIFO_CTRL_REG_A=0x2E,
jk1lot 0:e5bf52560a0c 67 FIFO_SRC_REG_A=0x2F,
jk1lot 0:e5bf52560a0c 68 INT1_A=0x30, //4bytes
jk1lot 0:e5bf52560a0c 69 INT2_A=0x34, //4bytes
jk1lot 0:e5bf52560a0c 70 CLICK_A=0x38, //3bytes
jk1lot 0:e5bf52560a0c 71 TIME_LIMIT_A=0x3B,
jk1lot 0:e5bf52560a0c 72 TIME_LATENCY_A=0x3C,
jk1lot 0:e5bf52560a0c 73 TIME_WINDOW_A=0x3D
jk1lot 0:e5bf52560a0c 74 };
jk1lot 0:e5bf52560a0c 75 static const int INT_CFG=0;
jk1lot 0:e5bf52560a0c 76 static const int INT_SRC=1;
jk1lot 0:e5bf52560a0c 77 static const int INT_THS=2;
jk1lot 0:e5bf52560a0c 78 static const int INT_DURATION=3;
jk1lot 0:e5bf52560a0c 79 enum magnet_regs {
jk1lot 0:e5bf52560a0c 80 CRA_REG_M=0x00,
jk1lot 0:e5bf52560a0c 81 CRB_REG_M=0x01,
jk1lot 0:e5bf52560a0c 82 MR_REG_M=0x02,
jk1lot 0:e5bf52560a0c 83 OUT_M=0x03, //6bytes
jk1lot 0:e5bf52560a0c 84 SR_REG_M=0x09,
jk1lot 0:e5bf52560a0c 85 IRA_REG_M=0x0A,
jk1lot 0:e5bf52560a0c 86 IRB_REG_M=0x0B,
jk1lot 0:e5bf52560a0c 87 IRC_REG_M=0x0C,
jk1lot 0:e5bf52560a0c 88 TEMP_OUT_M=0x31 //2bytes
jk1lot 0:e5bf52560a0c 89 };
jk1lot 0:e5bf52560a0c 90 private:
jk1lot 0:e5bf52560a0c 91 char _accel[6];
jk1lot 0:e5bf52560a0c 92 char _magnet[6];
jk1lot 0:e5bf52560a0c 93 };
jk1lot 0:e5bf52560a0c 94
jk1lot 0:e5bf52560a0c 95 #endif