base program for tilt measurement

Dependencies:   COG4050_ADT7420 ADXL362

Fork of COG4050_adxl355_adxl357-ver2 by ADI_CAC

Files at this revision

API Documentation at this revision

Comitter:
vtoffoli
Date:
Tue Aug 14 11:33:30 2018 +0000
Parent:
6:45d2393ef468
Child:
8:9e6ead2ee8d7
Commit message:
update 14.08.2018;

Changed in this revision

ADXL35x/ADXL355.cpp Show annotated file Show diff for this revision Revisions of this file
ADXL35x/ADXL355.h Show annotated file Show diff for this revision Revisions of this file
ADXRS290/ADXRS290.cpp Show annotated file Show diff for this revision Revisions of this file
READMEcalibration.md Show annotated file Show diff for this revision Revisions of this file
READMEconnection.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ADXL35x/ADXL355.cpp	Tue Aug 14 06:49:07 2018 +0000
+++ b/ADXL35x/ADXL355.cpp	Tue Aug 14 11:33:30 2018 +0000
@@ -6,6 +6,9 @@
 //DigitalOut int1;                ///< DigitalOut instance for the chipselect of the ADXL
 //DigitalOut int2;                ///< DigitalOut instance for the chipselect of the ADXL
 
+/** ----------------------------------- */
+/** SPI (MAX 10MHZ) and reset           */
+/** ----------------------------------- */
 ADXL355::ADXL355(PinName cs_pin, PinName MOSI, PinName MISO, PinName SCK): adxl355(MOSI, MISO, SCK), cs(cs_pin)
 {
     cs = 1;
@@ -13,15 +16,14 @@
     adxl355.lock();
     axis355_sens = 3.9e-6;
     axis357_sens = 19.5e-6;
+    calib_data.Sxx = 3.9e-6;
+    calib_data.Syy = 3.9e-6;
+    calib_data.Szz = 3.9e-6;
 }
-
-/** SPI bus frequency   */
 void ADXL355::frequency(int hz)
 {
     adxl355.frequency(hz);
 }
-
-/**  Software resets    */
 void ADXL355::reset(void)
 {
     adxl355.format(8, _SPI_MODE);
@@ -221,4 +223,18 @@
     ret_val |= static_cast<uint64_t>(y) << 24;
     ret_val |= static_cast<uint64_t>(z) ;
     return ret_val;
+    }
+
+/** ----------------------------------- */
+/** CALIBRATION AND CONVERSION          */
+/** ----------------------------------- */    
+float ADXL355::convert(uint32_t data){
+    // If a positive value, return it
+    if ((data & 0x80000) == 0)
+    {
+        return float(data);
+    }
+    //uint32_t rawValue = data<<(32-nbit);
+    // Otherwise perform the 2's complement math on the value
+    return float((~(data - 0x01)) & 0xfffff) * -1;
     }
\ No newline at end of file
--- a/ADXL35x/ADXL355.h	Tue Aug 14 06:49:07 2018 +0000
+++ b/ADXL35x/ADXL355.h	Tue Aug 14 11:33:30 2018 +0000
@@ -5,11 +5,32 @@
 class ADXL355
 {
 public: 
+    // -------------------------- //
+    // CONST AND VARIABLES        // 
+    // -------------------------- //
+    typedef struct {
+        // sensitivity
+        float Sxx;  
+        float Sxy;
+        float Sxz;
+        float Syx;
+        float Syy;
+        float Syz;
+        float Szx;
+        float Szy;
+        float Szz;
+        float St;
+        // 0g offset
+        float Bx;
+        float By;
+        float Bz;
+        float Bt;
+    } ADXL355_calib_t;   
     const static float t_sens = -9.05;    
     const static float t_bias = 1852;    
     float axis355_sens;
     float axis357_sens;
-             
+    ADXL355_calib_t calib_data;      
     // -------------------------- //
     // REGISTERS                  // 
     // -------------------------- //
@@ -115,7 +136,6 @@
         ST2 = 0x02,
         ST1 = 0x01
     } ADXL355_int_ctl_t;
-    
     // -------------------------- //
     // FUNCTIONS                  //  
     // -------------------------- //
@@ -161,8 +181,12 @@
     void fifo_setup(uint8_t nr_of_entries);
     uint32_t fifo_read_u32();
     uint64_t fifo_scan();
-    // ADXL tilt methods and calibration
-    // TBD
+    // ADXL calibration
+    float convert(uint32_t data);
+    ADXL355_calib_t convert_2p();
+    ADXL355_calib_t convert_3to8p();
+    ADXL355_calib_t convert_12p();
+    
 private:
     // SPI adxl355;                 ///< SPI instance of the ADXL
     SPI adxl355; DigitalOut cs;
