experimental fork

Dependencies:   NOKIA_5110 mbed

main.cpp

Committer:
mva111
Date:
2016-10-04
Revision:
2:66a2e52ad175
Parent:
1:549e331aefab
Child:
3:0a89a80bea94

File content as of revision 2:66a2e52ad175:

#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;
bool buzzer  = 0;

volatile int muon_total;
volatile int gm1_total;
volatile 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;
    
    if(buzzer){
        for(int i = 0; i < 32; i++){ 
        Buzzer = !Buzzer;
        wait(0.002);
        }} else {
            wait(0.05);
            }
        
    C_led = 0;
    }
    
void GM1_buzz(void){
    GM1_led = 1;
    if(buzzer){
        for(int i = 0; i < 16; i++)
        { Buzzer = !Buzzer; 
        wait(0.001);
        }} else {
            wait(0.05);
            }
    GM1_led = 0;
    }
    
void GM2_buzz(void){
    GM2_led = 1;
    if(buzzer){
        for(int i = 0; i < 16; i++)
        { Buzzer = !Buzzer; 
        wait(0.001);
        }} else {
            wait(0.05);
            }
    GM2_led = 0;
    }
    
    
//adds a detection onto the queue
void add_detection(int channel, float time){
    Detection* 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.2);
    while(1){
        if(detections.size() > 1){
            Detection temp = detections.front();
            pc.printf("%d, %d, %f, %f, %f \nr", detections.size(), temp.get_channel(), temp.get_time(), v1, v2);
            detections.pop();
            }
        wait(1);
        }
}