/*************************************** * d_hv_switch.c * * * * Switch sincronously one HV channel * * and one Distr. slot * ***************************************/ #include #include #include #include #include "../lib/mylib1.h" #include "a200.h" /* definitions relative to the CAEN system */ #include "a200.hmn" /* definitions relative to the CAEN system */ SY127_MBOX *hv_mod; int mev_id; char *caller; char hv_A_sem_name[11], hv_Ref_sem_name[11], d_sem_name[11]; int hv_A_sem_id, hv_Ref_sem_id, d_sem_id; sighand(signal) register int signal; { printf(" %s : signal %d received \n",*caller, signal); if ( signal >= 2 && signal <= 3){ /* ^C or ^E ? */ A200_end(); sem_unl_del(hv_A_sem_id, hv_A_sem_name, caller); sem_unl_del(hv_Ref_sem_id, hv_Ref_sem_name, caller); sem_unl_del(d_sem_id, d_sem_name, caller); exit(0); } } main(argc, argv) int argc; char *argv[]; { short ndata, data[33]; short i, j, value, value_A, value_Ref, code; int d_status[4], hv_status_A, hv_status_Ref ; int d_global_status; char *command_code; short d_crate[4], d_slot[4], nslots, n_on; short hv_crate, chan_A, chan_Ref, command; intercept(sighand); caller = argv[0]; if (argc < 13 || *argv[1] =='-'){ printf( " d_hv_switch : switch sincronously one HV chan and one Distr. slot\n"); printf("\n usage : d_hv_switch d_crate1 d_slot1 ... d_dlot4 hv_crate "); printf("chan_A chan_Ref command\n"); printf("\n ( the command can be 1/0 or on/off \n"); printf(" ( if less than 4 slots are associated to \n"); printf(" a chamber, the not used d_crate/d_slots \n"); printf(" has to set to negative number ) \n"); exit(0); } nslots = 0; d_crate[0] = (short) atoi(argv[1]); d_slot[0] = (short) atoi(argv[2]); if (d_crate[0] >= 0 && d_slot[0] >= 0) nslots++; d_crate[1] = (short) atoi(argv[3]); d_slot[1] = (short) atoi(argv[4]); if (d_crate[1] >= 0 && d_slot[1] >= 0) nslots++; d_crate[2] = (short) atoi(argv[5]); d_slot[2] = (short) atoi(argv[6]); if (d_crate[2] >= 0 && d_slot[2] >= 0) nslots++; d_crate[3] = (short) atoi(argv[7]); d_slot[3] = (short) atoi(argv[8]); if (d_crate[3] >= 0 && d_slot[3] >= 0) nslots++; hv_crate = (short) atoi(argv[9]); chan_A = (short) atoi(argv[10]); chan_Ref = (short) atoi(argv[11]); command_code = argv[12]; if (!isdigit( *command_code)) { if ((command = (short) find_code(command_code)) < 0 ) exit(_errmsg(0," %s error decoding value to be set\n",caller)); } else command = (short) atoi(command_code); hv_ev_name(hv_crate, chan_A, hv_A_sem_name); hv_ev_name(hv_crate, chan_Ref, hv_Ref_sem_name); hv_ev_name(d_crate[0], d_slot[0], d_sem_name); /* un semaforo associato alla prima slot e' sufficiente */ hv_A_sem_id = sem_link_cr(hv_A_sem_name, caller); hv_Ref_sem_id = sem_link_cr(hv_Ref_sem_name, caller); d_sem_id = sem_link_cr(d_sem_name, caller); A200_init(); sem_wait(hv_A_sem_id, caller); sem_wait(hv_Ref_sem_id, caller); sem_wait(d_sem_id, caller); /* check before status of channels */ for ( i = 0; i < nslots; i++){ ndata = 32; if ((code = D_Read( D_ONOFF, d_crate[i], d_slot[i], ALL, data, &ndata)) != 0) printf(" d_hv_switch: error %d calling D_Read: \n %s\n", code, A200_Error(code)); if (data[0] D_RELAY_IS_ON) d_status[i] = ON; else d_status[i] = OFF; } if ((code = HV_Read_One( STATUS, hv_crate, chan_A, &value)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n%s\n", code, A200_Error(code)); if (value IS_ON) hv_status_A = ON; else hv_status_A = OFF; if ((code = HV_Read_One( STATUS, hv_crate, chan_Ref, &value)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n%s\n", code, A200_Error(code)); if (value IS_ON) hv_status_Ref = ON; else hv_status_Ref = OFF; n_on = 0; for ( i = 0; i < nslots; i++){ if ( d_status[i] == ON ) n_on++; } if ( n_on == 0) d_global_status = OFF; else if (n_on == nslots) d_global_status = ON; else d_global_status = ONOFF; if (command == OFF){ if (d_global_status == OFF && hv_status_A == OFF && hv_status_Ref == OFF ) esci(); else if ( d_global_status == OFF && ( hv_status_A == ON || hv_status_Ref == ON) ){ if ( d_global_status == OFF && hv_status_A == ON ) HV_Set( SWITCH, hv_crate, chan_A, OFF); if ( d_global_status == OFF && hv_status_Ref == ON ) HV_Set( SWITCH, hv_crate, chan_Ref, OFF); esci(); } else if (d_global_status == ON && hv_status_A == OFF && hv_status_Ref == OFF){ for ( i = 0; i < nslots; i++ ) D_Set( D_ONOFF, d_crate[i], d_slot[i], ALL, OFF); esci(); } else if (d_global_status != OFF && ( hv_status_A == ON || hv_status_Ref == ON) ){ HV_Set( SWITCH, hv_crate, chan_Ref, OFF); HV_Set( SWITCH, hv_crate, chan_A, OFF); sleep(10); do { sleep(2); if ((code = HV_Read_One( STATUS, hv_crate, chan_A, &value_A)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n %s\n", code, A200_Error(code)); if ((code = HV_Read_One( STATUS, hv_crate, chan_Ref, &value_Ref)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n %s\n", code, A200_Error(code)); } while (value_A IS_RAMPD || value_A IS_ON || value_Ref IS_RAMPD || value_Ref IS_ON ); for ( i = 0; i < nslots; i++ ) D_Set( D_ONOFF, d_crate[i], d_slot[i], ALL, OFF); esci(); } else if (d_global_status != OFF && ( hv_status_A == OFF && hv_status_Ref == OFF)){ for ( i = 0; i < nslots; i++ ) D_Set( D_ONOFF, d_crate[i], d_slot[i], ALL, OFF); esci(); } } else if (command == ON){ if (d_global_status == ON && hv_status_A == ON && hv_status_Ref == ON ) esci(); else if( d_global_status == ON && ( hv_status_A == OFF || hv_status_Ref == OFF ) ) { /* for ( i = 0; i < nslots; i++ ) check_i_zero(d_crate[i], d_slot[i], caller); */ if ( hv_status_A == OFF ) HV_Set( SWITCH, hv_crate, chan_A, ON); if ( hv_status_Ref == OFF ) HV_Set( SWITCH, hv_crate, chan_Ref, ON); esci(); } else if ( d_global_status != ON && ( hv_status_A == ON || hv_status_Ref == ON) ){ HV_Set( SWITCH, hv_crate, chan_A, OFF); HV_Set( SWITCH, hv_crate, chan_Ref, OFF); sleep(10); do { sleep(10); if ((code = HV_Read_One( STATUS, hv_crate, chan_A, &value_A)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n %s\n", code, A200_Error(code)); if ((code = HV_Read_One( STATUS, hv_crate, chan_Ref, &value_Ref)) != 0) printf(" d_hv_switch: error %d calling HV_Read_ONE : \n %s\n", code, A200_Error(code)); } while ( value_A IS_RAMPD || value_A IS_ON || value_Ref IS_RAMPD || value_Ref IS_ON ); /* check_i_zero(d_crate, slot, caller); */ /* aggiunto una pausa di 30 s su suggerimento di Enrico Borsato per attendere la scarica dei tubi */ sleep(30); for ( i = 0; i < nslots; i++) D_Set( D_ONOFF, d_crate[i], d_slot[i], ALL, ON); HV_Set( SWITCH, hv_crate, chan_Ref, ON); HV_Set( SWITCH, hv_crate, chan_A, ON); esci(); } else if(d_global_status != ON && ( hv_status_A == OFF && hv_status_Ref == OFF ) ){ /* check_i_zero(d_crate, slot, caller); */ for ( i = 0; i < nslots; i++) D_Set( D_ONOFF, d_crate[i], d_slot[i], ALL, ON); HV_Set( SWITCH, hv_crate, chan_A, ON); HV_Set( SWITCH, hv_crate, chan_Ref, ON); esci(); } } } int find_code(par_code) char *par_code; { int i; for ( i = 0; *(par_code + i) != '\0'; i++) *(par_code + i) = (char) toupper((int) *(par_code + i)); for( i = 0; i < a200_conv_tab_size; i++) if (strcmp(a200_conv[i].key, par_code) == 0) break; if ( i < a200_conv_tab_size ) return a200_conv[i].code; else return -1; } esci() { sem_free(d_sem_id, caller); sem_free(hv_A_sem_id, caller); sem_free(hv_Ref_sem_id, caller); A200_end(); sem_unl_del(hv_A_sem_id, hv_A_sem_name, caller); sem_unl_del(hv_Ref_sem_id, hv_Ref_sem_name, caller); sem_unl_del(d_sem_id, d_sem_name, caller); exit(0); } check_i_zero( cr, sl, caller) short cr, sl; char *caller; { int i; float average = 0; int ires = 1; int imax = -1; short ndata, data[40]; short ndatai, datai[40]; short code; ndata = 32; if (code = D_Read(D_IRESOL, cr, sl, ALL, data, &ndata)){ printf("%s: error %d reading current resolution: %s\n", caller, code, A200_Error(code)); ires = 1; } else ires = data[0]; ndatai = 32; if (code = D_Read(D_IMON, cr, sl, ALL, datai, &ndatai)) printf(" %s: error %d reading I monitor: %s\n", caller, code, A200_Error(code)); for ( i = 0; i < ndatai; i++ ) { average += ires * datai[i]; if ( ires * datai[i] > imax) imax = ires * datai[i]; } average /= ndatai; if ( average > 20. || imax > 100. ){ printf(" ***********************************************************\n"); printf(" slot nr %d of crate %d would need a ZERO_CURENT reset\n", sl, cr); printf(" = %f, imax = %d \n", average, imax); printf(" ***********************************************************\n"); /* code = D_Set( D_Z_I_SET, cr, sl, ALL, D_ZERO); if (code) printf(" Zero current set: error code %d\n", code); sleep(30); */ } }