experimental fork

Dependencies:   NOKIA_5110 mbed

Committer:
mva111
Date:
Thu Oct 06 15:00:05 2016 +0000
Revision:
5:9c7c88f8f800
Parent:
4:87037e41c888
Child:
6:e95598c44a7f
workshop modifications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mva111 0:52895a475820 1 #include "mbed.h"
mva111 0:52895a475820 2 #include "hvcontrol.cpp"
mva111 0:52895a475820 3 #include "detection.cpp"
mva111 0:52895a475820 4 #include <queue>
mva111 0:52895a475820 5
mva111 5:9c7c88f8f800 6 //set the frequency of the oscillators below
mva111 5:9c7c88f8f800 7 int f1 = 4300;
mva111 5:9c7c88f8f800 8 int f2 = 4350;
mva111 5:9c7c88f8f800 9
mva111 5:9c7c88f8f800 10 //set the coincidence times in us below
mva111 5:9c7c88f8f800 11 int coincidence_wait = 150;
mva111 5:9c7c88f8f800 12 bool buzzer = 1;
mva111 5:9c7c88f8f800 13
mva111 5:9c7c88f8f800 14 /* NO USER CONFIGURABLE OPTIONS BELOW
mva111 5:9c7c88f8f800 15 */
mva111 5:9c7c88f8f800 16
mva111 0:52895a475820 17 //typically around 2 min data
mva111 0:52895a475820 18 //assuming normal background
mva111 0:52895a475820 19 #define DETECTION_MEMORY_LIMIT 128
mva111 0:52895a475820 20
mva111 0:52895a475820 21 //doesn't log the GM hits with a timestamp
mva111 0:52895a475820 22 //only counts them when set to 1
mva111 0:52895a475820 23 #define DETECT_MUONS_ONLY 0
mva111 0:52895a475820 24
mva111 0:52895a475820 25 //set the coincidence time window in us
mva111 5:9c7c88f8f800 26
mva111 0:52895a475820 27 bool mdet = 0;
mva111 5:9c7c88f8f800 28
mva111 0:52895a475820 29
mva111 2:66a2e52ad175 30 volatile int muon_total;
mva111 2:66a2e52ad175 31 volatile int gm1_total;
mva111 2:66a2e52ad175 32 volatile int gm2_total;
mva111 0:52895a475820 33
mva111 0:52895a475820 34 //storing a detections in a FIFO queue
mva111 0:52895a475820 35 queue<Detection> detections;
mva111 0:52895a475820 36
mva111 0:52895a475820 37 //High voltage controls
mva111 0:52895a475820 38 HVControl hv1(PA_3, PA_0);
mva111 0:52895a475820 39 HVControl hv2(PB_1, PA_1);
mva111 0:52895a475820 40
mva111 0:52895a475820 41 Serial rpi(PA_9,PA_10);
mva111 0:52895a475820 42
mva111 0:52895a475820 43 Timer coincidence_timer;
mva111 0:52895a475820 44 Timer main_t;
mva111 0:52895a475820 45
mva111 0:52895a475820 46 Serial pc(USBTX, USBRX);
mva111 0:52895a475820 47 // Ticker debug;
mva111 0:52895a475820 48 Ticker hv1monitor;
mva111 0:52895a475820 49 Ticker hv2monitor;
mva111 0:52895a475820 50 Ticker memory_checker;
mva111 0:52895a475820 51
mva111 0:52895a475820 52 DigitalOut led(LED1);
mva111 0:52895a475820 53 DigitalOut GM1_led(PA_12);
mva111 0:52895a475820 54 DigitalOut GM2_led(PA_8);
mva111 0:52895a475820 55 DigitalOut C_led(PA_11);
mva111 0:52895a475820 56 DigitalOut Buzzer(PF_0);
mva111 0:52895a475820 57
mva111 0:52895a475820 58 InterruptIn GM1(PB_0);
mva111 0:52895a475820 59 InterruptIn GM2(PF_1);
mva111 0:52895a475820 60
mva111 0:52895a475820 61 //global variables for voltages
mva111 0:52895a475820 62 volatile float v1 = 0;
mva111 0:52895a475820 63 volatile float v2 = 0;
mva111 0:52895a475820 64
mva111 0:52895a475820 65 //ui functions
mva111 0:52895a475820 66 void reset_counters (void){
mva111 0:52895a475820 67 muon_total = 0;
mva111 0:52895a475820 68 gm1_total = 0;
mva111 0:52895a475820 69 gm2_total = 0;
mva111 0:52895a475820 70 }
mva111 0:52895a475820 71
mva111 0:52895a475820 72 void muon_buzz(void){
mva111 0:52895a475820 73 C_led = 1;
mva111 1:549e331aefab 74
mva111 1:549e331aefab 75 if(buzzer){
mva111 1:549e331aefab 76 for(int i = 0; i < 32; i++){
mva111 1:549e331aefab 77 Buzzer = !Buzzer;
mva111 1:549e331aefab 78 wait(0.002);
mva111 1:549e331aefab 79 }} else {
mva111 1:549e331aefab 80 wait(0.05);
mva111 1:549e331aefab 81 }
mva111 1:549e331aefab 82
mva111 0:52895a475820 83 C_led = 0;
mva111 0:52895a475820 84 }
mva111 0:52895a475820 85
mva111 0:52895a475820 86 void GM1_buzz(void){
mva111 0:52895a475820 87 GM1_led = 1;
mva111 1:549e331aefab 88 if(buzzer){
mva111 0:52895a475820 89 for(int i = 0; i < 16; i++)
mva111 0:52895a475820 90 { Buzzer = !Buzzer;
mva111 0:52895a475820 91 wait(0.001);
mva111 1:549e331aefab 92 }} else {
mva111 1:549e331aefab 93 wait(0.05);
mva111 1:549e331aefab 94 }
mva111 0:52895a475820 95 GM1_led = 0;
mva111 0:52895a475820 96 }
mva111 0:52895a475820 97
mva111 0:52895a475820 98 void GM2_buzz(void){
mva111 0:52895a475820 99 GM2_led = 1;
mva111 1:549e331aefab 100 if(buzzer){
mva111 0:52895a475820 101 for(int i = 0; i < 16; i++)
mva111 0:52895a475820 102 { Buzzer = !Buzzer;
mva111 0:52895a475820 103 wait(0.001);
mva111 1:549e331aefab 104 }} else {
mva111 1:549e331aefab 105 wait(0.05);
mva111 1:549e331aefab 106 }
mva111 0:52895a475820 107 GM2_led = 0;
mva111 0:52895a475820 108 }
mva111 0:52895a475820 109
mva111 0:52895a475820 110
mva111 0:52895a475820 111 //adds a detection onto the queue
mva111 0:52895a475820 112 void add_detection(int channel, float time){
mva111 1:549e331aefab 113 Detection* det = new Detection(channel, time);
mva111 0:52895a475820 114 detections.push(*det);
mva111 0:52895a475820 115 delete det;
mva111 0:52895a475820 116 }
mva111 0:52895a475820 117
mva111 0:52895a475820 118 //callback for GM1 detection
mva111 0:52895a475820 119 void GM1_hit(void){
mva111 0:52895a475820 120 coincidence_timer.start();
mva111 0:52895a475820 121 while(coincidence_timer.read_us() < coincidence_wait){
mva111 0:52895a475820 122 if(GM2 == 0){
mva111 0:52895a475820 123 add_detection(3, main_t.read());
mva111 0:52895a475820 124 muon_buzz();
mva111 0:52895a475820 125 muon_total++;
mva111 0:52895a475820 126 mdet = 1;
mva111 0:52895a475820 127 }
mva111 0:52895a475820 128 wait(1e-7);
mva111 0:52895a475820 129 }
mva111 0:52895a475820 130 coincidence_timer.stop();
mva111 0:52895a475820 131 coincidence_timer.reset();
mva111 0:52895a475820 132 if(mdet == 0) GM1_buzz();
mva111 0:52895a475820 133 if ( !DETECT_MUONS_ONLY ) add_detection(1, main_t.read());
mva111 0:52895a475820 134 gm1_total++;
mva111 0:52895a475820 135 mdet = 0;
mva111 0:52895a475820 136 }
mva111 0:52895a475820 137
mva111 0:52895a475820 138 //callback for GM2 detection
mva111 0:52895a475820 139 void GM2_hit(void){
mva111 0:52895a475820 140 coincidence_timer.start();
mva111 0:52895a475820 141 while(coincidence_timer.read_us() < coincidence_wait){
mva111 0:52895a475820 142 if(GM1 == 0){
mva111 0:52895a475820 143 add_detection(3, main_t.read());
mva111 0:52895a475820 144 muon_buzz();
mva111 0:52895a475820 145 muon_total++;
mva111 0:52895a475820 146 mdet = 1;
mva111 0:52895a475820 147 }
mva111 0:52895a475820 148 wait(1e-7);
mva111 0:52895a475820 149 }
mva111 0:52895a475820 150 coincidence_timer.stop();
mva111 0:52895a475820 151 coincidence_timer.reset();
mva111 0:52895a475820 152 if(mdet == 0) GM2_buzz();
mva111 0:52895a475820 153 if ( !DETECT_MUONS_ONLY ) add_detection(2, main_t.read());
mva111 0:52895a475820 154 gm2_total++;
mva111 0:52895a475820 155 mdet = 0;
mva111 0:52895a475820 156 }
mva111 0:52895a475820 157
mva111 0:52895a475820 158 // high voltage software monitors
mva111 0:52895a475820 159 void hv1monitor_(void){
mva111 0:52895a475820 160 if(hv1.measure()){
mva111 0:52895a475820 161 if(v1 - hv1.get_voltage() > 100){ hv1.shutdown(); } //unusual voltage drop detected
mva111 0:52895a475820 162 }else{ hv1.shutdown(); }
mva111 0:52895a475820 163 v1 = hv1.get_voltage();
mva111 0:52895a475820 164 }
mva111 0:52895a475820 165
mva111 0:52895a475820 166 void hv2monitor_(void){
mva111 0:52895a475820 167 if(hv2.measure()){
mva111 0:52895a475820 168 if(v2 - hv2.get_voltage() > 100){ hv2.shutdown(); } //unusual voltage drop detected
mva111 0:52895a475820 169 }else{ hv2.shutdown(); }
mva111 0:52895a475820 170 v2 = hv2.get_voltage();
mva111 0:52895a475820 171 }
mva111 0:52895a475820 172
mva111 0:52895a475820 173 //discarding older items if buffer gets too big
mva111 0:52895a475820 174 void memory_checker_(void){
mva111 0:52895a475820 175 if ( detections.size() > (DETECTION_MEMORY_LIMIT - (DETECTION_MEMORY_LIMIT/8)) ){
mva111 0:52895a475820 176 while ( detections.size() > (DETECTION_MEMORY_LIMIT - (DETECTION_MEMORY_LIMIT/4)) ){
mva111 0:52895a475820 177 detections.pop();
mva111 0:52895a475820 178 }
mva111 0:52895a475820 179 }
mva111 0:52895a475820 180 }
mva111 0:52895a475820 181
mva111 2:66a2e52ad175 182
mva111 0:52895a475820 183 int main() {
mva111 0:52895a475820 184 //start main timer
mva111 0:52895a475820 185 main_t.start();
mva111 0:52895a475820 186
mva111 5:9c7c88f8f800 187 hv1.set_frequency(f1);
mva111 5:9c7c88f8f800 188 hv2.set_frequency(f2);
mva111 0:52895a475820 189 GM1.fall(&GM1_hit);
mva111 0:52895a475820 190 GM2.fall(&GM2_hit);
mva111 0:52895a475820 191
mva111 0:52895a475820 192 //tickers to monitor the high voltage
mva111 0:52895a475820 193 hv1monitor.attach(&hv1monitor_, 0.1);
mva111 0:52895a475820 194 hv2monitor.attach(&hv2monitor_, 0.1);
mva111 0:52895a475820 195
mva111 4:87037e41c888 196
mva111 0:52895a475820 197 //ticker to monitor memory usage
mva111 2:66a2e52ad175 198 memory_checker.attach(&memory_checker_, 0.2);
mva111 0:52895a475820 199 while(1){
mva111 3:0a89a80bea94 200 while(!detections.empty()){
mva111 1:549e331aefab 201 Detection temp = detections.front();
mva111 4:87037e41c888 202 pc.printf("%d, %d, %f, %f, %f \n\r", detections.size(), temp.channel, temp.time, v1, v2);
mva111 1:549e331aefab 203 detections.pop();
mva111 1:549e331aefab 204 }
mva111 1:549e331aefab 205 wait(1);
mva111 0:52895a475820 206 }
mva111 0:52895a475820 207 }