Used for calibration consumables
Dependencies: mbed MODSERIAL FastPWM ADS8568_ADC
main.cpp@31:35c86bee2ed9, 2020-03-25 (annotated)
- Committer:
- paullj
- Date:
- Wed Mar 25 09:16:44 2020 +0000
- Revision:
- 31:35c86bee2ed9
- Parent:
- 30:ab214b681d9b
Added old values back as a comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexStokoe | 0:362148ad8f6f | 1 | #include "mbed.h" |
justinbuckland | 16:2c563709cf09 | 2 | #include "ADS8568_ADC.h" |
justinbuckland | 16:2c563709cf09 | 3 | #include "FastPWM.h" |
justinbuckland | 16:2c563709cf09 | 4 | #include "MODSERIAL.h" |
AlexStokoe | 0:362148ad8f6f | 5 | |
paullj | 25:bd29d673e5d3 | 6 | #define MEAS_DELAY 60 // measurement delay after turning on FET (us) |
paullj | 28:ef9e567bed5b | 7 | #define LOG_INTERVAL 200 // log file interval (ms) |
paullj | 28:ef9e567bed5b | 8 | #define START_DELAY 100 // pause for startup (ms) |
justinbuckland | 22:aba3ee6df08e | 9 | #define N_STEPS 100 |
justinbuckland | 16:2c563709cf09 | 10 | #define BUFFER_SIZE 4096 |
AlexStokoe | 0:362148ad8f6f | 11 | |
justinbuckland | 17:25831977b98e | 12 | //UID lookup address and pointer |
justinbuckland | 17:25831977b98e | 13 | #define UID_ADDR 0x1FFF7A10 |
justinbuckland | 17:25831977b98e | 14 | unsigned long *uid = (unsigned long *) UID_ADDR; |
justinbuckland | 17:25831977b98e | 15 | |
justinbuckland | 17:25831977b98e | 16 | //UID and drive board calibration table |
justinbuckland | 17:25831977b98e | 17 | #define UID_TABLE_LENGTH 4 |
justinbuckland | 17:25831977b98e | 18 | |
justinbuckland | 17:25831977b98e | 19 | int drive_board_serial_number[UID_TABLE_LENGTH] = |
justinbuckland | 17:25831977b98e | 20 | {1, |
justinbuckland | 17:25831977b98e | 21 | 2, |
justinbuckland | 17:25831977b98e | 22 | 3, |
justinbuckland | 17:25831977b98e | 23 | 4}; |
justinbuckland | 17:25831977b98e | 24 | |
justinbuckland | 17:25831977b98e | 25 | unsigned long drive_board_uid[UID_TABLE_LENGTH][3] = |
justinbuckland | 17:25831977b98e | 26 | {{0x005B0060, 0x32375101, 0x32363531}, |
paullj | 29:f2585571ce25 | 27 | {0x00530028, 0x32375116, 0x30333732}, |
paullj | 21:4833e7fbcec7 | 28 | {0x00520060, 0x32375101, 0x32363531}, |
paullj | 21:4833e7fbcec7 | 29 | {0x00570060, 0x32375101, 0x32363531}}; |
justinbuckland | 17:25831977b98e | 30 | |
justinbuckland | 20:ab2d7aa17a20 | 31 | float drive_board_cal[UID_TABLE_LENGTH][2][2] = |
paullj | 29:f2585571ce25 | 32 | {{{0.098846, 10.190057}, {0.060713, 10.208784}}, |
paullj | 29:f2585571ce25 | 33 | {{0.060987, 10.154840}, {0.033058, 10.239591}}, |
paullj | 29:f2585571ce25 | 34 | {{0.014312, 10.405662}, {0.000000, 1.0000000}}, |
paullj | 29:f2585571ce25 | 35 | {{0.055152, 10.062970}, {0.114355, 10.123188}}}; |
paullj | 31:35c86bee2ed9 | 36 | |
paullj | 31:35c86bee2ed9 | 37 | // Old Calibration values |
paullj | 31:35c86bee2ed9 | 38 | // {{{0.0908347476278717, 10.1921343711427}, {0.0497613470164513, 10.2109327517567}}, |
paullj | 31:35c86bee2ed9 | 39 | // {{0.0596907336847412, 10.1550084867437}, {0.0320376283698263, 10.2580153464834}}, |
paullj | 31:35c86bee2ed9 | 40 | // {{0.0119648730956925, 10.4065902688349}, {0.0256785142683800, 10.2921134395920}}, |
paullj | 31:35c86bee2ed9 | 41 | // {{0.0482969653247984, 10.0688110602909}, {0.0882102280729402, 10.1322703041679}}}; |
justinbuckland | 17:25831977b98e | 42 | |
justinbuckland | 16:2c563709cf09 | 43 | MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers |
justinbuckland | 16:2c563709cf09 | 44 | ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3); |
justinbuckland | 16:2c563709cf09 | 45 | I2C i2c(PB_7, PB_8); //SDA, SCL |
sophiemeredith | 11:8e6c8e654004 | 46 | Timer timer; |
justinbuckland | 16:2c563709cf09 | 47 | DigitalIn adc_busy(PA_8); //Busy interrupt sig# |
sophiemeredith | 11:8e6c8e654004 | 48 | |
justinbuckland | 16:2c563709cf09 | 49 | //Heater Control |
justinbuckland | 16:2c563709cf09 | 50 | FastPWM drive_1(PC_9); |
justinbuckland | 16:2c563709cf09 | 51 | FastPWM drive_2(PC_8); |
justinbuckland | 16:2c563709cf09 | 52 | FastPWM guard_1(PC_7); |
justinbuckland | 16:2c563709cf09 | 53 | FastPWM guard_2(PC_6); |
AlexStokoe | 0:362148ad8f6f | 54 | |
justinbuckland | 16:2c563709cf09 | 55 | //Indicator LEDs |
justinbuckland | 16:2c563709cf09 | 56 | DigitalOut hb_led(PC_13); //Green |
justinbuckland | 16:2c563709cf09 | 57 | DigitalOut led_0(PC_4); //Red |
justinbuckland | 16:2c563709cf09 | 58 | DigitalOut led_1(PC_5); //Green |
justinbuckland | 16:2c563709cf09 | 59 | |
justinbuckland | 16:2c563709cf09 | 60 | //User buttons |
justinbuckland | 16:2c563709cf09 | 61 | DigitalIn user_0(PB_0); |
justinbuckland | 16:2c563709cf09 | 62 | DigitalIn user_1(PB_1); |
justinbuckland | 16:2c563709cf09 | 63 | |
justinbuckland | 16:2c563709cf09 | 64 | BusOut converts(PC_0, PC_1, PC_2, PC_3); |
AlexStokoe | 0:362148ad8f6f | 65 | |
AlexStokoe | 0:362148ad8f6f | 66 | int main() { |
sophiemeredith | 11:8e6c8e654004 | 67 | int eTime; |
justinbuckland | 16:2c563709cf09 | 68 | int v[2], curr[2]; |
justinbuckland | 17:25831977b98e | 69 | double r_adc[2], r_ohm[2]; |
justinbuckland | 16:2c563709cf09 | 70 | |
justinbuckland | 16:2c563709cf09 | 71 | int i_port[2] = {0,2}; |
justinbuckland | 16:2c563709cf09 | 72 | int v_port[2] = {1,3}; |
justinbuckland | 2:23f848b21b09 | 73 | |
justinbuckland | 16:2c563709cf09 | 74 | pc.baud(115200); |
justinbuckland | 16:2c563709cf09 | 75 | adc.init(); |
AlexStokoe | 0:362148ad8f6f | 76 | |
justinbuckland | 16:2c563709cf09 | 77 | // Initialsation - all heaters off |
justinbuckland | 16:2c563709cf09 | 78 | drive_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 79 | drive_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 80 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 81 | |
justinbuckland | 16:2c563709cf09 | 82 | guard_1.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 83 | guard_1.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 84 | guard_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 85 | |
justinbuckland | 16:2c563709cf09 | 86 | drive_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 87 | drive_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 88 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 89 | |
justinbuckland | 16:2c563709cf09 | 90 | guard_2.prescaler(1); |
justinbuckland | 16:2c563709cf09 | 91 | guard_2.period_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 92 | guard_2.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 93 | |
justinbuckland | 17:25831977b98e | 94 | pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]); |
paullj | 21:4833e7fbcec7 | 95 | int i_board = -1; |
justinbuckland | 17:25831977b98e | 96 | for (int i = 0; i < UID_TABLE_LENGTH; i++) |
justinbuckland | 17:25831977b98e | 97 | { |
justinbuckland | 17:25831977b98e | 98 | if (uid[0]==drive_board_uid[i][0] && uid[1]==drive_board_uid[i][1] && uid[2]==drive_board_uid[i][2]) |
justinbuckland | 17:25831977b98e | 99 | { |
justinbuckland | 20:ab2d7aa17a20 | 100 | i_board = i; |
justinbuckland | 17:25831977b98e | 101 | i = UID_TABLE_LENGTH; |
justinbuckland | 17:25831977b98e | 102 | } |
justinbuckland | 17:25831977b98e | 103 | } |
justinbuckland | 17:25831977b98e | 104 | |
paullj | 28:ef9e567bed5b | 105 | if (i_board != -1) pc.printf("Drive board: %d ADC measurement: PWM heater pulse. 60us delay\n",drive_board_serial_number[i_board]); |
justinbuckland | 17:25831977b98e | 106 | else pc.printf("Drive board UID match not found\n"); |
justinbuckland | 17:25831977b98e | 107 | |
paullj | 28:ef9e567bed5b | 108 | pc.printf("eTime, I1, V1, R1_adc, R1_ohm, I2, V2, R2_adc, R2_ohm\n"); |
AlexStokoe | 0:362148ad8f6f | 109 | |
justinbuckland | 16:2c563709cf09 | 110 | wait_ms(START_DELAY); |
justinbuckland | 16:2c563709cf09 | 111 | timer.start(); |
AlexStokoe | 0:362148ad8f6f | 112 | |
justinbuckland | 27:5dcd526d7598 | 113 | // indicate start |
justinbuckland | 27:5dcd526d7598 | 114 | led_0 = 0; |
justinbuckland | 27:5dcd526d7598 | 115 | led_1 = 1; |
justinbuckland | 27:5dcd526d7598 | 116 | |
paullj | 28:ef9e567bed5b | 117 | while(!user_1) { |
paullj | 28:ef9e567bed5b | 118 | while (pc.getcNb()!='p' && !user_0); |
justinbuckland | 16:2c563709cf09 | 119 | eTime = timer.read_ms(); |
paullj | 25:bd29d673e5d3 | 120 | |
justinbuckland | 16:2c563709cf09 | 121 | for (int iHeater=0; iHeater <2; iHeater++) { |
justinbuckland | 16:2c563709cf09 | 122 | // measure heater |
justinbuckland | 27:5dcd526d7598 | 123 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 124 | drive_1.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 125 | else |
justinbuckland | 16:2c563709cf09 | 126 | drive_2.pulsewidth_ticks(1000); |
justinbuckland | 16:2c563709cf09 | 127 | wait_us(MEAS_DELAY); |
justinbuckland | 4:694f0e328a52 | 128 | |
justinbuckland | 16:2c563709cf09 | 129 | //Start ADC conversion |
justinbuckland | 16:2c563709cf09 | 130 | adc.start_conversion(15); |
justinbuckland | 16:2c563709cf09 | 131 | |
justinbuckland | 16:2c563709cf09 | 132 | //Wait until ADC is free |
justinbuckland | 16:2c563709cf09 | 133 | while(adc_busy == 1) { |
justinbuckland | 16:2c563709cf09 | 134 | wait_us(1); |
justinbuckland | 16:2c563709cf09 | 135 | } |
justinbuckland | 16:2c563709cf09 | 136 | |
justinbuckland | 16:2c563709cf09 | 137 | //Turn off heater |
justinbuckland | 16:2c563709cf09 | 138 | if (iHeater==0) |
justinbuckland | 16:2c563709cf09 | 139 | drive_1.pulsewidth_ticks(0); |
justinbuckland | 16:2c563709cf09 | 140 | else |
justinbuckland | 16:2c563709cf09 | 141 | drive_2.pulsewidth_ticks(0); |
justinbuckland | 27:5dcd526d7598 | 142 | |
justinbuckland | 16:2c563709cf09 | 143 | wait_us(MEAS_DELAY); |
sophiemeredith | 14:d764e256ac6d | 144 | |
justinbuckland | 16:2c563709cf09 | 145 | //Get ADC values |
justinbuckland | 16:2c563709cf09 | 146 | adc.read_channels(); |
justinbuckland | 16:2c563709cf09 | 147 | curr[iHeater] = adc.read_channel_result(i_port[iHeater]); |
justinbuckland | 16:2c563709cf09 | 148 | v[iHeater] = adc.read_channel_result(v_port[iHeater]); |
justinbuckland | 17:25831977b98e | 149 | r_adc[iHeater] = (float)v[iHeater]/(float)curr[iHeater]; |
paullj | 21:4833e7fbcec7 | 150 | if (i_board != -1) |
justinbuckland | 20:ab2d7aa17a20 | 151 | r_ohm[iHeater] = drive_board_cal[i_board][iHeater][0] + r_adc[iHeater]*drive_board_cal[i_board][iHeater][1]; |
justinbuckland | 17:25831977b98e | 152 | else |
justinbuckland | 17:25831977b98e | 153 | r_ohm[iHeater] = 0.0; |
justinbuckland | 17:25831977b98e | 154 | |
justinbuckland | 24:899071abfc14 | 155 | //Wait before drivinng other heater |
justinbuckland | 24:899071abfc14 | 156 | wait_ms(LOG_INTERVAL/2); |
paullj | 28:ef9e567bed5b | 157 | wait_us(MEAS_DELAY); |
justinbuckland | 24:899071abfc14 | 158 | |
justinbuckland | 16:2c563709cf09 | 159 | } |
paullj | 25:bd29d673e5d3 | 160 | //Write output for iHeater |
paullj | 28:ef9e567bed5b | 161 | pc.printf("%10d, %10d, %10.6f, %10.6f, %10d, %10d, %10.6f, %10.6f \n", curr[0], v[0], r_adc[0], r_ohm[0], curr[1], v[1], r_adc[1], r_ohm[1]); |
justinbuckland | 24:899071abfc14 | 162 | |
justinbuckland | 16:2c563709cf09 | 163 | } |
justinbuckland | 27:5dcd526d7598 | 164 | |
justinbuckland | 27:5dcd526d7598 | 165 | // indicate stop |
justinbuckland | 27:5dcd526d7598 | 166 | led_0 = 1; |
justinbuckland | 27:5dcd526d7598 | 167 | led_1 = 0; |
justinbuckland | 24:899071abfc14 | 168 | |
justinbuckland | 5:67e4ee9a00dc | 169 | } |