--- a/ADXRS290/ADXRS290.cpp	Tue Aug 14 06:49:07 2018 +0000
+++ b/ADXRS290/ADXRS290.cpp	Tue Aug 14 11:33:30 2018 +0000
@@ -2,7 +2,9 @@
 #include "mbed.h"
 #include "ADXRS290.h"
 
-
+/** ----------------------------------- */
+/** SPI - MAX 5MHZ                      */
+/** ----------------------------------- */
 ADXRS290::ADXRS290(PinName cs_pin, PinName MOSI, PinName MISO, PinName SCK): adxrs290(MOSI, MISO, SCK), cs(cs_pin)
 {
     cs = 1;
@@ -11,8 +13,6 @@
     gyro_sens = 5e-3;
     t_sens = 0.1;
 }
-
-/** SPI bus frequency   */
 void ADXRS290::frequency(int hz)
 {
     adxrs290.frequency(hz);
@@ -21,15 +21,53 @@
 /** ----------------------------------- */
 /** Writes the reg register with data   */
 /** ----------------------------------- */
-void ADXRS290::write_reg(ADXRS290_register_t reg, uint8_t data){}
-uint8_t ADXRS290::read_reg(ADXRS290_register_t reg){}
-uint16_t ADXRS290::read_reg_u16(ADXRS290_register_t reg){}
-    
-void ADXRS290::set_power_ctl_reg(uint8_t data){}
-void ADXRS290::set_filter_ctl_reg(ADXRS290_filter_ctl_t hpf, ADXRS290_filter_ctl_t odr){}
+void ADXRS290::write_reg(ADXRS290_register_t reg, uint8_t data){
+    adxrs290.format(8, _SPI_MODE);
+    cs = false;
+    adxrs290.write(static_cast<uint8_t>(reg) | _WRITE_REG_CMD);
+    adxrs290.write(data);
+    cs = true;
+}
+/** ----------------------------------- */
+/** Reads the reg register              */
+/** ----------------------------------- */
+uint8_t ADXRS290::read_reg(ADXRS290_register_t reg){
+    uint8_t ret_val;
+    adxrs290.format(8, _SPI_MODE);
+    cs = false;
+    adxrs290.write(static_cast<uint8_t>(reg) | _READ_REG_CMD);
+    ret_val = adxrs290.write(_DUMMY_BYTE);
+    cs = true;
+    return ret_val;
+}
+uint16_t ADXRS290::read_reg_u16(ADXRS290_register_t reg){
+    uint16_t ret_val = 0;
+    adxrs290.format(8, _SPI_MODE);
+    cs = false;
+    adxrs290.write(static_cast<uint8_t>(reg) | _READ_REG_CMD);
+    ret_val = adxrs290.write(_DUMMY_BYTE);
+    ret_val = (ret_val) | (adxrs290.write(_DUMMY_BYTE)<<8);
+    cs = true;
+    return ret_val;
+}
+/** ----------------------------------- */
+/** Sets the CTL registers              */
+/** ----------------------------------- */    
+void ADXRS290::set_power_ctl_reg(uint8_t data){
+    write_reg(POWER_CTL, data);
+    wait(0.1);
+}
+void ADXRS290::set_filter_ctl_reg(ADXRS290_filter_ctl_t hpf, ADXRS290_filter_ctl_t lpf){
+    write_reg(FILTER, hpf|lpf);
+}
 void ADXRS290::set_sync(ADXRS290_dataready_ctl_t data){}
     
-uint16_t ADXRS290::scanx(){}
-uint16_t ADXRS290::scany(){}
-uint16_t ADXRS290::scant(){}
-    
\ No newline at end of file
+uint16_t ADXRS290::scanx(){    
+    return read_reg_u16(DATAX0);
+}
+uint16_t ADXRS290::scany(){
+    return read_reg_u16(DATAY0);
+}
+uint16_t ADXRS290::scant(){
+    return read_reg_u16(TEMP0);
+}    
\ No newline at end of file
--- a/READMEcalibration.md	Tue Aug 14 06:49:07 2018 +0000
+++ b/READMEcalibration.md	Tue Aug 14 11:33:30 2018 +0000
@@ -16,8 +16,7 @@
 We assumed that pij=0 when i!=j
 
 
-3 MEASUREMENT ORIENTATIONS
-4 MEASUREMENT ORIENTATIONS
-6 MEASUREMENT ORIENTATIONS
-8 MEASUREMENT ORIENTATIONS
+3to8 MEASUREMENT ORIENTATIONS
+
+
 12 MEASUREMENT ORIENTATIONS
\ No newline at end of file
--- a/READMEconnection.md	Tue Aug 14 06:49:07 2018 +0000
+++ b/READMEconnection.md	Tue Aug 14 11:33:30 2018 +0000
@@ -1,5 +1,6 @@
+// https://wiki.analog.com/resources/eval/user-guides/ev-gear-expander1z
 
-ADxl35x on the EV-GEAR-EXPANDER1Z
+ADXL35x on the EV-GEAR-EXPANDER1Z
 CONNECTION DIAGRAM through P4 PMOD_SP1
 
 SIGNAL_NAME     PIN NUM     EVAL-ADXL35xZ
@@ -12,3 +13,17 @@
 FT_EXT_VDD_OUT  PMOD-P4 12  P1 3
 
 
+ADXRS290 on the EV-GEAR-EXPANDER1Z
+CONNECTION DIAGRAM through A5 ARDUINO UNO COMPATIBLE 
+
+SIGNAL_NAME     PIN NUM     EVAL-ADXL35xZ
+SPI0_CS2        A5 8        P2 5
+SPI0_MOSI       A5 7        P2 3
+SPI0_MISO       A5 6        P2 4
+SPI0_CLK        A5 5        P2 2
+SYNC                        P2 1
+GND             A3 6        P1 3
+FT_EXT_VDD_OUT  A3 2        P1 1
+FT_EXT_VDD_OUT  A3 4        P1 2
+PDMX                        P1 4
+PDMY                        P1 5
\ No newline at end of file
--- a/main.cpp	Tue Aug 14 06:49:07 2018 +0000
+++ b/main.cpp	Tue Aug 14 11:33:30 2018 +0000
@@ -1,22 +1,13 @@
 #include "mbed.h"
 #include <inttypes.h>
 #include "ADXL355.h"
+#include "ADXRS290.h"
  
 Serial pc(USBTX, USBRX);
  
-ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK); // PMOD port
-
+ADXL355 accl(SPI1_CS0, SPI1_MOSI, SPI1_MISO, SPI1_SCLK);    // PMOD port
+ADXRS290 gyro(SPI0_CS2, SPI0_MOSI, SPI0_MISO, SPI0_SCLK);   // PMOD port
  
