Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ADS1015_fast KXTJ3
Diff: Sensorplate/main.cpp
- Revision:
- 7:d5e1c7c12a26
- Parent:
- 6:9c1944f3ebe5
- Child:
- 8:00b7a8cbd6ef
diff -r 9c1944f3ebe5 -r d5e1c7c12a26 Sensorplate/main.cpp
--- a/Sensorplate/main.cpp Thu Sep 14 09:43:21 2017 +0000
+++ b/Sensorplate/main.cpp Mon Aug 27 14:38:32 2018 +0000
@@ -1,74 +1,142 @@
#include "mbed.h"
#include "Adafruit_ADS1015.h"
#include "USBSerial.h"
-#include "MPU6050.h"
-I2C i2c(p28, p27); // I2C
-MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit
+#define SERIAL_BAUD_RATE 115200
+#define ADC_LIMIT 2048
+#define DYNAMIC_SCALE 0.6f
+// READOUT_FREQ should work up to around 200 I think
+#define READOUT_FREQ 120
+
+AnalogIn boobs(p15);
+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 pel(&i2c, 0x4B); // PiëzoElectric ADC
-Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection
-Timer t; // Timer for equally time-spaced samples
-Ticker sample_cycle; // Polling cycle
-int cycle_time = 100000; // Cycle time in us
-int i2c_freq = 400000; // I2C Frequency
-int usb_baud = 115200; // USB Baud rate
+Adafruit_ADS1115 ads0(&i2c, 0x4B);
+Adafruit_ADS1115 ads1(&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 elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle
-int angle = 0; // Accelerometer Z-axis
-int k = 0;
-float acce[3]; // Raw accelerometer data
-float gyro[3]; // Raw gyroscope data
+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
+short read[10];
+int done;
+int j = 0;
+int k=0;
+int l=0;
+int m=0;
+int n=0;
+int o=0;
+int p=0;
+int total_cycle=0;
+int gain=0;
+int stamp=0;
+int stamps=0;
+bool buzzer = 0;
+Timer times;
+
+int determine_res_gain(int resistive_signal)
+{
+ resistive_signal=abs(resistive_signal);
+ int gain_factor=0;
+ int result=1;
+ int resistive_normalized=resistive_signal/ADC_LIMIT;
+ if(resistive_signal-(resistive_normalized*ADC_LIMIT))resistive_normalized++;
+ for(int i=0; i<5; i++) {
+ if(resistive_normalized&(1<<i)) {
+ gain_factor=i;
+ }
+ }
+ for(int i=0; i<gain_factor; i++)result*=2;
+ if(((result*ADC_LIMIT)-resistive_signal)<(DYNAMIC_SCALE*ADC_LIMIT))gain_factor++;
+ return gain_factor;
+}
+
+int determine_gain(int electric_signal)
+{
+ electric_signal=abs(electric_signal);
+ int gain_factor=0;
+ int result=1;
+ int electric_normalized=electric_signal/ADC_LIMIT;
+ if(electric_signal-(electric_normalized*ADC_LIMIT))electric_normalized++;
+ for(int i=0; i<5; i++) {
+ if(electric_normalized&(1<<i)) {
+ gain_factor=i;
+ }
+ }
+ for(int i=0; i<gain_factor; i++)result*=2;
+ if(((result*ADC_LIMIT)-electric_signal)<(DYNAMIC_SCALE*ADC_LIMIT))gain_factor++;
+ if(gain_factor>4)gain_factor=4;
+ return gain_factor;
+}
+void read_all_adc_single_channel(uint8_t channel)
+{
+ 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];
+
+ 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];
+ }
+ 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];
+
+ 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];
+}
void read_adc()
{
- t.reset();
- t.start();
-
- elec[0] = pel.readADC_SingleEnded(0); //First PE readout
+ if(boobs.read() > 0.5) {
+ for (int i=0; i<4; i++) {
+ read_all_adc_single_channel(i);
+ wait_us(500);
+ }
- for (k = 0; k < 4; k = k + 1) {
- res[k] = pr1.readADC_SingleEnded(k); //First 4 PR readout
- }
- while(t.read_us()<(1*(cycle_time/5))) {} //Wait untill 20% of cycle
-
- elec[1] = pel.readADC_SingleEnded(0); //Second PE readout
-
- for (k = 0; k < 4; k = k + 1) {
- res[k+4] = pr2.readADC_SingleEnded(k); //Last 4 PR readout
- }
- while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle
+ 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,", ele[0],ele[1],ele[2]);
+ pc.printf("%d,%d,%d,", ele[3],ele[4],ele[5]);
+ pc.printf("\r\n");
- elec[2] = pel.readADC_SingleEnded(0); //Third PE readout
-
- agu.getAccelero(acce); //Get accelerometer data
- angle = acce[2]*10;
-
- while(t.read_us()<(3*(cycle_time/5))) {} //Wait untill 60% of cycle
-
- elec[3] = pel.readADC_SingleEnded(0); //Fourth PE readout
-
- agu.getGyro(gyro); //Get gyroscope data
-
- while(t.read_us()<(4*(cycle_time/5))) {} //Wait untill 80% of cycle
-
- elec[4] = pel.readADC_SingleEnded(0); //Fifth PE readout
-
- while(t.read_us()<(4.5*(cycle_time/5))) {} //Wait untill 90% of cycle
- pc.printf(",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\r\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4], acce[0]*100, acce[1]*100, acce[2]*100, gyro[0]*100, gyro[1]*100, gyro[2]*100); // print all to serial port
- //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes
+ } else {
+ pc.printf("%d,%d\r\n", -80085,-80085);
+ }
+}
+void noise()
+{
+ gain=determine_gain(ele[4]);
+ ads1.setGain(pga_table[gain]);
+ scaler_ele[4]=1;
+ for(int i=0; i<4-gain; i++)scaler_ele[4]*=2;
+ ele[4] = ads1.readADC_Differential(1)/scaler_ele[4];
+ pc.printf("%d\n",ele[4]);
}
int main()
{
- i2c.frequency(i2c_freq);
- pc.baud(usb_baud);
- pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
- pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
- pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
- sample_cycle.attach_us(&read_adc, cycle_time);
+ i2c.frequency(400000);
+ pc.baud(SERIAL_BAUD_RATE);
+ sample.attach_us(&read_adc, 1000000/READOUT_FREQ);
+ //sample.attach_us(&noise, 500);
+ times.start();
while (1) {
- wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms
+
}
}
\ No newline at end of file