Tummas Tomasson / Mbed 2 deprecated PingDetector_mbedadc

Dependencies:   mbed

Committer:
tummastt
Date:
Tue Aug 21 14:23:03 2012 +0000
Revision:
1:227db871d328
Parent:
0:efb27fbc92c0
Pinger detection program for mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tummastt 0:efb27fbc92c0 1 /*
tummastt 1:227db871d328 2 Program to detect a pinger signal and send
tummastt 1:227db871d328 3 it back to the PC for processing
tummastt 0:efb27fbc92c0 4 */
tummastt 0:efb27fbc92c0 5
tummastt 0:efb27fbc92c0 6 #include "mbed.h"
tummastt 0:efb27fbc92c0 7 #include "Parameters.h"
tummastt 0:efb27fbc92c0 8 #include "PingDetect/PingDetect.h"
tummastt 0:efb27fbc92c0 9
tummastt 0:efb27fbc92c0 10 Ticker t;
tummastt 0:efb27fbc92c0 11
tummastt 0:efb27fbc92c0 12
tummastt 0:efb27fbc92c0 13 DigitalOut l2(LED2);
tummastt 0:efb27fbc92c0 14 DigitalOut l3(LED3);
tummastt 0:efb27fbc92c0 15 DigitalOut l4(LED4);
tummastt 0:efb27fbc92c0 16
tummastt 0:efb27fbc92c0 17 bool Input=0, NewInput=0;
tummastt 0:efb27fbc92c0 18
tummastt 0:efb27fbc92c0 19 void Stop();
tummastt 0:efb27fbc92c0 20
tummastt 0:efb27fbc92c0 21 void Start();
tummastt 0:efb27fbc92c0 22
tummastt 0:efb27fbc92c0 23 void CalculateFFT();
tummastt 0:efb27fbc92c0 24
tummastt 0:efb27fbc92c0 25 void Run();
tummastt 0:efb27fbc92c0 26
tummastt 0:efb27fbc92c0 27 void Find();
tummastt 0:efb27fbc92c0 28
tummastt 0:efb27fbc92c0 29 int Command = doNothing;
tummastt 0:efb27fbc92c0 30 int Found = notFound;
tummastt 0:efb27fbc92c0 31
tummastt 0:efb27fbc92c0 32 PingDetect PD;
tummastt 0:efb27fbc92c0 33
tummastt 0:efb27fbc92c0 34 int main() {
tummastt 0:efb27fbc92c0 35
tummastt 0:efb27fbc92c0 36 while (1) {
tummastt 0:efb27fbc92c0 37
tummastt 0:efb27fbc92c0 38 //
tummastt 0:efb27fbc92c0 39 switch (Command) {
tummastt 0:efb27fbc92c0 40 case start:
tummastt 0:efb27fbc92c0 41 Start();
tummastt 0:efb27fbc92c0 42 break;
tummastt 0:efb27fbc92c0 43
tummastt 0:efb27fbc92c0 44 case calcFFT:
tummastt 0:efb27fbc92c0 45 CalculateFFT();
tummastt 0:efb27fbc92c0 46 break;
tummastt 0:efb27fbc92c0 47
tummastt 0:efb27fbc92c0 48 case stop:
tummastt 0:efb27fbc92c0 49 Stop();
tummastt 0:efb27fbc92c0 50 break;
tummastt 0:efb27fbc92c0 51
tummastt 0:efb27fbc92c0 52 case doNothing:
tummastt 0:efb27fbc92c0 53
tummastt 0:efb27fbc92c0 54 default:
tummastt 0:efb27fbc92c0 55 break;
tummastt 0:efb27fbc92c0 56 }
tummastt 0:efb27fbc92c0 57
tummastt 0:efb27fbc92c0 58 Command = PD.ReadSerial(Command);
tummastt 0:efb27fbc92c0 59 }
tummastt 0:efb27fbc92c0 60 }
tummastt 0:efb27fbc92c0 61
tummastt 1:227db871d328 62 // Stops all functions.
tummastt 0:efb27fbc92c0 63 void Stop() {
tummastt 0:efb27fbc92c0 64 t.detach();
tummastt 0:efb27fbc92c0 65 l2=0;
tummastt 0:efb27fbc92c0 66 l3=0;
tummastt 0:efb27fbc92c0 67 l4=0;
tummastt 0:efb27fbc92c0 68 Command = doNothing;
tummastt 0:efb27fbc92c0 69 }
tummastt 0:efb27fbc92c0 70
tummastt 1:227db871d328 71 // sets the find function to run every 5ms
tummastt 0:efb27fbc92c0 72 void Start() {
tummastt 0:efb27fbc92c0 73 l2=1;
tummastt 0:efb27fbc92c0 74 t.detach();
tummastt 0:efb27fbc92c0 75 t.attach_us(&Find,5000);
tummastt 0:efb27fbc92c0 76 Command = doNothing;
tummastt 0:efb27fbc92c0 77 }
tummastt 0:efb27fbc92c0 78
tummastt 1:227db871d328 79 // Calculates the FFT and sends to the PC 100 times
tummastt 0:efb27fbc92c0 80 void CalculateFFT() {
tummastt 0:efb27fbc92c0 81 t.detach();
tummastt 0:efb27fbc92c0 82 PD.CalculateFFT();
tummastt 0:efb27fbc92c0 83 Command = doNothing;
tummastt 0:efb27fbc92c0 84 }
tummastt 0:efb27fbc92c0 85
tummastt 1:227db871d328 86 // The run function samples the data from the filters
tummastt 1:227db871d328 87 // and sends to it to the PC
tummastt 0:efb27fbc92c0 88 void Run () {
tummastt 1:227db871d328 89 l4=0;
tummastt 0:efb27fbc92c0 90 l3=!l3;
tummastt 0:efb27fbc92c0 91 PD.Run();
tummastt 0:efb27fbc92c0 92 }
tummastt 0:efb27fbc92c0 93
tummastt 1:227db871d328 94 // Run the find function, if the signal is is found,
tummastt 1:227db871d328 95 // it sets the run function to run every second and
tummastt 1:227db871d328 96 // ensures the detected signal is in the centre of
tummastt 1:227db871d328 97 // the sampling window
tummastt 0:efb27fbc92c0 98 void Find() {
tummastt 1:227db871d328 99 l4=1;
tummastt 0:efb27fbc92c0 100 Found = PD.Search();
tummastt 0:efb27fbc92c0 101 if(Found == found){
tummastt 0:efb27fbc92c0 102 t.detach();
tummastt 0:efb27fbc92c0 103 wait_ms(1000-TIME_OFFSET);
tummastt 0:efb27fbc92c0 104 t.attach(&Run,1);
tummastt 0:efb27fbc92c0 105 Run();
tummastt 0:efb27fbc92c0 106 }
tummastt 0:efb27fbc92c0 107
tummastt 0:efb27fbc92c0 108 }