Danfoss-VLT2800-Pump functions OK, TODO: control the valve of the pump
This commit is contained in:
@@ -9,177 +9,311 @@ using TBF.Boxes;
|
||||
|
||||
namespace TBF.BenchControl.Danfoss.VLT2800
|
||||
{
|
||||
/// <summary>
|
||||
/// See page 20 of MG10S202 Operational Instructions Modbus RTU
|
||||
/// </summary>
|
||||
public enum CW : ushort
|
||||
{
|
||||
PresetRefLsb = (1 << 0),
|
||||
PresetRefMsb = (1 << 1),
|
||||
Not_DcBraking = (1 << 2),
|
||||
Not_CoastingStop = (1 << 3),
|
||||
Not_QuickStop = (1 << 4),
|
||||
Not_FreezeFreq = (1 << 5),
|
||||
Start_NotRampStop = (1 << 6),
|
||||
Reset = (1 << 7),
|
||||
Jog = (1 << 8),
|
||||
Ramp2_NotRamp1 = (1 << 9),
|
||||
DataValid = (1 << 10),
|
||||
Relay01Activ = (1 << 11),
|
||||
DOut46Activ = (1 << 12),
|
||||
SelectSetupLsb = (1 << 13),
|
||||
SelectSetupMsb = (1 << 14),
|
||||
Reversing = (1 << 15),
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See page 22 of MG10S202 Operational Instructions Modbus RTU
|
||||
/// </summary>
|
||||
public enum SW : ushort
|
||||
{
|
||||
ControlReady = (1 << 0),
|
||||
DriveReady = (1 << 1),
|
||||
CoastingStop = (1 << 2),
|
||||
Trip = (1 << 3),
|
||||
TripLock = (1 << 6),
|
||||
Warning = (1 << 7),
|
||||
SpeedEqRef = (1 << 8),
|
||||
RemoteCtrl = (1 << 9),
|
||||
FreqLimitOK = (1 << 10),
|
||||
MotorRunning = (1 << 11),
|
||||
VoltageWarn = (1 << 13),
|
||||
CurrentLimit = (1 << 14),
|
||||
ThermalWarn = (1 << 15),
|
||||
}
|
||||
|
||||
public enum Function : byte
|
||||
{
|
||||
ReadCoilStatus = 0x01,
|
||||
ForceSingleCoil = 0x05,
|
||||
ForceMultipleCoils = 0x0F,
|
||||
ReadHoldingRegisters = 0x03,
|
||||
PresetSingleRegister = 0x06,
|
||||
PresetMultipleRegisters = 0x10,
|
||||
}
|
||||
/// <summary>
|
||||
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
|
||||
/// Connection settings: 19200 Bd 8-bits No-parity 1-stop-bit Flow control: none or hardware.
|
||||
/// </summary>
|
||||
public class Pump : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient
|
||||
{
|
||||
public class Pump : ComponentBase, IDevice, IOperation, GenericDevices.IPumpFM, GenericDevices.IValve
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(Pump));
|
||||
public override string ToString() { return string.Format("Pump-Danfoss-VLT2800({0})", Cfg.ToString(1)); }
|
||||
|
||||
private readonly PumpCfg ambientCfg;
|
||||
private readonly PumpCfg pumpCfg;
|
||||
|
||||
public int Delay { get { return pumpCfg.Delay; } }
|
||||
public ValveCategory Category { get { return ValveCategory.Feeding; } } /// Pump category is Feeding
|
||||
public GenericDevices.IValve CoupledTo { get { return null; } }
|
||||
public bool InvertCouple { get { return false; } }
|
||||
public int LagOpening { get { return 0; } }
|
||||
public int LagClosing { get { return 0; } }
|
||||
|
||||
readonly Elde.ControlBoardDev controlBoard;
|
||||
readonly int bitPosition; /// 0 .. 63
|
||||
|
||||
public readonly ulong Mask; /// derived from bitPosition in the constructor
|
||||
|
||||
public bool State
|
||||
{
|
||||
get { return (controlBoard.Route & Mask) != 0; }
|
||||
}
|
||||
|
||||
/// Private fields
|
||||
SerialPort serialPort;
|
||||
StringBuilder measurementBuilder;
|
||||
bool dataToSendReady;
|
||||
byte[] dataToSend;
|
||||
StringBuilder responseBuilder;
|
||||
|
||||
/// <summary>The state of the measurement</summary>
|
||||
MsrmntState msrmntState;
|
||||
|
||||
/// <summary>Measured temperature when MsrmntState == MsrmntState.Valid</summary>
|
||||
float temperature;
|
||||
|
||||
/// <summary>Measured humidity when MsrmntState == MsrmntState.Valid</summary>
|
||||
float humidity;
|
||||
|
||||
/// <summary>Measured pressure when MsrmntState == MsrmntState.Valid</summary>
|
||||
float pressure;
|
||||
|
||||
/// <summary>Measurement time stamp when MsrmntState == MsrmntState.Valid</summary>
|
||||
int msrmntTimeStamp;
|
||||
|
||||
/// <summary>
|
||||
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
|
||||
/// Connection settings: 19200 Bd 8-bits No-parity 1-stop-bit Flow control: none or hardware.
|
||||
/// </summary>
|
||||
public Pump(PumpCfg cfg, IList<Generic.IComponent> components)
|
||||
: base(cfg)
|
||||
{
|
||||
ambientCfg = cfg;
|
||||
: base(cfg)
|
||||
{
|
||||
pumpCfg = cfg;
|
||||
|
||||
controlBoard = (Elde.ControlBoardDev)TbfComponents.FindComponent("CB", components); /// TODO: replace "CB"
|
||||
if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent");
|
||||
|
||||
//bitPosition = pumpCfg.BitPosition;
|
||||
bitPosition = 55; /// TODO: replace 55
|
||||
Mask = (((ulong)1) << bitPosition);
|
||||
|
||||
log.Debug(this.ToString());
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
|
||||
if (pumpCfg.DebugLevel == Entities.DebugMode.Simulate)
|
||||
{
|
||||
temperature = 20.0f;
|
||||
humidity = 40.0f;
|
||||
pressure = 1.0f;
|
||||
msrmntTimeStamp = StateMachine.Time;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
return;
|
||||
}
|
||||
|
||||
string comPortName = "COM" + ambientCfg.ComPortNr.ToString();
|
||||
serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits);
|
||||
serialPort.Handshake = ambientCfg.Handshake;
|
||||
string comPortName = "COM" + pumpCfg.ComPortNr.ToString();
|
||||
serialPort = new SerialPort(comPortName, pumpCfg.BaudRate, pumpCfg.Parity, pumpCfg.DataBits, pumpCfg.StopBits);
|
||||
serialPort.Handshake = Handshake.None;
|
||||
serialPort.Open();
|
||||
measurementBuilder = new StringBuilder(40);
|
||||
msrmntState = MsrmntState.Busy;
|
||||
responseBuilder = new StringBuilder(40);
|
||||
|
||||
log.FatalFormat("Successfully initialized device {0}", ToString());
|
||||
}
|
||||
log.FatalFormat("Successfully initialized device {0}", ToString());
|
||||
}
|
||||
|
||||
/// <summary>Run this device</summary>
|
||||
public void RunDeviceBefore()
|
||||
{
|
||||
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
|
||||
{
|
||||
msrmntTimeStamp = StateMachine.Time;
|
||||
return;
|
||||
}
|
||||
|
||||
measurementBuilder.Append(serialPort.ReadExisting());
|
||||
int len = measurementBuilder.Length;
|
||||
|
||||
///
|
||||
/// Device sends each 20 sec one line of ASCII text with data in the following format:
|
||||
/// (temp) 6 characters, two decimal digits, with leading spaces
|
||||
/// TAB 1 character
|
||||
/// (humi) 6 characters, two decimal digits, with leading spaces
|
||||
/// TAB 1 character
|
||||
/// (pressure) 6 characters, one decimal digits, with leading spaces is LT 1000
|
||||
/// CR + LF 2 characters
|
||||
/// Example:
|
||||
/// 23.42 38.08 1061.1
|
||||
/// 23.39 38.04 1060.8
|
||||
/// 23.39 38.08 1061.4
|
||||
///
|
||||
if (len >= 22)
|
||||
{
|
||||
string measurement = measurementBuilder.ToString();
|
||||
measurementBuilder.Clear();
|
||||
|
||||
float t = 0;
|
||||
float h = 0;
|
||||
float p = 0;
|
||||
if (float.TryParse(measurement.Substring(len - 22, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out t) &&
|
||||
measurement[len - 16] == '\t' &&
|
||||
float.TryParse(measurement.Substring(len - 15, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out h) &&
|
||||
measurement[len - 9] == '\t' &&
|
||||
float.TryParse(measurement.Substring(len - 8, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out p) &&
|
||||
measurement[len - 2] == '\r' &&
|
||||
measurement[len - 1] == '\n')
|
||||
{
|
||||
temperature = t;
|
||||
humidity = h;
|
||||
pressure = p;
|
||||
msrmntTimeStamp = StateMachine.Time;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
|
||||
log.InfoFormat("Msrmnt OK: time = {0}, temp={1}, humi={2}, pres={3}", msrmntTimeStamp, t, h, p);
|
||||
}
|
||||
else
|
||||
{
|
||||
log.Warn("Invalid string received: " + measurement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Run this device</summary>
|
||||
public void RunDeviceAfter()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Stop this device</summary>
|
||||
public void StopDevice()
|
||||
{
|
||||
if (serialPort != null) serialPort.Close();
|
||||
}
|
||||
|
||||
///
|
||||
/// Boxes for the operation result
|
||||
///
|
||||
FloatBox tempBox;
|
||||
FloatBox pressureBox;
|
||||
FloatBox humiBox;
|
||||
|
||||
public IOperation ReadAmbientOp(FloatBox temperature, FloatBox pressure, FloatBox humidity)
|
||||
/// <summary>Run this device</summary>
|
||||
public void RunDeviceBefore()
|
||||
{
|
||||
this.tempBox = temperature;
|
||||
this.pressureBox = pressure;
|
||||
this.humiBox = humidity;
|
||||
if (pumpCfg.DebugLevel == Entities.DebugMode.Simulate) return;
|
||||
|
||||
responseBuilder.Append(serialPort.ReadExisting());
|
||||
|
||||
/// Check if the response is complete
|
||||
if (responseBuilder.Length >= 8)
|
||||
{
|
||||
string response = responseBuilder.ToString();
|
||||
responseBuilder.Clear();
|
||||
|
||||
log.Info(Utils.LogMessage("Received ", response));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepares data to be sent in RunDeviceAfter() function
|
||||
/// </summary>
|
||||
bool SendData(byte[] data)
|
||||
{
|
||||
if (dataToSendReady) return false;
|
||||
dataToSend = data;
|
||||
dataToSendReady = true;
|
||||
|
||||
log.Info(Utils.LogMessage("Sending ", data));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Run this device</summary>
|
||||
public void RunDeviceAfter()
|
||||
{
|
||||
if (dataToSendReady)
|
||||
{
|
||||
if (pumpCfg.DebugLevel != Entities.DebugMode.Simulate)
|
||||
{
|
||||
serialPort.Write(dataToSend, 0, dataToSend.Length);
|
||||
}
|
||||
dataToSendReady = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stop this device</summary>
|
||||
public void StopDevice()
|
||||
{
|
||||
if (serialPort != null) serialPort.Close();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Turn the pmp on using Modbus commad, use the variable frequency
|
||||
/// </summary>
|
||||
/// <param name="powerArg">Frequency in percent (-200.0 .. 200.0)</param>
|
||||
public void TurnOn(float powerArg)
|
||||
{
|
||||
log.DebugFormat("{0}.TurnOn({1})", Name, powerArg);
|
||||
|
||||
//UInt16 controlWord = 0x047C;
|
||||
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
||||
CW.Not_CoastingStop |
|
||||
CW.Not_QuickStop |
|
||||
CW.Not_FreezeFreq |
|
||||
CW.Start_NotRampStop |
|
||||
CW.DataValid);
|
||||
|
||||
UInt16 serialComReference = (UInt16)(Int16)((float)0x4000 * powerArg / 100.0f + 0.5f);
|
||||
|
||||
byte[] msg = new byte[13];
|
||||
msg[0] = (byte)pumpCfg.ModbusAddress;
|
||||
msg[1] = (byte)Function.ForceMultipleCoils;
|
||||
msg[2] = 0; /// Data Hi
|
||||
msg[3] = 0; /// Data Lo
|
||||
msg[4] = 0; /// Nr. of coils Hi
|
||||
msg[5] = 0x20; /// Nr. of coils Lo
|
||||
msg[6] = 4; /// Byte count
|
||||
msg[7] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
||||
msg[8] = (byte)(controlWord >> 8); /// Control Word Hi
|
||||
msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
||||
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
||||
|
||||
Utils.CalculateCRC(msg);
|
||||
|
||||
SendData(msg);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Turn the pmp off using Modbus command
|
||||
/// </summary>
|
||||
public void TurnOff()
|
||||
{
|
||||
log.DebugFormat("{0}.TurnOff()", Name);
|
||||
|
||||
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
||||
CW.Not_CoastingStop |
|
||||
CW.Not_QuickStop |
|
||||
CW.Not_FreezeFreq |
|
||||
//CW.Start_NotRampStop |
|
||||
CW.DataValid);
|
||||
|
||||
UInt16 serialComReference = 0;
|
||||
|
||||
byte[] msg = new byte[13];
|
||||
msg[0] = (byte)pumpCfg.ModbusAddress;
|
||||
msg[1] = (byte)Function.ForceMultipleCoils;
|
||||
msg[2] = 0; /// Data Hi
|
||||
msg[3] = 0; /// Data Lo
|
||||
msg[4] = 0; /// Nr. of coils Hi
|
||||
msg[5] = 0x20; /// Nr. of coils Lo
|
||||
msg[6] = 4; /// Byte count
|
||||
msg[7] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
||||
msg[8] = (byte)(controlWord >> 8); /// Control Word Hi
|
||||
msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
||||
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
||||
|
||||
Utils.CalculateCRC(msg);
|
||||
|
||||
SendData(msg);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The currently running operation.
|
||||
/// </summary>
|
||||
public enum CurrentOp
|
||||
{
|
||||
None,
|
||||
TurnOn,
|
||||
TurnOff,
|
||||
}
|
||||
///
|
||||
CurrentOp currentOp;
|
||||
|
||||
float powerForOp;
|
||||
|
||||
/// <summary>
|
||||
/// Turn the pump frequency inverter on.
|
||||
/// </summary>
|
||||
public IOperation TurnOnOp(float powerArg)
|
||||
{
|
||||
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
||||
currentOp = CurrentOp.TurnOn;
|
||||
powerForOp = powerArg;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Start this operation</summary>
|
||||
public void Start()
|
||||
/// <summary>
|
||||
/// Turn the pump frequency inverter on.
|
||||
/// </summary>
|
||||
public IOperation TurnOffOp()
|
||||
{
|
||||
if (tempBox != null) tempBox.Val = temperature;
|
||||
if (pressureBox != null) pressureBox.Val = pressure;
|
||||
if (humiBox != null) humiBox.Val = humidity;
|
||||
}
|
||||
|
||||
/// <summary>Run this operation</summary>
|
||||
/// <returns>
|
||||
/// Event.FlowInDone or Event.FlowOutDone
|
||||
/// </returns>
|
||||
public Event Run()
|
||||
{
|
||||
if (tempBox != null) tempBox.Val = temperature;
|
||||
if (pressureBox != null) pressureBox.Val = pressure;
|
||||
if (humiBox != null) humiBox.Val = humidity;
|
||||
return Event.AmbientDone;
|
||||
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
||||
currentOp = CurrentOp.TurnOff;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Stop this operation</summary>
|
||||
/// <summary>Start the operation</summary>
|
||||
public void Start()
|
||||
{
|
||||
switch (currentOp)
|
||||
{
|
||||
case CurrentOp.TurnOn:
|
||||
TurnOn(powerForOp);
|
||||
return;
|
||||
case CurrentOp.TurnOff:
|
||||
default:
|
||||
TurnOff();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Run the operation</summary>
|
||||
public Event Run()
|
||||
{
|
||||
return Event.TurnPumpOnOffDone;
|
||||
}
|
||||
|
||||
/// <summary>Stop the operation</summary>
|
||||
public void Stop()
|
||||
{
|
||||
currentOp = CurrentOp.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user