experimental fork

Dependencies:   NOKIA_5110 mbed

Revision:
0:52895a475820
Child:
1:549e331aefab
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 04 09:34:20 2016 +0000
@@ -0,0 +1,178 @@
+#include "mbed.h"
+#include "hvcontrol.cpp"
+#include "detection.cpp"
+#include <queue>
+
+//typically around 2 min data
+//assuming normal background
+#define DETECTION_MEMORY_LIMIT 128
+
+//doesn't log the GM hits with a timestamp
+//only counts them when set to 1
+#define DETECT_MUONS_ONLY 0
+
+//set the coincidence time window in us
+int coincidence_wait = 150;
+bool mdet = 0;
+
+int muon_total;
+int gm1_total;
+int gm2_total;
+
+//storing a detections in a FIFO queue
+queue<Detection> detections;
+
+//High voltage controls
+HVControl hv1(PA_3, PA_0);
+HVControl hv2(PB_1, PA_1);
+
+Serial rpi(PA_9,PA_10);
+
+Timer coincidence_timer;
+Timer main_t;
+
+Serial pc(USBTX, USBRX);
+// Ticker debug;
+Ticker hv1monitor;
+Ticker hv2monitor;
+Ticker memory_checker;
+
+DigitalOut led(LED1);
+DigitalOut GM1_led(PA_12);
+DigitalOut GM2_led(PA_8);
+DigitalOut C_led(PA_11);
+DigitalOut Buzzer(PF_0);
+
+InterruptIn GM1(PB_0);
+InterruptIn GM2(PF_1);
+
+//global variables for voltages
+volatile float v1 = 0;
+volatile float v2 = 0;
+
+//ui functions
+void reset_counters (void){
+    muon_total = 0;
+    gm1_total = 0;
+    gm2_total = 0;
+    }
+    
+void muon_buzz(void){
+    C_led = 1;
+    for(int i = 0; i < 32; i++){ 
+    Buzzer = !Buzzer;
+    wait(0.002);
+    }
+    C_led = 0;
+    }
+    
+void GM1_buzz(void){
+    GM1_led = 1;
+        for(int i = 0; i < 16; i++)
+        { Buzzer = !Buzzer; 
+        wait(0.001);
+        }
+    GM1_led = 0;
+    }
+    
+void GM2_buzz(void){
+    GM2_led = 1;
+        for(int i = 0; i < 16; i++)
+        { Buzzer = !Buzzer; 
+        wait(0.001);
+        }
+    GM2_led = 0;
+    }
+    
+    
+//adds a detection onto the queue
+void add_detection(int channel, float time){
+    Detection* det;
+    det = new Detection(channel, time);
+    detections.push(*det);
+    delete det;
+    }
+    
+//callback for GM1 detection
+void GM1_hit(void){
+    coincidence_timer.start();
+    while(coincidence_timer.read_us() < coincidence_wait){
+        if(GM2 == 0){
+            add_detection(3, main_t.read());
+            muon_buzz();
+            muon_total++;
+            mdet = 1;
+            }
+        wait(1e-7);
+        } 
+    coincidence_timer.stop();
+    coincidence_timer.reset();
+    if(mdet == 0) GM1_buzz();
+    if ( !DETECT_MUONS_ONLY ) add_detection(1, main_t.read());
+    gm1_total++;
+    mdet = 0;
+    }
+
+//callback for GM2 detection
+void GM2_hit(void){
+   coincidence_timer.start();
+    while(coincidence_timer.read_us() < coincidence_wait){
+        if(GM1 == 0){
+            add_detection(3, main_t.read());
+            muon_buzz();
+            muon_total++;
+            mdet = 1;
+            }
+        wait(1e-7);
+        }
+    coincidence_timer.stop();
+    coincidence_timer.reset();    
+    if(mdet == 0) GM2_buzz();
+    if ( !DETECT_MUONS_ONLY ) add_detection(2, main_t.read());
+    gm2_total++;
+    mdet = 0;
+    }
+    
+// high voltage software monitors
+void hv1monitor_(void){
+    if(hv1.measure()){
+        if(v1 - hv1.get_voltage() > 100){ hv1.shutdown(); } //unusual voltage drop detected
+        }else{ hv1.shutdown(); }
+    v1 = hv1.get_voltage();
+    }
+
+void hv2monitor_(void){
+    if(hv2.measure()){
+        if(v2 - hv2.get_voltage() > 100){ hv2.shutdown(); } //unusual voltage drop detected
+        }else{ hv2.shutdown(); }
+    v2 = hv2.get_voltage();
+    }
+
+//discarding older items if buffer gets too big
+void memory_checker_(void){
+     if ( detections.size() > (DETECTION_MEMORY_LIMIT - (DETECTION_MEMORY_LIMIT/8)) ){
+            while ( detections.size() > (DETECTION_MEMORY_LIMIT - (DETECTION_MEMORY_LIMIT/4)) ){
+                detections.pop();
+            }
+        }
+    }
+    
+int main() { 
+//start main timer
+main_t.start();
+
+hv1.set_frequency(4500);
+hv2.set_frequency(4500);
+GM1.fall(&GM1_hit);
+GM2.fall(&GM2_hit);
+
+//tickers to monitor the high voltage
+hv1monitor.attach(&hv1monitor_, 0.1);
+hv2monitor.attach(&hv2monitor_, 0.1);
+
+//ticker to monitor memory usage 
+memory_checker.attach(&memory_checker_, 0.1);
+
+    while(1){
+        }
+}
\ No newline at end of file