eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*************************************************************
00002 
00003  main.c
00004 
00005 *************************************************************/
00006 #include "geiger.h"
00007 
00008 void calculate_sv(){
00009     cpm = 0;
00010     for(int i=0; i<=first_touch_index; i++){
00011         cpm += cpm_array[i];
00012     }
00013     cpm /= ((first_touch_index+1)*((float)CHUNK/60));
00014     
00015     uSv = cpm/carib + offset_usv;
00016     if (uSv <0) uSv = 0;
00017   
00018     //peak usv set
00019     if(peak_uSv<uSv){
00020         peak_uSv=uSv;
00021     }
00022 
00023     // for long term average
00024     long_cpm=0;
00025     unsigned long cpm_sum=0;
00026     for (int i=0; i<= long_first_touch_index; i++) {
00027         cpm_sum += long_cpm_array[i];
00028     }
00029     cpm_sum *= 60;
00030     long_cpm = (float)cpm_sum / ((long_first_touch_index)*60+count_second);
00031     long_uSv = long_cpm/carib + offset_usv;
00032     
00033 #ifdef DEBUG
00034     pc.printf("cpm=%3d,usv=%.3f,longcpm=%ld,long_usv=%.3f\r\n",(int)cpm,uSv,(long)long_cpm,long_uSv);
00035 #endif
00036 
00037 }
00038 
00039 void next_geiger_countup(){
00040  
00041     cnt = 0;
00042     if(++cpm_index>=cpm_array_max){
00043         cpm_index=0;
00044     }  
00045     //first touch index count up
00046     if(cpm_index>first_touch_index){
00047         first_touch_index=cpm_index;
00048     }
00049     cpm_array[cpm_index]=cnt;
00050 
00051     // for long term average
00052     if (++count_second >= 60) {
00053         if (++long_cpm_index >= long_cpm_array_max+1) {
00054             long_cpm_index = 0;
00055         }
00056         long_cpm_array[long_cpm_index] = 0;
00057         count_second = 0;
00058 
00059         if(long_cpm_index > long_first_touch_index){
00060             long_first_touch_index = long_cpm_index;
00061         }
00062     }
00063 
00064 }
00065 
00066 
00067 // Timer -- called every 1 min.
00068 Ticker timer;
00069 
00070 void attime(){
00071 
00072     next_geiger_countup();
00073     if(++updateintervalcount > updateinterval){
00074         updateflag=1;
00075         updateintervalcount=0;
00076     }
00077 }
00078 
00079 
00080 void geiger()
00081 {
00082     if(mode==1){
00083         read_dma_data();
00084     }
00085 
00086     cnt++;
00087     cpm_array[cpm_index]=cnt;
00088     long_cpm_array[long_cpm_index]++;
00089     
00090     *outLED=1;
00091     if(sound==1) *buzzer=1;
00092     click.attach(&buzzer_off,0.03);
00093 }
00094 
00095 void whereami() {
00096      if(gps->sample()&& gps->longitude!=0 && gps->latitude!=0) {
00097          printf("GPS:%f/%f,", gps->longitude, gps->latitude);
00098      }
00099 }
00100 
00101 
00102 void console_output(){
00103     char msg[256];
00104     msg[0]='\0';
00105     if(++console_output_flag>2){
00106       printable_msg(msg);
00107       printf("%s",msg);
00108       console_output_flag=0;
00109     }
00110 }
00111 
00112 //NTP setup
00113 void ntpget() {
00114 
00115     Host server(IpAddr(), 123, "jp.pool.ntp.org");
00116     lcd.cls();
00117     lcd.locate(0,0);
00118     lcd.printf("Get NTP.");
00119     ntpRet = ntp.setTime(server);
00120 
00121 }
00122 
00123 void pulldown() {
00124     DigitalIn dp05(p5);     dp05.mode(PullDown);
00125     DigitalIn dp06(p6);     dp06.mode(PullDown);
00126     DigitalIn dp07(p7);     dp07.mode(PullDown);
00127     DigitalIn dp08(p8);     dp08.mode(PullDown);
00128     DigitalIn dp09(p9);     dp09.mode(PullDown);
00129     DigitalIn dp10(p10);    dp10.mode(PullDown);
00130     DigitalIn dp11(p11);    dp11.mode(PullDown);
00131     DigitalIn dp12(p12);    dp12.mode(PullDown);
00132     DigitalIn dp13(p13);    dp13.mode(PullDown);
00133     DigitalIn dp14(p14);    dp14.mode(PullDown);
00134     DigitalIn dp15(p15);    dp15.mode(PullDown);
00135     DigitalIn dp16(p16);    dp16.mode(PullDown);
00136     DigitalIn dp17(p17);    dp17.mode(PullDown);
00137     //DigitalIn dp18(p18);    dp18.mode(PullDown);
00138     DigitalIn dp19(p19);    dp19.mode(PullDown);
00139     DigitalIn dp20(p20);    dp20.mode(PullDown);
00140     DigitalIn dp21(p21);    dp21.mode(PullDown);
00141     //DigitalIn dp22(p22);    dp22.mode(PullDown);
00142     //DigitalIn dp23(p23);    dp23.mode(PullDown);
00143     //DigitalIn dp24(p24);    dp24.mode(PullDown);
00144     DigitalIn dp25(p25);    dp25.mode(PullDown);
00145     //DigitalIn dp26(p26);    dp26.mode(PullDown);
00146     //DigitalIn dp27(p27);    dp27.mode(PullDown);
00147     //DigitalIn dp28(p28);    dp28.mode(PullDown);
00148     //DigitalIn dp29(p29);    dp29.mode(PullDown);
00149     //DigitalIn dp30(p30);    dp30.mode(PullDown);
00150 }
00151 
00152 /****************  main  ***************************/
00153 
00154 int main() {
00155     pulldown();
00156 
00157     AnalogOut tri(p18);
00158     tri = 0.0;
00159 
00160     //cpu_setting();
00161 
00162     // See http://mbed.org/users/no2chem/notebook/mbed-clock-control--benchmarks/
00163     setSystemFrequency(0x3, 0x1, cpu_speed, 1); // Set clock freq 96MHz (full speed) 
00164     SystemCoreClockUpdate();
00165     pc.baud(460800);
00166 
00167     EthernetErr ethErr;
00168 
00169     environment_setting(fwserver);
00170 
00171     // malloc for long average  
00172     long_cpm_array = (unsigned int *)malloc((long_cpm_array_max+1) * sizeof(unsigned int) );
00173     if (long_cpm_array == NULL) {
00174         lcd.cls();
00175         lcd.printf("Can't malloc for long_cpm_array");
00176         pc.printf("Can't malloc for long_cpm_array");
00177         abort();
00178     }
00179     
00180     if (mode != -1) {
00181         switch(updatetype){
00182         case 0:
00183             break;
00184         case 1:
00185             twitter_setup();
00186             break;
00187         case 2:
00188             pachube_setup(&appconf);
00189             break;
00190         default:
00191             lcd.cls();lcd.locate(0,0);
00192             lcd.printf("No update to the internet\n");
00193             break;
00194         }
00195         wait(0.5);
00196     }
00197     
00198   // ******  Initializing objects ********
00199     PachubeV2CSV web(appconf.apikey);
00200     
00201     if (mode==1) {
00202         AnalogIn Sense15_(pin_assign(AnalogInput_pin_number));
00203         Sense15=&Sense15_;
00204     }
00205 
00206     InterruptIn Geiger_signal(pin_assign(GMInterrupt_pin_number)); // Geiger signal
00207 
00208     DigitalOut buzzer_(pin_assign(Buzzer_pin_number));
00209     buzzer=&buzzer_;
00210 
00211     DigitalOut outLED_(pin_assign(LED_pin_number));
00212     outLED=&outLED_;
00213 
00214     GPS gps_(pin_assign(GPStx_pin_number), pin_assign(GPSrx_pin_number));
00215     gps=&gps_;
00216    
00217     if (mode != -1) {
00218         printf("Version: %s\n",VERSION);
00219         lcd.cls();
00220         lcd.printf("Ver %s\n",VERSION);
00221         wait(2);
00222         
00223         char msg[256] = "";
00224         fullcalendarHyoji(msg);
00225         printf("Self Clock: %s\n",msg);
00226         
00227         // Ethernet setup
00228         fwup = new FirmwareUpdater (fwserver, BINNAME, &commonClient,true);
00229     
00230         if (eth0.link()) {
00231             lcd.locate(0,0);lcd.cls();lcd.printf("Setting up the Internet(DHCP)\n");
00232             ethErr = eth.setup();
00233             lcd.cls(); lcd.locate(0,0);
00234             if(ethErr){
00235                 lcd.printf("Please plug into the Internet.");
00236                 printf("Error %d in setup.\n", ethErr);
00237             }else{
00238                 noInternet=0;
00239                 IpAddr ipadr = eth.getIp();
00240                 lcd.printf("IP=%d.%d.%d.%d\n",
00241                     ipadr[0],ipadr[1],ipadr[2],ipadr[3]);
00242                 wait(2);
00243                 if(firmware_update_interval > 0) {
00244                     check_newfirm();
00245                 }
00246                 ntpget();
00247     
00248                 memset((char *)msg, '\0', 256);
00249                 fullcalendarHyoji(msg);
00250                 printf("NTP Clock:  %s\n",msg);
00251             }
00252         }
00253     } // end if (mode != -1)
00254 
00255     if (mode == 1) {
00256         setup_adcdma(); 
00257     }
00258 
00259     // Geiger setup
00260     float stepvolt;
00261     const int step=40;
00262     for (stepvolt=0; stepvolt<volt; ) {
00263         lcd.cls();
00264         lcd.printf("volt=%.0f\n", stepvolt);
00265         tri = stepvolt/voltagedivider/3.3;
00266         wait(0.5);
00267         stepvolt+=step;
00268     }
00269     lcd.cls();
00270     lcd.printf("volt=%.0f\n", volt);
00271     tri = volt/voltagedivider/3.3;
00272     wait(0.5);
00273     
00274     for(int i=10; i>0; i--) {
00275         lcd.cls();
00276         lcd.printf("Wait %2dsec for stabilize voltage",i);
00277         wait(1);
00278     }
00279 
00280     Geiger_signal.rise(&geiger);
00281     Geiger_signal.mode(PullDown);
00282 
00283     // timer interrupt
00284     timer.attach(&attime, CHUNK);
00285 
00286     int firmware_update_term = firmware_update_interval * 60 / (updateinterval/60);
00287     int firmware_update_counter=0;
00288     
00289     if (mode != -1) {
00290         for(int i=init_stabilize_time; i>0; i--) {
00291             lcd.cls();
00292             lcd.printf("Wait %2dsec for stabilize cpm",i);
00293             wait(1);
00294         }
00295     }
00296 
00297     //************* Main Loop ******************//
00298     while(1){
00299 
00300         if (mode == -1) {
00301             LCD_cpm_output();
00302         } else if (mode != 3) {
00303             LCD_time_and_output();
00304         }
00305 //      console_output();
00306 
00307         wait(1.0);
00308         if(mode==2){
00309             whereami();
00310         }
00311         if(mode != -1 && updateflag){
00312             updateflag=0;
00313             if (noInternet==1 && eth0.link()==1){
00314                 ethErr = eth.setup();
00315                 if(ethErr) {
00316                 } else {
00317                     noInternet=0;
00318                     ntpget();
00319                 }
00320             }
00321 
00322             if (!noInternet) {
00323                 switch(updatetype){
00324                 case 0:
00325                     break;
00326                 case 1:          
00327                     twitter_output();
00328                     break;
00329                 case 2:
00330                     printf("pachube\r\n");
00331                     pachube_output(&web, &commonClient);
00332                     printf("data uploaded to pachube\n");
00333                     break;
00334                 default:
00335                     break;
00336                 }
00337         
00338                 if(firmware_update_interval > 0 &&
00339                    firmware_update_counter++ >= firmware_update_term){
00340                     if(fwup->exist()==0) {
00341                         mbed_reset();
00342                     } else {
00343                         firmware_update_counter = 0;
00344                     }
00345                     ntpget();
00346                 }          
00347             }// !noInternet
00348         }//if update flag
00349     }//while
00350 }//main
00351 
00352