228 lines
8.2 KiB
C#
228 lines
8.2 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using SchematicDrawing;
|
|
using TBF.BenchControl.ControlBoard.Uni;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.Uni.RegValve
|
|
{
|
|
public class RegValve : ComponentBase, IDevice, GenericDevices.IRegulValve, IDrawingItCmpntWithSetpoint
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(RegValve));
|
|
public override string ToString() { return string.Format("RegulValve({0})", Cfg.ToString(1)); }
|
|
|
|
|
|
public readonly RegValveCfg regValveCfg;
|
|
public IDrawingItem DrawingItem { get { return regValveCfg as IDrawingItem; } }
|
|
|
|
IDictionary<double, float> dict;
|
|
public IDictionary<double, float> Dict { get { return dict; } }
|
|
|
|
readonly UniCB uniCB;
|
|
|
|
public ValveCategory Category { get { return regValveCfg.Category; } }
|
|
public int Idx1 { get { return regValveCfg.Idx1; } } /// 1..7
|
|
public int DacValueClosed { get { return regValveCfg.AdcValueClosed; } } /// 0..1023
|
|
public int DacValueOpen { get { return regValveCfg.AdcValueOpen; } } /// 0..1023
|
|
public int StableTime { get { return regValveCfg.StableTime; } } /// 0=200ms, step=50ms, max. 1500ms (max.26)
|
|
public int FlowStableSec { get { return regValveCfg.FlowStableSec; } } /// 0..60 sec
|
|
///
|
|
public bool IsCoax { get { return false; } }
|
|
///
|
|
public float Position { get { return Raw2Position(uniCB.AnalogInput(AdcChannel)); } } /// 0 .. 100%
|
|
///
|
|
public double SetpointVal
|
|
{
|
|
get { return (double)TargetRegValvePosition; }
|
|
set { } // TODO
|
|
}
|
|
///
|
|
public void IncreaseSetpoint()
|
|
{
|
|
/// TODO
|
|
}
|
|
///
|
|
public void DecreaseSetpoint()
|
|
{
|
|
/// TODO
|
|
}
|
|
|
|
public int AdcChannel;
|
|
public RegValveMode RegValveMode;
|
|
public RegValveState RegValveState;
|
|
public float TargetRegValvePosition;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="analogInputValue"></param>
|
|
/// <returns></returns>
|
|
float Raw2Position(Int16 analogInputValue)
|
|
{
|
|
/// TODO
|
|
return 0.5f; /// return 50%
|
|
}
|
|
|
|
public void SendCommand(Command command, int v1, int v2, int v3, StopDevs v4)
|
|
{
|
|
/// TODO
|
|
}
|
|
|
|
public void SetFlow(int flowMeterId, double freqLo, double freqHi)
|
|
{
|
|
uniCB.SetFlow(flowMeterId, Idx1, freqLo, freqHi);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Incremental movement of the regulation valve.
|
|
/// </summary>
|
|
/// <param name="time">Time in seconds, positive value opens the reg. valve</param>
|
|
public void IncrementalMovement(double time)
|
|
{
|
|
uniCB.RegVlvIncrMove(Idx1, time);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Move a regulation valve to specfied position
|
|
/// </summary>
|
|
/// <param name="rvId">Reg. valve ID</param>
|
|
/// <param name="position">Reg. valve position (0 .. 1.0)</param>
|
|
public void MoveToPosition(double positionLo, double positionHi = -1)
|
|
{
|
|
uniCB.RegVlvMoveToPos(Idx1, positionLo, positionHi);
|
|
}
|
|
|
|
|
|
#region Configuration Change Handling
|
|
|
|
public static void OnCfgChange(object sender, CfgChangeArgs args)
|
|
{
|
|
if (CfgChangeHandler == null) return;
|
|
try { CfgChangeHandler(sender, args); }
|
|
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
|
|
}
|
|
|
|
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
|
|
|
|
public override void StartChangeHandler()
|
|
{
|
|
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
|
|
{
|
|
RegValveCfg tmpcfg = args.Cfg as RegValveCfg;
|
|
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
|
|
{
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
|
{
|
|
regValveCfg.StableTimeMs = tmpcfg.StableTimeMs;
|
|
regValveCfg.FlowStableSec = tmpcfg.FlowStableSec;
|
|
}
|
|
else if (args.Command == CfgChangeCmd.RVOpenStep)
|
|
{
|
|
IncrementalMovement(0.5);
|
|
}
|
|
else if (args.Command == CfgChangeCmd.RVCloseStep)
|
|
{
|
|
IncrementalMovement(-0.5);
|
|
}
|
|
else if (args.Command == CfgChangeCmd.GetAdc1 || args.Command == CfgChangeCmd.GetAdc2)
|
|
{
|
|
short adcValue = uniCB.AnalogInput(AdcChannel);
|
|
RegValveCfgCtrl.OnCmdResponse(this, new CmdResponseArgs(args.Command, Idx1, adcValue));
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
|
|
|
|
public RegValve()
|
|
{
|
|
}
|
|
|
|
public RegValve(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
regValveCfg = cfg as RegValveCfg;
|
|
|
|
this.uniCB = TbfComponents.FindComponent(cfg.ParentName, components) as UniCB;
|
|
if (this.uniCB == null) throw new Exception(string.Format("Cannot find {0} (a parent of {1})", cfg.ParentName, Name));
|
|
|
|
this.dict = new Dictionary<double, float>();
|
|
|
|
StartChangeHandler();
|
|
|
|
log.Warn(this.ToString());
|
|
}
|
|
|
|
|
|
///
|
|
/// IDevice interface implementation
|
|
///
|
|
public void Initialize() { }
|
|
public void RunDeviceBefore()
|
|
{
|
|
Int16 adcValue = uniCB.Data.AnalogInput[AdcChannel];
|
|
Handlers.OnAdcChanged(this, new CmdResponseArgs(CfgChangeCmd.GetAdc, Idx1, adcValue));
|
|
}
|
|
public void RunDeviceAfter() { }
|
|
public void StopDevice() { }
|
|
public void StopDevice2() { }
|
|
|
|
|
|
/// <summary>
|
|
/// Operation to set the water flow within tolerances
|
|
/// Events: Event.None, Event.FlowReached, Event.FlowTimeOut
|
|
/// </summary>
|
|
/// <param name="flowMeter">Flowmeter component</param>
|
|
/// <param name="requiredFlowLo">Lower limit of the required flow [m3/h]</param>
|
|
/// <param name="requiredFlowHi">Higher limit of the required flow [m3/h]</param>
|
|
/// <param name="pidCoef">PID coefficient (float)</param>
|
|
/// <param name="measuredFlow">Measured flow [m3/h]</param>
|
|
/// <param name="timeout">Timeout in [s]</param>
|
|
/// <returns>SetFlowOp instance</returns>
|
|
public IOperation SetFlowOp(GenericDevices.IFlowMeter flowMeter, double requiredFlowLo, double requiredFlowHi, DoubleBox measuredFlow, int timeout)
|
|
{
|
|
return new SetFlowOp(uniCB, this, flowMeter, requiredFlowLo, requiredFlowHi, measuredFlow, regValveCfg.ReTryCommands, timeout);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Operation to set the water flow within tolerances. Leave the measurement running.
|
|
/// Events: Event.None, Event.FlowReached, Event.FlowTimeOut
|
|
/// </summary>
|
|
/// <param name="flowMeter">Flowmeter component</param>
|
|
/// <param name="requiredFlowLo">Lower limit of the required flow [m3/h]</param>
|
|
/// <param name="requiredFlowHi">Higher limit of the required flow [m3/h]</param>
|
|
/// <param name="measuredFlow">Measured flow [m3/h]</param>
|
|
/// <param name="timeout">Timeout in [s]</param>
|
|
/// <returns>SetFlowOp instance</returns>
|
|
public IOperation SetFlowAndMeasureOp(GenericDevices.IFlowMeter flowMeter, double requiredFlowLo, double requiredFlowHi,
|
|
DoubleBox measuredFlow, int timeout, float filterConstant)
|
|
{
|
|
return new SetFlowOp(uniCB, this, flowMeter, requiredFlowLo, requiredFlowHi, measuredFlow, regValveCfg.ReTryCommands, timeout, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Operation to set the regulation valve to a required position
|
|
/// Events: Event.None
|
|
/// </summary>
|
|
/// <param name="flowLo">Lower limit of the required valve position in [%]</param>
|
|
/// <param name="flowLo">Higher limit of the required valve position in [%]</param>
|
|
/// <returns>SetPositionOp instance</returns>
|
|
public IOperation SetRegulValvePositionOp(float pctLo, float pctHi, int timeoutMs)
|
|
{
|
|
return new SetRegValvePositionOp(uniCB, this, pctLo, pctHi, timeoutMs);
|
|
}
|
|
|
|
public IOperation ChangeRegulValvePositionOp(float timePulseSec)
|
|
{
|
|
return new ChangeRegulValvePositionOp(uniCB, this, timePulseSec);
|
|
}
|
|
}
|
|
}
|