Files
tbf/TBF/Rig/Uni/RegValveAnalog/RegValve.cs
T

162 lines
6.7 KiB
C#

///
/// Copyright (c) 2023 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using SchematicDrawing;
using TBF.Rig.ControlBoard.Uni;
using TBF.Boxes;
namespace TBF.Rig.Uni.RegValveAnalog
{
public class RegValve : ComponentBase, GenericDevices.IRegValve, IDrawingItCmpntWithSetpoint
{
private static readonly ILog log = LogManager.GetLogger(typeof(RegValve));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
public const int RVAnalogIdx = 9; /// Reg. valve ID used in communication with UniCB
public const double CalibDA = 550.0;
readonly RegValveCfg regValveCfg;
public IDrawingItem DrawingItem { get { return regValveCfg as IDrawingItem; } }
public ValveCategory Category { get { return regValveCfg.Category; } }
public int Idx1 { get { return RVAnalogIdx; } } /// Always 9
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 StoredPositionReuse { get { return regValveCfg.StoredPositionReuse; } }
IDictionary<double, double> dict;
public IDictionary<double, double> Dict { get { return dict; } }
readonly UniCB uniCB;
///
public bool IsCoax { get { return true; } }
///
public double Position
{
get
{
/// Converts int value (0 .. 55000) to double value (0.0 .. 100.0)
return (1 / CalibDA) * uniCB.Data.StavDA[0];
}
}
///
public double SetpointVal
{
get { return Position; }
set
{
/// Converts double value (0.0 .. 100.0) to int value (0 .. 65535)
(StateMachine.ControlBoard as TBF.Rig.ControlBoard.Uni.UniCB).RegVlvMoveToPos(true, RVAnalogIdx, Convert.ToInt32(CalibDA * value));
}
}
///
public void IncreaseSetpoint()
{
int currentVal = uniCB.Data.StavDA[0];
int newValue = Math.Min(currentVal + 1500, (int)(100 * CalibDA));
(StateMachine.ControlBoard as TBF.Rig.ControlBoard.Uni.UniCB).RegVlvMoveToPos(true, RVAnalogIdx, newValue);
}
///
public void DecreaseSetpoint()
{
int currentVal = uniCB.Data.StavDA[0];
int newValue = Math.Max(0, currentVal - 1500);
(StateMachine.ControlBoard as TBF.Rig.ControlBoard.Uni.UniCB).RegVlvMoveToPos(true, RVAnalogIdx, newValue);
}
/// <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(bool isFromUI, double posPct)
{
/// Converison assuming: 0..100 % ~ 4..20 mA , 0..20 mA ~ 0..1023
int targetAdcVal = Math.Max(0, Math.Min(1023, Convert.ToInt32(Math.Round(8.19 * posPct + 204.0))));
//uniCB.RegVlvMoveToPos(isFromUI, Idx1, targetAdcVal);
uniCB.RegVlvMoveToPos(isFromUI, Idx1, targetAdcVal, targetAdcVal);
}
public RegValve() { }
public RegValve(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
regValveCfg = cfg as RegValveCfg;
uniCB = TbfComponents.FindComponent(cfg.ParentName, components) as UniCB;
if (uniCB == null) throw new Exception("Cannot find " + Name + " parent");
}
public override void Initialize()
{
this.dict = new Dictionary<double, double>();
TBF.UiBridge.Bridge.SetpointChangeHandler += delegate(object sndr, TBF.UiBridge.SetpointChangeArgs args)
{
if (args.Name == Name)
{
if (args.Increase) IncreaseSetpoint(); else DecreaseSetpoint();
}
};
log.FatalFormat("{0} initialized: {1}", Name, this);
}
/// <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="qFrom">Lower limit of the required flow [m3/h]</param>
/// <param name="qTo">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>
/// <param name="delay">Flow setting starts after this delay in [s]</param>
/// <returns>SetFlowOp instance</returns>
public IOperation SetFlowOp(GenericDevices.IFlowMeter flowMeter, double qFrom, double qTo, DoubleBox measuredFlow, int timeout, int delay)
{
return new SetFlowOp(uniCB, this, flowMeter, qFrom, qTo, measuredFlow, timeout, delay);
}
/// <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="qFrom">Lower limit of the required flow [m3/h]</param>
/// <param name="qTo">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 qFrom, double qTo,
DoubleBox measuredFlow, int timeout, float filterConstant)
{
return new SetFlowOp(uniCB, this, flowMeter, qFrom, qTo, measuredFlow, timeout, 0, 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 SetRegValvePositionOp(double pctLo, double pctHi, int timeoutMs)
{
return new SetRegValvePositionOp(uniCB, this, pctLo, pctHi, timeoutMs);
}
public IOperation ChangeRegValvePositionOp(double timePulseSec)
{
return new ChangeRegValvePositionOp(uniCB, this, timePulseSec);
}
}
}