ADC logging with demo drive board for calibration ADC read synchronised with heater on time
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@13:0ec8fa0a0429, 2019-06-28 (annotated)
- Committer:
- sophiemeredith
- Date:
- Fri Jun 28 09:57:13 2019 +0000
- Revision:
- 13:0ec8fa0a0429
- Parent:
- 12:3f1df385d781
- Child:
- 14:d764e256ac6d
AD logging with camera trigger;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexStokoe | 0:362148ad8f6f | 1 | #include "mbed.h" |
AlexStokoe | 0:362148ad8f6f | 2 | |
justinbuckland | 5:67e4ee9a00dc | 3 | #define CH_A 1 // value of convst bus to read channel A only |
justinbuckland | 5:67e4ee9a00dc | 4 | #define CH_AC 5 // value of convst bus to read channels A and C |
justinbuckland | 5:67e4ee9a00dc | 5 | #define CH_ABCD 15 // value of convst bus to read all chanels simultaneously |
justinbuckland | 9:3c5a43ce68bb | 6 | |
sophiemeredith | 10:a64434365090 | 7 | #define MEAS_DELAY 1000 // measurement delay after turning on FET (us) (themocycling control programme uses 50 us) |
sophiemeredith | 10:a64434365090 | 8 | #define LOG_INTERVAL 5000 // log file interval (ms) |
sophiemeredith | 10:a64434365090 | 9 | |
sophiemeredith | 13:0ec8fa0a0429 | 10 | #define N_STEPS 1000 |
sophiemeredith | 12:3f1df385d781 | 11 | #define CAM_TRIG 20 // camera trigger pulse width (us) |
AlexStokoe | 0:362148ad8f6f | 12 | |
AlexStokoe | 0:362148ad8f6f | 13 | Serial pc(USBTX, USBRX); // tx, rx |
AlexStokoe | 0:362148ad8f6f | 14 | |
justinbuckland | 5:67e4ee9a00dc | 15 | DigitalOut drive(p21); |
justinbuckland | 5:67e4ee9a00dc | 16 | DigitalOut yLED(p27); |
justinbuckland | 5:67e4ee9a00dc | 17 | DigitalOut gLED(p28); |
justinbuckland | 5:67e4ee9a00dc | 18 | DigitalOut rLED(p26); |
sophiemeredith | 10:a64434365090 | 19 | DigitalOut camTrig(p24); |
sophiemeredith | 10:a64434365090 | 20 | DigitalOut mbedIO(p25); // MBED IO |
AlexStokoe | 0:362148ad8f6f | 21 | |
AlexStokoe | 0:362148ad8f6f | 22 | AnalogIn battVolt(p19); |
AlexStokoe | 0:362148ad8f6f | 23 | AnalogIn auxVolt(p20); |
AlexStokoe | 0:362148ad8f6f | 24 | |
AlexStokoe | 0:362148ad8f6f | 25 | BusOut convt(p11, p12, p13, p14); |
sophiemeredith | 11:8e6c8e654004 | 26 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
sophiemeredith | 11:8e6c8e654004 | 27 | DigitalOut cs(p8); // chip select |
AlexStokoe | 0:362148ad8f6f | 28 | DigitalIn busy(p9); |
AlexStokoe | 0:362148ad8f6f | 29 | DigitalOut reset(p10); |
sophiemeredith | 11:8e6c8e654004 | 30 | DigitalIn userButton(p16); |
sophiemeredith | 11:8e6c8e654004 | 31 | Timer timer; |
sophiemeredith | 11:8e6c8e654004 | 32 | LocalFileSystem local("local"); |
AlexStokoe | 0:362148ad8f6f | 33 | |
sophiemeredith | 11:8e6c8e654004 | 34 | FILE *fp = fopen("/local/TEST_LOG.csv", "w"); // Open "test_log" on the local file system for writing |
sophiemeredith | 11:8e6c8e654004 | 35 | |
sophiemeredith | 11:8e6c8e654004 | 36 | char outString[100]; |
AlexStokoe | 0:362148ad8f6f | 37 | char buffer16[16]; |
AlexStokoe | 0:362148ad8f6f | 38 | int val_array[8]; |
AlexStokoe | 0:362148ad8f6f | 39 | const char dummy = 0; |
justinbuckland | 6:75b09f0bcbd9 | 40 | int drivetime_ms = 1; |
AlexStokoe | 0:362148ad8f6f | 41 | |
justinbuckland | 5:67e4ee9a00dc | 42 | int readChannels (char buffer[16], int values[8]){ |
justinbuckland | 2:23f848b21b09 | 43 | //simultaneously samples and reads into buffer |
AlexStokoe | 0:362148ad8f6f | 44 | short int val_array[8]; |
justinbuckland | 2:23f848b21b09 | 45 | |
justinbuckland | 2:23f848b21b09 | 46 | //send convert signal to channels |
justinbuckland | 7:2d695116d636 | 47 | convt = CH_ABCD; |
justinbuckland | 2:23f848b21b09 | 48 | wait_us(1); |
justinbuckland | 2:23f848b21b09 | 49 | convt = 0; |
justinbuckland | 2:23f848b21b09 | 50 | |
justinbuckland | 2:23f848b21b09 | 51 | //SPI(like) data transfer |
justinbuckland | 2:23f848b21b09 | 52 | cs = 0; |
justinbuckland | 2:23f848b21b09 | 53 | spi.write(&dummy, 1, buffer, 16); |
justinbuckland | 2:23f848b21b09 | 54 | cs=1; |
justinbuckland | 2:23f848b21b09 | 55 | |
justinbuckland | 2:23f848b21b09 | 56 | //loop over bytes to add channel voltage values |
justinbuckland | 2:23f848b21b09 | 57 | for (int i=0; i<8; i++){ |
justinbuckland | 2:23f848b21b09 | 58 | val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1]; |
justinbuckland | 2:23f848b21b09 | 59 | values [i] = val_array[i]; |
justinbuckland | 5:67e4ee9a00dc | 60 | } |
justinbuckland | 5:67e4ee9a00dc | 61 | |
justinbuckland | 5:67e4ee9a00dc | 62 | return 0; |
justinbuckland | 5:67e4ee9a00dc | 63 | } |
justinbuckland | 4:694f0e328a52 | 64 | |
AlexStokoe | 0:362148ad8f6f | 65 | |
AlexStokoe | 0:362148ad8f6f | 66 | int main() { |
sophiemeredith | 11:8e6c8e654004 | 67 | int eTime; |
justinbuckland | 7:2d695116d636 | 68 | double r1; |
justinbuckland | 7:2d695116d636 | 69 | double r2; |
justinbuckland | 7:2d695116d636 | 70 | double r1_max = 0; |
justinbuckland | 7:2d695116d636 | 71 | double r1_min = 1e10; |
justinbuckland | 7:2d695116d636 | 72 | double r1_sum = 0; |
justinbuckland | 7:2d695116d636 | 73 | double r1_sum2 = 0; |
justinbuckland | 7:2d695116d636 | 74 | double r1_mean; |
justinbuckland | 7:2d695116d636 | 75 | double r1_mean2; |
justinbuckland | 7:2d695116d636 | 76 | double r1_sd; |
justinbuckland | 7:2d695116d636 | 77 | double r1_cv; |
justinbuckland | 8:325f68c1e3d2 | 78 | double r2_max = 0; |
justinbuckland | 8:325f68c1e3d2 | 79 | double r2_min = 1e10; |
justinbuckland | 8:325f68c1e3d2 | 80 | double r2_sum = 0; |
justinbuckland | 8:325f68c1e3d2 | 81 | double r2_sum2 = 0; |
justinbuckland | 8:325f68c1e3d2 | 82 | double r2_mean; |
justinbuckland | 8:325f68c1e3d2 | 83 | double r2_mean2; |
justinbuckland | 8:325f68c1e3d2 | 84 | double r2_sd; |
justinbuckland | 8:325f68c1e3d2 | 85 | double r2_cv; |
justinbuckland | 2:23f848b21b09 | 86 | |
AlexStokoe | 0:362148ad8f6f | 87 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 88 | yLED = 0; |
AlexStokoe | 0:362148ad8f6f | 89 | gLED = 0; |
AlexStokoe | 0:362148ad8f6f | 90 | |
AlexStokoe | 0:362148ad8f6f | 91 | drive = 0; |
justinbuckland | 2:23f848b21b09 | 92 | |
AlexStokoe | 0:362148ad8f6f | 93 | pc.baud(115200); |
justinbuckland | 5:67e4ee9a00dc | 94 | pc.printf("Test start\r\n"); |
justinbuckland | 5:67e4ee9a00dc | 95 | |
AlexStokoe | 0:362148ad8f6f | 96 | //Reset ADC sequence |
AlexStokoe | 0:362148ad8f6f | 97 | reset = 1; |
AlexStokoe | 0:362148ad8f6f | 98 | wait_ms(1); |
AlexStokoe | 0:362148ad8f6f | 99 | reset = 0; |
AlexStokoe | 0:362148ad8f6f | 100 | |
AlexStokoe | 0:362148ad8f6f | 101 | //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) |
AlexStokoe | 0:362148ad8f6f | 102 | spi.format(8,2); |
AlexStokoe | 0:362148ad8f6f | 103 | spi.frequency(2000000); |
AlexStokoe | 0:362148ad8f6f | 104 | spi.set_default_write_value(0x00); |
AlexStokoe | 0:362148ad8f6f | 105 | cs = 1; |
AlexStokoe | 0:362148ad8f6f | 106 | |
justinbuckland | 5:67e4ee9a00dc | 107 | rLED = 1; |
justinbuckland | 5:67e4ee9a00dc | 108 | yLED = 0; |
justinbuckland | 5:67e4ee9a00dc | 109 | gLED = 1; |
AlexStokoe | 0:362148ad8f6f | 110 | |
sophiemeredith | 13:0ec8fa0a0429 | 111 | //sprintf(outString, "iSteps, eTime, R1, R2 \n"); |
sophiemeredith | 13:0ec8fa0a0429 | 112 | sprintf(outString, "iSteps, eTime, I1SIG, IREF, V1POS, V1NEG, R1,I2SIG,IREF, V2POS, V2NEG, R2\n"); |
sophiemeredith | 13:0ec8fa0a0429 | 113 | pc.printf("%s", outString); |
sophiemeredith | 11:8e6c8e654004 | 114 | fprintf(fp, outString); |
justinbuckland | 4:694f0e328a52 | 115 | |
sophiemeredith | 12:3f1df385d781 | 116 | for (int iStep=0; iStep<N_STEPS; iStep++) { |
sophiemeredith | 11:8e6c8e654004 | 117 | eTime = timer.read_ms(); |
sophiemeredith | 11:8e6c8e654004 | 118 | |
sophiemeredith | 10:a64434365090 | 119 | // trigger measurement |
justinbuckland | 5:67e4ee9a00dc | 120 | drive = 1; |
justinbuckland | 5:67e4ee9a00dc | 121 | yLED = 1; |
sophiemeredith | 10:a64434365090 | 122 | camTrig = 1; |
sophiemeredith | 10:a64434365090 | 123 | mbedIO = 1; |
sophiemeredith | 10:a64434365090 | 124 | wait_us(CAM_TRIG); |
sophiemeredith | 10:a64434365090 | 125 | camTrig = 0; |
sophiemeredith | 10:a64434365090 | 126 | mbedIO = 0; |
sophiemeredith | 10:a64434365090 | 127 | wait_us(MEAS_DELAY - CAM_TRIG); |
justinbuckland | 5:67e4ee9a00dc | 128 | readChannels (buffer16, val_array); |
justinbuckland | 5:67e4ee9a00dc | 129 | drive = 0; |
justinbuckland | 5:67e4ee9a00dc | 130 | yLED = 0; |
justinbuckland | 4:694f0e328a52 | 131 | |
justinbuckland | 7:2d695116d636 | 132 | r1 = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]); |
justinbuckland | 7:2d695116d636 | 133 | r2 = (double)(val_array[7]-val_array[6])/(double)(val_array[1]-val_array[2]); |
justinbuckland | 7:2d695116d636 | 134 | if (r1 < r1_min) { r1_min = r1; } |
justinbuckland | 7:2d695116d636 | 135 | if (r1 > r1_max) { r1_max = r1; } |
justinbuckland | 8:325f68c1e3d2 | 136 | if (r2 < r2_min) { r2_min = r2; } |
justinbuckland | 8:325f68c1e3d2 | 137 | if (r2 > r2_max) { r2_max = r2; } |
justinbuckland | 7:2d695116d636 | 138 | r1_sum = r1_sum + r1; |
justinbuckland | 7:2d695116d636 | 139 | r1_sum2 = r1_sum2 + (r1*r1); |
justinbuckland | 8:325f68c1e3d2 | 140 | r2_sum = r2_sum + r2; |
justinbuckland | 8:325f68c1e3d2 | 141 | r2_sum2 = r2_sum2 + (r2*r2); |
justinbuckland | 5:67e4ee9a00dc | 142 | |
sophiemeredith | 13:0ec8fa0a0429 | 143 | //printf(outString, "%10d,%10d,%10f,%10f\n", iStep, eTime, r1, r2); // log data |
sophiemeredith | 13:0ec8fa0a0429 | 144 | sprintf(outString, "%5d, %10d, %5d, %5d, %5d, %5d, %f %5d, %5d, %5d, %5d, %f\n", iStep, eTime, val_array[0], val_array[1], val_array[4], val_array[5], r1, val_array[2], val_array[1], val_array[6], val_array[7], r2); |
sophiemeredith | 13:0ec8fa0a0429 | 145 | pc.printf("%s", outString); |
sophiemeredith | 11:8e6c8e654004 | 146 | fprintf(fp, outString); |
justinbuckland | 9:3c5a43ce68bb | 147 | wait_ms(LOG_INTERVAL); |
sophiemeredith | 12:3f1df385d781 | 148 | iStep++; |
justinbuckland | 5:67e4ee9a00dc | 149 | } |
justinbuckland | 5:67e4ee9a00dc | 150 | |
sophiemeredith | 11:8e6c8e654004 | 151 | //r1_mean = r1_sum/n_samples; |
sophiemeredith | 11:8e6c8e654004 | 152 | //r1_mean2 = r1_sum2/n_samples; |
sophiemeredith | 11:8e6c8e654004 | 153 | //r1_sd = sqrt(r1_mean2-(r1_mean*r1_mean)); |
sophiemeredith | 11:8e6c8e654004 | 154 | //r1_cv = r1_sd/r1_mean; |
justinbuckland | 5:67e4ee9a00dc | 155 | |
sophiemeredith | 11:8e6c8e654004 | 156 | //r2_mean = r2_sum/n_samples; |
sophiemeredith | 11:8e6c8e654004 | 157 | //r2_mean2 = r2_sum2/n_samples; |
sophiemeredith | 11:8e6c8e654004 | 158 | //r2_sd = sqrt(r2_mean2-(r2_mean*r2_mean)); |
sophiemeredith | 11:8e6c8e654004 | 159 | //r2_cv = r2_sd/r1_mean; |
justinbuckland | 8:325f68c1e3d2 | 160 | |
sophiemeredith | 10:a64434365090 | 161 | //pc.printf("Statistics:\r\n"); |
sophiemeredith | 10:a64434365090 | 162 | //pc.printf("n_samples : %d\r\n", n_samples); |
sophiemeredith | 10:a64434365090 | 163 | //pc.printf("r1_mean : %f\r\n", r1_mean); |
sophiemeredith | 10:a64434365090 | 164 | //pc.printf("r1_min : %f\r\n", r1_min); |
sophiemeredith | 10:a64434365090 | 165 | //pc.printf("r1_max : %f\r\n", r1_max); |
sophiemeredith | 10:a64434365090 | 166 | //pc.printf("r1_sd : %f\r\n", r1_sd); |
sophiemeredith | 10:a64434365090 | 167 | //pc.printf("r1_cv : %f\r\n", r1_cv); |
justinbuckland | 5:67e4ee9a00dc | 168 | |
sophiemeredith | 10:a64434365090 | 169 | //pc.printf("r2_mean : %f\r\n", r2_mean); |
sophiemeredith | 10:a64434365090 | 170 | //pc.printf("r2_min : %f\r\n", r2_min); |
sophiemeredith | 10:a64434365090 | 171 | //pc.printf("r2_max : %f\r\n", r2_max); |
sophiemeredith | 10:a64434365090 | 172 | //pc.printf("r2_sd : %f\r\n", r2_sd); |
sophiemeredith | 10:a64434365090 | 173 | //.printf("r2_cv : %f\r\n", r2_cv); |
justinbuckland | 8:325f68c1e3d2 | 174 | |
justinbuckland | 5:67e4ee9a00dc | 175 | rLED = 0; |
sophiemeredith | 11:8e6c8e654004 | 176 | fclose(fp); |
justinbuckland | 5:67e4ee9a00dc | 177 | |
justinbuckland | 5:67e4ee9a00dc | 178 | } |