// UNCLASSIFIED /*------------------------------------------------------------------------- RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON COMPANY. THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT. UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. -------------------------------------------------------------------------*/ using NLog; using Raytheon.Common; using Raytheon.Units; using System; using System.Net.Sockets; using System.Threading; namespace Raytheon.Instruments { /// /// A class that provides an interface for controlling simulated power supply systems and their modules. /// public class PowerSupplySim : IDCPwr { #region PublicClassMembers #pragma warning disable CS0067 public event EventHandler OverCurrent; public event EventHandler OverVoltage; #pragma warning restore #endregion #region PublicFuctions /// /// The constructor for a sim power supply (simulated). /// /// The overcurrent protection setting (Amps). /// The overvoltage protection setting (Volts). /// The voltage setpoint (Volts). /// The max voltage setpoint (Volts). /// The min voltage setpoint (Volts). /// The module number (multiple modules in a system). public PowerSupplySim(string name, double overCurrentProtection, double overVoltageProtection, double voltageSetpoint, double maxVoltageSetpoint, double minVoltageSetpoint, double slewRateVoltsPerSecond, int moduleNumber = -1) { _name = name; _logger = LogManager.GetCurrentClassLogger(); _overCurrentProtection = overCurrentProtection; _overVoltageProtection = overVoltageProtection; _voltageSetpoint = voltageSetpoint; _voltageSetpointInitial = voltageSetpoint; _maxVoltageSetpoint = maxVoltageSetpoint; _minVoltageSetpoint = minVoltageSetpoint; _moduleNumber = moduleNumber; _isPowerOn = false; _slewRateVoltsPerSecond = slewRateVoltsPerSecond; // make sure it is off Enabled = false; // set up power supply OutputVoltage = Voltage.FromVolts(_voltageSetpoint); _state = State.Uninitialized; } /// /// /// /// public bool ClearErrors() { throw new NotImplementedException(); } /// /// /// public Current CurrentLimit { get { // a small 10 ms sleep for simulation Thread.Sleep(10); return Current.FromAmps(_overCurrentProtection); } set { _overCurrentProtection = value.Amps; } } /// /// /// public string DetailedStatus { get { return "This is a Sim Power Supply called " + _name; } } /// /// /// public bool DisplayEnabled { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } /// /// Dispose of this object's resources. /// public void Dispose() { try { Dispose(true); GC.SuppressFinalize(this); } catch (Exception) { try { //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace); } catch (Exception) { //Do not rethrow. Exception from error logger that has already been garbage collected } } } /// /// /// public bool Enabled { get { // a small 10 ms sleep for simulation Thread.Sleep(10); return _isPowerOn; } set { // a small 10 ms sleep for simulation Thread.Sleep(10); if (value == false) { _isPowerOn = false; } else { _isPowerOn = true; } } } /// /// /// public bool FrontPanelEnabled { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } /// /// /// /// public double GetSlewRate() { return _slewRateVoltsPerSecond; } /// /// /// public bool InhibitEnabled { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } /// /// /// public void Initialize() { // if we have not yet been initialized, go ahead and create the socket if (_state == State.Uninitialized) { _state = State.Ready; } else { throw new Exception("PowerSupplySim::Initialize() - expected the supply " + _name + " to be Uninitialized, state was: " + _state.ToString()); } } /// /// /// public InstrumentMetadata Info { get { throw new NotImplementedException(); } } /// /// Control the power supply internal mechanical relay state /// /// True to connect, false to disconnect public void MechanicalRelayOutputControl(bool shallWeConnect) { // nothing to do here } /// /// /// /// public Current MeasureCurrent() { return Current.FromAmps(ReadCurrent()); } /// /// /// /// public Voltage MeasureVoltage() { return Voltage.FromVolts(ReadVoltage()); } /// /// /// public string Name { get { return _name; } set { _name = value; } } /// /// /// public Voltage OutputVoltage { get { // a small 10 ms sleep for simulation Thread.Sleep(10); return Voltage.FromVolts(_voltageSetpoint); } set { // a small 10 ms sleep for simulation Thread.Sleep(10); double volts = value.Volts; // do not let host set the voltage out of range, unless it is being set to 0 if (volts != 0.0) { if (volts > _maxVoltageSetpoint || volts < _minVoltageSetpoint) { throw new Exception("PowerSupplySim::OutputVoltage() - Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString()); } } _voltageSetpoint = volts; } } /// /// /// public Voltage OverVoltageProtection { get { // a small 10 ms sleep for simulation Thread.Sleep(10); return Voltage.FromVolts(_overVoltageProtection); } set { _overVoltageProtection = value.Volts; } } /// /// /// public bool OverVoltageProtectionEnabled { get { return true; } set { throw new NotImplementedException(); } } /// /// /// /// public SelfTestResult PerformSelfTest() { throw new NotImplementedException(); } /// /// Reads the overcurrent and overvoltage protection (simulation). /// /// No Errors (simulated). public int ReadProtectionStatus() { const int PROTECTION_STATUS = 0; // a small 10 ms sleep for simulation Thread.Sleep(10); return PROTECTION_STATUS; } /// /// /// public void Reset() { // nothing to do } /// /// /// public SelfTestResult SelfTestResult { get { throw new NotImplementedException(); } } /// /// /// /// /*public void SetSlewRate(double slew) { _slew = slew; }*/ /// /// /// public State Status { get { return _state; } } /// /// /// public void Shutdown() { if (_state == State.Ready) { Off(); Reset(); _state = State.Uninitialized; } } /// /// /// public Voltage VoltageSoftLimit { get { return Voltage.FromVolts(_maxVoltageSetpoint); } set { throw new NotImplementedException(); } } #endregion #region PrivateClassMembers private string _name; private double _overCurrentProtection; private double _overVoltageProtection; private double _voltageSetpoint; private double _voltageSetpointInitial; private readonly double _maxVoltageSetpoint; private readonly double _minVoltageSetpoint; private readonly int _moduleNumber; private bool _isPowerOn; private double _slewRateVoltsPerSecond; private State _state; /// /// NLog logger /// private readonly ILogger _logger; #endregion #region PrivateFuctions /// /// The finalizer. /// ~PowerSupplySim() { Dispose(false); } /// /// Dispose of this object. /// /// True = currently disposing, False = not disposing. protected virtual void Dispose(bool disposing) { try { if (disposing) { if (_state == State.Ready) { Off(); _state = State.Uninitialized; } } } catch (Exception) { try { //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace); } catch (Exception) { //Do not rethrow. Exception from error logger that has already been garbage collected } } } /// /// Returns this object module number. /// /// The module number. /*public int GetModuleNumber() { return _moduleNumber; }*/ /// /// Turn the output off (simulated). /// private void Off() { // a small 10 ms sleep for simulation Thread.Sleep(10); _isPowerOn = false; } /// /// Turn the output on (simulated). /// private void On() { // a small 10 ms sleep for simulation Thread.Sleep(10); _isPowerOn = true; } /// /// Read the current (simulated). /// /// The current (simulated). private double ReadCurrent() { // a small 10 ms sleep for simulation Thread.Sleep(100); double currentToReturn = 0.0; if (_isPowerOn) { double maxCurrent = _overCurrentProtection; double minCurrent = _overCurrentProtection - .5; Random rnd = new Random(); double seed = rnd.NextDouble(); currentToReturn = (seed * (maxCurrent - minCurrent)) + minCurrent; } return currentToReturn; } /// /// Read the voltage. /// /// The voltage (simulated). private double ReadVoltage() { // a small 10 ms sleep for simulation Thread.Sleep(100); double voltageToReturn = 0.0; if (_isPowerOn) { double maxVoltage = _voltageSetpoint + 1; double minVoltage = _voltageSetpoint - 1; Random rnd = new Random(); double seed = rnd.NextDouble(); voltageToReturn = (seed * (maxVoltage - minVoltage)) + minVoltage; } return voltageToReturn; } #endregion } }