/* * File: StateDevice.cc * Author: lpiccoli * * Created on June 2, 2010, 1:42 PM */ #include "StateDevice.h" #include "Log.h" #include #include #include #include USING_FF_NAMESPACE /** * StateDevice constructor specifying the name and buffer size * * @param name device name * @param bufferSize number of state values kept in internal buffer * @author L.Piccoli */ StateDevice::StateDevice(std::string loopName, std::string name, int bufferSize, PatternMask patternMask, int patternIndex) : ActuatorDevice(loopName, name, bufferSize, patternMask, patternIndex), _setpoint(0), _error(0), _offset(0) { //_usedByLoopPv(loopName + " " + name + "USEDBYLOOP", true) { _caModePv = false; _setFbckPv = false; // States do not have FBCK PV! std::ostringstream strstream; strstream << loopName << " " << name << "OFFSET" << patternIndex; _offsetPv = new PvDataDouble(strstream.str(), 0); } /** * Save the specified value and timestamp as a DataPoint waiting to be written. * * @param value new actuator value * @param timestamp for the specified value * @return 0 if value is between _loloLimit and _hihiLimit, -1 if it is outside * the limits * @author L.Piccoli */ int StateDevice::set(double value, epicsTimeStamp timestamp) { // Update the state error - difference between value and setpoint if (_setpoint != 0) { _error = value - _setpoint->peek(); } else { _error = 0; } return ActuatorDevice::set(value, timestamp); } /** * Save the specified value in the internal buffer. Value is not written out * using the communication channel until write() is called. * * When set() is called, the difference between the value and the setpoint is * saved in the _error attribute. * * @author L.Piccoli */ int StateDevice::set(double value) { // Update the state error - difference between value and setpoint if (_setpoint != 0) { _error = value - _setpoint->peek(); } else { _error = 0; } return ActuatorDevice::set(value); } /** * Write out the next value using the communication channel. The actual value * written is added to the existing _offset. * * UPDATE: Actually, the states do not have an associated PV as Measurements or * Actuators have (e.g. XCOR:IN20:551:BDES). The write() method should do nothing * more than just update the status. However, to maintain the same status set * by the ActuatorDevice::write() the communicationChannel of the StateDevice * should be set to a NullChannel (see Loop::reconfigure() and * Device::configure()) * * @return 0 on success, -1 if there are no values to be written or * there has been a problem with the communication channel * @author L.Piccoli */ int StateDevice::write() { return ActuatorDevice::write(_buffer[_nextWrite]._value + _offset); } /** * Return last calculated error, i.e. difference between last value set() and * the last setpoint available. * * @return current error * @author L.Piccoli */ double StateDevice::getError() { return _error; } /** * Return the current offset, which is added to the value set() when it is * written out using the communication channel. * * @return state offset * @author L.Piccoli */ double StateDevice::getOffset() { // return _offset; return _offsetPv->getValue(); } /** * Change the state offset to the specified value. * * @param offset new state offset * @author L.Piccoli */ void StateDevice::setOffset(double offset) { *_offsetPv = offset; } /** * Specify the setpoint device that should be used by this StateDevice. * * @param setpoint new SetpointDevice that contains the setpoints for the * state * @author L.Piccoli */ void StateDevice::setSetpointDevice(SetpointDevice *setpoint) { _setpoint = setpoint; } /** * Override ActuatorDevice::getInitialSetting() method. There is no initial * setting for StateDevices. * * @return always 0 * @author L.Piccoli */ int StateDevice::getInitialSetting() { return 0; } /** * Returns the current value of the setpoint for this device. * * @return current setpoint value or ZERO if the setpoind device * has not been assigned (NULL). * @author L.Piccoli */ double StateDevice::getSetpoint() { if (_setpoint != NULL) { return _setpoint->peek(); } else { return 0; } } void StateDevice::show() { Device::show(); if (_setpoint != NULL) { std::cout << "[" << _setpoint->getName() << "=" << _setpoint->peek() << ", offset=" << _offsetPv->getValue(); if (_usedByLoopPv == true) { std::cout << ", used=true"; } std::cout << "]"; } } /** * Defines a unique Fcom device name for all states in the IOC. The name has to include * the IOC name, which is available in the $(LOCA_NAME) environment variable set * in the startup script. * * The ActuatorDevice::configure() method is bypassed because states do not need to * get an initial value. The Device::configure() is called directly. * * In Device::configure() the Fcom device name is built based on the _devNamePv * variable ($(LOOP):SxxNAME PV). In order to have the correct name used by Fcom * to get a unique id accross all states in this IOC we have to temporarily assign * a name to the _devNamePv. The actual SxxNAME PV value is restored only the * Device::configure() method returns. * * @return 0 on success, -1 on failure * @author L.Piccoli */ int StateDevice::configure(FastFeedback::CommunicationChannel::AccessType accessType, int patternIndex) { std::string deviceName = _devNamePv.getValue(); std::string iocName = getenv("LOCA_NAME"); std::string fcomDeviceName = "FBCK:" + iocName + ":" + _slotName + ":STATES"; // Include the state index (number in the string S1, S2, S3 or S4) into // the fcomDeviceName. This tells FcomChannel to put the state value // into the correct place when sending out states on FCOM. fcomDeviceName += _name.substr(_name.size() - 1, 1); _devNamePv = fcomDeviceName; int status = Device::configure(accessType, patternIndex); _devNamePv = deviceName; return status; }