/* * File: LoopThread.cc * Author: lpiccoli * * Created on June 7, 2010, 2:13 PM */ #include "LoopThread.h" #include "PatternManager.h" #include "CollectorThread.h" #include "TimeUtil.h" #include "Log.h" USING_FF_NAMESPACE LoopThread::LoopThread(const std::string name, LoopConfiguration *configuration) : Thread(name), _state(WAITING_PATTERN), _configuration(configuration), _enabledPv(name + " STATE", true, this, RECONFIGURE_EVENT), _modePv(name + " MODE", false, this, MODE_EVENT), _patternCount(0), _measurementCount(0), _unexpectedPatternCount(0), _unknownPatternCount(0), _unexpectedMeasurementCount(0), _unexpectedMeasurementPatternCount(0), _pulseIdMismatchCount(0), _patternWitouthLoop(0), _pendingEventCount(0), _calculateStats(50, name + " Processing Time (algorithm time)"), _patternStats(50, name + " Pattern Stats (time between POIs)"), _loopRateStats(10, name + " Loop rate stats (time between calculations"), _ratePv(name + " RATE", 0) { createLoops(); // TODO: replace with configure() _configuration->_eventReceiver = this; } LoopThread::~LoopThread() { } /** * (Re)configure the Loops of a LoopThread based on the current configuration. * This method is invoked when the LoopThead is created and when the system * is reconfigured. * * Currently if there are Loops configured, they are deleted and created again. * * @return 0 on success, -1 on failure * @author L.Piccoli */ int LoopThread::configure() { // First delete all Loops std::vector::iterator it; for (it = _loops.begin(); it != _loops.end(); ++it) { Loop *loop = (*it); delete loop; } _loops.clear(); if (createLoops() != 0) { return -1; } return 0; } /** * Create the loops based on the patterns and algorithm in the configuration. * There is one loop per pattern - usually feedbacks respond to a single pattern * only. * * @return 0 if success, -1 on failure * @author L.Piccoli */ int LoopThread::createLoops() { std::vector::iterator it; for (it = _configuration->_patternMasks.begin(); it != _configuration->_patternMasks.end(); ++it) { // Create the Loop PatternMask patternMask = (*it); Loop *loop = new Loop(_configuration, patternMask); _loops.push_back(loop); } return 0; } /** * Process PATTERN_EVENTS, MEASUREMENT_EVENTS and RECONFIGURE_EVENT. * * @param event Event received by the LoopThread * @return 0 if event was recognized and processed, -1 on failure * @author L.Piccoli */ int LoopThread::processEvent(Event& event) { switch (event._type) { case MODE_EVENT: _configuration->_logger << Log::clearpv; return changeMode(); case RECONFIGURE_EVENT: _configuration->_logger << Log::clearpv; return reconfigure(); case PATTERN_EVENT: if (_enabledPv == true && _configuration->_configured) { return processPatternEvent(event); } return -1; case MEASUREMENT_EVENT: if (_enabledPv == true && _configuration->_configured) { return processMeasurementEvent(event); } return -1; case DISCONNECT_EVENT: return disconnectDevices(); default: return -1; } } /** * This method is invoked when the Loop STATE goes from ON to OFF. All * Devices are disconnected. New connections will be made when STATE goes * back to ON. * * @author L.Piccoli */ int LoopThread::disconnectDevices() { // Turn all FBCK PVs OFF when loop is turned OFF std::vector::iterator it; Loop *loop; for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); if (loop->setFeedbackDevices(false, false) != 0) { return -1; } } MeasurementCollector::getInstance().lock(); for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); loop->disconnectDevices(); } MeasurementCollector::getInstance().unlock(); return 0; } /** * Change mode of operation -> either to ENABLE or to COMPUTE * * If STATE is ON and MODE is ENABLE: * o Measurements FBCK ON * o Actuators FBCK ON * * If STATE is ON and MODE is COMPUTE: * o Measurements FBCK ON * o Actuators FBCK OFF - do not set actuators * * If STATE is OFF and MODE is ENABLE *or* COMPUTE * o Measurements FBCK OFF * o Actuators FBCK OFF * * If STATE is ON and MODE changes from COMPUTE to ENABLE: * o Must retrieve current actuator values before calculating * the next feedback value * * @author L.Piccoli */ int LoopThread::changeMode() { std::vector::iterator it; Loop *loop; bool actuatorFbck = false; bool measurementFbck = false; if (_enabledPv == true) { if (_modePv == true) { actuatorFbck = true; measurementFbck = true; // If system is ON and MODE was previously in COMPUTE then // we must update the actuators with new initial values if (_configuration->_mode == false) { if (resetActuators() != 0) { _modePv = false; _enabledPv = false; return -1; } // Reset Loop algorithms for (it = _loops.begin(); it != _loops.end(); ++it) { (*it)->resetAlgorithm(); } // Also clear all pending events in the queue and start fresh if (getPendingEvents() > 0) { std::cout << "WARN: discarding " << getPendingEvents() << " events... "; clear(); std::cout << " done." << std::endl; } } } else { measurementFbck = true; } } _configuration->_mode = _modePv.getValue(); _configuration->_logger << Log::clearpv; // Set FBCK for all actuator and measurement devices. // If feedback is off (STATE = OFF) then this call will not // result in setting the FBCK PV, because the devices are not // connected. for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); if (loop->setFeedbackDevices(measurementFbck, actuatorFbck) != 0) { return -1; } } return 0; } /** * This method reconfigures the LoopThread when the STATE PV transitions * from ON to OFF or from OFF to ON. * * All configuration is reloaded from the current values of the Loop PVs. * * TODO: method too long! * * @return 0 if Loop thread is ready, or -1 if there is a problem during * reconfiguration, in which case the Loop will remain OFF. * @author L.Piccoli */ int LoopThread::reconfigure() { if (_enabledPv == false) { // If Loop was configured, then mark it not configured, so the next // time _enable is true ($(LOOP):STATE = On) the configuration is // reloaded. if (_configuration->_configured == true) { // At this point disconnect all Devices, exclude them // from MeasurementCollector as well disconnectDevices(); // Remove this thread from the CollectorThread list CollectorThread::getInstance().remove(*this); // Remove this thread from the PatternManager list long numPatterns = _configuration->_totalPoiPv.getValue(); PatternManager::getInstance().lock(); for (int i = 0; i < numPatterns; ++i) { PatternManager::getInstance().remove(_configuration->_patternMasks[i], *this); } PatternManager::getInstance().unlock(); _configuration->_configured = false; _loopRateStats.clear(); } _configuration->_logger << Log::clearpv; return 0; } if (_configuration->_configured == true) { _configuration->_logger << "Loop " << _configuration->_loopNamePv.getValue().c_str() << " configured." << Log::flushpvnoalarm; return 0; } _configuration->_logger << "Reconfiguring Loop " << _configuration->_loopNamePv.getValue().c_str() << Log::flushpvnoalarm; // Configure if and only if the INSTALLED PV is set to true if (_configuration->_installedPv == false) { _enabledPv = false; _configuration->_logger << "INSTALLED PV set to NO, cannot configure. " << Log::flushpv; return -1; } if (_configuration->configure() != 0) { _configuration->_logger << "ERROR: Failed to reconfigure Loop." << Log::flush; _enabledPv = false; } else { long numPatterns = _configuration->_totalPoiPv.getValue(); MeasurementCollector::getInstance().lock(); for (int i = 0; i < numPatterns; ++i) { // Reconfigure Loop if (_loops[i]->reconfigure(_configuration->_patternMasks[i]) != 0) { _configuration->_logger << "ERROR: Failed to reconfigure Loop " << i << Log::flush; _enabledPv = false; MeasurementCollector::getInstance().unlock(); disconnectDevices(); return -1; } // Add the LoopThread as a receiver for the PATTERN_EVENT containing // the configured patterns PatternManager::getInstance().lock(); int status = PatternManager::getInstance().add(_configuration->_patternMasks[i], *this); PatternManager::getInstance().unlock(); if (status != 0) { _configuration->_logger << "ERROR: Failed to add pattern to PatternManager." << _configuration->_patternMasks[i].toString().c_str() << Log::flush; _enabledPv = false; MeasurementCollector::getInstance().unlock(); disconnectDevices(); return -1; } } MeasurementCollector::getInstance().unlock(); // Clear collected statistics _calculateStats.clear(); _patternStats.clear(); _loopRateStats.clear(); // Turn on the FBCK PV on Actuators and Measurements if (changeMode() != 0) { return -1; } CollectorThread::getInstance().add(*this); _configuration->_configured = true; _configuration->_logger << "INFO: Loop reconfigured successfully" << Log::flush; } _configuration->_logger << Log::clearpv; // After configuration has finished, clear all pending events in the queue // and start fresh :) if (getPendingEvents() > 0) { std::cout << "WARN: discarding " << getPendingEvents() << " events... "; clear(); std::cout << " done." << std::endl; } return 0; } /** * When a PATTERN_EVENT is received, the processEvent() method is called. * processEvent() is a virtual function defined in the class Thread that is * implemented by the LoopThread. It is basically a switch statement that routes * the PATTERN_EVENT to the processPattern() method. * * Only the reception of a PATTERN_EVENT that contains a pattern of interest * will signal the LoopThread to process the feedback on the next * MEASUREMENT_EVENT. A LoopThread can expect multiple patterns. * * A PATTERN_EVENT received after another PATTERN_EVENT indicates problems with * getting measurements. * * Multiple POI: If two or more patterns are used then the feedback loop * must keep track the offsets between actuator values. These offsets are * used when the beam rate changes, e.g. going from 2 patterns to 1 pattern. * In this case when the second pattern has beam again the actuators must start * from the position last calculated to pattern 1 minus the last calculated * offset (when there was beam on pattern 2). * * @param event a PATTERN_EVENT * @returns 0 if pattern was processed, -1 if pattern was unexpected or unknown * @author L.Piccoli */ int LoopThread::processPatternEvent(Event &event) { #ifdef DEBUG_EVENT _configuration->_logger << "DEBUG: LoopThread got PATTERN_EVENT pulseId=" << (int) event._pattern.getPulseId() << Log::flush; #endif std::vector::iterator it; bool knownPattern = false; Loop *loop; int matchCounter = 0; for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); PatternMask mask = loop->getPatternMask(); if (event._pattern.match(mask)) { knownPattern = true; matchCounter++; } } if (!knownPattern) { _unknownPatternCount++; if (_state == WAITING_PATTERN) { return -1; } } if (matchCounter != 1) { _configuration->_logger << "ERROR: EVR pattern matched multiple POIs" << Log::flush; _configuration->_logger << "Suspicious POI configuration" << Log::flushpvonly; return -1; } if (_state != WAITING_PATTERN) { #ifdef DEBUG_EVENT _configuration->_logger << "DEBUG: LoopThread was waiting MEAS_EVENT " << "for pattern " << (int) _pattern.getPulseId() << ", " << "got PATT_EVENT (PulseId " << (int) event._pattern.getPulseId() << ")" << Log::flush; #endif // TODO: If the pattern is known, it means the MEASUREMENT_EVENT for the // previous PATTERN_EVENT is late. _unexpectedPatternCount++; // return -1; } // Keep pattern statistics (elapsed time between valid patterns) _patternStats.end(); _patternStats.start(); // Save the pattern for later, to make sure next MEASUREMENT_EVENT is from // the same pattern _pattern = event._pattern; _configuration->_pulseIdPv = _pattern.getPulseId(); _patternCount++; _state = WAITING_MEASUREMENT; return 0; } /** * This method is called when a MEASUREMENT_EVENT is received by the LoopThread. * * When a MEASUREMENT_EVENT is received, the processEvent() method is called. * processEvent() is a virtual function defined in the class Thread that is * implemented by the LoopThread. processEvent() is basically a switch statement * that routes the MEASUREMENT_EVENT to the processEvent() method. * * If a MEASUREMENT_EVENT is received after a PATTERN_MEASUREMENT then the * pattern timestamp and pulse id must be checked against timestamp and pulse id * of the measurements. If there is a match then the feedback calculation can * proceed. In case of a mismatch this event should be logged and an alarm * dispatched if necessary. * * If the pattern within the MEASUREMENT_EVENT is not the expected pattern * saved in the _pattern attribute the LoopThread should go back to the * WAITING_PATTERN state and get the next PATTERN_EVENT. * * @return 0 on success, -1 on failure * @author L.Piccoli */ bool MULTIPLE_MATCHES = false; int LoopThread::processMeasurementEvent(Event &event) { #ifdef DEBUG_EVENT _configuration->_logger << "DEBUG: LoopThread got MEASUREMENT_EVENT pulseId=" << (int) event._pattern.getPulseId() << Log::flush; #endif if (_state == WAITING_PATTERN) { #ifdef DEBUG_EVENT _configuration->_logger << "DEBUG: LoopThread was waiting PATT_EVENT " << "PulseId " << (int) _pattern.getPulseId() << ", " << "got MEAS_EVENT (PulseId " << (int) event._pattern.getPulseId() << ")" << Log::flush; #endif _unexpectedMeasurementCount++; return -1; } // If pattern received here is not the same received previously, // then the latest measurements may not correspond to the correct pattern, // skip this pattern/measurement and wait for the next matching pattern if (_pattern != event._pattern) { Log::getInstance() << "LoopThread got *unexpected pattern* MEAS_EVENT pulseId=" << (int) event._pattern.getPulseId() << Log::flush; _unexpectedMeasurementPatternCount++; _state = WAITING_PATTERN; return -1; } // Check if pulseId from PATTERN_EVENT is the same received in the // MEASUREMENT_EVENT if (_pattern.getPulseId() != event._pattern.getPulseId()) { _pulseIdMismatchCount++; } // Make sure the PulseId from the PATTERN_EVENT is the same // one received in the MEASUREMENT_EVENT. if (_pattern.getPulseId() != event._pattern.getPulseId()) { _unexpectedMeasurementPatternCount++; _state = WAITING_PATTERN; return -1; } _measurementCount++; std::vector::iterator it; Loop *loop; bool match = false; int loopStatus = 0; for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); PatternMask mask = loop->getPatternMask(); if (event._pattern.match(mask)) { if (match) { _configuration->_logger << "ERROR: Multiple pattern matches" << Log::flushpvonly; if (MULTIPLE_MATCHES == false) { std::cout << &event._pattern << std::endl; std::vector::iterator _it = _loops.begin(); Loop *l = (*_it); PatternMask m = l->getPatternMask(); std::cout << &m; if (event._pattern.match(m)) { std::cout << "MATCH" << std::endl; } else { std::cout << "NO MATCH" << std::endl; } _it++; l = (*_it); PatternMask m2 = l->getPatternMask(); std::cout << &m2; if (event._pattern.match(m2)) { std::cout << "MATCH" << std::endl; } else { std::cout << "NO MATCH" << std::endl; } MULTIPLE_MATCHES = true; } } else { _calculateStats.start(); _loopRateStats.end(); _ratePv = _loopRateStats.getRate(); loopStatus = loop->calculate(event._pattern); _calculateStats.end(); _loopRateStats.start(); match = true; } } } if (!match) { _patternWitouthLoop++; } // A pulse mismatch is indicated when a -2 status is returned from // the loop->calculate() method. if (loopStatus == -2) { if (getPendingEvents() > 0) { _configuration->_logger << "Discarding " << (int) getPendingEvents() << " events. " << Log::flushpvonly; clear(); } } _state = WAITING_PATTERN; // Let EPICS know that PVs can be updated now scanIoRequest(_configuration->_scanlist); // Count pending events in the LoopThread input queue _pendingEventCount += getPendingEvents(); #ifdef DEBUG_EVENT _configuration->_logger << "DEBUG: LoopThread processed MEASUREMENT_EVENT pulseId=" << (int) event._pattern.getPulseId() << Log::flush; #endif return 0; } /** * Read current actuator values. Used when feedback loop returns to ENABLE mode * from COMPUTE mode. * * @return 0 * @author L.Piccoli */ int LoopThread::resetActuators() { std::vector::iterator it; Loop *loop; int status = 0; for (it = _loops.begin(); it != _loops.end(); ++it) { loop = (*it); if (loop->resetActuators() != 0) { status = -1; } } return status; } void LoopThread::show() { // std::cout << "--- LoopThread " << _name << std::endl; std::cout << " name: " << _configuration->_loopNamePv.getValue() << std::endl; std::cout << " installed: "; if (_configuration->_installedPv == true) { std::cout << "yes" << std::endl; } else { std::cout << "no (not configured)" << std::endl; } std::cout << " state: "; if (_enabledPv == true) { std::cout << "on" << std::endl; } else { std::cout << "off" << std::endl; } std::cout << " mode: "; if (_modePv == true) { std::cout << "enable" << std::endl; } else { std::cout << "compute" << std::endl; } std::cout << " algorithm: " << _configuration->_algorithmNamePv.getValue() << std::endl; std::cout << " # patterns: " << _configuration->_totalPoiPv.getValue() << std::endl << std::endl; std::cout << " -- Loops: " << std::endl; int size = _configuration->_totalPoiPv.getValue(); for (int i = 0; i < size; ++i) { std::cout << " " << i << ") "; _loops[i]->show(); } std::cout << "---" << std::endl; } void LoopThread::showConfig() { std::cout << " name: " << _configuration->_loopNamePv.getValue() << std::endl; std::cout << " -- Matrices: " << std::endl; _configuration->showMatrices(); std::cout << std::endl; std::cout << " -- Reference Orbit: " << std::endl; _configuration->showReferenceOrbit(); std::cout << std::endl; std::cout << " -- Actuator Energy: " << std::endl; _configuration->showActuatorEnergy(); std::cout << std::endl; std::cout << " -- Dispersion: " << std::endl; _configuration->showDispersion(); std::cout << std::endl; std::cout << "---" << std::endl; } void LoopThread::showDebug() { Thread::showDebug(); std::cout << " --" << std::endl; std::cout << " events rcvd: " << _eventCount << std::endl; std::cout << " events ignored:" << getDiscardedEvents() << std::endl; std::cout << " pending events:" << _pendingEventCount << std::endl; std::cout << " patterns rcvd: " << _patternCount << ", " << _unexpectedPatternCount << " (unexpected), " << _unknownPatternCount << " (unknown)" << std::endl; std::cout << " measurements: " << _measurementCount << ", " << _unexpectedMeasurementCount << " (unexpected), " << _unexpectedMeasurementPatternCount << " (unexpected pattern)" << std::endl; std::cout << " pulseId mismatches: " << _pulseIdMismatchCount << std::endl; std::cout << " patternWitouthLoop: " << _patternWitouthLoop << std::endl; _calculateStats.show(" "); _patternStats.show(" "); std::cout << " -- Loops: " << std::endl; int size = _configuration->_totalPoiPv.getValue(); for (int i = 0; i < size; ++i) { std::cout << " " << i << ") "; _loops[i]->showDebug(); } }