![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Location Aware Sensing System (LASS) Use POCKET Geiger Arduino Example code changed to mbed code http://www.radiation-watch.org/2011/05/professional.html
main.cpp@0:558eaaae3f2b, 2016-09-02 (annotated)
- Committer:
- royedfa5229
- Date:
- Fri Sep 02 02:56:26 2016 +0000
- Revision:
- 0:558eaaae3f2b
Location Aware Sensing System (LASS); Use POCKET Geiger http://www.radiation-watch.org/2011/05/professional.html
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
royedfa5229 | 0:558eaaae3f2b | 1 | #include "mbed.h" |
royedfa5229 | 0:558eaaae3f2b | 2 | |
royedfa5229 | 0:558eaaae3f2b | 3 | Serial pc(USBTX, USBRX); // tx, rx |
royedfa5229 | 0:558eaaae3f2b | 4 | DigitalIn signPin(p21); //Radiation Pulse (Yellow) |
royedfa5229 | 0:558eaaae3f2b | 5 | DigitalIn noisePin(p22); //Vibration Noise Pulse (White) |
royedfa5229 | 0:558eaaae3f2b | 6 | Timer t; |
royedfa5229 | 0:558eaaae3f2b | 7 | |
royedfa5229 | 0:558eaaae3f2b | 8 | const double alpha = 53.032; // cpm = uSv x alpha |
royedfa5229 | 0:558eaaae3f2b | 9 | |
royedfa5229 | 0:558eaaae3f2b | 10 | int index = 0; //Number of loops |
royedfa5229 | 0:558eaaae3f2b | 11 | char msg[256] = ""; //Message buffer for serial output |
royedfa5229 | 0:558eaaae3f2b | 12 | |
royedfa5229 | 0:558eaaae3f2b | 13 | int signCount = 0; //Counter for Radiation Pulse |
royedfa5229 | 0:558eaaae3f2b | 14 | int noiseCount = 0; //Counter for Noise Pulse |
royedfa5229 | 0:558eaaae3f2b | 15 | |
royedfa5229 | 0:558eaaae3f2b | 16 | int sON = 0; //Lock flag for Radiation Pulse |
royedfa5229 | 0:558eaaae3f2b | 17 | int nON = 0; //Lock flag for Noise Puls |
royedfa5229 | 0:558eaaae3f2b | 18 | |
royedfa5229 | 0:558eaaae3f2b | 19 | double cpm = 0; //Count rate [cpm] of current |
royedfa5229 | 0:558eaaae3f2b | 20 | double cpmHistory[200]; //History of count rates |
royedfa5229 | 0:558eaaae3f2b | 21 | int cpmIndex = 0; //Position of current count rate on cpmHistory[] |
royedfa5229 | 0:558eaaae3f2b | 22 | int cpmIndexPrev = 0; //Flag to prevent duplicative counting |
royedfa5229 | 0:558eaaae3f2b | 23 | |
royedfa5229 | 0:558eaaae3f2b | 24 | //Timing Settings for Loop Interval |
royedfa5229 | 0:558eaaae3f2b | 25 | int prevTime = 0; |
royedfa5229 | 0:558eaaae3f2b | 26 | int currTime = 0; |
royedfa5229 | 0:558eaaae3f2b | 27 | |
royedfa5229 | 0:558eaaae3f2b | 28 | int totalSec = 0; //Elapsed time of measurement [sec] |
royedfa5229 | 0:558eaaae3f2b | 29 | int totalHour = 0; //Elapsed time of measurement [hour] |
royedfa5229 | 0:558eaaae3f2b | 30 | |
royedfa5229 | 0:558eaaae3f2b | 31 | //Time settings for CPM calcuaration |
royedfa5229 | 0:558eaaae3f2b | 32 | int cpmTimeMSec = 0; |
royedfa5229 | 0:558eaaae3f2b | 33 | int cpmTimeSec = 0; |
royedfa5229 | 0:558eaaae3f2b | 34 | int cpmTimeMin = 0; |
royedfa5229 | 0:558eaaae3f2b | 35 | |
royedfa5229 | 0:558eaaae3f2b | 36 | //String buffers of float values for serial output |
royedfa5229 | 0:558eaaae3f2b | 37 | char cpmBuff[20]; |
royedfa5229 | 0:558eaaae3f2b | 38 | char uSvBuff[20]; |
royedfa5229 | 0:558eaaae3f2b | 39 | char uSvdBuff[20]; |
royedfa5229 | 0:558eaaae3f2b | 40 | |
royedfa5229 | 0:558eaaae3f2b | 41 | int main() { |
royedfa5229 | 0:558eaaae3f2b | 42 | //9600bps |
royedfa5229 | 0:558eaaae3f2b | 43 | pc.baud(9600); |
royedfa5229 | 0:558eaaae3f2b | 44 | |
royedfa5229 | 0:558eaaae3f2b | 45 | //PIN setting for Radiation Pulse |
royedfa5229 | 0:558eaaae3f2b | 46 | signPin.mode(PullUp); |
royedfa5229 | 0:558eaaae3f2b | 47 | //PIN setting for Noise Pulse |
royedfa5229 | 0:558eaaae3f2b | 48 | noisePin.mode(PullUp); |
royedfa5229 | 0:558eaaae3f2b | 49 | |
royedfa5229 | 0:558eaaae3f2b | 50 | //CSV-formatting for serial output (substitute , for _) |
royedfa5229 | 0:558eaaae3f2b | 51 | pc.printf("hour[h]_sec[s]_count_cpm_uSv/h_uSv/hError"); |
royedfa5229 | 0:558eaaae3f2b | 52 | |
royedfa5229 | 0:558eaaae3f2b | 53 | //Initialize cpmHistory[] |
royedfa5229 | 0:558eaaae3f2b | 54 | for (int i = 0; i < 200; i++) { |
royedfa5229 | 0:558eaaae3f2b | 55 | cpmHistory[i] = 0; |
royedfa5229 | 0:558eaaae3f2b | 56 | } |
royedfa5229 | 0:558eaaae3f2b | 57 | t.start(); |
royedfa5229 | 0:558eaaae3f2b | 58 | |
royedfa5229 | 0:558eaaae3f2b | 59 | while (1) { |
royedfa5229 | 0:558eaaae3f2b | 60 | // Raw data of Radiation Pulse: Not-detected -> High, Detected -> Low |
royedfa5229 | 0:558eaaae3f2b | 61 | int sign = signPin; |
royedfa5229 | 0:558eaaae3f2b | 62 | // Raw data of Noise Pulse: Not-detected -> Low, Detected -> High |
royedfa5229 | 0:558eaaae3f2b | 63 | int noise = noisePin; |
royedfa5229 | 0:558eaaae3f2b | 64 | //Radiation Pulse normally keeps low for about 100[usec] |
royedfa5229 | 0:558eaaae3f2b | 65 | if (sign == 0 && sON == 0) { //Deactivate Radiation Pulse counting for a while |
royedfa5229 | 0:558eaaae3f2b | 66 | sON = 1; |
royedfa5229 | 0:558eaaae3f2b | 67 | signCount++; |
royedfa5229 | 0:558eaaae3f2b | 68 | } else if (sign == 1 && sON == 1) { |
royedfa5229 | 0:558eaaae3f2b | 69 | sON = 0; |
royedfa5229 | 0:558eaaae3f2b | 70 | } |
royedfa5229 | 0:558eaaae3f2b | 71 | //Output readings to serial port, after 10000 loops |
royedfa5229 | 0:558eaaae3f2b | 72 | if (index == 10000) //About 160-170 msec in Arduino Nano(ATmega328) |
royedfa5229 | 0:558eaaae3f2b | 73 | { |
royedfa5229 | 0:558eaaae3f2b | 74 | //Get current time |
royedfa5229 | 0:558eaaae3f2b | 75 | currTime = t.read_ms(); |
royedfa5229 | 0:558eaaae3f2b | 76 | //No noise detected in 10000 loops |
royedfa5229 | 0:558eaaae3f2b | 77 | if (noiseCount == 0) { |
royedfa5229 | 0:558eaaae3f2b | 78 | //Shift an array for counting log for each 6 sec. |
royedfa5229 | 0:558eaaae3f2b | 79 | if (totalSec % 6 == 0 && cpmIndexPrev != totalSec) { |
royedfa5229 | 0:558eaaae3f2b | 80 | cpmIndexPrev = totalSec; |
royedfa5229 | 0:558eaaae3f2b | 81 | cpmIndex++; |
royedfa5229 | 0:558eaaae3f2b | 82 | |
royedfa5229 | 0:558eaaae3f2b | 83 | if (cpmIndex >= 200) { |
royedfa5229 | 0:558eaaae3f2b | 84 | cpmIndex = 0; |
royedfa5229 | 0:558eaaae3f2b | 85 | } |
royedfa5229 | 0:558eaaae3f2b | 86 | |
royedfa5229 | 0:558eaaae3f2b | 87 | if (cpmHistory[cpmIndex] > 0) { |
royedfa5229 | 0:558eaaae3f2b | 88 | cpm -= cpmHistory[cpmIndex]; |
royedfa5229 | 0:558eaaae3f2b | 89 | } |
royedfa5229 | 0:558eaaae3f2b | 90 | cpmHistory[cpmIndex] = 0; |
royedfa5229 | 0:558eaaae3f2b | 91 | } |
royedfa5229 | 0:558eaaae3f2b | 92 | |
royedfa5229 | 0:558eaaae3f2b | 93 | //Store count log |
royedfa5229 | 0:558eaaae3f2b | 94 | cpmHistory[cpmIndex] += signCount; |
royedfa5229 | 0:558eaaae3f2b | 95 | //Add number of counts |
royedfa5229 | 0:558eaaae3f2b | 96 | cpm += signCount; |
royedfa5229 | 0:558eaaae3f2b | 97 | |
royedfa5229 | 0:558eaaae3f2b | 98 | //Get ready time for 10000 loops |
royedfa5229 | 0:558eaaae3f2b | 99 | cpmTimeMSec += abs(currTime - prevTime); |
royedfa5229 | 0:558eaaae3f2b | 100 | //Transform from msec. to sec. (to prevent overflow) |
royedfa5229 | 0:558eaaae3f2b | 101 | if (cpmTimeMSec >= 1000) { |
royedfa5229 | 0:558eaaae3f2b | 102 | cpmTimeMSec -= 1000; |
royedfa5229 | 0:558eaaae3f2b | 103 | //Add measurement time to calcurate cpm readings (max=20min.) |
royedfa5229 | 0:558eaaae3f2b | 104 | if (cpmTimeSec >= 20 * 60) { |
royedfa5229 | 0:558eaaae3f2b | 105 | cpmTimeSec = 20 * 60; |
royedfa5229 | 0:558eaaae3f2b | 106 | } else { |
royedfa5229 | 0:558eaaae3f2b | 107 | cpmTimeSec++; |
royedfa5229 | 0:558eaaae3f2b | 108 | } |
royedfa5229 | 0:558eaaae3f2b | 109 | |
royedfa5229 | 0:558eaaae3f2b | 110 | //Total measurement time |
royedfa5229 | 0:558eaaae3f2b | 111 | totalSec++; |
royedfa5229 | 0:558eaaae3f2b | 112 | //Transform from sec. to hour. (to prevent overflow) |
royedfa5229 | 0:558eaaae3f2b | 113 | if (totalSec >= 3600) { |
royedfa5229 | 0:558eaaae3f2b | 114 | totalSec -= 3600; |
royedfa5229 | 0:558eaaae3f2b | 115 | totalHour++; |
royedfa5229 | 0:558eaaae3f2b | 116 | } |
royedfa5229 | 0:558eaaae3f2b | 117 | } |
royedfa5229 | 0:558eaaae3f2b | 118 | |
royedfa5229 | 0:558eaaae3f2b | 119 | //Elapsed time of measurement (max=20min.) |
royedfa5229 | 0:558eaaae3f2b | 120 | double min = cpmTimeSec / 60.0; |
royedfa5229 | 0:558eaaae3f2b | 121 | if (min != 0) { |
royedfa5229 | 0:558eaaae3f2b | 122 | //Calculate cpm, uSv/h and error of uSv/h |
royedfa5229 | 0:558eaaae3f2b | 123 | sprintf(cpmBuff, "%f", cpm / min); |
royedfa5229 | 0:558eaaae3f2b | 124 | sprintf(uSvBuff, "%f", cpm / min / alpha); |
royedfa5229 | 0:558eaaae3f2b | 125 | sprintf(uSvdBuff, "%f", sqrt(cpm) / min / alpha); |
royedfa5229 | 0:558eaaae3f2b | 126 | } else { |
royedfa5229 | 0:558eaaae3f2b | 127 | //Devision by zero |
royedfa5229 | 0:558eaaae3f2b | 128 | sprintf(cpmBuff, "%f", 0.0); |
royedfa5229 | 0:558eaaae3f2b | 129 | sprintf(uSvBuff, "%f", 0.0); |
royedfa5229 | 0:558eaaae3f2b | 130 | sprintf(uSvdBuff, "%f", 0.0); |
royedfa5229 | 0:558eaaae3f2b | 131 | } |
royedfa5229 | 0:558eaaae3f2b | 132 | |
royedfa5229 | 0:558eaaae3f2b | 133 | //Create message for serial port |
royedfa5229 | 0:558eaaae3f2b | 134 | sprintf(msg, "%d,%d.%03d,%d,%s,%s,%s",totalHour,totalSec,cpmTimeMSec,signCount,cpmBuff,uSvBuff,uSvdBuff); |
royedfa5229 | 0:558eaaae3f2b | 135 | |
royedfa5229 | 0:558eaaae3f2b | 136 | //Send message to serial port |
royedfa5229 | 0:558eaaae3f2b | 137 | pc.puts(msg); |
royedfa5229 | 0:558eaaae3f2b | 138 | pc.printf("\r\n"); |
royedfa5229 | 0:558eaaae3f2b | 139 | } |
royedfa5229 | 0:558eaaae3f2b | 140 | |
royedfa5229 | 0:558eaaae3f2b | 141 | //Initialization for next 10000 loops |
royedfa5229 | 0:558eaaae3f2b | 142 | prevTime = currTime; |
royedfa5229 | 0:558eaaae3f2b | 143 | signCount = 0; |
royedfa5229 | 0:558eaaae3f2b | 144 | noiseCount = 0; |
royedfa5229 | 0:558eaaae3f2b | 145 | index = 0; |
royedfa5229 | 0:558eaaae3f2b | 146 | } |
royedfa5229 | 0:558eaaae3f2b | 147 | index++; |
royedfa5229 | 0:558eaaae3f2b | 148 | } |
royedfa5229 | 0:558eaaae3f2b | 149 | } |