Jared Baxter
/
Impedance_Fast_Circuitry
Fork of DSP_200kHz by
main.cpp@77:1ee17a9e9f8b, 2017-06-05 (annotated)
- Committer:
- baxterja
- Date:
- Mon Jun 05 18:09:37 2017 +0000
- Revision:
- 77:1ee17a9e9f8b
- Parent:
- 76:704fc58ffcd0
- Child:
- 78:10b2916b8f5c
Fixed printing problem. Made it so it so that ADC 0 only reads one pin & doesn't flip flop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
timmey9 | 22:523e316cbe70 | 2 | |
timmey9 | 52:5a40cc58c4c2 | 3 | // Sampling |
bmazzeo | 53:83a90a47c1fd | 4 | #include "DMA_sampling/adc.h" |
bmazzeo | 54:1697dc574b96 | 5 | #include "DMA_sampling/dac.h" |
bmazzeo | 55:2526b3317bc8 | 6 | #include "DMA_sampling/pdb.h" |
baxterja | 73:b059b5bdc664 | 7 | #include "Jareds_DSP/filters_jared.h" |
baxterja | 75:8bb94685c80b | 8 | #include "Jareds_DSP/demodulate.h" |
bmazzeo | 67:ec0c58490ce6 | 9 | // DSP |
bmazzeo | 67:ec0c58490ce6 | 10 | #include "dsp.h" |
baxterja | 76:704fc58ffcd0 | 11 | #include "print_data.h" |
bmazzeo | 67:ec0c58490ce6 | 12 | |
baxterja | 72:0b554f5026b9 | 13 | #define PRINT_BUFFER_LENGTH 10000 |
baxterja | 73:b059b5bdc664 | 14 | #define GATHER_STATISTICS 1 |
bmazzeo | 67:ec0c58490ce6 | 15 | |
baxterja | 74:ebc9f09fda11 | 16 | |
timmey9 | 22:523e316cbe70 | 17 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 18 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 19 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 20 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 21 | DigitalOut led_blue(LED_BLUE); |
bmazzeo | 58:4bee89daccff | 22 | DigitalOut status_0(D0); |
bmazzeo | 58:4bee89daccff | 23 | DigitalOut status_1(D1); |
baxterja | 77:1ee17a9e9f8b | 24 | DigitalOut status_3(D3); |
baxterja | 73:b059b5bdc664 | 25 | DigitalIn sw2(SW2);//Button 2 |
baxterja | 73:b059b5bdc664 | 26 | DigitalIn sw3(SW3);//Button 3 |
timmey9 | 51:43143a3fc2d7 | 27 | |
timmey9 | 52:5a40cc58c4c2 | 28 | // defined in dma.cpp |
timmey9 | 45:d591d138cdeb | 29 | extern int len; |
baxterja | 73:b059b5bdc664 | 30 | extern uint16_t static_input_array0[];//ADC 0(swaps between a0 and a1. Used to measure current) |
baxterja | 73:b059b5bdc664 | 31 | extern uint16_t static_input_array1[];//ADC 1(measures the voltage between the probe and ground) |
baxterja | 73:b059b5bdc664 | 32 | extern uint16_t static_output_array0[];//DAC outputs whatever wave form we want. |
baxterja | 73:b059b5bdc664 | 33 | extern uint16_t sampling_status;//used to determine when adc's are done reading. |
bmazzeo | 57:7b8c49e1c1f6 | 34 | |
baxterja | 73:b059b5bdc664 | 35 | #define INPUT_ARRAY_SIZE 32 |
baxterja | 73:b059b5bdc664 | 36 | #define DECIMATION_FACTOR 8 |
baxterja | 73:b059b5bdc664 | 37 | #define DEMODULATED_SIGNAL_LENGTH 64 |
baxterja | 73:b059b5bdc664 | 38 | |
baxterja | 73:b059b5bdc664 | 39 | float *input_50k[8]; |
baxterja | 73:b059b5bdc664 | 40 | |
baxterja | 73:b059b5bdc664 | 41 | |
baxterja | 73:b059b5bdc664 | 42 | |
baxterja | 73:b059b5bdc664 | 43 | |
baxterja | 69:014d4bbd4e03 | 44 | #define pre_compute_length 500 |
baxterja | 73:b059b5bdc664 | 45 | #define demodulation_length 125 |
baxterja | 69:014d4bbd4e03 | 46 | #define CarrierFrequency 200 |
baxterja | 69:014d4bbd4e03 | 47 | #define SAMPLEFREQUENCY 100000 |
baxterja | 73:b059b5bdc664 | 48 | //float i_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; |
baxterja | 73:b059b5bdc664 | 49 | //float q_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; |
baxterja | 73:b059b5bdc664 | 50 | uint16_t out_val_pre[pre_compute_length]; //used to write values to the dac |
baxterja | 69:014d4bbd4e03 | 51 | float twopi = 3.14159265359 * 2; |
baxterja | 69:014d4bbd4e03 | 52 | |
baxterja | 69:014d4bbd4e03 | 53 | |
baxterja | 69:014d4bbd4e03 | 54 | void pre_compute_tables() { |
baxterja | 69:014d4bbd4e03 | 55 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 69:014d4bbd4e03 | 56 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 73:b059b5bdc664 | 57 | out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0);//12 bit cos wave |
baxterja | 69:014d4bbd4e03 | 58 | } |
baxterja | 73:b059b5bdc664 | 59 | |
baxterja | 73:b059b5bdc664 | 60 | //float decimated_frequency = 6250; |
baxterja | 73:b059b5bdc664 | 61 | //for(int precompute_counter = 0; precompute_counter < demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR); precompute_counter++){ |
baxterja | 73:b059b5bdc664 | 62 | // i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); |
baxterja | 73:b059b5bdc664 | 63 | // q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); |
baxterja | 73:b059b5bdc664 | 64 | |
baxterja | 69:014d4bbd4e03 | 65 | } |
baxterja | 69:014d4bbd4e03 | 66 | |
baxterja | 69:014d4bbd4e03 | 67 | |
emilmont | 7:65188f4a8c25 | 68 | int main() { |
baxterja | 73:b059b5bdc664 | 69 | pre_compute_tables(); |
baxterja | 75:8bb94685c80b | 70 | precompute_tables(); |
baxterja | 73:b059b5bdc664 | 71 | //turn off all LEDs |
timmey9 | 22:523e316cbe70 | 72 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 73 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 74 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 75 | |
baxterja | 73:b059b5bdc664 | 76 | |
baxterja | 69:014d4bbd4e03 | 77 | int DAC_COUNTER = 0; |
baxterja | 73:b059b5bdc664 | 78 | |
baxterja | 73:b059b5bdc664 | 79 | pc.baud(230400); |
baxterja | 73:b059b5bdc664 | 80 | pc.printf("Starting\r\n"); |
baxterja | 76:704fc58ffcd0 | 81 | float Coeffs_50k[64] = {-0.00580977186764374, -0.00233724597509602, -0.00268496034893966, -0.00296892106107616, -0.00316332842506790, -0.00323734224087795, -0.00316066924650306, -0.00289929624029755, -0.00242701604537671, -0.00171828775863439, -0.000756395728753172, 0.000477926265449492, 0.00199540689821131, 0.00380158746045340, 0.00588165900929199, 0.00823408890481248, 0.0108348019613589, 0.0136476376350624, 0.0166466423869460, 0.0197784366663972, 0.0229973536627058, 0.0262449065159363, 0.0294642414819848, 0.0325921536969478, 0.0355683455017601, 0.0383295801638416, 0.0408195908282943, 0.0429841210797836, 0.0447761053872137, 0.0461555992329706, 0.0470945515138455, 0.0475679104762209, 0.0475679104762209, 0.0470945515138455, 0.0461555992329706, 0.0447761053872137, 0.0429841210797836, 0.0408195908282943, 0.0383295801638416, 0.0355683455017601, 0.0325921536969478, 0.0294642414819848, 0.0262449065159363, 0.0229973536627058, 0.0197784366663972, 0.0166466423869460, 0.0136476376350624, 0.0108348019613589, 0.00823408890481248, 0.00588165900929199, 0.00380158746045340, 0.00199540689821131, 0.000477926265449492, -0.000756395728753172, -0.00171828775863439, -0.00242701604537671, -0.00289929624029755, -0.00316066924650306, -0.00323734224087795, -0.00316332842506790, -0.00296892106107616, -0.00268496034893966, -0.00233724597509602, -0.00580977186764374}; |
baxterja | 76:704fc58ffcd0 | 82 | float Coeffs_6250[64] = {0.00127484193009558, 0.000864570357881826, 0.00102534214649288, 0.00108510384382452, 0.000993133224929705, 0.000703051795928013, 0.000176984381104620, -0.000604044101219087, -0.00163463261391366, -0.00287921914505944, -0.00427024372870053, -0.00570053545539321, -0.00703321069757274, -0.00810283335889244, -0.00872356764128486, -0.00870634354462360, -0.00786670117946960, -0.00604576027868247, -0.00312333623326739, 0.000966301735603311, 0.00622373889666060, 0.0125751900868018, 0.0198716618758688, 0.0278909988243224, 0.0363459953874327, 0.0449001710734076, 0.0531846513791541, 0.0608209828946068, 0.0674455509096558, 0.0727320474263388, 0.0764143241129908, 0.0783043078535981, 0.0783043078535981, 0.0764143241129908, 0.0727320474263388, 0.0674455509096558, 0.0608209828946068, 0.0531846513791541, 0.0449001710734076, 0.0363459953874327, 0.0278909988243224, 0.0198716618758688, 0.0125751900868018, 0.00622373889666060, 0.000966301735603311, -0.00312333623326739, -0.00604576027868247, -0.00786670117946960, -0.00870634354462360, -0.00872356764128486, -0.00810283335889244, -0.00703321069757274, -0.00570053545539321, -0.00427024372870053, -0.00287921914505944, -0.00163463261391366, -0.000604044101219087, 0.000176984381104620, 0.000703051795928013, 0.000993133224929705, 0.00108510384382452, 0.00102534214649288, 0.000864570357881826, 0.00127484193009558}; |
baxterja | 76:704fc58ffcd0 | 83 | float Coeffs_781[32] = {-0.00137496047241108, -0.00189912303426691, -0.00110565158528889, 0.00208160047524475, 0.00686479755292052, 0.0101590507660848, 0.00771645862767646, -0.00299753239510026, -0.0195878466903650, -0.0334711291636072, -0.0323084479325981, -0.00584063791909296, 0.0475547954053657, 0.117412154826256, 0.183528429426864, 0.223771458379020, 0.223771458379020, 0.183528429426864, 0.117412154826256, 0.0475547954053657, -0.00584063791909296, -0.0323084479325981, -0.0334711291636072, -0.0195878466903650, -0.00299753239510026, 0.00771645862767646, 0.0101590507660848, 0.00686479755292052, 0.00208160047524475, -0.00110565158528889, -0.00189912303426691, -0.00137496047241108}; |
baxterja | 76:704fc58ffcd0 | 84 | |
baxterja | 73:b059b5bdc664 | 85 | float Coeffs[20] = {0.0328368433284673, 0.0237706090075265, 0.0309894695180997, 0.0385253568846695, 0.0459996974310349, 0.0530165318016261, 0.0591943866845610, 0.0641755708098907, 0.0676960677594849, 0.0694621149975389, 0.0694621149975389, 0.0676960677594849, 0.0641755708098907, 0.0591943866845610, 0.0530165318016261, 0.0459996974310349, 0.0385253568846695, 0.0309894695180997, 0.0237706090075265, 0.0328368433284673}; |
baxterja | 73:b059b5bdc664 | 86 | float Coeffs2[20] = {-0.00506451294187997, -0.00824932319607346, -0.00986370066237912, -0.00518913235010027, 0.00950858650162284, 0.0357083149022659, 0.0711557142019980, 0.109659494661247, 0.142586101123340, 0.161603335553589, 0.161603335553589, 0.142586101123340, 0.109659494661247, 0.0711557142019980, 0.0357083149022659, 0.00950858650162284, -0.00518913235010027, -0.00986370066237912, -0.00824932319607346, -0.00506451294187997}; |
baxterja | 73:b059b5bdc664 | 87 | |
baxterja | 73:b059b5bdc664 | 88 | |
baxterja | 73:b059b5bdc664 | 89 | for(int i = 0; i < 8; i++) |
baxterja | 77:1ee17a9e9f8b | 90 | input_50k[i] = new float[64];//each array represents the input of the adcs |
baxterja | 73:b059b5bdc664 | 91 | |
baxterja | 77:1ee17a9e9f8b | 92 | filters f3 = filters(4, 8, NULL, 8, 32, Coeffs_781,false); |
baxterja | 73:b059b5bdc664 | 93 | |
baxterja | 73:b059b5bdc664 | 94 | //6250->781 |
baxterja | 77:1ee17a9e9f8b | 95 | filters f2 = filters(4, 8, &f3, 8, 64, Coeffs_6250,false); |
baxterja | 73:b059b5bdc664 | 96 | |
baxterja | 73:b059b5bdc664 | 97 | //50,000->6250 |
baxterja | 77:1ee17a9e9f8b | 98 | filters f1 = filters(2, 8, &f2, 64, 64, Coeffs_50k,true); |
baxterja | 69:014d4bbd4e03 | 99 | |
baxterja | 77:1ee17a9e9f8b | 100 | filters f_pre = filters(2, 2, &f1, 64, 64, Coeffs_50k,false); |
baxterja | 69:014d4bbd4e03 | 101 | |
baxterja | 73:b059b5bdc664 | 102 | //float output_print_buffer[PRINT_BUFFER_LENGTH];//used to store the adc0 values(current measurment) |
baxterja | 73:b059b5bdc664 | 103 | //float output_print_buffer2[PRINT_BUFFER_LENGTH];//used to store the adc0 values(voltage measurment) |
baxterja | 70:8cd7a8a2c153 | 104 | int print_buffer_count = 0; |
baxterja | 70:8cd7a8a2c153 | 105 | |
bmazzeo | 66:72c5c24e532c | 106 | pdb_init(); // Initalize PDB |
bmazzeo | 66:72c5c24e532c | 107 | dac_init(); // initializes DAC |
timmey9 | 45:d591d138cdeb | 108 | adc_init(); // always initialize adc before dma |
bmazzeo | 53:83a90a47c1fd | 109 | pc.printf("ADC Initialized\r\n"); |
baxterja | 74:ebc9f09fda11 | 110 | dma_init2(); // initializes DMAs |
bmazzeo | 55:2526b3317bc8 | 111 | dma_reset(); // This clears any DMA triggers that may have gotten things into a different state |
bmazzeo | 67:ec0c58490ce6 | 112 | pc.printf("Buffer Size: %i\r\n", len); |
bmazzeo | 54:1697dc574b96 | 113 | |
timmey9 | 46:a015ebf4663b | 114 | led_green = 1; |
timmey9 | 38:ec3b16c130d7 | 115 | |
timmey9 | 40:bd6d8c35e822 | 116 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 117 | |
bmazzeo | 64:bb4a4bd57681 | 118 | pdb_start(); |
baxterja | 70:8cd7a8a2c153 | 119 | //while(print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 73:b059b5bdc664 | 120 | while(!GATHER_STATISTICS||print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 73:b059b5bdc664 | 121 | { |
baxterja | 77:1ee17a9e9f8b | 122 | |
baxterja | 73:b059b5bdc664 | 123 | while(sampling_status == 0)//wait until the ADCs read a new value |
baxterja | 73:b059b5bdc664 | 124 | { |
baxterja | 73:b059b5bdc664 | 125 | status_0 = 1; |
baxterja | 77:1ee17a9e9f8b | 126 | print_filter_data(&pc); |
baxterja | 75:8bb94685c80b | 127 | //Thread::wait(.0001); |
baxterja | 73:b059b5bdc664 | 128 | } |
baxterja | 73:b059b5bdc664 | 129 | sampling_status = 0;//sets sampling status to 0. DMA sets it to one once ADCs sample |
baxterja | 73:b059b5bdc664 | 130 | status_0 = 0;//Tied to D0. use an O-scope to measure how much free time there is to play with. |
baxterja | 73:b059b5bdc664 | 131 | status_1 = 1;//Tied to D1. use an O-scope to measure how much time the processing takes |
baxterja | 73:b059b5bdc664 | 132 | |
baxterja | 77:1ee17a9e9f8b | 133 | for(int i = 0; i < len; i++) |
bmazzeo | 61:a56cca07d4a6 | 134 | { |
baxterja | 73:b059b5bdc664 | 135 | static_output_array0[i] = out_val_pre[DAC_COUNTER];//DAC output |
baxterja | 73:b059b5bdc664 | 136 | DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table |
baxterja | 73:b059b5bdc664 | 137 | if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter |
baxterja | 77:1ee17a9e9f8b | 138 | //static_output_array0[i+1] = out_val_pre[DAC_COUNTER];//DAC output |
baxterja | 77:1ee17a9e9f8b | 139 | //DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table |
baxterja | 77:1ee17a9e9f8b | 140 | //if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter |
baxterja | 73:b059b5bdc664 | 141 | |
baxterja | 77:1ee17a9e9f8b | 142 | input_50k[1][i]=float(static_input_array1[i]); |
baxterja | 77:1ee17a9e9f8b | 143 | // input_50k[3][i/2]=static_input_array1[i+1]; |
baxterja | 77:1ee17a9e9f8b | 144 | input_50k[0][i]=float(static_input_array0[i]); |
baxterja | 77:1ee17a9e9f8b | 145 | //input_50k[2][i/2]=static_input_array0[i+1]; |
baxterja | 73:b059b5bdc664 | 146 | |
baxterja | 73:b059b5bdc664 | 147 | |
baxterja | 73:b059b5bdc664 | 148 | |
baxterja | 73:b059b5bdc664 | 149 | }//End of for loop going throught the buffer of adc samples |
baxterja | 77:1ee17a9e9f8b | 150 | //input_50k[0] = static_input_array0; |
baxterja | 77:1ee17a9e9f8b | 151 | //input_50k[1] = static_input_array1; |
baxterja | 77:1ee17a9e9f8b | 152 | f_pre.input(input_50k,64); |
baxterja | 77:1ee17a9e9f8b | 153 | //status_3 = 1; |
baxterja | 77:1ee17a9e9f8b | 154 | //pc.printf("Y"); |
baxterja | 77:1ee17a9e9f8b | 155 | //status_3 = 0; |
baxterja | 73:b059b5bdc664 | 156 | |
baxterja | 70:8cd7a8a2c153 | 157 | |
baxterja | 73:b059b5bdc664 | 158 | status_1 = 0;//turn off D1 used in deterimining how long processing is taking |
baxterja | 73:b059b5bdc664 | 159 | //filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); |
baxterja | 73:b059b5bdc664 | 160 | //arm_fir_f32(&S, filter_input_array, filter_output_array, len); |
baxterja | 73:b059b5bdc664 | 161 | |
baxterja | 73:b059b5bdc664 | 162 | }//end of while loop |
baxterja | 73:b059b5bdc664 | 163 | |
baxterja | 73:b059b5bdc664 | 164 | //for(int i = 0; i<PRINT_BUFFER_LENGTH; i++)//print all the adc values measured |
baxterja | 73:b059b5bdc664 | 165 | //{ |
baxterja | 73:b059b5bdc664 | 166 | // printf("%.1f %.1f\n\r",output_print_buffer[i],output_print_buffer2[i]); |
baxterja | 73:b059b5bdc664 | 167 | //} |
baxterja | 73:b059b5bdc664 | 168 | |
baxterja | 70:8cd7a8a2c153 | 169 | |
bmazzeo | 61:a56cca07d4a6 | 170 | } |
bmazzeo | 63:7903a33e2fd4 | 171 |