Skip to content

Instantly share code, notes, and snippets.

@tasasaki-github
Created May 3, 2015 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasasaki-github/0677d8b169c7c9070563 to your computer and use it in GitHub Desktop.
Save tasasaki-github/0677d8b169c7c9070563 to your computer and use it in GitHub Desktop.
mbedでPocketGeiger Type5のデータを読み取る試み
#include "mbed.h"
#define HISTSIZE 200
#define LOOPSIZE 10000
DigitalIn signPin(dp25);
DigitalIn noisePin(dp26);
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx
Timer t;
const double alpha=53.032; // cpm = uSv x alpha
int index=0; //Number of loops
char msg[256]=""; //Message buffer for serial output
int signCount=0; //Counter for Radiation Pulse
int noiseCount=0; //Counter for Noise Pulse
int sON=0;//Lock flag for Radiation Pulse
int nON=0;//Lock flag for Noise Puls
double cpm = 0; //Count rate [cpm] of current
double cpmHistory[HISTSIZE]; //History of count rates
int cpmIndex=0;//Position of current count rate on cpmHistory[]
int cpmIndexPrev=0;//Flag to prevent duplicative counting
//Timing Settings for Loop Interval
int prevTime=0;
int currTime=0;
int totalSec=0; //Elapsed time of measurement [sec]
int totalHour=0; //Elapsed time of measurement [hour]
//Time settings for CPM calcuaration
int cpmTimeMSec=0;
int cpmTimeSec=0;
int cpmTimeMin=0;
//String buffers of float values for serial output
char cpmBuff[20];
char uSvBuff[20];
char uSvdBuff[20];
int main()
{
// setup
signPin.mode(PullUp);
noisePin.mode(PullUp);
//CSV-formatting for serial output (substitute , for _)
pc.puts("hour[h]_sec[s]_count_cpm_uSv/h_uSv/hError");
//Initialize cpmHistory[]
for(int i=0; i<HISTSIZE; i++ ) {
cpmHistory[i]=0;
}
t.start();
while(1) {
// Raw data of Radiation Pulse: Not-detected -> High, Detected -> Low
int sign = signPin;
// Raw data of Noise Pulse: Not-detected -> Low, Detected -> High
int noise = noisePin;
//Radiation Pulse normally keeps low for about 100[usec]
if(sign==0 && sON==0) {
//Deactivate Radiation Pulse counting for a while
sON = 1;
signCount++;
} else if(sign==1 && sON==1) {
sON = 0;
}
//Noise Pulse normally keeps high for about 100[usec]
if(noise==1 && nON==0) {
//Deactivate Noise Pulse counting for a while
nON = 1;
noiseCount++;
} else if(noise==0 && nON==1) {
nON = 0;
}
//Output readings to serial port, after LOOPSIZE loops
if(index==LOOPSIZE) { //About 160-170 msec in Arduino Nano(ATmega328)
//Get current time
//currTime = millis();
currTime = t.read_ms();
//char buf0[256];
//sprintf(buf0, "%d", currTime);
//pc.puts(buf0);
//No noise detected in LOOPSIZE loops
if(noiseCount == 0) {
//Shift an array for counting log for each 6 sec.
if( totalSec % 6 == 0 && cpmIndexPrev != totalSec) {
cpmIndexPrev = totalSec;
cpmIndex++;
if(cpmIndex >= HISTSIZE) {
cpmIndex = 0;
}
if(cpmHistory[cpmIndex] > 0) {
cpm -= cpmHistory[cpmIndex];
}
cpmHistory[cpmIndex]=0;
}
//Store count log
cpmHistory[cpmIndex] += signCount;
//Add number of counts
cpm += signCount;
//Get ready time for LOOPSIZE loops
cpmTimeMSec += abs(currTime - prevTime);
//Transform from msec. to sec. (to prevent overflow)
if(cpmTimeMSec >= 1000) {
cpmTimeMSec -= 1000;
//Add measurement time to calcurate cpm readings (max=20min.)
if( cpmTimeSec >= 20*60 ) {
cpmTimeSec = 20*60;
} else {
cpmTimeSec++;
}
//Total measurement time
totalSec++;
//Transform from sec. to hour. (to prevent overflow)
if(totalSec >= 3600) {
totalSec -= 3600;
totalHour++;
}
}
//Elapsed time of measurement (max=20min.)
double min = cpmTimeSec / 60.0;
if(min!=0) {
//Calculate cpm, uSv/h and error of uSv/h
//dtostrf(cpm / min, -1, 3, cpmBuff);
sprintf(cpmBuff, "%f", cpm / min);
//dtostrf(cpm / min / alpha, -1, 3, uSvBuff);
sprintf(uSvBuff, "%f", cpm / min / alpha);
//dtostrf(sqrt(cpm) / min / alpha, -1, 3, uSvdBuff);
sprintf(uSvdBuff, "%f", sqrt(cpm) / min / alpha);
} else {
//Devision by zero
//dtostrf(0, -1, 3, cpmBuff);
//dtostrf(0, -1, 3, uSvBuff);
//dtostrf(0, -1, 3, uSvdBuff);
sprintf(cpmBuff, "%f", 0.0);
sprintf(uSvBuff, "%f", 0.0);
sprintf(uSvdBuff, "%f", 0.0);
}
//Create message for serial port
sprintf(msg, "%d,%d.%03d,%d,%s,%s,%s",
totalHour,
totalSec,
cpmTimeMSec,
signCount,
cpmBuff,
uSvBuff,
uSvdBuff
);
//Send message to serial port
//Serial.println(msg);
pc.puts(msg);
pc.puts("\r\n");
//index=0; // Humm, this is suspicious!
}
index=0;
//Initialization for next LOOPSIZE loops
prevTime = currTime;
signCount=0;
noiseCount=0;
}
index++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment