![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
slave2
Dependencies: PinDetect dsp mbed-rtos mbed
main.cpp@3:18d0a7397658, 2016-04-27 (annotated)
- Committer:
- cpark308
- Date:
- Wed Apr 27 20:45:16 2016 +0000
- Revision:
- 3:18d0a7397658
- Parent:
- 2:919431d2e1a7
Master code with 10 try auto sample, verbose output option
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cpark308 | 0:7ee25b048596 | 1 | #include "mbed.h" |
cpark308 | 0:7ee25b048596 | 2 | #include "PinDetect.h" |
cpark308 | 0:7ee25b048596 | 3 | #include "rtos.h" |
cpark308 | 0:7ee25b048596 | 4 | |
cpark308 | 2:919431d2e1a7 | 5 | //stuff from mike |
cpark308 | 2:919431d2e1a7 | 6 | #include "Phase_Finder.h" |
cpark308 | 2:919431d2e1a7 | 7 | #include "arm_math.h" |
cpark308 | 2:919431d2e1a7 | 8 | //Filter coefficients. DO NOT DELETE. |
cpark308 | 2:919431d2e1a7 | 9 | float b[51] = {-0.007772, -0.003741, -0.004376, -0.004861, -0.005128, -0.005108, -0.004726, -0.003925, -0.002650, -0.000867, 0.001443, 0.004277, 0.007607, 0.011379, 0.015521, 0.019940, 0.024525, 0.029149, 0.033671, 0.037953, 0.041851, 0.045248, 0.048020, 0.050070, 0.051335, 0.251747, 0.051335, 0.050070, 0.048020, 0.045248, 0.041851, 0.037953, 0.033671, 0.029149, 0.024525, 0.019940, 0.015521, 0.011379, 0.007607, 0.004277, 0.001443, -0.000867, -0.002650, -0.003925, -0.004726, -0.005108, -0.005128, -0.004861, -0.004376, -0.003741, -0.007772}; |
cpark308 | 2:919431d2e1a7 | 10 | |
cpark308 | 2:919431d2e1a7 | 11 | |
cpark308 | 1:caeb5792f292 | 12 | DigitalOut myled(LED1); //LEDS for debug purposes |
cpark308 | 0:7ee25b048596 | 13 | DigitalOut myled2(LED2); |
cpark308 | 0:7ee25b048596 | 14 | DigitalOut myled4(LED4); |
cpark308 | 1:caeb5792f292 | 15 | AnalogIn audioIn(p20);//pin for sound capture |
cpark308 | 1:caeb5792f292 | 16 | //AnalogOut outfunction(p18); |
cpark308 | 1:caeb5792f292 | 17 | //DigitalOut masterSignalOut(p26); |
cpark308 | 1:caeb5792f292 | 18 | InterruptIn startButton(p21); |
cpark308 | 1:caeb5792f292 | 19 | InterruptIn printButton(p22); |
cpark308 | 2:919431d2e1a7 | 20 | InterruptIn sendToMasterButton(p23); |
cpark308 | 3:18d0a7397658 | 21 | InterruptIn repeatRecording(p24); |
cpark308 | 1:caeb5792f292 | 22 | //InterruptIn trigger(p25); |
cpark308 | 1:caeb5792f292 | 23 | Serial pc(USBTX,USBRX);//serial to computer |
cpark308 | 2:919431d2e1a7 | 24 | Serial master(p13,p14);//serial to master |
cpark308 | 2:919431d2e1a7 | 25 | #define samples 301 //how many samples to take |
cpark308 | 2:919431d2e1a7 | 26 | float* signal; //set up the recording array |
cpark308 | 1:caeb5792f292 | 27 | float* dataSlave1; //set up array for slave1's data |
cpark308 | 1:caeb5792f292 | 28 | Timer ti; //Timer to determine sampling rate |
cpark308 | 1:caeb5792f292 | 29 | int startTime; |
cpark308 | 1:caeb5792f292 | 30 | int endTime; |
cpark308 | 1:caeb5792f292 | 31 | int sampleRate; |
cpark308 | 0:7ee25b048596 | 32 | int counter; |
cpark308 | 1:caeb5792f292 | 33 | int start; |
cpark308 | 1:caeb5792f292 | 34 | int print; |
cpark308 | 2:919431d2e1a7 | 35 | int send; |
cpark308 | 3:18d0a7397658 | 36 | int verbose; |
cpark308 | 0:7ee25b048596 | 37 | |
cpark308 | 1:caeb5792f292 | 38 | void startRecording(void){ |
cpark308 | 1:caeb5792f292 | 39 | start = 1; |
cpark308 | 1:caeb5792f292 | 40 | myled2 = 1; |
cpark308 | 1:caeb5792f292 | 41 | } |
cpark308 | 1:caeb5792f292 | 42 | void startPrint(void){ |
cpark308 | 1:caeb5792f292 | 43 | print = 1; |
cpark308 | 1:caeb5792f292 | 44 | myled4 = 1; |
cpark308 | 0:7ee25b048596 | 45 | } |
cpark308 | 2:919431d2e1a7 | 46 | void startSend(void){ |
cpark308 | 2:919431d2e1a7 | 47 | send = 1; |
cpark308 | 2:919431d2e1a7 | 48 | } |
cpark308 | 0:7ee25b048596 | 49 | int main() { |
cpark308 | 2:919431d2e1a7 | 50 | |
cpark308 | 2:919431d2e1a7 | 51 | |
cpark308 | 2:919431d2e1a7 | 52 | |
cpark308 | 2:919431d2e1a7 | 53 | //from mikes code |
cpark308 | 2:919431d2e1a7 | 54 | float phases; |
cpark308 | 2:919431d2e1a7 | 55 | float filteredSignal[351] = {}; //this array holds the signal after filtering. Make sure the input signal is > 300 samples. |
cpark308 | 2:919431d2e1a7 | 56 | Phase_Finder phase(50000, 900); //Create phase object |
cpark308 | 2:919431d2e1a7 | 57 | |
cpark308 | 2:919431d2e1a7 | 58 | |
cpark308 | 2:919431d2e1a7 | 59 | |
cpark308 | 0:7ee25b048596 | 60 | ti.reset(); |
cpark308 | 1:caeb5792f292 | 61 | start = 0; |
cpark308 | 0:7ee25b048596 | 62 | myled2 = 0; |
cpark308 | 0:7ee25b048596 | 63 | myled4 = 0; |
cpark308 | 2:919431d2e1a7 | 64 | myled = 0; |
cpark308 | 3:18d0a7397658 | 65 | verbose = 0; |
cpark308 | 1:caeb5792f292 | 66 | //allocate memory to the data arrays |
cpark308 | 2:919431d2e1a7 | 67 | signal = (float*) malloc(sizeof(float)*samples); |
cpark308 | 1:caeb5792f292 | 68 | dataSlave1 = (float*) malloc(sizeof(float)*samples); |
cpark308 | 1:caeb5792f292 | 69 | startButton.fall(&startRecording); |
cpark308 | 1:caeb5792f292 | 70 | printButton.fall(&startPrint); |
cpark308 | 2:919431d2e1a7 | 71 | sendToMasterButton.fall(&startSend); |
cpark308 | 3:18d0a7397658 | 72 | repeatRecording.fall(&startRecording); |
cpark308 | 1:caeb5792f292 | 73 | //trigger.rise(&interruptrecv); |
cpark308 | 1:caeb5792f292 | 74 | //Thread threadanalog(analogScope); |
cpark308 | 1:caeb5792f292 | 75 | //threadanalog.set_priority(osPriorityLow); |
cpark308 | 3:18d0a7397658 | 76 | pc.printf("\nThis is Slave2\n"); |
cpark308 | 0:7ee25b048596 | 77 | while(1) { |
cpark308 | 1:caeb5792f292 | 78 | if(start){ //once button is pressed |
cpark308 | 3:18d0a7397658 | 79 | //startTime = ti.read_us(); //get start time |
cpark308 | 1:caeb5792f292 | 80 | for(int i = 0;i<samples;i++){ //record 300 samples |
cpark308 | 2:919431d2e1a7 | 81 | signal[i] = audioIn; |
cpark308 | 1:caeb5792f292 | 82 | } |
cpark308 | 3:18d0a7397658 | 83 | //endTime = ti.read_us(); //get end time |
cpark308 | 3:18d0a7397658 | 84 | //sampleRate = (int)(samples*1000000/(endTime-startTime)); //calculate sample rate |
cpark308 | 2:919431d2e1a7 | 85 | |
cpark308 | 2:919431d2e1a7 | 86 | |
cpark308 | 2:919431d2e1a7 | 87 | arm_conv_f32(signal, 301 , b, 51, filteredSignal ) ; //Filtering operation |
cpark308 | 3:18d0a7397658 | 88 | float calcSignal[251] = {}; |
cpark308 | 2:919431d2e1a7 | 89 | for (int i = 0; i<251; i++){ |
cpark308 | 3:18d0a7397658 | 90 | calcSignal[i] = filteredSignal[i+51]; |
cpark308 | 2:919431d2e1a7 | 91 | } |
cpark308 | 2:919431d2e1a7 | 92 | //Determine phase |
cpark308 | 2:919431d2e1a7 | 93 | phases = phase.estimate(calcSignal, 251); |
cpark308 | 2:919431d2e1a7 | 94 | |
cpark308 | 2:919431d2e1a7 | 95 | |
cpark308 | 1:caeb5792f292 | 96 | start = 0; //stop |
cpark308 | 3:18d0a7397658 | 97 | send = 1; //set send to high to automatically start sending data |
cpark308 | 2:919431d2e1a7 | 98 | |
cpark308 | 2:919431d2e1a7 | 99 | |
cpark308 | 2:919431d2e1a7 | 100 | |
cpark308 | 1:caeb5792f292 | 101 | } |
cpark308 | 1:caeb5792f292 | 102 | if(print){ |
cpark308 | 3:18d0a7397658 | 103 | if(!verbose){ |
cpark308 | 3:18d0a7397658 | 104 | verbose = 1; |
cpark308 | 3:18d0a7397658 | 105 | print = 0; |
cpark308 | 1:caeb5792f292 | 106 | } |
cpark308 | 3:18d0a7397658 | 107 | else{ |
cpark308 | 3:18d0a7397658 | 108 | pc.printf("\n"); |
cpark308 | 3:18d0a7397658 | 109 | for(int i = 0;i<samples;i++){ |
cpark308 | 3:18d0a7397658 | 110 | pc.printf("%5f ",signal[i]); |
cpark308 | 3:18d0a7397658 | 111 | } |
cpark308 | 3:18d0a7397658 | 112 | //pc.printf("\n %5d",sampleRate); |
cpark308 | 3:18d0a7397658 | 113 | print = 0; |
cpark308 | 3:18d0a7397658 | 114 | } |
cpark308 | 3:18d0a7397658 | 115 | wait(0.3); |
cpark308 | 1:caeb5792f292 | 116 | } |
cpark308 | 2:919431d2e1a7 | 117 | if(send){ |
cpark308 | 2:919431d2e1a7 | 118 | myled = 1; |
cpark308 | 3:18d0a7397658 | 119 | wait(0.5); |
cpark308 | 2:919431d2e1a7 | 120 | char buffer[128]; |
cpark308 | 2:919431d2e1a7 | 121 | //for(int i =0;i<samples;i++){ |
cpark308 | 3:18d0a7397658 | 122 | //pc.printf("%7f\n",phases); |
cpark308 | 2:919431d2e1a7 | 123 | master.printf("%7f\n",phases); |
cpark308 | 2:919431d2e1a7 | 124 | //master.printf("291.313\n",phases); |
cpark308 | 3:18d0a7397658 | 125 | //pc.printf("\nSamples:%7f,%7f,%7f",signal[0],signal[1],signal[300]); |
cpark308 | 2:919431d2e1a7 | 126 | |
cpark308 | 2:919431d2e1a7 | 127 | //} |
cpark308 | 3:18d0a7397658 | 128 | if(verbose){ |
cpark308 | 3:18d0a7397658 | 129 | pc.printf("\n***************\n"); |
cpark308 | 3:18d0a7397658 | 130 | for(int i = 0;i<samples;i++){ |
cpark308 | 3:18d0a7397658 | 131 | pc.printf("%5f ",signal[i]); |
cpark308 | 3:18d0a7397658 | 132 | } |
cpark308 | 3:18d0a7397658 | 133 | } |
cpark308 | 2:919431d2e1a7 | 134 | send = 0; |
cpark308 | 2:919431d2e1a7 | 135 | myled = 0; |
cpark308 | 2:919431d2e1a7 | 136 | } |
cpark308 | 0:7ee25b048596 | 137 | } |
cpark308 | 0:7ee25b048596 | 138 | } |