Momo-Medical / Mbed 2 deprecated SP_BEUNBOX_code

Dependencies:   mbed ADS1015_fast KXTJ3

Revision:
8:00b7a8cbd6ef
Parent:
7:d5e1c7c12a26
Child:
9:ea6536825a29
--- a/Sensorplate/main.cpp	Mon Aug 27 14:38:32 2018 +0000
+++ b/Sensorplate/main.cpp	Wed Aug 29 07:19:30 2018 +0000
@@ -3,7 +3,7 @@
 #include "USBSerial.h"
 
 #define SERIAL_BAUD_RATE    115200
-#define ADC_LIMIT           2048
+#define ADC_MAX_VALUE       2048
 #define DYNAMIC_SCALE       0.6f
 // READOUT_FREQ should work up to around 200 I think
 #define READOUT_FREQ        120
@@ -12,17 +12,17 @@
 DigitalOut buz1(p21);
 DigitalOut buz2(p22);
 I2C i2c(p28, p27);
-Adafruit_ADS1115 pr1(&i2c, 0x48);   // first PiëzoResistive ADC
-Adafruit_ADS1115 pr2(&i2c, 0x49);   // second PiëzoResistive ADC
-Adafruit_ADS1115 ads0(&i2c, 0x4B);
-Adafruit_ADS1115 ads1(&i2c, 0x4A);
+Adafruit_ADS1115 piezo_resistive_adc1(&i2c, 0x48);   // first PiëzoResistive ADC
+Adafruit_ADS1115 piezo_resistive_adc2(&i2c, 0x49);   // second PiëzoResistive ADC
+Adafruit_ADS1115 piezo_electric_adc(&i2c, 0x4B);
+Adafruit_ADS1115 piezo_electric_adc2(&i2c, 0x4A);
 adsGain_t pga_table[]= {GAIN_SIXTEEN,GAIN_EIGHT,GAIN_FOUR,GAIN_TWO,GAIN_ONE};
 Serial pc(USBTX, USBRX); // tx, rx
 Ticker sample;
