LSM303DLHC library with magneometer calibration support
Fork of LSM303DLHC by
LSM303DLHC.cpp
00001 /** LSM303DLHC Interface Library 00002 * 00003 * base on code by Michael Shimniok http://bot-thoughts.com 00004 * 00005 * and on test program by @tosihisa and 00006 * 00007 * and on Pololu sample library for LSM303DLHC 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 #include "mbed.h" 00036 #include "LSM303DLHC.h" 00037 00038 00039 const int addr_acc = 0x32; 00040 const int addr_mag = 0x3c; 00041 00042 enum REG_ADDRS { 00043 /* --- Mag --- */ 00044 CRA_REG_M = 0x00, 00045 CRB_REG_M = 0x01, 00046 MR_REG_M = 0x02, 00047 OUT_X_M = 0x03, 00048 OUT_Y_M = 0x05, 00049 OUT_Z_M = 0x07, 00050 /* --- Acc --- */ 00051 CTRL_REG1_A = 0x20, 00052 CTRL_REG4_A = 0x23, 00053 OUT_X_A = 0x28, 00054 OUT_Y_A = 0x2A, 00055 OUT_Z_A = 0x2C, 00056 }; 00057 00058 bool LSM303DLHC::write_reg(int addr_i2c,int addr_reg, char v) 00059 { 00060 char data[2] = {addr_reg, v}; 00061 return LSM303DLHC::_LSM303.write(addr_i2c, data, 2) == 0; 00062 } 00063 00064 bool LSM303DLHC::read_reg(int addr_i2c,int addr_reg, char *v) 00065 { 00066 char data = addr_reg; 00067 bool result = false; 00068 00069 __disable_irq(); 00070 if ((_LSM303.write(addr_i2c, &data, 1) == 0) && (_LSM303.read(addr_i2c, &data, 1) == 0)){ 00071 *v = data; 00072 result = true; 00073 } 00074 __enable_irq(); 00075 return result; 00076 } 00077 00078 00079 LSM303DLHC::LSM303DLHC(PinName sda, PinName scl): 00080 _LSM303(sda, scl) 00081 { 00082 char reg_v; 00083 _LSM303.frequency(100000); 00084 00085 reg_v = 0; 00086 00087 reg_v |= 0x27; /* X/Y/Z axis enable. */ 00088 write_reg(addr_acc,CTRL_REG1_A,reg_v); 00089 00090 reg_v = 0; 00091 // reg_v |= 0x01 << 6; /* 1: data MSB @ lower address */ 00092 reg_v = 0x01 << 4; /* +/- 4g */ 00093 write_reg(addr_acc,CTRL_REG4_A,reg_v); 00094 00095 /* -- mag --- */ 00096 reg_v = 0; 00097 reg_v |= 0x04 << 2; /* Minimum data output rate = 15Hz */ 00098 write_reg(addr_mag,CRA_REG_M,reg_v); 00099 00100 reg_v = 0; 00101 reg_v |= 0x01 << 5; /* +-1.3Gauss */ 00102 //reg_v |= 0x07 << 5; /* +-8.1Gauss */ 00103 write_reg(addr_mag,CRB_REG_M,reg_v); 00104 00105 reg_v = 0; /* Continuous-conversion mode */ 00106 write_reg(addr_mag,MR_REG_M,reg_v); 00107 } 00108 00109 00110 static float magn_ellipsoid_center_x = -0.0446786; 00111 static float magn_ellipsoid_center_y = 0.0251516; 00112 static float magn_ellipsoid_center_z = -0.44229; 00113 00114 static float magn_ellipsoid_transform_0_x = 0.978841; 00115 static float magn_ellipsoid_transform_0_y = 0.0078251; 00116 static float magn_ellipsoid_transform_0_z = 0.0120719; 00117 static float magn_ellipsoid_transform_1_x = 0.0078251; 00118 static float magn_ellipsoid_transform_1_y = 0.996114; 00119 static float magn_ellipsoid_transform_1_z = 0.00302665; 00120 static float magn_ellipsoid_transform_2_x = 0.0120719; 00121 static float magn_ellipsoid_transform_2_y = 0.00302665; 00122 static float magn_ellipsoid_transform_2_z = 0.936561; 00123 bool LSM303DLHC::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) { 00124 char acc[6], mag[6]; 00125 00126 if (recv(addr_acc, OUT_X_A, acc, 6) && recv(addr_mag, OUT_X_M, mag, 6)) { 00127 *ax = float(short(acc[1] << 8 | acc[0]))/8192; //32768/4=8192 00128 *ay = float(short(acc[3] << 8 | acc[2]))/8192; 00129 *az = float(short(acc[5] << 8 | acc[4]))/8192; 00130 //full scale magnetic readings are from -2048 to 2047 00131 00132 //gain is x,y = 1100; z = 980 LSB/gauss 00133 float mx_tmp = float(short(mag[0] << 8 | mag[1]))/1100 - magn_ellipsoid_center_x; 00134 float my_tmp = float(short(mag[2] << 8 | mag[3]))/1100 - magn_ellipsoid_center_y; 00135 float mz_tmp = float(short(mag[4] << 8 | mag[5]))/980 - magn_ellipsoid_center_z; 00136 *mx = magn_ellipsoid_transform_0_x * mx_tmp + magn_ellipsoid_transform_0_y * my_tmp + magn_ellipsoid_transform_0_z * mz_tmp - 0.0791235566; 00137 *my = magn_ellipsoid_transform_1_x * mx_tmp + magn_ellipsoid_transform_1_y * my_tmp + magn_ellipsoid_transform_1_z * mz_tmp + 0.8915525079; 00138 *mz = magn_ellipsoid_transform_2_x * mx_tmp + magn_ellipsoid_transform_2_y * my_tmp + magn_ellipsoid_transform_2_z * mz_tmp + 0.5963887572; 00139 00140 return true; 00141 } 00142 00143 return false; 00144 } 00145 00146 00147 bool LSM303DLHC::recv(char sad, char sub, char *buf, int length) { 00148 if (length > 1) sub |= 0x80; 00149 00150 return _LSM303.write(sad, &sub, 1, true) == 0 && _LSM303.read(sad, buf, length) == 0; 00151 }
Generated on Wed Jul 13 2022 14:48:20 by 1.7.2