Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using log4net;
|
||||
using SchematicDrawing;
|
||||
using TBF.Rig.ControlBoard.Uni;
|
||||
using TBF.Rig.Generic;
|
||||
using TBF.Boxes;
|
||||
|
||||
namespace TBF.Rig.Uni.RegValve
|
||||
{
|
||||
public class RegValve : ComponentBase, IDevice, GenericDevices.IRegValve, 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, double> dict;
|
||||
public IDictionary<double, double> Dict { get { return dict; } }
|
||||
|
||||
readonly UniCB uniCB;
|
||||
readonly int adcChannel;
|
||||
|
||||
public ValveCategory Category { get { return regValveCfg.Category; } }
|
||||
public int Idx1 { get { return regValveCfg.Idx1; } } /// 1 .. 8
|
||||
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; } }
|
||||
|
||||
/// 0.0 .. 100.0
|
||||
public double Position
|
||||
{
|
||||
get
|
||||
{
|
||||
int denominator = DacValueOpen - DacValueClosed;
|
||||
if (denominator == 0) denominator = 1;
|
||||
return 100.0 * Convert.ToDouble(uniCB.AnalogInput(adcChannel) - DacValueClosed) / Convert.ToDouble(denominator);
|
||||
}
|
||||
}
|
||||
///
|
||||
public double SetpointVal
|
||||
{
|
||||
get { return Position; }
|
||||
set { TargetRegValvePosition = value; }
|
||||
}
|
||||
///
|
||||
public void IncreaseSetpoint()
|
||||
{
|
||||
/// TODO
|
||||
}
|
||||
///
|
||||
public void DecreaseSetpoint()
|
||||
{
|
||||
/// TODO
|
||||
}
|
||||
|
||||
public RegValveState RegValveState;
|
||||
public double TargetRegValvePosition;
|
||||
|
||||
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;
|
||||
adcChannel = regValveCfg.Idx1 - 1;
|
||||
|
||||
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, double>();
|
||||
|
||||
log.Warn(this.ToString());
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// IDevice interface implementation
|
||||
///
|
||||
public void Initialize() { }
|
||||
public void RunDeviceBefore()
|
||||
{
|
||||
Int16 adcValue = uniCB.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(double pctLo, double pctHi, int timeoutMs)
|
||||
{
|
||||
return new SetRegValvePositionOp(uniCB, this, pctLo, pctHi, timeoutMs);
|
||||
}
|
||||
|
||||
public IOperation ChangeRegulValvePositionOp(double timePulseSec)
|
||||
{
|
||||
return new ChangeRegulValvePositionOp(uniCB, this, timePulseSec);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user