-short res[8] = {0,0,0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
-short scaler_res[8] = {0,0,0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
-short ele[6] = {0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
-short scaler_ele[6] = {0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
+int16_t res[8] = {0,0,0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
+uint8_t scaler_res[8] = {1,1,1,1,1,1,1,1};   // 8 PR sensors 1 time per cycle
+int ele[6] = {0,0,0,0,0,0};   // 8 PR sensors 1 time per cycle
+uint8_t scaler_ele[6] = {1,1,1,1,1,1};   // 8 PR sensors 1 time per cycle
 short read[10];
 int done;
 int j = 0;
@@ -37,8 +37,10 @@
 int stamp=0;
 int stamps=0;
 bool buzzer = 0;
+int channel_electric=0;
+int channel_resistive=0;
 Timer times;
-
+/*
 int determine_res_gain(int resistive_signal)
 {
     resistive_signal=abs(resistive_signal);
@@ -73,52 +75,99 @@
     if(gain_factor>4)gain_factor=4;
     return gain_factor;
 }
-void read_all_adc_single_channel(uint8_t channel)
+*/
+uint8_t determineNextGain(int last_sample)
 {
-    if(channel<3) {
-        gain=determine_gain(ele[channel]);
-        ads0.setGain(pga_table[gain]);
-        scaler_ele[(channel+0)%3+0]=1;
-        for(int i=0; i<4-gain; i++)scaler_ele[(channel+0)%3+0]*=2;
-        ele[(channel+2)%3+0] = ads0.readADC_Differential(channel)/scaler_ele[(channel+2)%3+0];
+    if(last_sample<0) {
+        last_sample=abs(last_sample);
+    }
+    uint8_t gain_factor=0;
+    uint8_t result=1;
+    uint16_t sample_normalized=last_sample/ADC_MAX_VALUE;//! determine what would have been the ideal gain setting of the last sample
+    for(uint8_t i=0; i<5; i++) {//! Find the highest power of two in the number (all gain settings are powers of two)
+        if(sample_normalized&(1<<i)) {
+            gain_factor=i+1;
+        }
+    }
+    for(uint8_t i=0; i<gain_factor; i++)result*=2;//! 2^(gain_factor)
+    if(((result*ADC_MAX_VALUE)-last_sample)<(DYNAMIC_SCALE*(result*ADC_MAX_VALUE/2)))gain_factor++;//! If the last sample was closer to the max value of the ideal gain setting than the threshold take one gain setting broader
+    if(gain_factor>4)gain_factor=4;
+    return gain_factor;
+}
+
+void getSingleResistive()
+{
+    if(channel_resistive>3)return;//! make sure it is a valid channel
+    uint8_t pga_gain=determineNextGain(res[(channel_resistive+1)%4]);//! calculate pga setting for the next sample conversion of that adc
+    scaler_res[(channel_resistive+1)%4]=16;
+    for(uint8_t i=0; i<pga_gain; i++)scaler_res[(channel_resistive+1)%4]/=2; //! Update the scaler to correctly process the gain setting of the incoming adc value later on
+    piezo_resistive_adc1.setGain(pga_table[pga_gain]);//! Set pga for the next sample conversion
+    res[channel_resistive]=piezo_resistive_adc1.readADC_SingleEnded(((channel_resistive+1)%4))/scaler_res[channel_resistive];//! Readout sample and start next conversion
 
-        gain=determine_gain(ele[channel+3]);
-        ads1.setGain(pga_table[gain]);
-        scaler_ele[(channel+0)%3+3]=1;
-        for(int i=0; i<4-gain; i++)scaler_ele[(channel+0)%3+3]*=2;
-        ele[(channel+2)%3+3] = ads1.readADC_Differential(channel)/scaler_ele[(channel+2)%3+3];
+    //! same as above but for the other PR ADC
+    pga_gain=determineNextGain(res[((channel_resistive+1)%4)+4]);
+    scaler_res[((channel_resistive+1)%4)+4]=16;
+    for(uint8_t i=0; i<pga_gain; i++)scaler_res[((channel_resistive+1)%4)+4]/=2;
+    piezo_resistive_adc2.setGain(pga_table[pga_gain]);
+    res[channel_resistive+4]=piezo_resistive_adc2.readADC_SingleEnded(((channel_resistive+1)%4))/scaler_res[channel_resistive+4];
+    channel_resistive=(channel_resistive+1)%4;
+}
+
+void getSingleElectric()
+{
+
+    //! Same as getResistive but for the PE ADCs
+    if(channel_electric>2) {
+        channel_electric=(channel_electric+1)%4;
+        return;//! Make sure it is a valid channel
     }
-    gain=determine_res_gain(res[channel]);
-    pr1.setGain(pga_table[gain]);
-    scaler_res[(channel+0)%4+0]=1;
-    for(int i=0; i<4-gain; i++)scaler_res[(channel+0)%4+0]*=2;
-    res[(channel+3)%4+0] = pr1.readADC_SingleEnded(channel)/scaler_res[(channel+3)%4+0];
+    uint8_t pga_gain=determineNextGain(ele[(channel_electric+1)%3]);
+    scaler_ele[(channel_electric+1)%3]=16;
+    for(uint8_t i=0; i<pga_gain; i++)scaler_ele[(channel_electric+1)%3]/=2;
+    piezo_electric_adc.setGain(pga_table[pga_gain]);
+    ele[channel_electric]=piezo_electric_adc.readADC_Differential((channel_electric+1)%3)/scaler_ele[channel_electric];
+
+    //2nd adc
 
-    gain=determine_res_gain(res[channel+4]);
-    pr2.setGain(pga_table[gain]);
-    scaler_res[(channel+0)%4+4]=1;
-    for(int i=0; i<4-gain; i++)scaler_res[(channel+0)%4+4]*=2;
-    res[(channel+3)%4+4] = pr2.readADC_SingleEnded(channel)/scaler_res[(channel+3)%4+4];
+    pga_gain=determineNextGain(ele[((channel_electric+1)%3)+3]);
+    scaler_ele[((channel_electric+1)%3)+3]=16;
+    for(uint8_t i=0; i<pga_gain; i++)scaler_ele[((channel_electric+1)%3)+3]/=2;
+    piezo_electric_adc2.setGain(pga_table[pga_gain]);
+    ele[channel_electric+3]=piezo_electric_adc2.readADC_Differential((channel_electric+1)%3)/scaler_ele[channel_electric+3];
+
+
+    channel_electric=(channel_electric+1)%4;
+}
+
+void read_all_adc_single_channel()
+{
+    getSingleElectric();
+    getSingleResistive();
 }
 
 void read_adc()
 {
     if(boobs.read() > 0.5) {
         for (int i=0; i<4; i++) {
-            read_all_adc_single_channel(i);
-            wait_us(500);
+            read_all_adc_single_channel();
+            wait_us(800);
         }
 
         pc.printf("%d,%d,%d,%d,", res[0], res[1], res[2], res[3]);
         pc.printf("%d,%d,%d,%d,", res[4], res[5], res[6], res[7]);
+        //pc.printf("%d,%d,%d,%d,", scaler_res[0], scaler_res[1], scaler_res[2], scaler_res[3]);
+        //pc.printf("%d,%d,%d,%d,", scaler_res[4], scaler_res[5], scaler_res[6], scaler_res[7]);
         pc.printf("%d,%d,%d,", ele[0],ele[1],ele[2]);
         pc.printf("%d,%d,%d,", ele[3],ele[4],ele[5]);
+        //pc.printf("%d,%d,%d,",scaler_ele[0],scaler_ele[1],scaler_ele[2]);
+        //pc.printf("%d,%d,%d,",scaler_ele[3],scaler_ele[4],scaler_ele[5]);
         pc.printf("\r\n");
 
     } else {
         pc.printf("%d,%d\r\n", -80085,-80085);
     }
 }
+/*
 void noise()
 {
     gain=determine_gain(ele[4]);
@@ -128,7 +177,7 @@
     ele[4] = ads1.readADC_Differential(1)/scaler_ele[4];
     pc.printf("%d\n",ele[4]);
 }
-
+*/
 int main()
 {
     i2c.frequency(400000);