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-29
- Revision:
- 8:00b7a8cbd6ef
- Parent:
- 7:d5e1c7c12a26
- Child:
- 9:ea6536825a29
File content as of revision 8:00b7a8cbd6ef:
#include "mbed.h"
#include "Adafruit_ADS1015.h"
#include "USBSerial.h"
#define SERIAL_BAUD_RATE 115200
#define ADC_MAX_VALUE 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 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;
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;
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;
int channel_electric=0;
int channel_resistive=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;
}
*/
uint8_t determineNextGain(int last_sample)
{
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
//! 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
}
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
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();
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]);
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) {
}
}