![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ADC logging with demo drive board for calibration
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@10:a64434365090, 2019-06-28 (annotated)
- Committer:
- sophiemeredith
- Date:
- Fri Jun 28 08:23:36 2019 +0000
- Revision:
- 10:a64434365090
- Parent:
- 9:3c5a43ce68bb
- Child:
- 11:8e6c8e654004
Updated AD logging program 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 | 10:a64434365090 | 10 | #define CAM_TRIG 20 // camera trigger pulse width (us) |
AlexStokoe | 0:362148ad8f6f | 11 | |
AlexStokoe | 0:362148ad8f6f | 12 | Serial pc(USBTX, USBRX); // tx, rx |
AlexStokoe | 0:362148ad8f6f | 13 | |
justinbuckland | 5:67e4ee9a00dc | 14 | DigitalOut drive(p21); |
justinbuckland | 5:67e4ee9a00dc | 15 | DigitalOut yLED(p27); |
justinbuckland | 5:67e4ee9a00dc | 16 | DigitalOut gLED(p28); |
justinbuckland | 5:67e4ee9a00dc | 17 | DigitalOut rLED(p26); |
sophiemeredith | 10:a64434365090 | 18 | DigitalOut camTrig(p24); |
sophiemeredith | 10:a64434365090 | 19 | DigitalOut mbedIO(p25); // MBED IO |
AlexStokoe | 0:362148ad8f6f | 20 | |
AlexStokoe | 0:362148ad8f6f | 21 | AnalogIn battVolt(p19); |
AlexStokoe | 0:362148ad8f6f | 22 | AnalogIn auxVolt(p20); |
AlexStokoe | 0:362148ad8f6f | 23 | |
AlexStokoe | 0:362148ad8f6f | 24 | BusOut convt(p11, p12, p13, p14); |
justinbuckland | 5:67e4ee9a00dc | 25 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
justinbuckland | 5:67e4ee9a00dc | 26 | DigitalOut cs(p8); //chip select |
AlexStokoe | 0:362148ad8f6f | 27 | DigitalIn busy(p9); |
AlexStokoe | 0:362148ad8f6f | 28 | DigitalOut reset(p10); |
AlexStokoe | 0:362148ad8f6f | 29 | |
AlexStokoe | 0:362148ad8f6f | 30 | char buffer16[16]; |
AlexStokoe | 0:362148ad8f6f | 31 | int val_array[8]; |
AlexStokoe | 0:362148ad8f6f | 32 | const char dummy = 0; |
justinbuckland | 6:75b09f0bcbd9 | 33 | int drivetime_ms = 1; |
AlexStokoe | 0:362148ad8f6f | 34 | |
justinbuckland | 5:67e4ee9a00dc | 35 | char outString[100]; |
AlexStokoe | 0:362148ad8f6f | 36 | |
justinbuckland | 5:67e4ee9a00dc | 37 | int readChannels (char buffer[16], int values[8]){ |
justinbuckland | 2:23f848b21b09 | 38 | //simultaneously samples and reads into buffer |
AlexStokoe | 0:362148ad8f6f | 39 | short int val_array[8]; |
justinbuckland | 2:23f848b21b09 | 40 | |
justinbuckland | 2:23f848b21b09 | 41 | //send convert signal to channels |
justinbuckland | 7:2d695116d636 | 42 | convt = CH_ABCD; |
justinbuckland | 2:23f848b21b09 | 43 | wait_us(1); |
justinbuckland | 2:23f848b21b09 | 44 | convt = 0; |
justinbuckland | 2:23f848b21b09 | 45 | |
justinbuckland | 2:23f848b21b09 | 46 | //SPI(like) data transfer |
justinbuckland | 2:23f848b21b09 | 47 | cs = 0; |
justinbuckland | 2:23f848b21b09 | 48 | spi.write(&dummy, 1, buffer, 16); |
justinbuckland | 2:23f848b21b09 | 49 | cs=1; |
justinbuckland | 2:23f848b21b09 | 50 | |
justinbuckland | 2:23f848b21b09 | 51 | //loop over bytes to add channel voltage values |
justinbuckland | 2:23f848b21b09 | 52 | for (int i=0; i<8; i++){ |
justinbuckland | 2:23f848b21b09 | 53 | val_array[i] = buffer[2*i]<<8 | buffer16[(2*i) + 1]; |
justinbuckland | 2:23f848b21b09 | 54 | values [i] = val_array[i]; |
justinbuckland | 5:67e4ee9a00dc | 55 | } |
justinbuckland | 5:67e4ee9a00dc | 56 | |
justinbuckland | 5:67e4ee9a00dc | 57 | return 0; |
justinbuckland | 5:67e4ee9a00dc | 58 | } |
justinbuckland | 4:694f0e328a52 | 59 | |
AlexStokoe | 0:362148ad8f6f | 60 | |
AlexStokoe | 0:362148ad8f6f | 61 | int main() { |
justinbuckland | 6:75b09f0bcbd9 | 62 | int n_samples = 20; |
justinbuckland | 7:2d695116d636 | 63 | double r1; |
justinbuckland | 7:2d695116d636 | 64 | double r2; |
justinbuckland | 7:2d695116d636 | 65 | double r1_max = 0; |
justinbuckland | 7:2d695116d636 | 66 | double r1_min = 1e10; |
justinbuckland | 7:2d695116d636 | 67 | double r1_sum = 0; |
justinbuckland | 7:2d695116d636 | 68 | double r1_sum2 = 0; |
justinbuckland | 7:2d695116d636 | 69 | double r1_mean; |
justinbuckland | 7:2d695116d636 | 70 | double r1_mean2; |
justinbuckland | 7:2d695116d636 | 71 | double r1_sd; |
justinbuckland | 7:2d695116d636 | 72 | double r1_cv; |
justinbuckland | 8:325f68c1e3d2 | 73 | double r2_max = 0; |
justinbuckland | 8:325f68c1e3d2 | 74 | double r2_min = 1e10; |
justinbuckland | 8:325f68c1e3d2 | 75 | double r2_sum = 0; |
justinbuckland | 8:325f68c1e3d2 | 76 | double r2_sum2 = 0; |
justinbuckland | 8:325f68c1e3d2 | 77 | double r2_mean; |
justinbuckland | 8:325f68c1e3d2 | 78 | double r2_mean2; |
justinbuckland | 8:325f68c1e3d2 | 79 | double r2_sd; |
justinbuckland | 8:325f68c1e3d2 | 80 | double r2_cv; |
justinbuckland | 2:23f848b21b09 | 81 | |
AlexStokoe | 0:362148ad8f6f | 82 | rLED = 0; |
AlexStokoe | 0:362148ad8f6f | 83 | yLED = 0; |
AlexStokoe | 0:362148ad8f6f | 84 | gLED = 0; |
AlexStokoe | 0:362148ad8f6f | 85 | |
AlexStokoe | 0:362148ad8f6f | 86 | drive = 0; |
justinbuckland | 2:23f848b21b09 | 87 | |
AlexStokoe | 0:362148ad8f6f | 88 | pc.baud(115200); |
justinbuckland | 5:67e4ee9a00dc | 89 | pc.printf("Test start\r\n"); |
justinbuckland | 5:67e4ee9a00dc | 90 | |
AlexStokoe | 0:362148ad8f6f | 91 | //Reset ADC sequence |
AlexStokoe | 0:362148ad8f6f | 92 | reset = 1; |
AlexStokoe | 0:362148ad8f6f | 93 | wait_ms(1); |
AlexStokoe | 0:362148ad8f6f | 94 | reset = 0; |
AlexStokoe | 0:362148ad8f6f | 95 | |
AlexStokoe | 0:362148ad8f6f | 96 | //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) |
AlexStokoe | 0:362148ad8f6f | 97 | spi.format(8,2); |
AlexStokoe | 0:362148ad8f6f | 98 | spi.frequency(2000000); |
AlexStokoe | 0:362148ad8f6f | 99 | spi.set_default_write_value(0x00); |
AlexStokoe | 0:362148ad8f6f | 100 | cs = 1; |
AlexStokoe | 0:362148ad8f6f | 101 | |
justinbuckland | 5:67e4ee9a00dc | 102 | rLED = 1; |
justinbuckland | 5:67e4ee9a00dc | 103 | yLED = 0; |
justinbuckland | 5:67e4ee9a00dc | 104 | gLED = 1; |
AlexStokoe | 0:362148ad8f6f | 105 | |
justinbuckland | 8:325f68c1e3d2 | 106 | sprintf(outString, "I1SIG, IREF, V1POS, V1NEG, R1, I2SIG, IREF, V2POS, V2NEG, R2\r\n"); |
justinbuckland | 4:694f0e328a52 | 107 | pc.printf("%s", outString); |
justinbuckland | 4:694f0e328a52 | 108 | |
justinbuckland | 5:67e4ee9a00dc | 109 | for (int x=0; x<n_samples; x++) { |
sophiemeredith | 10:a64434365090 | 110 | // trigger measurement |
justinbuckland | 5:67e4ee9a00dc | 111 | drive = 1; |
justinbuckland | 5:67e4ee9a00dc | 112 | yLED = 1; |
sophiemeredith | 10:a64434365090 | 113 | camTrig = 1; |
sophiemeredith | 10:a64434365090 | 114 | mbedIO = 1; |
sophiemeredith | 10:a64434365090 | 115 | wait_us(CAM_TRIG); |
sophiemeredith | 10:a64434365090 | 116 | camTrig = 0; |
sophiemeredith | 10:a64434365090 | 117 | mbedIO = 0; |
sophiemeredith | 10:a64434365090 | 118 | wait_us(MEAS_DELAY - CAM_TRIG); |
justinbuckland | 5:67e4ee9a00dc | 119 | readChannels (buffer16, val_array); |
justinbuckland | 5:67e4ee9a00dc | 120 | drive = 0; |
justinbuckland | 5:67e4ee9a00dc | 121 | yLED = 0; |
justinbuckland | 4:694f0e328a52 | 122 | |
justinbuckland | 7:2d695116d636 | 123 | r1 = (double)(val_array[5]-val_array[4])/(double)(val_array[1]-val_array[0]); |
justinbuckland | 7:2d695116d636 | 124 | r2 = (double)(val_array[7]-val_array[6])/(double)(val_array[1]-val_array[2]); |
justinbuckland | 7:2d695116d636 | 125 | if (r1 < r1_min) { r1_min = r1; } |
justinbuckland | 7:2d695116d636 | 126 | if (r1 > r1_max) { r1_max = r1; } |
justinbuckland | 8:325f68c1e3d2 | 127 | if (r2 < r2_min) { r2_min = r2; } |
justinbuckland | 8:325f68c1e3d2 | 128 | if (r2 > r2_max) { r2_max = r2; } |
justinbuckland | 7:2d695116d636 | 129 | r1_sum = r1_sum + r1; |
justinbuckland | 7:2d695116d636 | 130 | r1_sum2 = r1_sum2 + (r1*r1); |
justinbuckland | 8:325f68c1e3d2 | 131 | r2_sum = r2_sum + r2; |
justinbuckland | 8:325f68c1e3d2 | 132 | r2_sum2 = r2_sum2 + (r2*r2); |
justinbuckland | 5:67e4ee9a00dc | 133 | |
justinbuckland | 7:2d695116d636 | 134 | sprintf(outString, "%5d\t %5d\t %5d\t %5d\t %f %5d\t %5d\t %5d\t %5d\t %f\r\n", 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); |
justinbuckland | 5:67e4ee9a00dc | 135 | pc.printf("%s", outString); |
justinbuckland | 9:3c5a43ce68bb | 136 | wait_ms(LOG_INTERVAL); |
justinbuckland | 4:694f0e328a52 | 137 | |
justinbuckland | 5:67e4ee9a00dc | 138 | } |
justinbuckland | 5:67e4ee9a00dc | 139 | |
justinbuckland | 7:2d695116d636 | 140 | r1_mean = r1_sum/n_samples; |
justinbuckland | 7:2d695116d636 | 141 | r1_mean2 = r1_sum2/n_samples; |
justinbuckland | 7:2d695116d636 | 142 | r1_sd = sqrt(r1_mean2-(r1_mean*r1_mean)); |
justinbuckland | 7:2d695116d636 | 143 | r1_cv = r1_sd/r1_mean; |
justinbuckland | 5:67e4ee9a00dc | 144 | |
justinbuckland | 8:325f68c1e3d2 | 145 | r2_mean = r2_sum/n_samples; |
justinbuckland | 8:325f68c1e3d2 | 146 | r2_mean2 = r2_sum2/n_samples; |
justinbuckland | 8:325f68c1e3d2 | 147 | r2_sd = sqrt(r2_mean2-(r2_mean*r2_mean)); |
justinbuckland | 8:325f68c1e3d2 | 148 | r2_cv = r2_sd/r1_mean; |
justinbuckland | 8:325f68c1e3d2 | 149 | |
sophiemeredith | 10:a64434365090 | 150 | //pc.printf("Statistics:\r\n"); |
sophiemeredith | 10:a64434365090 | 151 | //pc.printf("n_samples : %d\r\n", n_samples); |
sophiemeredith | 10:a64434365090 | 152 | //pc.printf("r1_mean : %f\r\n", r1_mean); |
sophiemeredith | 10:a64434365090 | 153 | //pc.printf("r1_min : %f\r\n", r1_min); |
sophiemeredith | 10:a64434365090 | 154 | //pc.printf("r1_max : %f\r\n", r1_max); |
sophiemeredith | 10:a64434365090 | 155 | //pc.printf("r1_sd : %f\r\n", r1_sd); |
sophiemeredith | 10:a64434365090 | 156 | //pc.printf("r1_cv : %f\r\n", r1_cv); |
justinbuckland | 5:67e4ee9a00dc | 157 | |
sophiemeredith | 10:a64434365090 | 158 | //pc.printf("r2_mean : %f\r\n", r2_mean); |
sophiemeredith | 10:a64434365090 | 159 | //pc.printf("r2_min : %f\r\n", r2_min); |
sophiemeredith | 10:a64434365090 | 160 | //pc.printf("r2_max : %f\r\n", r2_max); |
sophiemeredith | 10:a64434365090 | 161 | //pc.printf("r2_sd : %f\r\n", r2_sd); |
sophiemeredith | 10:a64434365090 | 162 | //.printf("r2_cv : %f\r\n", r2_cv); |
justinbuckland | 8:325f68c1e3d2 | 163 | |
justinbuckland | 5:67e4ee9a00dc | 164 | rLED = 0; |
justinbuckland | 5:67e4ee9a00dc | 165 | |
justinbuckland | 5:67e4ee9a00dc | 166 | } |