/******************************************* * tubi_sitter.c * * * *******************************************/ #include #include #include #include #include #include #include #include "a200.h" /* definitions relative to the CAEN system */ #include "a200.hmn" #include "../lib/comx.h" #include "../lib/mylib1.h" #include "../dbase/hv_dbase.h" #define SLEEP_MAX 0 /* nr of cycles for which a chan. is put to sleep */ #define SECONDS #define MINUTES * 60 #define TWENTY_S 20 /* timeout value ( in seconds ) */ SY227_MBOX *sy227_mod; int sem_227mod_id; SY127_MBOX *sy127_mod; hv_mod_struct *mid; int sem_127mod_id; char *caller; char line[80], cline[80], tmpfile[64], tmpfile_h[64]; int n_order, alarm[40], sleep_count[40]; int cpu_nr, tcp_ip_nr; FILE *fp, *fp1, *fp2, *fopen(); char *outfile = "tubi_sitter.log"; char *outfile_h = "tubi_sitter_hosp.log"; char *outfile_m = "tubi_sitter.msg"; char *tmpfile_0 = "t_s_temp"; char *tmpfile_h_0 = "t_s_h_temp"; char *remfile = "tubi_sitter.log"; char *remfile_h = "tubi_sitter_hosp.log"; int pout, pout1; char *scatola_nera = "scatola_nera"; char *fili_caldi = "fili_caldi"; int hosp; int nout1, nout2, nout3; int err1, err2, err3; int to_be_sent, to_be_sent_h; sighand(signal) register int signal; { if ( signal >= 2 && signal <= 3){ /* ^C or ^E ? */ A200_end(); sem_unl_del(sem_127mod_id, "sy127mod", caller); munlink(sy127_mod); sem_unl(sem_227mod_id, "sy227mod", caller); munlink(sy227_mod); munlink(mid); exit(0); } } main(argc, argv) int argc; char *argv[]; { char chamber_code[6]; char old_chamber[6]; char *mod_name0 = "hv_config0"; char *mod_name1 = "hv_config1"; char *mod_name2 = "hv_config2"; char *mod_name, *hv_config[3]; int dis, sl; int ch1, ch2, first_print; int nwrite, nerror, par_val; short ndata, data[45], prot_word; short i, ir, j, k, chan, value, code; int n_good, n_bad; int v1_active, v1_active_old; float imean, vmean; short ch_imax, imax, d_vmon[32], d_imon[32], d_status[32]; short hv_ch_A, hv_ch_Ref, on_off, hv_in; int sy127_mod_vim_tim[32]; int sy127_mod_sta_tim[32]; int sy227_mod_vim_tim[32]; int sy227_mod_sta_tim[32]; int sy127_mod_nvim; int sy127_mod_nvim_err; int sy127_mod_nsta; int sy127_mod_nsta_err; int sy227_mod_nvim; int sy227_mod_nvim_err; int sy227_mod_nsta; int sy227_mod_nsta_err; short status_hv_ch_Ref, status_hv_ch_A; short v0_hv_ch_Ref, v0_hv_ch_A; short vmon_hv_ch_Ref, vmon_hv_ch_A; short i0_hv_ch_Ref, i0_hv_ch_A; short imon_hv_ch_Ref, imon_hv_ch_A; int yy, mm, dd, hh, min, sec; int jtime1, jdate1; int jtime2, jdate2; int tick; short day; int time_lost, time_to_sleep; int ncycle, ncycle1, n_anomalous[40]; int n_anomalous_1[40], n_anomalous_2[40]; int skip_ch, switch_off; int send_to_vax = 0; int check_distr = 1; /* if =0 no averages on distr */ int switched = 0; int new_chamber = 1; int history_reset; /* 0 : do nothing -1 : no_reset at the beginning 1 : force a reset and exit */ int sitter_cond; history_reset = 0; if ( argc > 1 ){ upperword(argv[1]); if (strcmp(argv[1],"NORESET") == 0 ) history_reset = -1; else if (strcmp(argv[1],"RESET") == 0 ) history_reset = 1; else history_reset = 0; } strcpy(old_chamber, "??"); intercept(sighand); if ((pout = create(scatola_nera, 0x03, 0x03)) == -1) if ((pout = open(scatola_nera, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",scatola_nera)); close(pout); if ((pout1 = create(fili_caldi, 0x03, 0x03)) == -1) if ((pout1 = open(fili_caldi, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",fili_caldi)); close(pout1); if((sy127_mod = (SY127_MBOX *)modlink("sy127mod", 0)) == (SY127_MBOX *) -1) exit(_errmsg(errno,"tubi_sitter: err. link data module\n")); sem_127mod_id = sem_link_cr("SY127mod", argv[0]); if((sy227_mod = (SY227_MBOX *)modlink("sy227mod", 0)) == (SY227_MBOX *) -1) exit(_errmsg(errno,"tubi_sitter: err. link data module\n")); sem_227mod_id = sem_link_cr("SY227mod", argv[0]); get_config(&tcp_ip_nr, &cpu_nr, hv_config); mod_name = hv_config[cpu_nr]; if ((mid = (hv_mod_struct *)modlink(mod_name, 0)) == (hv_mod_struct *)-1) if ((mid = (hv_mod_struct *)modload(mod_name, 0)) == (hv_mod_struct *)-1) exit(_errmsg(errno, "%s: error linking module %s\n", argv[0], mod_name)); strcpy (chamber_code, "0"); for (i = 0; i < 40; i++) { alarm[i] = sleep_count[i] = n_anomalous[i] = 0; n_anomalous_1[i] = n_anomalous_2[i] = 0; } if( ( fp = ( FILE *) fopen(outfile, "a+")) == NULL){ sprintf(cline, "%s cannot open %s", argv[0], outfile); write(pout, cline, strlen(cline)); } fprintf(fp," codes of chamber/slot status ( bit 1 is the LSB ):\n\n"); fprintf(fp," bit nr. 1: HV Ref is OVERVOLTAGE\n"); fprintf(fp," 2: HV Ref is UNDERVOLTAGE\n"); fprintf(fp," 3: HV Ref is TRIP\n"); fprintf(fp," 4: HV A is OVERVOLTAGE\n"); fprintf(fp," 5: HV A is UNDERVOLTAGE\n"); fprintf(fp," 6: HV A is TRIP\n"); fprintf(fp," 7: Distributor Slot anomalously in Relais-OFF\n"); fprintf(fp," 8: Distributor Slot anomalously in Switch-OFF\n\n"); fclose(fp); ncycle = ncycle1 = 0; while(1){ ncycle++; ncycle1++; if ((pout = open(scatola_nera, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",scatola_nera)); if (lseek(pout, 0, 0) == -1) exit(_errmsg(errno," error rewinding %s\n",scatola_nera)); data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d Sittering starts\n",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); if ((pout1 = open(fili_caldi, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",fili_caldi)); if (lseek(pout1, 0, 0) == -1) exit(_errmsg(errno," error rewinding %s\n",fili_caldi)); data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d Sittering starts\n",dd, mm, yy, hh, min); write(pout1, cline, strlen(cline)); sem_wait(sem_127mod_id, argv[0]); sy127_mod->sit_tim[0] = yy; sy127_mod->sit_tim[1] = mm; sy127_mod->sit_tim[2] = dd; sy127_mod->sit_tim[3] = hh; sy127_mod->sit_tim[4] = min; sy127_mod->sit_tim[5] = sec; sem_free(sem_127mod_id, argv[0]); to_be_sent = 0; to_be_sent_h = 0; n_order = -1; sem_wait(sem_127mod_id, argv[0]); prot_word = sy127_mod->hv_par[1].prot_word; sem_free(sem_127mod_id, argv[0]); v1_active = prot_word V1_ACTIVE; if (ncycle == 1) { v1_active_old = v1_active; if ( history_reset != -1 ) mid->sitter_v1_controls = 0; } if (v1_active != v1_active_old) { v1_active_old = v1_active; sleep(180); } if (v1_active) mid->sitter_v1_controls++; for(dis = 0; dis < mid->n_crates; dis++) for(sl = 0; sl < 4; sl++) /* successivamente si potrebbe chiedere soltanto una camera */ if((strcmp(chamber_code, mid->distr[dis].slot[sl].chamber) == 0 || strcmp(chamber_code, mid->distr[dis].slot[sl].cable_name) == 0 || ( strcmp("0", chamber_code) == 0 && mid->distr[dis].slot[sl].hv_ch_Ref >= 0) ) && mid->distr[dis].slot[sl].d_used_ch && !(mid->distr[dis].slot[sl].cond IS_CONDITIONING) ){ /* not cond */ if (strcmp(old_chamber, mid->distr[dis].slot[sl].chamber) ){ switched = 0; new_chamber = 1; /*** ATT ! il controllo e' fatto soltanto per la prima slot di una camera ( in realta' si controlla soltanto l'alimentatore ! ). Ricordarsene se si vogliono mettere anche dei controlli sui distributori ****/ strcpy(old_chamber, mid->distr[dis].slot[sl].chamber); } else { new_chamber = 0; } data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d ",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); sprintf(cline, "cr %2d, sl %2d : ",dis +1 , sl); write(pout, cline, strlen(cline)); if (strcmp(mid->distr[dis].slot[sl].chamber,"00") == 0) hosp = 1; else hosp = 0; if (mid->distr[dis].slot[sl].d_used_ch){ hv_ch_Ref = mid->distr[dis].slot[sl].hv_ch_Ref; hv_ch_A = mid->distr[dis].slot[sl].hv_ch_A; sem_wait(sem_127mod_id, argv[0]); status_hv_ch_Ref = sy127_mod->hv_par[1].status[hv_ch_Ref]; v0_hv_ch_Ref = sy127_mod->hv_par[1].v0[hv_ch_Ref]; vmon_hv_ch_Ref = sy127_mod->hv_par[1].vmon[hv_ch_Ref]; i0_hv_ch_Ref = sy127_mod->hv_par[1].i0[hv_ch_Ref]; imon_hv_ch_Ref = sy127_mod->hv_par[1].imon[hv_ch_Ref]; status_hv_ch_A = sy127_mod->hv_par[1].status[hv_ch_A]; v0_hv_ch_A = sy127_mod->hv_par[1].v0[hv_ch_A]; vmon_hv_ch_A = sy127_mod->hv_par[1].vmon[hv_ch_A]; i0_hv_ch_A = sy127_mod->hv_par[1].i0[hv_ch_A]; imon_hv_ch_A = sy127_mod->hv_par[1].imon[hv_ch_A]; sem_free(sem_127mod_id, argv[0]); if (strcmp(mid->distr[dis].slot[sl].chamber,"01")) { /* distributors */ if(check_distr) { sem_wait(sem_227mod_id, argv[0]); on_off = sy227_mod->sl_par[dis][sl].onoff; hv_in = sy227_mod->sl_par[dis][sl].hvin; for ( i = 0; i < 32; i++) { d_vmon[i] = sy227_mod->sl_par[dis][sl].vmon[i]; d_imon[i] = sy227_mod->sl_par[dis][sl].imon[i]; d_status[i] = sy227_mod->sl_par[dis][sl].status[i]; } sem_free(sem_227mod_id, argv[0]); } n_bad = 0; n_good = 0; ch_imax = -1; imax = -1; imean = 0.; vmean = 0.; if (check_distr) { if (ncycle == 1 && history_reset != -1) mid->distr[dis].slot[sl].bad = 0; for (i = 0; i < 32; i++){ if (ncycle == 1 && history_reset != -1) { mid->distr[dis].slot[sl].imass[i] = 0; mid->distr[dis].slot[sl].imedia[i] = 0.; mid->distr[dis].slot[sl].nvolte[i] = 0; } if (mid->distr[dis].slot[sl].d_used_ch & ( 1 << i ) ) { if ( !(d_status[i] D_IS_OK) && history_reset != 1){ n_bad++; if ( !(status_hv_ch_Ref IS_RAMPU) && !(status_hv_ch_Ref IS_RAMPD) && !(status_hv_ch_A IS_RAMPU) && !(status_hv_ch_A IS_RAMPD) ) { if (d_imon[i] > mid->distr[dis].slot[sl].imass[i]) mid->distr[dis].slot[sl].imass[i] = d_imon[i]; mid->distr[dis].slot[sl].nvolte[i]++; mid->distr[dis].slot[sl].imedia[i] = ( mid->distr[dis].slot[sl].imedia[i] * ((float)mid->distr[dis].slot[sl].nvolte[i]-1.) + (float)d_imon[i] ) / (float)mid->distr[dis].slot[sl].nvolte[i]; sprintf(cline, "> %s (cr %2d, sl %2d) ch %2d : ", mid->distr[dis].slot[sl].cable_name, dis +1 , sl, i); write(pout1, cline, strlen(cline)); sprintf(cline, "V_A = %4d, V_Ref = %4d, ", vmon_hv_ch_A, vmon_hv_ch_Ref); write(pout1, cline, strlen(cline)); sprintf(cline, "v = %4d, i = %4d \n", d_vmon[i], d_imon[i]); write(pout1, cline, strlen(cline)); } } else { imean += (float) d_imon[i]; vmean += (float) d_vmon[i]; n_good++; } if ( d_imon[i] > imax ){ ch_imax = i; imax = d_imon[i]; } } } if ( n_good > 0 ){ imean = imean / (float)(n_good); vmean = vmean / (float)(n_good); } else { imean = -1.; vmean = -1.; } skip_ch = 0; switch_off = 0; if ( !(on_off D_SWITCH_IS_ON) ) { /* non perde tempo dietro */ switch_off = 1; /* le slot non leggibili */ n_bad = 0; imean = 0.; imax = 0.; ch_imax = -1; } } /* end if(check_distr) */ } else { skip_ch = 1; n_bad = 0; imean = 0.; imax = 0.; ch_imax = -1; } n_order++; sprintf(cline, "127_st: %2d,%2d; %4.0f, n_bad %2d ", status_hv_ch_Ref, status_hv_ch_A, imean, n_bad); write(pout, cline, strlen(cline)); if ( n_anomalous_2[n_order] >= 100 && mid->distr[dis].slot[sl].bad == 0 ) n_anomalous_2[n_order] = 0; if (status_hv_ch_Ref IS_ON && !(on_off D_RELAY_IS_ON) && !skip_ch ){ n_anomalous[n_order]++; if ( n_anomalous[n_order] > 1 ) { n_anomalous_2[n_order] += 5; printf(" n_anomalous[%d] = %d \n", n_order, n_anomalous[n_order] ); } } else { n_anomalous[n_order] = 0; if ( n_anomalous_2[n_order] < 100) n_anomalous_2[n_order]--; if ( n_anomalous_2[n_order] < 0 ) n_anomalous_2[n_order] = 0; } /* anomalous_2 e' incrementato di 5 se n_anomalous > 1 e' decrementato di 1 se n_anomalous = 0 e' lasciato invariato se n_anomalous = 1 -> se diventa negativo viene posto a 0 -> se arriva a 15 la slot e' dichiarata bad e non viene piu' controllata */ sitter_cond = 0; if(status_hv_ch_Ref IS_OVERV) sitter_cond |= 1; if(status_hv_ch_Ref IS_UNDERV) sitter_cond |= 1<<1; if(status_hv_ch_Ref IS_TRIP) sitter_cond |= 1<<2; if(status_hv_ch_A IS_OVERV) sitter_cond |= 1<<(0+3); if(status_hv_ch_A IS_UNDERV) sitter_cond |= 1<<(1+3); if(status_hv_ch_A IS_TRIP) sitter_cond |= 1<<(2+3); if(n_anomalous[n_order] >= 2){ if( on_off D_SWITCH_IS_ON ) sitter_cond |= 1<<(0+6); else sitter_cond |= 1<<(1+6); } if ( n_anomalous_2[n_order] >= 15 && n_anomalous_2[n_order]<100){ mid->distr[dis].slot[sl].bad = 1; } if( (( ( status_hv_ch_Ref IS_OVERV || status_hv_ch_Ref IS_UNDERV ) && !(status_hv_ch_Ref IS_RAMPU) && !(status_hv_ch_Ref IS_RAMPD) ) || status_hv_ch_Ref IS_TRIP || ( ( status_hv_ch_A IS_OVERV || status_hv_ch_A IS_UNDERV )&& !(status_hv_ch_A IS_RAMPU) && !(status_hv_ch_A IS_RAMPD) ) || status_hv_ch_A IS_TRIP || ( n_anomalous[n_order] >= 2 && mid->distr[dis].slot[sl].bad == 0 ) /* || ( imax > 3000 || n_bad > 5 ) */ ) && history_reset != 1 ) { sprintf(cline, "-> ON\n"); write(pout, cline, strlen(cline)); if (n_anomalous[n_order] >= 2) n_anomalous[n_order] = 0; if ( !switched ) { switched++; system("a200_resetter 0"); strcpy(line, "hv_switch "); strcat(line, mid->distr[dis].slot[sl].chamber); strcat(line, " on "); strcat(line, argv[0]); strcat(line, " >>>/nil"); system(line); } data_ora(&yy, &mm, &dd, &hh, &min, &sec); if( ( fp = ( FILE *) fopen(outfile, "a+")) == NULL){ sprintf(cline, "%s cannot open %s", argv[0], outfile); write(pout, cline, strlen(cline)); } nout1 = fprintf(fp, "%2d/%2d %2d:%2d %s ", dd, mm, hh, min, mid->distr[dis].slot[sl].cable_name); err1 = ferror(fp); nout2 = fprintf(fp, " V=%4d,V_A=%4d,St=%3o(Oct);=%4.0f,imax=%4d(ch%2d),", vmon_hv_ch_Ref,vmon_hv_ch_A, sitter_cond, imean, imax, ch_imax); err2 = ferror(fp); nout3 = fprintf(fp, "n_bad=%2d\n", n_bad); err3 = ferror(fp); fclose(fp); if (err1 || err2 || err3 ){ sprintf(cline, " err1, err2, err3 : %d, %d, %d \n", err1, err2, err3); write(pout, cline, strlen(cline)); } } else if ( mid->distr[dis].slot[sl].bad == 1 && n_anomalous_2[n_order] < 100 ) { n_anomalous_2[n_order] = 100; sprintf(cline, " -> *BAD*\n"); write(pout, cline, strlen(cline)); data_ora(&yy, &mm, &dd, &hh, &min, &sec); if( ( fp = ( FILE *) fopen(outfile, "a+")) == NULL){ sprintf(cline, "%s cannot open %s", argv[0], outfile); write(pout, cline, strlen(cline)); } nout1 = fprintf(fp, "%2d/%2d %2d:%2d %s ", dd, mm, hh, min, mid->distr[dis].slot[sl].cable_name); err1 = ferror(fp); nout2 = fprintf(fp, " V=%4d,V_A=%4d,St=%3o(Oct): -> *BAD*\n", vmon_hv_ch_Ref,vmon_hv_ch_A, sitter_cond); err2 = ferror(fp); fclose(fp); if (err1 || err2){ sprintf(cline, " err1, err2 :%d, %d \n", err1, err2 ); write(pout, cline, strlen(cline)); } } else { sprintf(cline, " \n"); write(pout, cline, strlen(cline)); } } } /* end if on used slot */ if (ncycle == 1 && history_reset == 1 ){ sem_unl_del(sem_127mod_id, "sy127mod", caller); munlink(sy127_mod); sem_unl(sem_227mod_id, "sy227mod", caller); munlink(sy227_mod); munlink(mid); close(pout); close(pout1); exit(0); } time_lost = 0; time_to_sleep = 60 - time_lost; /* time in seconds */ if (time_to_sleep < 1) time_to_sleep = 1; data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d *** CYCLE FINISHED *******\n\n\n", dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); write(pout1, cline, strlen(cline)); sprintf(cline, " Ignore following lines \n\n\n"); write(pout, cline, strlen(cline)); write(pout1, cline, strlen(cline)); close(pout); close(pout1); /* if (ncycle1 == 60){ ncycle1 = 0; system("date >>>+fil_parade.log; fil_parade 1000 >>>+fil_parade.log"); } */ sleep( time_to_sleep); } sem_unl_del(sem_127mod_id, "SY127mod", caller); sem_unl_del(sem_227mod_id, "SY127mod", caller); munlink(sy127_mod); munlink(sy227_mod); munlink(mid); }