#include #include #include #include "streamDispatch.h" #include "streamDataProc.h" #include "streamDataType.h" #include "KlystronDataProc.h" static Klystron_rfData_t *p_rf; static Klystron_rfCtrlParam_t *p_ctrl; static Klystron_rfCalc_t *p_calc; static int ts(unsigned timeslot); static double n_angle(double angle); void KlystronDataProc_delayMeasure(void *buf, t_delay_t t) { KlystronDataProc_delay_t *p = NULL; switch(t) { case DAQ: p = &p_ctrl->daq_delay; break; case CALC: p = &p_ctrl->calc_delay; break; case PVPOST: p = &p_ctrl->pvpost_delay; break; } if(!p) return; p->delay = getTimeMeasure(buf, -1); if(p->delay < p->min) p->min = p->delay; if(p->delay > p->max) p->max = p->delay; } void KlystronDataProc_setPrevPCorr(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc +ts(timeslot); pCalc->prev_pCorr = v; } void KlystronDataProc_setPrevACorr(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc+ts(timeslot); pCalc->prev_aCorr = v; } double KlystronDataProc_getAvgPhase(unsigned channel, unsigned timeslot) { Klystron_rfData_t *pRf = KlystronDataProc_getDataPointer(channel, timeslot); return pRf->avg_phase; } double KlystronDataProc_getAvgAmplitude(unsigned channel, unsigned timeslot) { Klystron_rfData_t *pRf = KlystronDataProc_getDataPointer(channel, timeslot); return pRf->avg_amplitude; } double KlystronDataProc_getAvgPower(unsigned channel, unsigned timeslot) { Klystron_rfData_t *pRf = KlystronDataProc_getDataPointer(channel, timeslot); return pRf->avg_power; } double KlystronDataProc_getRefSubPhase(unsigned channel, unsigned timeslot) { Klystron_rfData_t *pRf = KlystronDataProc_getDataPointer(channel, timeslot); return pRf->refsub_phase; } void KlystronDataProc_setPdes(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); v = n_angle(v); if(pCalc->prev_pdes <= 180. && pCalc->prev_pdes >= -180.) { pCalc->prev_pdes = pCalc->pdes; // for the normal case, just copy PDES } else { pCalc->prev_pdes = v; // abnormal case, very first run, just use current PDES } pCalc->pdes = v; } void KlystronDataProc_setAreq(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); pCalc->areq = v; } void KlystronDataProc_setPCorr(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); pCalc->pCorr = v; } void KlystronDataProc_setACorr(double v, unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); pCalc->aCorr = v; } double KlystronDataProc_getPErr(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->pErr; } double KlystronDataProc_getPCorr(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->pCorr; } double KlystronDataProc_getACorr(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->aCorr; } double KlystronDataProc_getPrevPCorr(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->prev_pCorr; } double KlystronDataProc_getPrevACorr(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->prev_aCorr; } double KlystronDataProc_getPhase(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->phase; } double KlystronDataProc_getPhaseFirmware(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->phase_firmware; } double KlystronDataProc_getAmplitude(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->amplitude; } double KlystronDataProc_getPower(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->power; } double KlystronDataProc_getBvoltPeak(unsigned timeslot) { Klystron_rfCalc_t *pCalc = p_calc + ts(timeslot); return pCalc->bvolt_peak; } void KlystronDataProc_setPocFirmware(double v) { p_ctrl->poc_firmware = v; } void KlystronDataProc_setPoc(double v, unsigned c) { p_ctrl->poc[c] = v; } void KlystronDataProc_setAmplScale(double v, unsigned c) { p_ctrl->ampl_scale[c] = v; } void KlystronDataProc_setPowerScale(double v, unsigned c) { p_ctrl->power_scale[c] = v; } void KlystronDataProc_setBvoltScale(double v) { p_ctrl->bvolt_scale = v; } double KlystronDataProc_getBvoltScale() { return p_ctrl->bvolt_scale; } void KlystronDataProc_setAvgWindStart(unsigned u, unsigned c) { p_ctrl->avgwind_start[c] = u; } void KlystronDataProc_setAvgWindEnd(unsigned u, unsigned c) { p_ctrl->avgwind_end[c] = u; } void KlystronDataProc_setRefSubtract(unsigned u) { p_ctrl->ref_subtract = u; } void KlystronDataProc_setRefChannel(unsigned c) { p_ctrl->ref_channel = c; } void KlystronDataProc_setWeight(double w, unsigned c) { p_ctrl->weight[c] = w; } void KlystronDataProc_setPSmooth(double v) { p_ctrl->psmooth = v; } void KlystronDataProc_setASmooth(double v) { p_ctrl->asmooth = v; } void KlystronDataProc_setPhaseControl(unsigned u) { p_ctrl->phase_control = u; } void KlystronDataProc_setAmplitudeControl(unsigned u) { p_ctrl->amplitude_control = u; } void KlystronDataProc_setUseFirmwarePhase(unsigned u) { p_ctrl->use_firmware_phase = u; } void KlystronDataProc_setPCorrLowerLimit(double v) { p_ctrl->pcorr_limit_lower = v; } void KlystronDataProc_setPCorrUpperLimit(double v) { p_ctrl->pcorr_limit_upper = v; } void KlystronDataProc_setACorrLowerLimit(double v) { p_ctrl->acorr_limit_lower = v; } void KlystronDataProc_setACorrUpperLimit(double v) { p_ctrl->acorr_limit_upper = v; } void KlystronDataProc_setAThreshold(double v) { p_ctrl->athreshold = v; } void KlystronDataProc_setDriveLowerLimit(double v) { p_ctrl->drive_limit_lower = v; } void KlystronDataProc_setDriveUpperLimit(double v) { p_ctrl->drive_limit_upper = v; } static double n_angle(double a) { if(a >= 180.) return n_angle(a - 360.); if(a < -180.) return n_angle(a + 360.); return a; } static double avg(unsigned start, unsigned end, double *data) { int i; int j =0; double v = 0.; for(i=start; i<=end; i++, j++) v += *(data+i); return (v/(double) j); } static double phase_avg(unsigned start, unsigned end, double *data) { int i; double cutoff = 270.; double diff = 0.; double avg = 0.; for(i=start; i<=end; i++) { avg += n_angle(data[i]); /* phase unwrapping */ diff = data[i] - data[start]; if (diff > cutoff) avg -= 360.; else if (diff < -1.0*cutoff) avg += 360.; } return n_angle( avg / (double) (1+end-start) ); } static int ts(unsigned timeslot) { /* timeslot 0, 1, 2, 3, 4, 5, 6 */ // input /* array index -1, 1, 2, 3, 0, 4, 5 */ // output int _ts[] = {-1, 1, 2, 3, 0, 4, 5 }; if(timeslot > 6) return -1; return _ts[timeslot]; } int KlystronDataProc_init(void) { int i; p_rf = (Klystron_rfData_t *) malloc(sizeof(Klystron_rfData_t) * MAX_CHANNEL_NUMBER * MAX_TIMESLOT); p_ctrl = (Klystron_rfCtrlParam_t *) malloc(sizeof(Klystron_rfCtrlParam_t)); p_calc = (Klystron_rfCalc_t *) malloc(sizeof(Klystron_rfCalc_t) * MAX_TIMESLOT); Klystron_rfCalc_t *pCalc; if(!p_rf || !p_ctrl || !p_calc) return -1; p_ctrl->daq_delay.max = p_ctrl->calc_delay.max = p_ctrl->pvpost_delay.max = 0.; p_ctrl->daq_delay.min = p_ctrl->calc_delay.min = p_ctrl->pvpost_delay.min = 1000.; p_ctrl->poc_firmware = 0.; for(i=0; ipoc[i] = 0.; p_ctrl->ampl_scale[i] = 1.; p_ctrl->power_scale[i] = 1.; p_ctrl->avgwind_start[i] = 0.; p_ctrl->avgwind_end[i] = 128.; } p_ctrl->ref_subtract = 1; p_ctrl->ref_channel = 1; for(i=0; iweight[i] = 1.; p_ctrl->psmooth = 0.1; p_ctrl->asmooth = 0.1; p_ctrl->pcorr_limit_lower = -360.; p_ctrl->pcorr_limit_upper = +360.; p_ctrl->acorr_limit_lower = 0.; p_ctrl->acorr_limit_upper = 10.; p_ctrl->athreshold = 0.; p_ctrl->drive_limit_lower = 0.; p_ctrl->drive_limit_upper = 1.; p_ctrl->phase_control = 0; p_ctrl->amplitude_control = 0; p_ctrl->use_firmware_phase = 0; for(i=0; iphase_firmware = 0.; pCalc->phase = 0.; pCalc->amplitude = 0.; pCalc->power = 0.; pCalc->pdes = 0.; pCalc->areq = 0.; pCalc->pCorr = 0.; pCalc->aCorr = 0.; pCalc->prev_pCorr = 0.; pCalc->prev_aCorr = 0.; pCalc->prev_pdes = -360.; pCalc->prev_areq = -1.; } return 0; } Klystron_rfData_t * KlystronDataProc_getDataPointer(unsigned channel, unsigned timeslot) { return (p_rf + ts(timeslot) * MAX_CHANNEL_NUMBER + channel); } void KlystronDataProc_collectDaq(void *buf, unsigned timeslot) { Klystron_rfData_t *pRf; int i; for(i=0;iraw = readRawData(i, timeslot, &pRf->time, &pRf->num_raw); pRf->i = readiData(i, timeslot, &pRf->time, &pRf->num_i); pRf->q = readqData(i, timeslot, &pRf->time, &pRf->num_q); pRf->phase = readPhaseData(i, timeslot, &pRf->time, &pRf->num_phase); pRf->amplitude = readAmplitudeData(i, timeslot, &pRf->time, &pRf->num_amplitude); pRf->elapsedTime = getTimeMeasure(buf, -1); } } void KlystronDataProc_calcSignal(void *buf, unsigned timeslot) { Klystron_rfData_t *pRf; Klystron_rfCalc_t *pCalc; double ref_phase; double weight_sum = 0.; double weight_phase = 0.; double weight_amplitude = 0.; double weight_power = 0.; double a; int i; pCalc = p_calc + ts(timeslot); // pCalc->phase_firmware = n_angle(p_ctrl->poc_firmware + readPhaseErrorData(timeslot)); pCalc->phase_firmware = readPhaseErrorData(timeslot); // calculate beam volts peak pRf = KlystronDataProc_getDataPointer(BEAMVOLTS_CHANNEL, timeslot); pCalc->bvolt_peak = p_ctrl->bvolt_scale * pRf->raw[0]; for(i=1; inum_raw; i++) { if (pCalc->bvolt_peak < p_ctrl->bvolt_scale * pRf->raw[i]) pCalc->bvolt_peak = p_ctrl->bvolt_scale * pRf->raw[i]; } // calculate average phase and amplitude for each channel for(i=0; i< MAX_CHANNEL_NUMBER; i++) { pRf = KlystronDataProc_getDataPointer(i, timeslot); // get channel data pRf->avg_phase = n_angle(phase_avg(p_ctrl->avgwind_start[i], p_ctrl->avgwind_end[i], pRf->phase) + p_ctrl->poc[i]); // calculate average phase a = avg(p_ctrl->avgwind_start[i], p_ctrl->avgwind_end[i], pRf->amplitude); pRf->avg_amplitude = a * p_ctrl->ampl_scale[i]; // calculate amplitude pRf->avg_power = a * a * p_ctrl->power_scale[i]; // calculate power; if(!(p_ctrl->ref_subtract)) pRf->refsub_phase = pRf->avg_phase; // copy average phase to refsub phase when the reference subtract is off } // calculate reference subtract if(p_ctrl->ref_subtract) { pRf = KlystronDataProc_getDataPointer(p_ctrl->ref_channel, timeslot); // get data pointer for reference channel ref_phase = pRf->avg_phase; // get reference phase for(i =0; i< MAX_CHANNEL_NUMBER; i++) { pRf = KlystronDataProc_getDataPointer(i, timeslot); // get each channel data pRf->refsub_phase = n_angle(pRf->avg_phase - ref_phase); // subtract reference phase } } // calculate weight average for(i=0; iweight[i]; // calculate normalization factor pRf = KlystronDataProc_getDataPointer(i, timeslot); // get each channel data weight_phase += pRf->refsub_phase * p_ctrl->weight[i]; // calculate phase weight weight_amplitude += pRf->avg_amplitude * p_ctrl->weight[i]; // calculate amplitude weight weight_power += pRf->avg_power * p_ctrl->weight[i]; // calculate power weight } weight_phase /= weight_sum; // calculate phase average weight_amplitude /= weight_sum; // calculate amplitude average weight_power /= weight_sum; // calculate power average pCalc->phase = n_angle(weight_phase); // calculate final phase reading pCalc->amplitude = weight_amplitude; // calculate amplitude pCalc->power = weight_power; // calculate power if(pCalc->amplitude < p_ctrl->athreshold) return; /* do not feedback when amplitude is below the threshold */ // phase feedback if(p_ctrl->phase_control) { if(p_ctrl->use_firmware_phase) a = pCalc->phase_firmware; // firmware provides phase error instead of the phase reading else a = pCalc->pdes - pCalc->phase; // in software mode, need to calculate phase error a = n_angle(a); // wrap round the angle if(a < 0.) { if(a < -p_ctrl->pcorr_limit_upper) a = -p_ctrl->pcorr_limit_upper; // make clamping if(a > -p_ctrl->pcorr_limit_lower) a = 0.; // mkae deadband } else { if(a < p_ctrl->pcorr_limit_lower) a = 0.; // make deadband if(a > p_ctrl->pcorr_limit_upper) a = p_ctrl->pcorr_limit_upper; // make clamping } /* // we mgiht not need the wrap round here a = n_angle(a); // wrap round the angle */ pCalc->pCorr = n_angle(pCalc->prev_pCorr + a * p_ctrl->psmooth); // increment the correction pCalc->pErr = a; // post out the phase error to PV, for debugging purpose a = (pCalc->pdes - pCalc->prev_pdes) * ((p_ctrl->psmooth>0.)?1.:-1.); pCalc->pCorr += a; // apply PDES change immediately for very low gain situation pCalc->prev_pdes = pCalc->pdes; // updat prevous PDES for the next iteration /* the prev_pdes also can be updated when the acorr is set by epics PV, * may be need to consider a race condition, but did not see symptom yet */ /* event in firmware mode, software needs to keep tracking the ration */ } // amplitude feedback if(p_ctrl->amplitude_control) { if(pCalc->amplitude > 0.01) a = pCalc->areq / pCalc->amplitude; else a = 1.; if(a > p_ctrl->acorr_limit_upper) a = p_ctrl->acorr_limit_upper; // clamping on the correction limit - upper boundary if(a < p_ctrl->acorr_limit_lower) a = p_ctrl->acorr_limit_lower; // clamping on the correction limit - lower boundary pCalc->aCorr = pCalc->prev_aCorr * (p_ctrl->asmooth * (a -1.) + 1.); // apply correction if(pCalc->aCorr > p_ctrl->drive_limit_upper) pCalc->aCorr = p_ctrl->drive_limit_upper; // clamping with the driver limit - upeer boundary if(pCalc->aCorr < p_ctrl->drive_limit_lower) pCalc->aCorr = p_ctrl->drive_limit_lower; // clamping with the driver limit - lower boundary } } void KlystronDataProc_resetDelayMeasurements(void) { p_ctrl->daq_delay.max = p_ctrl->daq_delay.min = p_ctrl->daq_delay.delay; p_ctrl->calc_delay.max = p_ctrl->calc_delay.min = p_ctrl->calc_delay.delay; p_ctrl->pvpost_delay.max = p_ctrl->pvpost_delay.min = p_ctrl->pvpost_delay.delay; } static void report_control_params(void) { int i; printf(" --------------------------------- \n"); printf(" KlystronDataProc Control Pararams \n"); printf(" --------------------------------- \n"); printf(" phase control: %d, amplitude control: %d\n", p_ctrl->phase_control, p_ctrl->amplitude_control); printf(" phase correction limits: [%f, %f]\n", p_ctrl->pcorr_limit_lower, p_ctrl->pcorr_limit_upper); printf(" amppl correction limits: [%f, %f]\n", p_ctrl->acorr_limit_lower, p_ctrl->acorr_limit_upper); printf(" drive limits: [%f, %f]\n", p_ctrl->drive_limit_lower, p_ctrl->drive_limit_upper); printf(" ampl threshold for correction: %f\n", p_ctrl->athreshold); printf(" smoothing factor: phase %f, amplitude %f\n", p_ctrl->psmooth, p_ctrl->asmooth); printf(" phase reference: channel# %d, reference subtract %d\n", p_ctrl->ref_channel, p_ctrl->ref_subtract); printf("\n"); printf(" channel #, avg_wind start, avg_wind end, weight, poc, ampl_scale, power_scale\n"); for(i = 0; i< MAX_CHANNEL_NUMBER; i++) printf(" %9d, %14d, %11d, %f, %f, %f, %f\n", i, p_ctrl->avgwind_start[i], p_ctrl->avgwind_end[i], p_ctrl->weight[i], p_ctrl->poc[i], p_ctrl->ampl_scale[i], p_ctrl->power_scale[i]); printf(" ------------------\n"); printf(" delay measurements\n"); printf(" ------------------\n"); printf(" %14s %14s %14s %14s\n", "description", "delay (usec)", "min (usec)", "max (usec)" ); printf(" %14s %14.0f %14.0f %14.0f\n", "daq", p_ctrl->daq_delay.delay, p_ctrl->daq_delay.min, p_ctrl->daq_delay.max); printf(" %14s %14.0f %14.0f %14.0f\n", "calc", p_ctrl->calc_delay.delay, p_ctrl->calc_delay.min, p_ctrl->calc_delay.max); printf(" %14s %14.0f %14.0f %14.0f\n", "pvpost", p_ctrl->pvpost_delay.delay, p_ctrl->pvpost_delay.min, p_ctrl->pvpost_delay.max); } static void report_control_loop(void) { } static void report_phase_amplitude_measure(void) { } void KlystronDataProc_report(void) { report_control_params(); report_phase_amplitude_measure(); report_control_loop(); }