Pilot 1 working code (excluding new patient after calculation). %d changed in %f sensordata serial log.
Dependencies: ADS1015 MPU6050 PixelArray mbed
Fork of Momo_New by
Sensorplate/main.cpp@4:367af005956b, 2017-09-14 (annotated)
- Committer:
- DEldering
- Date:
- Thu Sep 14 09:24:07 2017 +0000
- Revision:
- 4:367af005956b
- Parent:
- 3:cb38f2b1fd5a
- Child:
- 5:fa879b059fd1
Extra comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DEldering | 0:c0e44c46c573 | 1 | #include "mbed.h" |
DEldering | 0:c0e44c46c573 | 2 | #include "Adafruit_ADS1015.h" |
DEldering | 0:c0e44c46c573 | 3 | #include "USBSerial.h" |
DEldering | 0:c0e44c46c573 | 4 | #include "MPU6050.h" |
DEldering | 0:c0e44c46c573 | 5 | |
DEldering | 1:a8e61f3910ad | 6 | I2C i2c(p28, p27); // I2C |
DEldering | 1:a8e61f3910ad | 7 | MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit |
DEldering | 1:a8e61f3910ad | 8 | Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 9 | Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 10 | Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC |
DEldering | 1:a8e61f3910ad | 11 | Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection |
DEldering | 1:a8e61f3910ad | 12 | Timer t; // Timer for equally time-spaced samples |
DEldering | 1:a8e61f3910ad | 13 | Ticker sample_cycle; // Polling cycle |
DEldering | 1:a8e61f3910ad | 14 | int cycle_time = 100000; // Cycle time in us |
DEldering | 1:a8e61f3910ad | 15 | int i2c_freq = 400000; // I2C Frequency |
DEldering | 1:a8e61f3910ad | 16 | int usb_baud = 115200; // USB Baud rate |
DEldering | 1:a8e61f3910ad | 17 | short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle |
DEldering | 1:a8e61f3910ad | 18 | short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle |
DEldering | 1:a8e61f3910ad | 19 | int angle = 0; // Accelerometer Z-axis |
DEldering | 1:a8e61f3910ad | 20 | int k = 0; |
DEldering | 1:a8e61f3910ad | 21 | float acce[3]; // Raw accelerometer data |
DEldering | 1:a8e61f3910ad | 22 | float gyro[3]; // Raw gyroscope data |
DEldering | 0:c0e44c46c573 | 23 | |
DEldering | 0:c0e44c46c573 | 24 | void read_adc() |
DEldering | 0:c0e44c46c573 | 25 | { |
DEldering | 1:a8e61f3910ad | 26 | t.reset(); |
DEldering | 1:a8e61f3910ad | 27 | t.start(); |
DEldering | 1:a8e61f3910ad | 28 | |
DEldering | 4:367af005956b | 29 | elec[0] = pel.readADC_SingleEnded(0); //First PE readout |
DEldering | 1:a8e61f3910ad | 30 | |
DEldering | 0:c0e44c46c573 | 31 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 32 | res[k] = pr1.readADC_SingleEnded(k); //First 4 PR readout |
DEldering | 1:a8e61f3910ad | 33 | } |
DEldering | 4:367af005956b | 34 | while(t.read_us()<(1*(cycle_time/5))) {} //Wait untill 20% of cycle |
DEldering | 1:a8e61f3910ad | 35 | |
DEldering | 4:367af005956b | 36 | elec[1] = pel.readADC_SingleEnded(0); //Second PE readout |
DEldering | 1:a8e61f3910ad | 37 | |
DEldering | 1:a8e61f3910ad | 38 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 39 | res[k+4] = pr2.readADC_SingleEnded(k); //Last 4 PR readout |
DEldering | 0:c0e44c46c573 | 40 | } |
DEldering | 4:367af005956b | 41 | while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle |
DEldering | 1:a8e61f3910ad | 42 | |
DEldering | 4:367af005956b | 43 | elec[2] = pel.readADC_SingleEnded(0); //Third PE readout |
DEldering | 1:a8e61f3910ad | 44 | |
DEldering | 4:367af005956b | 45 | agu.getAccelero(acce); //Get accelerometer data |
DEldering | 1:a8e61f3910ad | 46 | angle = acce[2]*10; |
DEldering | 1:a8e61f3910ad | 47 | |
DEldering | 4:367af005956b | 48 | while(t.read_us()<(3*(cycle_time/5))) {} //Wait untill 60% of cycle |
DEldering | 1:a8e61f3910ad | 49 | |
DEldering | 4:367af005956b | 50 | elec[3] = pel.readADC_SingleEnded(0); //Fourth PE readout |
DEldering | 1:a8e61f3910ad | 51 | |
DEldering | 4:367af005956b | 52 | agu.getGyro(gyro); //Get gyroscope data |
DEldering | 1:a8e61f3910ad | 53 | |
DEldering | 4:367af005956b | 54 | while(t.read_us()<(4*(cycle_time/5))) {} //Wait untill 80% of cycle |
DEldering | 1:a8e61f3910ad | 55 | |
DEldering | 4:367af005956b | 56 | elec[4] = pel.readADC_SingleEnded(0); //Fifth PE readout |
DEldering | 1:a8e61f3910ad | 57 | |
DEldering | 4:367af005956b | 58 | while(t.read_us()<(4.5*(cycle_time/5))) {} //Wait untill 90% of cycle |
DEldering | 1:a8e61f3910ad | 59 | pc.printf(",%d,%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], angle, 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 |
DEldering | 0:c0e44c46c573 | 60 | } |
DEldering | 0:c0e44c46c573 | 61 | |
DEldering | 0:c0e44c46c573 | 62 | int main() |
DEldering | 0:c0e44c46c573 | 63 | { |
DEldering | 1:a8e61f3910ad | 64 | i2c.frequency(i2c_freq); |
DEldering | 1:a8e61f3910ad | 65 | pc.baud(usb_baud); |
DEldering | 1:a8e61f3910ad | 66 | pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 67 | pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 68 | pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 69 | sample_cycle.attach_us(&read_adc, cycle_time); |
DEldering | 0:c0e44c46c573 | 70 | while (1) { |
DEldering | 1:a8e61f3910ad | 71 | wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms |
DEldering | 0:c0e44c46c573 | 72 | } |
DEldering | 0:c0e44c46c573 | 73 | } |