/* * Mitigation.c * MPSLinkProcessor * * Created by Stephen Norum. * Copyright 2008 Stanford Linear Accelerator Center. All rights reserved. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Timing.h" #include "MachineStateBuffer.h" #include "MachineRatesBuffer.h" #include "LinkNodeCommunication.h" #include "Mitigation.h" #include "MitigationConstants.h" #if 0 #pragma mark - #pragma mark Permit Message #endif static epicsUInt8 determineShutterPermit(MPSBeamRate maxRateAfterDevice, epicsUInt8 sentPermitOnLastTimeslot, unsigned int timeslotsSinceLastPermitSent, unsigned int minCloseTime) { epicsUInt8 beamRequestedPastShutter = epicsFalse; epicsUInt8 shouldSendPermit = epicsFalse; /* Always allow the shutters to close immediately. However, to prevent cycling * the shutters at a high rate, only allow them to open if they have been closed * for a minimum amount of time. */ /* Shutters remain open for all rates greater than 0 Hz */ if ((maxRateAfterDevice > MPSRate0Hz) && (maxRateAfterDevice <= MPSRate120Hz)) { beamRequestedPastShutter = epicsTrue; } else { beamRequestedPastShutter = epicsFalse; } shouldSendPermit = epicsFalse; if (beamRequestedPastShutter) { if (sentPermitOnLastTimeslot) { /* * Beam was permitted on the previous timeslot and is requested on this timeslot. * Permit the shutter to open. */ shouldSendPermit = epicsTrue; } else { /* * Beam was not permitted on the previous timeslot. * Only allow the shutter to open if it has been closed long enough. */ if (timeslotsSinceLastPermitSent >= minCloseTime) { shouldSendPermit = epicsTrue; } } } return shouldSendPermit; } static epicsUInt8 determineFastDevicePermit(MPSBeamRate maxRateAfterDevice, unsigned int timeslotsSinceLastBeam, MPSBeamRate burstModeStatus, const MPSTimingData *queuedTimingData) { const MPSTimingData *currentTimingData = NULL; const MPSTimingData *nextTimingData = NULL; const MPSTimingData *afterNextTimingData = NULL; epicsUInt8 shouldSendPermit = epicsFalse; epicsUInt8 rateSynchronizedWithEvg = epicsFalse; epicsUInt8 deviceDowntimeIsSufficient = epicsFalse; epicsUInt8 burstAllowedOrNotRelevant = epicsFalse; unsigned int minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime0Hz; /* Convenience pointers */ currentTimingData = &(queuedTimingData[MPSTimingIndexCurrent]); nextTimingData = &(queuedTimingData[MPSTimingIndexNext]); afterNextTimingData = &(queuedTimingData[MPSTimingIndexAfterNext]); /* ---------------------------------------------------- */ /* Standard beam rates */ /* ---------------------------------------------------- */ switch(maxRateAfterDevice) { case MPSRate120Hz: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime120Hz; rateSynchronizedWithEvg = epicsTrue; break; case MPSRate60Hz: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime60Hz; rateSynchronizedWithEvg = nextTimingData->baseRate60Hz || afterNextTimingData->baseRate60Hz; break; case MPSRate30Hz: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime30Hz; rateSynchronizedWithEvg = nextTimingData->baseRate30Hz || afterNextTimingData->baseRate30Hz; break; case MPSRate10Hz: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime10Hz; rateSynchronizedWithEvg = nextTimingData->baseRate10Hz || afterNextTimingData->baseRate10Hz; break; case MPSRate1Hz: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime1Hz; rateSynchronizedWithEvg = nextTimingData->baseRate1Hz || afterNextTimingData->baseRate1Hz; break; default: minimumTimeslotsForRateDowntime = MPSMitigationMinDowntime0Hz; rateSynchronizedWithEvg = epicsFalse; break; } if (timeslotsSinceLastBeam >= minimumTimeslotsForRateDowntime) { deviceDowntimeIsSufficient = epicsTrue; } /* ---------------------------------------------------- */ /* Burst and single shot modes */ /* ---------------------------------------------------- */ switch (burstModeStatus) { /* Burst mode not active: * - Burst mode restrictions not relevant. */ case MPSRateBurstModeNotActive: burstAllowedOrNotRelevant = epicsTrue; break; /* Burst mode active: * - Only permit beam if burst is requested on * one of the two upcoming 360 Hz pulses. */ case MPSRateBurstMode: if (nextTimingData->burstPulseRequested || afterNextTimingData->burstPulseRequested) { burstAllowedOrNotRelevant = epicsTrue; } break; /* Single shot mode: * - Allow single shots at a max rate of 1 Hz. * - Only permit beam if shot is requested on * one of the two upcoming 360 Hz pulses and * - Only permit that shot if it is requested * on the 1 Hz base rate. */ case MPSRateSingleShot: if ((nextTimingData->baseRate1Hz && nextTimingData->burstPulseRequested ) || (afterNextTimingData->baseRate1Hz && afterNextTimingData->burstPulseRequested)) { burstAllowedOrNotRelevant = epicsTrue; } break; /* Default */ default: burstAllowedOrNotRelevant = epicsFalse; break; } /* ---------------------------------------------------- */ /* Final permit logic */ /* ---------------------------------------------------- */ if (deviceDowntimeIsSufficient && rateSynchronizedWithEvg && burstAllowedOrNotRelevant ) { shouldSendPermit = epicsTrue; } else { shouldSendPermit = epicsFalse; } return shouldSendPermit; } /* Must be called at 360 Hz. Must lock state and rate mutexes before calling. */ int mitigateFaultsAndGeneratePermitMessage(const MPSTimingData *queuedTimingData, unsigned int queuedTimingDataLength, MPSLinkPermitMessage *permitMessage) { /* Arrays to store values for mitigation devices * * shouldSendPermit values are initialized to true so beam is assumed * to have gone past devices, requiring min downtime before next permit */ static epicsUInt32 timeslotsSinceLastBeam[MPSMitigationDeviceCount] = {0, 0, 0, 0}; static epicsUInt32 timeslotsSinceLastPermitSent[MPSMitigationDeviceCount] = {0, 0, 0, 0}; static epicsUInt32 lastMinTimeslotsSinceLastBeam[MPSMitigationDeviceCount] = {0, 0, 0, 0}; static epicsUInt8 sentPermitOnLastTimeslot[MPSMitigationDeviceCount] = {epicsTrue, epicsTrue, epicsTrue, epicsTrue}; static epicsUInt8 shouldSendPermit[MPSMitigationDeviceCount] = {epicsTrue, epicsTrue, epicsTrue, epicsTrue}; static MPSBeamRate maxRateAfterDevice[MPSMitigationDeviceCount] = {MPSRate0Hz, MPSRate0Hz, MPSRate0Hz, MPSRate0Hz}; static unsigned int consecutiveSameMechanicalShutterPermits = 0; static unsigned int consecutiveSameLaserHeaterShutterPermits = 0; int i; int err = MPS_ERROR; MPSBeamRate burstModeStatus = MPSRateBurstInvalid; const MPSTimingData *currentTimingData = NULL; epicsUInt8 outputControl = 0; epicsUInt8 beamRequestedOnNonLclsTimeslot = epicsTrue; epicsUInt8 mechanicalShutterIsStable = epicsFalse; epicsUInt8 laserHeaterShutterIsStable = epicsFalse; /* Convenience pointers */ currentTimingData = &(queuedTimingData[MPSTimingIndexCurrent]); /* * Update stats and initialize */ /* For each mitigation device */ for (i = 0; i < MPSMitigationDeviceCount; i++) { /* Store previous permit states*/ sentPermitOnLastTimeslot[i] = shouldSendPermit[i]; /* Increment timeslot counts */ if (timeslotsSinceLastBeam[i] < MPSMitigationMaxTimeslotsSinceLastBeam) { timeslotsSinceLastBeam[i]++; } if (timeslotsSinceLastPermitSent[i] < MPSMitigationTimeslotsRelevantToPermitMessage) { timeslotsSinceLastPermitSent[i]++; } /* Update counts to estimate rate */ if (lastMinTimeslotsSinceLastBeam[i] < timeslotsSinceLastBeam[i]) { lastMinTimeslotsSinceLastBeam[i] = timeslotsSinceLastBeam[i]; } /* Set beam permit to false */ shouldSendPermit[i] = epicsFalse; /* Initialize rate after device to 0 Hz */ maxRateAfterDevice[i] = MPSRate0Hz; } /* * Reset "timeslot counts since last beam" where beam may reach on this timeslot. * Assume beam goes past a mitigation device if the number of timeslots since a permit * was last sent is less than or equal to the minimum amount of time the mitigation * device requires to close securely. */ if (queuedTimingData[MPSTimingIndexCurrent].baseRate120Hz) { /* Pockels Cell is open and laser is being sent through it. */ if (timeslotsSinceLastPermitSent[MPSMitigationDevicePockelsCell] <= MPSMitigationMinStableCloseTimePockelsCell) { lastMinTimeslotsSinceLastBeam[MPSMitigationDevicePockelsCell] = timeslotsSinceLastBeam[MPSMitigationDevicePockelsCell]; timeslotsSinceLastBeam[MPSMitigationDevicePockelsCell] = 0; /* Mechanical Shutter is open and laser is being sent through it. */ if (timeslotsSinceLastPermitSent[MPSMitigationDeviceMechanicalShutter] <= MPSMitigationMinStableCloseTimeMechanicalShutter) { lastMinTimeslotsSinceLastBeam[MPSMitigationDeviceMechanicalShutter] = timeslotsSinceLastBeam[MPSMitigationDeviceMechanicalShutter]; timeslotsSinceLastBeam[MPSMitigationDeviceMechanicalShutter] = 0; /* BYKIK was in standby and beam is being sent by it. */ if (timeslotsSinceLastPermitSent[MPSMitigationDeviceBykik] <= MPSMitigationMinStableCloseTimeBykik) { lastMinTimeslotsSinceLastBeam[MPSMitigationDeviceBykik] = timeslotsSinceLastBeam[MPSMitigationDeviceBykik]; timeslotsSinceLastBeam[MPSMitigationDeviceBykik] = 0; } } } } /* * Validate beam request and max rate data */ #warning snorum: Move this code to the timing function before algorithm is run and add the logic to the MPL file. Maybe create a base MPL file that is always included? /* Check to see if beam is requested only on 120 Hz base rate */ beamRequestedOnNonLclsTimeslot = epicsFalse; for (i = 0; i < queuedTimingDataLength; i++) { if ((queuedTimingData[i].baseRate120Hz == epicsFalse) && ((queuedTimingData[i].beamRequested) || (queuedTimingData[i].burstPulseRequested))) { beamRequestedOnNonLclsTimeslot = epicsTrue; break; } } if (beamRequestedOnNonLclsTimeslot) { deviceStateMakeFaulted(LP_TS_BEAM_REQUEST__INDEX, currentTimingData->timestamp, currentTimingData->timeslot); setAllRatesToMostRestrictiveAndClearBurstModes(); } else { deviceStateMakeOk(LP_TS_BEAM_REQUEST__INDEX, currentTimingData->timestamp, currentTimingData->timeslot); } /* Get the max allowed rates as determined by the algorithm */ err = getRatesAfterAllDevices(maxRateAfterDevice); /* Set rates outside 0-120 Hz to 0 Hz */ for (i = 0; i < sizeof(maxRateAfterDevice) / sizeof(maxRateAfterDevice[0]); i++) { if ((maxRateAfterDevice[i] < MPSRate0Hz) || (maxRateAfterDevice[i] > MPSRate120Hz)) { maxRateAfterDevice[i] = MPSRate0Hz; setRateAfterDevice_warning_allows_any_rate(MPSRate0Hz, i); } } /* * Determine which mitigation devices should permit beam * * Find out if beam past the mechanical shutter is allowed first since the * Pockels Cell and BYKIK needs to stop beam while the mechanical shutter * isn't stable. */ #if 0 #pragma mark - #pragma mark - #pragma mark #pragma mark Mechanical Shutter Permit #endif /****************************************************************************** * Mechanical Shutter ******************************************************************************/ shouldSendPermit[MPSMitigationDeviceMechanicalShutter] = determineShutterPermit(maxRateAfterDevice[MPSMitigationDeviceMechanicalShutter], sentPermitOnLastTimeslot[MPSMitigationDeviceMechanicalShutter], timeslotsSinceLastPermitSent[MPSMitigationDeviceMechanicalShutter], MPSMitigationMinCloseTimeMechanicalShutter); /* Reset or increment the number of consecutive same permit statuses */ if (shouldSendPermit[MPSMitigationDeviceMechanicalShutter] == sentPermitOnLastTimeslot[MPSMitigationDeviceMechanicalShutter]) { if (consecutiveSameMechanicalShutterPermits < MPSMitigationMinStableCloseTimeMechanicalShutter) { consecutiveSameMechanicalShutterPermits++; } } else { consecutiveSameMechanicalShutterPermits = 0; } /* Determine if the mechanical shutter is stable */ if (consecutiveSameMechanicalShutterPermits >= MPSMitigationMinStableCloseTimeMechanicalShutter) { mechanicalShutterIsStable = epicsTrue; } else { mechanicalShutterIsStable = epicsFalse; } #if 0 #pragma mark #pragma mark Laser Heater Shutter Permit #endif /****************************************************************************** * Laser Heater Shutter ******************************************************************************/ shouldSendPermit[MPSMitigationDeviceLaserHeaterShutter] = determineShutterPermit(maxRateAfterDevice[MPSMitigationDeviceLaserHeaterShutter], sentPermitOnLastTimeslot[MPSMitigationDeviceLaserHeaterShutter], timeslotsSinceLastPermitSent[MPSMitigationDeviceLaserHeaterShutter], MPSMitigationMinCloseTimeLaserHeaterShutter); /* Reset or increment the number of consecutive same permit statuses */ if (shouldSendPermit[MPSMitigationDeviceLaserHeaterShutter] == sentPermitOnLastTimeslot[MPSMitigationDeviceLaserHeaterShutter]) { if (consecutiveSameLaserHeaterShutterPermits < MPSMitigationMinStableCloseTimeLaserHeaterShutter) { consecutiveSameLaserHeaterShutterPermits++; } } else { consecutiveSameLaserHeaterShutterPermits = 0; } /* Determine if the laser heater shutter is stable */ if (consecutiveSameLaserHeaterShutterPermits >= MPSMitigationMinStableCloseTimeLaserHeaterShutter) { laserHeaterShutterIsStable = epicsTrue; } else { laserHeaterShutterIsStable = epicsFalse; } #if 0 #pragma mark #pragma mark Pockels Cell and BYKIK Permits #pragma mark #endif /****************************************************************************** * Pockels Cell and BYKIK ******************************************************************************/ /* Only open the Pockels Cell and BYKIK if the shutters have been held open or closed for * their minimum stable times. And to avoid permitting beam on the current timeslot, send * Pockels Cell and BYKIK permit signals on non-LCLS timeslots only. */ if ((mechanicalShutterIsStable) && (laserHeaterShutterIsStable) && (currentTimingData->baseRate120Hz == epicsFalse)) { err = getBurstModeStatusForDevice(&burstModeStatus, MPSMitigationDevicePockelsCell); shouldSendPermit[MPSMitigationDevicePockelsCell] = determineFastDevicePermit(maxRateAfterDevice[MPSMitigationDevicePockelsCell], timeslotsSinceLastBeam[MPSMitigationDevicePockelsCell], burstModeStatus, queuedTimingData); err = getBurstModeStatusForDevice(&burstModeStatus, MPSMitigationDeviceBykik); shouldSendPermit[MPSMitigationDeviceBykik] = determineFastDevicePermit(maxRateAfterDevice[MPSMitigationDeviceBykik], timeslotsSinceLastBeam[MPSMitigationDeviceBykik], burstModeStatus, queuedTimingData); } else { shouldSendPermit[MPSMitigationDevicePockelsCell] = epicsFalse; shouldSendPermit[MPSMitigationDeviceBykik] = epicsFalse; } /* Don't permit beam past BYKIK if the Pockels cell denies beam */ if (shouldSendPermit[MPSMitigationDevicePockelsCell] == epicsFalse) { shouldSendPermit[MPSMitigationDeviceBykik] = epicsFalse; } #if 0 #pragma mark - #pragma mark - #pragma mark Build Permit message #endif /****************************************************************************** * Fill data into permit message ******************************************************************************/ permitMessage->timestamp = currentTimingData->timestamp; permitMessage->timeslot = currentTimingData->timeslot; permitMessage->destination = MPSDestinationInvalid; permitMessage->outputControl = outputControl; permitMessage->estimatedRate = ((currentTimingData->beamRequested) ? 1 << 6: 0) | ((currentTimingData->burstPulseRequested) ? 1 << 5: 0) | ((currentTimingData->baseRate120Hz) ? 1 << 4: 0) | ((currentTimingData->baseRate60Hz) ? 1 << 3: 0) | ((currentTimingData->baseRate30Hz) ? 1 << 2: 0) | ((currentTimingData->baseRate10Hz) ? 1 << 1: 0) | ((currentTimingData->baseRate1Hz) ? 1 << 0: 0); for (i = 0; i < MPSMitigationDeviceCount; i++) { permitMessage->rateAfterDevice[i] = maxRateAfterDevice[i]; permitMessage->shouldSendPermit[i] = shouldSendPermit[i]; } /* Zero timeslots since last permit sent for all devices sent a positive permit signal. * Always assume the permit message broadcast is successful */ for (i = 0; i < MPSMitigationDeviceCount; i++) { if (shouldSendPermit[i]) { timeslotsSinceLastPermitSent[i] = 0; } } #if 0 { /****************************************************************************** * Estimate Beam Rate At Mitigation Devices ******************************************************************************/ #warning Implement better code for the estimated beam rate and max beam rate info for the MPG /* * Estimate beam rate */ /* Get the current destination as determined by the algorithm */ beamDestination = getBeamDestination(); if (beamDestination <= MPSDestinationBykikDump) { device = MPSMitigationDeviceMechanicalShutter; } else { device = MPSMitigationDeviceBykik; } estimatedRate = MPSRate120Hz; if (lastMinTimeslotsSinceLastBeam[device] >= (360 / 60)) { estimatedRate = MPSRate60Hz; } if (lastMinTimeslotsSinceLastBeam[device] >= (360 / 30)) { estimatedRate = MPSRate30Hz; } if (lastMinTimeslotsSinceLastBeam[device] >= (360 / 10)) { estimatedRate = MPSRate10Hz; } if (lastMinTimeslotsSinceLastBeam[device] >= (360 / 1)) { estimatedRate = MPSRate1Hz; } if (maxRateAfterDevice[device] == MPSRate0Hz) { estimatedRate = MPSRate0Hz; } /* * Max beam rate info for MPG */ /* ========================= * Bit Description * ------------------------- * 0 Allow rate > 0 Hz * 1 Allow rate > 1 Hz * 2 Allow rate > 10 Hz * 3 Allow rate > 30 Hz * 4 Allow rate > 60 Hz * ========================= */ outputControl = 0x00; rate = maxRateAfterDevice[MPSMitigationDeviceMechanicalShutter]; if ((rate > MPSRate0Hz) && (rate <= MPSRate120Hz)) { if (rate > MPSRate0Hz) { outputControl |= 0x01 << 0; } if (rate > MPSRate1Hz) { outputControl |= 0x01 << 1; } if (rate > MPSRate10Hz) { outputControl |= 0x01 << 2; } if (rate > MPSRate30Hz) { outputControl |= 0x01 << 3; } if (rate > MPSRate60Hz) { outputControl |= 0x01 << 4; } } } #endif return err; } #if 0 #pragma mark - #pragma mark Initialize Mitigation #endif int initializeMitigation() { return MPS_SUCCESS; }