channel = param.channel;
pllCycleStartFrequency = param.pllCycleStartFrequency;
pllInitialFrequency = param.pllInitialFrequency;
pllInitialFrequencyDelay = param.pllInitialFrequencyDelay;
pllFrefGain = param.pllFrefGain;
pllGain = param.pllGain;
pllDdsMinimum = param.pllDdsMinimum;
pllDdsMaximum = param.pllDdsMaximum;
frefPhaseDelay = param.frefPhaseDelay;
stateTable = param.stateTable;
settings = param.settings;
}
double CycleParamEdit::value(int numSamples, int harmonic, double phase, int sample){
double d;
double p;
double e;
d = double(numSamples) / harmonic;
p = double(numSamples) * phase;
e = fmod(sample - p, d) / d;
if(e < 0)
e = 1 + e;
return e;
}
int CycleParamEdit::bunch(int numSamples, int harmonic, double phase, int sample){
double d;
double p;
d = double(numSamples) / harmonic;
p = double(numSamples) * phase;
if(p > sample)
return int((numSamples + sample - p) / d) % harmonic;
else
return int((sample - p) / d) % harmonic;
}
/// This function will generate the phase tables for a given state.
/// It is passed the parameters for the LO1 reference and the LO2 reference.
/// If lo?Harmonic is 1, then FREF is generated.
BError CycleParamEdit::generateState(int num, Tms::TmsState state, CycleParamState stateParam, int lo1Harmonic, double lo1Phase, int lo2Harmonic, double lo2Phase){
BError err;
int loHarmonic;
int numSamples = 512;
int s;
int b;
double e;
double w;
double ms;
double me;
TmsPhase pt;
int numBunches = 0;
// printf("CycleParamEdit::generateState: %d: LO: %d %d,%f %d,%f\n", num, state.pllFeedbackSelect, lo1Harmonic, lo1Phase, lo2Harmonic, lo2Phase);
stateTable[num].phaseTable.resize(numSamples);
if(!stateParam.useLoFref){
// If configured to use the PLL's address counters MSB as FREF, set the appropriate state table bits
// In this case the appropriate LO1/LO2 phase table signal will not be used.
if(state.pllReference1 == 0)
state.pllLO1FromAddress = 1;
if(state.pllReference2 == 0)
state.pllLO2FromAddress = 1;
}
if(state.pllFeedbackSelect)
loHarmonic = lo2Harmonic;
else
loHarmonic = lo1Harmonic;
// Calculate the number of particle bunches
for(s = 0; s < loHarmonic; s++){
if(stateParam.bunchMask & (1 << s)){
numBunches++;
}
}
// Generate the phase table entries
for(s = 0; s < numSamples; s++){
#ifndef __Lynx__
pt.value = 0;
#else
pt.value() = 0;
#endif
// Generate the LO1 reference
e = value(numSamples, lo1Harmonic, -lo1Phase + 1.0 / (4 * lo1Harmonic), s);
if(e >= 0.5)
pt.lo1 = 0;
else
pt.lo1 = 1;
// Generate the LO2 reference
e = value(numSamples, lo2Harmonic, -lo2Phase + 1.0 / (4 * lo2Harmonic), s);
if(e >= 0.5)
pt.lo2 = 0;
else
pt.lo2 = 1;
if(stateParam.acquireData){
// Generate the GATE
e = value(numSamples, loHarmonic, stateParam.gatePhase/loHarmonic, s);
b = bunch(numSamples, loHarmonic, stateParam.gatePhase/loHarmonic, s);
w = stateParam.gateWidth;
#ifdef ZAP
if(num == 3)
printf("%d: %d %f %f\n", s, b, e, w);
#endif
if(stateParam.bunchMask & (1 << b)){
if(e < w)
pt.gate = 1;
}
// Generate the mean filter strobes
ms = w + 4.0 * loHarmonic / 512;
me = ms + 4.0 * loHarmonic / 512;
if(stateParam.mean1Mask & (1 << b)){
if((e >= ms) && (e < me))
pt.meanFilter1 = 1;
}
if(stateParam.mean2Mask & (1 << b)){
if((e >= ms) && (e < me))
pt.meanFilter2 = 1;
}
// Generate the BLR
#ifndef ZAP
e = value(numSamples, loHarmonic, stateParam.blrPhase/loHarmonic, s);
w = stateParam.blrWidth;
if(e < w)
pt.blr = 1;
#else
if(s < 64)
pt.blr = 1;
if(s > 512 - 64)
pt.blr = 1;
#endif
}
#ifndef __Lynx__
stateTable[num].phaseTable[s] = pt.value;
#else
stateTable[num].phaseTable[s] = pt.value();
#endif
}
stateTable[num].period = stateParam.period;
#ifndef __Lynx__
stateTable[num].state = state.value;
#else
stateTable[num].state = state.value();
#endif
stateTable[num].harmonic = loHarmonic;
stateTable[num].numBunches = numBunches;
stateTable[num].bunchMask = stateParam.bunchMask;
return err;
}
BError CycleParamEdit::setStates(BList<CycleParamState> cycleStates){
BError err;
unsigned int i;
Tms::TmsState state;
UInt32 loHarmonic;
double loPhase;
UInt32 loNextHarmonic;
double loNextPhase;
int lo = 0;
int num = 0;
int errorState = 15;
int stopState = 15;
CycleParamState defState;
if((cycleStates.number() < 2) || (cycleStates.number() > 10)){
return err.set(ErrorParam, "Needs at least two and less than 10 cycle states to be set up");
}
stateTable.clear();
stateTable.resize(cycleStates.number() + 2);
// Set up start state
#ifndef __Lynx__
state.value = 0x0;
#else
state.value() = 0x0;
#endif
state.calStart = 1;
state.calStop = errorState;
state.injection = 3;
state.hchange = errorState;
state.cycleStop = stopState;
state.delay = num;
state.aquireData = 1;
state.pllReference1 = 0;
state.pllReference2 = 1;
state.pllFeedbackSelect = lo;
loHarmonic = 1;
loPhase = 0.0;
loNextHarmonic = cycleStates[1].loHarmonic;
loNextPhase = cycleStates[1].loPhase;
generateState(num, state, defState, loHarmonic, loPhase, loNextHarmonic, loNextPhase);
num++;
// Calibration state
state.calStart = errorState;
state.calStop = 2;
state.injection = errorState;
state.hchange = errorState;
state.cycleStop = stopState;
state.delay = num;
state.aquireData = 1;
state.pllReference1 = 0;
state.pllReference2 = 1;
state.pllFeedbackSelect = 1;
loHarmonic = 1;
loPhase = 0.0;
loNextHarmonic = cycleStates[0].loHarmonic;
loNextPhase = cycleStates[0].loPhase;
generateState(num, state, cycleStates[0], loHarmonic, loPhase, loNextHarmonic, loNextPhase);
num++;
// Await injection state
state.calStart = errorState;
state.calStop = errorState;
state.injection = 3;
state.hchange = errorState;
state.cycleStop = stopState;
state.delay = num;
state.aquireData = 1;
state.pllReference1 = 0;
state.pllReference2 = 1;
state.pllFeedbackSelect = 0;
loHarmonic = 1;
loPhase = 0.0;
loNextHarmonic = cycleStates[1].loHarmonic;
loNextPhase = cycleStates[1].loPhase;
generateState(num, state, defState, loHarmonic, loPhase, loNextHarmonic, loNextPhase);
num++;
lo = 1;
for(i = 1; i < cycleStates.number(); i++){
// Set up state
#ifndef __Lynx__
state.value = 0x0;
#else
state.value() = 0x0;
#endif
state.calStart = errorState;
state.calStop = errorState;
state.injection = errorState;
state.cycleStop = stopState;
state.delay = num;
state.aquireData = 1;
if(lo){
state.pllReference1 = 0;
state.pllReference2 = 1;
}
else {
state.pllReference1 = 1;
state.pllReference2 = 0;
}
state.pllFeedbackSelect = lo;
loHarmonic = cycleStates[i].loHarmonic;
loPhase = cycleStates[i].loPhase;
if(i + 1 >= cycleStates.number()){
loNextHarmonic = 1;
loNextPhase = 0.0;
state.injection = errorState;
state.hchange = errorState;
}
else {
loNextHarmonic = cycleStates[i + 1].loHarmonic;
loNextPhase = cycleStates[i + 1].loPhase;
state.injection = num + 1;
state.hchange = num + 1;
}
if(lo){
generateState(num, state, cycleStates[i], loNextHarmonic, loNextPhase, loHarmonic, loPhase);
lo = 0;
}
else {
generateState(num, state, cycleStates[i], loHarmonic, loPhase, loNextHarmonic, loNextPhase);
lo = 1;
}
num++;
}
// Save settings for further edits
settings.resize(cycleStates.number());
for(i = 0; i < cycleStates.number(); i++){
settings[i] = cycleStates[i].getString();
}
return err;
}
BError CycleParamEdit::getStates(BList<CycleParamState>& cycleStates){
BError err;
CycleParamState st;
unsigned int i;
cycleStates.clear();
for(i = 0; i < settings.size(); i++){
st.setString(settings[i]);
cycleStates.append(st);
}
return err;
}
void CycleParamEdit::getDefaultState(CycleParamState& state){
state.bunchMask = 0x00;
state.mean1Mask = 0x00;
state.mean2Mask = 0x00;
state.loHarmonic = 1;
state.loPhase = 0.0;
state.useLoFref = 0;
state.acquireData = 1;
state.gateWidth = 0.8;
state.gatePhase = 0.0;
state.blrWidth = 0.1;
state.blrPhase = 0.85;
}
/// Calculates the base pickup phase values for the PS ring
void CycleParamEdit::getdefaultPickupPositions(BArray<Int32>& pos){
unsigned int p = 43; // The first pickup in the PS ring after injection
unsigned int n;
pos.clear();
pos.resize(tmsNumPickups);
for(n = 0; n < tmsNumPickups; n++){
pos[n] = int(round(((p - 43) * 512.0) / 100));
if((p % 10) == 0){
p += 3;
}
else if((p % 10) == 3){
p += 2;
}
else if((p % 10) == 5){
p += 2;
}
else if((p % 10) == 7){
p += 3;
}
}
}
BString CycleParamEdit::getString(){
unsigned int n;
unsigned int p;
BString s;
BString st;
BString str;
s.printf("cycleType: %s\n", cycleType.retStr()); str += s;