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