Based on https://os.mbed.com/users/Airium/code/module_LSM303DLHC/ Main difference is the usage of the I2C - now it is passed as parameter making cooperative use easier. LSM303DLHC Full Driver: Readings For Acc, Mag and Temp; Class Method for frequently-used 13 sensor parameters; Class Method to direct modify registers; Support Calibration (offset scale);

Files at this revision

API Documentation at this revision

Comitter:
Pythia
Date:
Mon Feb 03 07:43:48 2020 +0000
Parent:
0:a4131de4bddd
Commit message:
Version working. Temperature sensor provide strange results. Is to be extended with orientation measurement.

Changed in this revision

LSM303DLHC.cpp Show annotated file Show diff for this revision Revisions of this file
LSM303DLHC.h Show annotated file Show diff for this revision Revisions of this file
diff -r a4131de4bddd -r 4ee6df2df73a LSM303DLHC.cpp
--- a/LSM303DLHC.cpp	Fri Jan 24 16:06:24 2020 +0000
+++ b/LSM303DLHC.cpp	Mon Feb 03 07:43:48 2020 +0000
@@ -40,48 +40,95 @@
     temp_scale[0] = 1.;
 }
 
-void LSM303DLHC::GetAcc(float arr[3]){
+int LSM303DLHC::GetAcc(float arr[3]){
+    int result;
     data[0] = OUT_X_L_A | (1 << 7); // MSB=1 to read multiple bytes
-    _i2c->write(ACC_ADDRESS, data, 1);
-    _i2c->read(ACC_ADDRESS, data, 6);
+    result = _i2c->write(ACC_ADDRESS, data, 1);
+    if (0 != result) 
+    {
+        printf("\nI2C write issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
+    result = _i2c->read(ACC_ADDRESS, data, 6);
+    if (0 != result) 
+    {
+        printf("\nI2C read issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
 
     int count_x = ((((int)(int8_t)data[1])<<8 | ((uint8_t)data[0])) >> 4);
     int count_y = ((((int)(int8_t)data[3])<<8 | ((uint8_t)data[2])) >> 4);
     int count_z = ((((int)(int8_t)data[5])<<8 | ((uint8_t)data[4])) >> 4);
 
-//    printf("\nA %02X.%02X %02X.%02X %02X.%02X %04x %04x %04x\n", data[1], data[0], data[3], data[2], data[5], data[4], count_x, count_y, count_z);
+    float a_x = acc_scale[0] * (acc_offset[0] + count_x * acc_scale_multiplier);
+    float a_y = acc_scale[1] * (acc_offset[1] + count_y * acc_scale_multiplier);
+    float a_z = acc_scale[2] * (acc_offset[2] + count_z * acc_scale_multiplier);
+    arr[0] = a_x;
+    arr[1] = a_y;
+    arr[2] = a_z;
 
-    arr[0] = acc_scale[0] * (acc_offset[0] + count_x * acc_scale_multiplier);
-    arr[1] = acc_scale[1] * (acc_offset[1] + count_y * acc_scale_multiplier);
-    arr[2] = acc_scale[2] * (acc_offset[2] + count_z * acc_scale_multiplier);
+//    printf("\nA %02X.%02X %02X.%02X %02X.%02X %04x %04x %04x %7.3f %7.3f %7.3f \n", data[1], data[0], data[3], data[2], data[5], data[4], count_x, count_y, count_z, a_x, a_y, a_z);
+
+    return 0;
 }
 
-void LSM303DLHC::GetMag(float arr[3]){
+int LSM303DLHC::GetMag(float arr[3]){
+    int result;
     data[0] = OUT_X_H_M;
-    _i2c->write(MAG_ADDRESS, data, 1);
-    _i2c->read(MAG_ADDRESS, data, 6);
-    
+    result = _i2c->write(MAG_ADDRESS, data, 1);
+    if (0 != result) 
+    {
+        printf("\nI2C write issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
+    result = _i2c->read(MAG_ADDRESS, data, 6);
+    if (0 != result) 
+    {
+        printf("\nI2C read issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
+
     int count_x = ((int)(int8_t)data[0])<<8 | ((uint8_t)data[1]);
     int count_y = ((int)(int8_t)data[4])<<8 | ((uint8_t)data[5]);
     int count_z = ((int)(int8_t)data[2])<<8 | ((uint8_t)data[3]);
     
-//    printf("\nM %02X.%02X %02X.%02X %02X.%02X %04x %04x %04x\n", data[1], data[0], data[3], data[2], data[5], data[4], count_x, count_y, count_z);
+    float m_x = mag_scale[0] * (mag_offset[0] + count_x * mag_scale_x_multiplier);
+    float m_y = mag_scale[1] * (mag_offset[1] + count_y * mag_scale_y_multiplier);
+    float m_z = mag_scale[2] * (mag_offset[2] + count_z * mag_scale_z_multiplier);
 
-    arr[0] = mag_scale[0] * (mag_offset[0] + count_x * mag_scale_x_multiplier);
-    arr[1] = mag_scale[1] * (mag_offset[1] + count_y * mag_scale_y_multiplier);
-    arr[2] = mag_scale[2] * (mag_offset[2] + count_z * mag_scale_z_multiplier);
+    arr[0] = m_x;
+    arr[1] = m_y;
+    arr[2] = m_z;
+
+//    printf("\nM %02X.%02X %02X.%02X %02X.%02X %04x %04x %04x %7.3f %7.3f %7.3f \n", data[1], data[0], data[3], data[2], data[5], data[4], count_x, count_y, count_z, m_x, m_y, m_z);
+
+    return 0;
 }
 
-void LSM303DLHC::GetTemp(float arr[1]){
+int LSM303DLHC::GetTemp(float arr[1]){
+    int result;
     data[0] = TEMP_OUT_H_M;
-    _i2c->write(MAG_ADDRESS, data, 1);
-    _i2c->read(MAG_ADDRESS, data, 2);
-    
+    result = _i2c->write(MAG_ADDRESS, data, 1);
+    if (0 != result) 
+    {
+        printf("\nI2C write issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
+    result = _i2c->read(MAG_ADDRESS, data, 2);
+    if (0 != result) 
+    {
+        printf("\nI2C read issue (%02x)%d\n", MAG_ADDRESS, result);
+        return result;
+    }
+
     int count = (((int)(int8_t)data[0]<<8) | ((uint8_t)data[1]))>>4;
-    
-//    printf("\nT %02X.%02X %04X %d\n", data[0], data[1], count, count);
-    
-    arr[0] = temp_scale[0] * (temp_offset[0] + count/8.);
+    float temp = count / 8.;
+
+//    printf("\nT %02X.%02X %04X %d %7.3f\n", data[0], data[1], count, count, temp);
+
+    arr[0] = temp_scale[0] * (temp_offset[0] + temp);
+
+    return 0;
 }
 
 void LSM303DLHC::ACtrl(ACC_ODR cmd){
diff -r a4131de4bddd -r 4ee6df2df73a LSM303DLHC.h
--- a/LSM303DLHC.h	Fri Jan 24 16:06:24 2020 +0000
+++ b/LSM303DLHC.h	Mon Feb 03 07:43:48 2020 +0000
@@ -132,9 +132,9 @@
 
         //// Get Data
         // please refer to the top: COMPACT USER INSTRUCTION        
-        void GetAcc(float arr[3]);  // Get acc
-        void GetMag(float arr[3]);  // Get mag
-        void GetTemp(float arr[1]); // Get temp
+        int GetAcc(float arr[3]);  // Get acc
+        int GetMag(float arr[3]);  // Get mag
+        int GetTemp(float arr[1]); // Get temp
         
         //// sensor reading calibration
         // please refer to the top: COMPACT USER INSTRUCTION