-float convert(uint32_t data){
-    // If a positive value, return it
-    if ((data & 0x80000) == 0)
-    {
-        return float(data);
-    }
-    //uint32_t rawValue = data<<(32-nbit);
-    // Otherwise perform the 2's complement math on the value
-    return float((~(data - 0x01)) & 0xfffff) * -1;
-    }
     
 int main(){
     pc.baud(9600);
@@ -24,27 +15,28 @@
     pc.printf("GET device ID\n");
     accl.reset();
     uint8_t d; 
-        d=accl.read_reg(accl.DEVID_AD);
-        pc.printf("AD id = %x \r\n",d);
-        d=accl.read_reg(accl.DEVID_MST);
-        pc.printf("MEMS id = %x \r\n",d);
-        d=accl.read_reg(accl.PARTID);
-        pc.printf("device id = %x \r\n",d);
-        d=accl.read_reg(accl.REVID);
-        pc.printf("revision id = %x \r\n",d);
+    d=accl.read_reg(accl.DEVID_AD);
+    pc.printf("AD id = %x \r\n",d);
+    d=accl.read_reg(accl.DEVID_MST);
+    pc.printf("MEMS id = %x \r\n",d);
+    d=accl.read_reg(accl.PARTID);
+    pc.printf("device id = %x \r\n",d);
+    d=accl.read_reg(accl.REVID);
+    pc.printf("revision id = %x \r\n",d);
     pc.printf("GET device data [x, y, z, t] \r\n");
     accl.set_power_ctl_reg(accl.MEASUREMENT);
     d=accl.read_reg(accl.POWER_CTL);
     pc.printf("power control on measurement mode = %x \r\n",d);
     float x, y,z;
     float t;
-    while(1) {
-        x = convert(accl.scanx())*accl.axis355_sens;
-        y = convert(accl.scany())*accl.axis355_sens;
-        z = convert(accl.scanz())*accl.axis355_sens;
+    // save data info a file
+    while(1000) {
+        x = accl.convert(accl.scanx())*accl.axis355_sens;
+        y = accl.convert(accl.scany())*accl.axis355_sens;
+        z = accl.convert(accl.scanz())*accl.axis355_sens;
         t = 25+float(accl.scant()-1852)/(-9.05);
         pc.printf("%f \t %f \t %f  \t %f \r\n" , x,y,z,t);
-        wait(0.50);
+        wait(0.1);
   }
 }
  
\ No newline at end of file