/******************************************* * * *******************************************/ #include #include #include #include #include #include #include "a200.h" /* definitions relative to the CAEN system */ #include "a200.hmn" #include "../lib/mylib1.h" #include "../dbase/hv_dbase.h" #define SLEEP_MAX 6 /* number of cycles in which a chan. is put to sleep */ #define SECONDS #define MINUTES * 60 #define NSLOTS 32 #define N_TYPES 3 #define PAUSE 1001 #define CONTINUE 1002 #define STOP 1003 #define COND_PAUSED 0x20000000 /* bit 29 */ #define COND_UP 0x10000000 /* bit 28 */ #define CYCLES_UP 0x000000ff /* bits 0-7 */ #define CYCLES_DOWN 0x00000f00 /* bits 8-11 */ #define STEP_TRIALS 0x0000f000 /* bits 12-15 */ #define COND_END 0x00010000 /* bits 16 : END of CONDITIONING */ #define GOOD_END 0x00020000 /* bits 17 : good end */ #define FAIL 0x00040000 /* bits 18 : fails the step */ #define STEP_NR 0x00f00000 /* bits 20-23 */ #define COND_TYPE 0x07000000 /* bits 24-26 : 7 kinds, at maximum */ #define PROMOVE 0x08000000 /* bit 27 */ #define CYCLES_DOWN_B 8 #define STEP_TRIALS_B 12 #define STEP_NR_B 20 #define COND_TYPE_B 24 SY227_MBOX *sy227_mod; int sem_227mod_id; int sem_cond_id; /* commands to xxcond */ int sem_cond_id1; /* replay from xxcond */ SY127_MBOX *sy127_mod; hv_mod_struct *mid; int sem_127mod_id; int evpro_id; PROCS_INFO *hv_procs_mod; char *caller; char line[80], str[20]; int n_order, alarm[40], sleep_count[40]; int cpu_nr, tcp_ip_nr; FILE *fp, *fp1, *fopen(); int stop_all = 0; struct cond_type{ short v1; short i1; short imax; short cycles_high; short cycles_low; short max_try; }; cond_type conditioner[N_TYPES][MAX_STEPS]; int cond_steps[N_TYPES]; cond_type cond_a[] = { /* V1 I1 imax cyclys_high cycles_low max_try */ 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3 }; int nsteps_a = 3; cond_type cond_b[] = { /* V1 I1 imax cyclys_high cycles_low max_try */ 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3 }; int nsteps_b = 5; cond_type cond_c[] = { /* V1 I1 imax cyclys_high cycles_low max_try */ 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3, 2000, 30, 1000, 10, 2, 3 }; int nsteps_c = 7; int dis, sl, k, ch_exist; short dis0, sl0, hv_ch0; char ch_name[5]; char *p_in = "/pipe/cond_com" char *p_out = "/pipe/cond_replay" int ssignal; sighand(signal) register int signal; { printf(" %s : signal %d received \n", *caller, signal); if ( signal >= 2 && signal <= 3){ /* ^C or ^E ? */ A200_end(); sem_wait(evpro_id, caller); hv_procs_mod->xxcond_id = 0; sem_free(evpro_id, caller); sem_unl(evpro_id, caller); munlink(hv_procs_mod); sem_unl_del(sem_cond_id, "cond_com", caller); sem_unl_del(sem_cond_id1, "cond_replay", caller); 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); } else if ( (signal => 2100 && signal < 2100 + N_TYPES) || (signal => 2200 && signal < 2200 + N_TYPES) || (signal == 2300) ) ssignal = signal; else printf(" xxcond has received an unkown signal %d\n", signal); } main(argc, argv) int argc; char *argv[]; { char *chamber_code; 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; float imean, vmean; short ch_imax, imax, d_vmon[32], d_imon[32], d_status[32]; short hv_ch, 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; short v0_hv_ch; short vmon_hv_ch; short i0_hv_ch; short imon_hv_ch; short old_v1, old_i1; int yy, mm, dd, hh, min, sec; int jtime1, jdate1; int jtime2, jdate2; int time_lost, time_to_sleep, time_past; intercept(sighand); A200_init(); 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]); sem_cond_id = sem_link_cr("cond_com", argv[0]); sem_cond_id1 = sem_link_cr("cond_replay", 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]); evpro_id = sem_link("hv_pr_info", argv[0]); if((hv_procs_mod = (PROCS_INFO *)modlink("hv_pr_info", 0)) == (PROCS_INFO *)-1) exit(_errmsg(errno, " err. link data_mod hv_procs_info, errno %d\n", errno)); 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)); for ( i = 0; i < MAX_STEPS; i++ ){ if ((cond_steps[0] = nsteps_a) <= i){ conditioner[0][i].v1 = cond_a[i].v1; conditioner[0][i].i1 = cond_a[i].i1; conditioner[0][i].imax = cond_a[i].imax; conditioner[0][i].cycles_high = cond_a[i].cycles_high; conditioner[0][i].cycles_low = cond_a[i].cycles_low; conditioner[0][i].max_try = cond_a[i].max_try; } if ((cond_steps[1] = nsteps_b) <= i){ conditioner[1][i].v1 = cond_b[i].v1; conditioner[1][i].i1 = cond_b[i].i1; conditioner[1][i].imax = cond_b[i].imax; conditioner[1][i].cycles_high = cond_b[i].cycles_high; conditioner[1][i].cycles_low = cond_b[i].cycles_low; conditioner[1][i].max_try = cond_b[i].max_try; } if ((cond_steps[2] = nsteps_b) <= i){ conditioner[2][i].v1 = cond_c[i].v1; conditioner[2][i].i1 = cond_c[i].i1; conditioner[2][i].imax = cond_c[i].imax; conditioner[2][i].cycles_high = cond_c[i].cycles_high; conditioner[2][i].cycles_low = cond_c[i].cycles_low; conditioner[2][i].max_try = cond_c[i].max_try; } } chamber_code = "0"; for (i = 0; i < 40; i++) alarm[i] = sleep_count[i] = 0; ncycle = 0; while(1){ ncycle++; 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)); sem_wait(sem_cond_id, argv[0]); 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; for(dis = 0; dis < mid->n_crates; dis++) for(sl = 0; sl < 4; sl++) for(k = 0; k < 2; k++) if (mid->distr[dis].slot[sl].d_used_ch && mid->distr[dis].slot[sl].cond IS_CONDITIONING ){ if (!v1_active) /* if V0 is active the condictioning is paused */ mid->distr[dis].slot[sl].cond |= COND_PAUSED; else { /* conditioning active */ mid->distr[dis].slot[sl].cond &= ~COND_PAUSED; } n_order = -1; if (v1_active) for(dis = 0; dis < mid->n_crates; dis++) for(sl = 0; sl < 4; sl++) for(k = 0; k < 2; k++) if (mid->distr[dis].slot[sl].d_used_ch && mid->distr[dis].slot[sl].cond IS_CONDITIONING ){ 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)); hv_ch = mid->distr[dis].slot[sl].hv_ch; cond_id = (mid->distr[dis].slot[sl].cond & COND_TYPE) >> COND_TYPE_B; /* get parameters for next steps */ if ( mid->distr[dis].slot[sl].cond & PROMOVE ){ step_nr = (mid->distr[dis].slot[sl].cond & STEP_NR) >> STEP_NR_B; if (step_nr--){ mid->distr[dis].slot[sl].cond &= ~STEP_NR; /* clear bits */ mid->distr[dis].slot[sl].cond |= step_nr << STEP_NR_B; mid->distr[dis].slot[sl].cond |= COND_UP; mid->distr[dis].slot[sl].cond &= ~(STEP_TRIALS | CYCLES_UP CYCLES_DOWN | PROMOVE ); HV_Set(V1, mid->hv_crate, hv_ch, conditioner[cond_id][cond_steps[cond_id] - step_nr].v1); HV_Set(I1, mid->hv_crate, hv_ch, conditioner[cond_id][cond_steps[cond_id] - step_nr].i1); } else { mid->distr[dis].slot[sl].cond |= ( COND_END + GOOD_END ); sem_wait(sem_127mod_id, argv[0]); HV_Set(V1, mid->hv_crate, hv_ch, sy127_mod->hv_par[0].v1[hv_ch] ); HV_Set(I1, mid->hv_crate, hv_ch, sy127_mod->hv_par[0].i1[hv_ch] ); sem_free(sem_127mod_id, argv[0]); } } if(!(mid->distr[dis].slot[sl].cond & COND_END)) { step_nr = (mid->distr[dis].slot[sl].cond & STEP_NR) >> STEP_NR_B; sem_wait(sem_127mod_id, argv[0]); status_hv_ch = sy127_mod->hv_par[1].status[hv_ch]; v0_hv_ch = sy127_mod->hv_par[1].v0[hv_ch]; vmon_hv_ch = sy127_mod->hv_par[1].vmon[hv_ch]; i0_hv_ch = sy127_mod->hv_par[1].i0[hv_ch]; imon_hv_ch = sy127_mod->hv_par[1].imon[hv_ch]; sem_free(sem_127mod_id, argv[0]); /* distributors */ 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.; for (i = 0; i < 32; i++){ if (mid->distr[dis].slot[sl].d_used_ch & ( 1 << i ) ) { if ( ! (d_status[i] D_IS_OK ) ) n_bad++; 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.; } n_order++; if ( alarm[n_order] && !( mid->distr[dis].slot[sl].cond IS_CONDITIONING ) ) alarm[n_order] = 0; if (!alarm[n_order]) { /* is in the state ON */ sprintf(cline, "127_st: %2d; imean %4.0f, imax %2d ", status_hv_ch, imean, imax); write(pout, cline, strlen(cline)); if ( status_hv_ch IS_OVERV || status_hv_ch IS_TRIP || ((status_hv_ch IS_ON) && !(on_off D_RELAY_IS_ON) ) || ( imax > conditioner[cond_id][cond_steps[cond_id]-step_nr].imax)){ /* something BAD -> OFF */ sprintf(cline, "-> OFF\n"); write(pout, cline, strlen(cline)); alarm[n_order] = 1; mid->distr[dis].slot[sl].cond &= ~COND_UP; ntry = mid->distr[dis].slot[sl].cond & STEP_TRIALS; if ((mid->distr[dis].slot[sl].cond & STEP_TRIALS = ++ntry) > conditioner[cond_id][cond_steps[cond_id]-step_nr].max_try){ mid->distr[dis].slot[sl].cond |= COND_END; mid->distr[dis].slot[sl].cond &= ~GOOD_END; } else { strcpy(line, "hv_switch "); strcat(line, mid->distr[dis].slot[sl].cable[k].cable_name); strcat(line, " off "); strcat(line, argv[0]); strcat(line, " >>>/nil"); data_ora(&yy, &mm, &dd, &hh, &min, &sec); } } else { /* is in status ON and remains OK */ cycles_up = mid->distr[dis].slot[sl].cond & CYCLES_UP; mid->distr[dis].slot[sl].cond &= ~CYCLES_UP; mid->distr[dis].slot[sl].cond |= cycles_up + 1; sprintf(cline, " \n"); write(pout, cline, strlen(cline)); } } else if( sleep_count[n_order] > conditioner[cond_id][step_nr].cycles_low ) { /* was in status OFF: -> ON */ mid->distr[dis].slot[sl].cond |= COND_UP; mid->distr[dis].slot[sl].cond &= ~CYCLES_UP; sprintf(cline, " -> ON\n"); write(pout, cline, strlen(cline)); alarm[n_order] = 0; sleep_count[n_order] = 0; strcpy(line, "hv_switch "); strcat(line, mid->distr[dis].slot[sl].cable[k].cable_name); strcat(line, " on "); strcat(line, argv[0]); strcat(line, " >>>/nil"); data_ora(&yy, &mm, &dd, &hh, &min, &sec); system(line); } else { /* "SLEEPING" */ /* remains in status OFF */ sleep_count[n_order]++; mid->distr[dis].slot[sl].cond &= ~CYCLES_DOWN; mid->distr[dis].slot[sl].cond |= sleep_count[n_order] << CYCLES_DOWN_B; sprintf(cline, " SLEEPING\n"); write(pout, cline, strlen(cline)); } } } sem_free(sem_cond_id, argv[0]); ssignal = 0; time_to_sleep = 60; /* time in seconds */ _sysdate(1, &jtime1, &jdate1, &day, &tick); sem_wait(evpro_id, argv[0]); hv_procs_mod->xxcond_active = 0; sem_free(evpro_id, argv[0]); sleep (time_to_sleep); /* check if there are commands ( PAUSE/CONTINUE/STOP ) */ sem_wait(evpro_id, argv[0]); command = hv_procs_mod->xxcond_com; hv_procs_mod->xxcond_com = 0; /* reset immediately command */ hv_procs_mod->xxcond_active = 1; sem_free(evpro_id, argv[0]); if(command == PAUSE) sleep(0); else if(command == STOP) break; sem_wait(evpro_id, argv[0]); hv_procs_mod->xxcond_active = 1; sem_free(evpro_id, argv[0]); sem_wait(sem_cond_id, argv[0]); if ( (ssignal >= 2100 && ssignal < 2100 + N_TYPES) || /* start */ (ssignal >= 2200 && ssignal < 2200 + N_TYPES) )) { /* stop */ if ( ( pid_in = open(p_in, S_IREAD) ) == -1 ) exit(_errmsg(errno ," cannot open %s \n", pipe)); if ( ( nread = read(pid_in, ch_name, 5)) < 5) exit(_errmsg(0, " bad cable_name from pipe: '%s'\n", ch_name); ch_exist = 0; dis0 = -1; sl0 = -1; ch0 = -1; for(dis = 0; dis < mid->n_crates; dis++) for(sl = 0; sl < 4; sl++) for(k = 0; k < 2; k++) if(strcmp(ch_name, mid->distr[dis].slot[sl].cable[k].cable_name) == 0){ ch_exist++; dis0 = (short) dis; sl0 = (short) sl; ch0 = mid->distr[dis].slot[sl].hv_ch; if ( (!mid->distr[dis].slot[sl].cond IS_CONDITIONING) && ssignal >= 2200 ) strcpy(ret_code, "3"); /* slot not in conditioning */ else if ( (mid->distr[dis].slot[sl].cond IS_CONDITIONING) && ssignal < 2200 ) strcpy(ret_code, "2"); /* slot already in conditioning */ } if (ch_exist) strcpy(ret_code, "0"); /* OK */ else strcpy(ret_code, "1"); /* slot does not exist */ if((pid_out = create(p_out, 0x03+S_ISIZE, 0x03, strlen(ret_code))) == -1){ printf(" tubi_sitter : errno %d\n", errno); exit(_errmsg(errno," error creating %s\n",pipe)); } write(pid, ret_code, strlen(ret_code)); if (ssignal < 2200){ /* start new conditioning */ /* bit 31 ( 0-31 ! ) : conditioning bits 24-27 : conditioning type bits 20-23 :step_nr nr ( goes from nstps_.. to 0 ( end )) bits 0-8 : number on minutes ( c.a xxcond cycles ) still to remain in this on this cycle */ mid->distr[dis0].slot[sl0].cond |= CONDITIONING; mid->distr[dis0].slot[sl0].cond &= 0xf0000000; /* clean bits to use */ mid->distr[dis0].slot[sl0].cond |= PROMOVE; cond_id = ssignal - 2200; mid->distr[dis0].slot[sl0].cond |= cond_id << COND_TYPE_B; mid->distr[dis0].slot[sl0].cond |= (cond_steps[cond_id]+1) << STEP_NR_B; else { /* stop conditioning of one slot */ mid->distr[dis0].slot[sl0].cond |= ~CONDITIONING; mid->distr[dis0].slot[sl0].cond &= 0xf0000000; /* clean bits to use */ HV_Set (SWITCH, mid->hv_crate, ch0, OFF); sem_wait(sem_127mod_id, argv[0]); old_v1 = sy127_mod->hv_par[0].v1[ch0]; old_i1 = sy127_mod->hv_par[0].i1[ch0]; sem_free(sem_127mod_id, argv[0]); HV_Set (V1, mid->hv_crate, hv_ch0, old_v1); HV_Set (I1, mid->hv_crate, hv_ch0, old_i1); } } else if(ssignal == 2300){ } sem_free(sem_cond_id, argv[0]); _sysdate(1, &jtime2, &jdate2, &day, &tick); time_past = ( jdate2 - jdate1) * 86399 + ( jtime2 - jtime1 ); if ( (time_to_sleep - time_past) > 0) sleep (time_to_sleep - time_past); } /* end of while(0) */ A200_end(); sem_wait(evpro_id, caller); hv_procs_mod->xxcond_id = 0; sem_free(evpro_id, caller); sem_unl(evpro_id, caller); munlink(hv_procs_mod); sem_unl_del(sem_cond_id, "cond_com", caller); sem_unl_del(sem_cond_id1, "cond_replay", caller); sem_unl_del(sem_127mod_id, "SY127mod", caller); sem_unl_del(sem_227mod_id, "SY127mod", caller); munlink(sy127_mod); munlink(sy227_mod); munlink(mid); } if (mid->distr[dis].slot[sl].cond & COND_UP){ cycles_up = mid->distr[dis].slot[sl].cond & CYCLES_UP; if (cycles_up--){ mid->distr[dis].slot[sl].cond = cycles_up; } else mid->distr[dis].slot[sl].cond |= PROMOVE;