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
Sensorplate/main.cpp
- Committer:
- deldering95
- Date:
- 2018-08-27
- Revision:
- 7:d5e1c7c12a26
- Parent:
- 6:9c1944f3ebe5
- Child:
- 8:00b7a8cbd6ef
File content as of revision 7:d5e1c7c12a26:
#include "mbed.h"
#include "Adafruit_ADS1015.h"
#include "USBSerial.h"
#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 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 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()
{
if(boobs.read() > 0.5) {
for (int i=0; i<4; i++) {
read_all_adc_single_channel(i);
wait_us(500);
}
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");
} 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(400000);
pc.baud(SERIAL_BAUD_RATE);
sample.attach_us(&read_adc, 1000000/READOUT_FREQ);
//sample.attach_us(&noise, 500);
times.start();
while (1) {
}
}