﻿<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
  <POU Name="PMSvessel" Id="{099db2a6-fce6-4dd6-a9ec-d0a2fd41adfc}" SpecialFunc="None">
    <Declaration><![CDATA[(*
DESCRIPTION
===========

This is an interface class between the vessel PMS and P6 PMS to 
be able to adapt to the various vessel systems.

Methods And Properties
======================

*)
FUNCTION_BLOCK PMSvessel IMPLEMENTS p6power.IPMSVessel
VAR_INPUT
	Config					: REFERENCE TO configPMSVessel;
	IO_In					: REFERENCE TO PMSvesselIO_In;
	IO_Out					: REFERENCE TO PMSvesselIO_Out;
	//PowerAvailableSensor	: p6com.IAnalogSensor;
	//PowerRegenLimitSensor	: p6com.IAnalogSensor;
	// PowerUsageOut			: p6com.IAnalogOut;
	PMS						: p6power.IPMS;
END_VAR
VAR
	PowerAvailableSensor	: LREAL;  //p6com.IAnalogSensor;
	PowerRegenLimitSensor	: LREAL; //p6com.IAnalogSensor;
	// 
	// Properties
	//
	_inhibit					: BOOL;
	_habourMode					: BOOL;
	_powerLimit					: LREAL;
	_powerLimitCritical			: LREAL;
	_powerRegenLimit			: LREAL;
	_powerRegenCritical			: LREAL;
	
	_powerLimitReached 			: BOOL;
	_powerLimitCriticalReached 	: BOOL;
	_powerRegenLimitReached		: BOOL;
	_powerRegenCriticalReached	: BOOL;
	
	_powerUsage					: LREAL;
	_powerRegenUsage			: LREAL;
	_powerAvailable				: LREAL;
	_powerRegenAvailable		: LREAL;
	
	_stopPowerUsage				: BOOL;
	_systemActive				: BOOL;
END_VAR

VAR CONSTANT
	POWER_SCALING_FACTOR		: LREAL := 225;	// as agreed with ABB
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[
//PowerAvailableSensor() ; //:= IO_In.PowerAvailableSensor.input;
//PowerRegenLimitSensor(); // := Io_in.PowerRegenLimit.input;

{warning disable C0139} // Code with no effect
io_IN.PowerAvailableSensor.input ;
IO_In.PowerRegenLimit.input;

//PowerAvailableSensor.Value;


THIS^.update();
]]></ST>
    </Implementation>
    <Property Name="PowerRegenCritical" Id="{0416bb31-a5e4-0a4e-2701-9f940971758f}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerRegenCritical : LREAL]]></Declaration>
      <Get Name="Get" Id="{093aeee7-8bb3-0eaf-102f-0068089a7cf7}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenCritical := _powerRegenAvailable;
]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerRegenLimit" Id="{1e3c5861-2318-0a37-2452-a90b6083e99a}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerRegenLimit : LREAL]]></Declaration>
      <Get Name="Get" Id="{8a3b1041-c18a-0b1a-1bc0-d406eefbace6}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenLimit := _powerRegenAvailable;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Method Name="_calcBaseValues" Id="{24b63363-012f-0aed-00ae-7662f6376f48}">
      <Declaration><![CDATA[METHOD PRIVATE _calcBaseValues : BOOL
]]></Declaration>
      <Implementation>
        <ST><![CDATA[

PowerAvailableSensor :=  scale(value :=  IO_In.PowerAvailableSensor.input,InputMin :=0, InputMax := 32767 ,OutputMin := 0,OutputMax := POWER_SCALING_FACTOR, LimitOutput := FALSE);
PowerRegenLimitSensor :=  scale(value :=  IO_In.PowerRegenLimit.input,InputMin :=0, InputMax := 32767 ,OutputMin := 0,OutputMax := POWER_SCALING_FACTOR, LimitOutput := FALSE);



_powerLimit						:=  SEL(_inhibit,  PowerAvailableSensor, config.powerLimit);
_powerLimitCritical				:= _powerLimit;
_powerRegenLimit				:= SEL(_inhibit, PowerRegenLimitSensor, config.powerRegenLimit);
_powerRegenCritical				:= _powerRegenLimit;

_powerRegenUsage 				:= PMS.PowerRegenUsage;
_powerAvailable					:= _powerLimit;
_powerRegenAvailable  			:= _powerRegenLimit;

_systemActive 					:= _powerUsage > 5.0;

 IO_Out.SystemActive := _systemActive;

//  PowerUsageOut.Value := _powerUsage - _powerRegenUsage;
IO_Out.PowerUsed.output := LREAL_TO_INT( scale(value :=   _powerUsage - _powerRegenUsage ,InputMin :=-POWER_SCALING_FACTOR, InputMax := POWER_SCALING_FACTOR  ,OutputMin := 0,OutputMax := 32767, LimitOutput := TRUE));
//IO_Out.PowerUsed.output  := _powerUsage - _powerRegenUsage;
]]></ST>
      </Implementation>
    </Method>
    <Property Name="PowerLimitReached" Id="{39d3782e-1dd3-0a0e-31ce-d50f7f1e0ee9}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerLimitReached : BOOL]]></Declaration>
      <Get Name="Get" Id="{d130c6be-db43-0f63-3e95-957dc0ea29d2}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerLimitReached := _powerLimitReached;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerRegenCriticalReached" Id="{39ec819d-34e4-0885-333c-175393cb8b4f}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerRegenCriticalReached : BOOL]]></Declaration>
      <Get Name="Get" Id="{579629fe-7ada-099e-2d13-2afc7a4f084d}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenCriticalReached :=  _powerLimitCriticalReached;
]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="Inhibit" Id="{3e22f875-26d0-4784-bc2c-90bcc4c68518}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY Inhibit : BOOL]]></Declaration>
      <Set Name="Set" Id="{5ef24903-640f-44e3-867f-b4960d143f49}">
        <Declaration><![CDATA[]]></Declaration>
        <Implementation>
          <ST><![CDATA[_inhibit := Inhibit;
]]></ST>
        </Implementation>
      </Set>
      <Get Name="Get" Id="{b1b52b12-6b9d-0d59-3d11-4578cebda949}">
        <Declaration><![CDATA[]]></Declaration>
        <Implementation>
          <ST><![CDATA[Inhibit := _inhibit ;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerRegenLimitReached" Id="{4b3e4e43-96f0-04ea-0e44-235771456734}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerRegenLimitReached : BOOL]]></Declaration>
      <Get Name="Get" Id="{1131e105-80c5-0599-2b94-f5eecc1f18db}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenLimitReached :=  _powerRegenLimitReached;
]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Method Name="update" Id="{52cff8ec-da2a-0f14-13b8-4e6256c66ac9}">
      <Declaration><![CDATA[METHOD update : BOOL
]]></Declaration>
      <Implementation>
        <ST><![CDATA[
IF (PMS <> 0) AND __ISVALIDREF(config) AND __ISVALIDREF(IO_IN) AND __ISVALIDREF(IO_OUT) THEN

	// no clue.....		
	//PowerAvailableSensor := IO_In.PowerAvailableSensor.input;
	//PowerRegenLimitSensor := Io_in.PowerRegenLimit.input;
	THIS^._calcBaseValues();
	THIS^._calcLimitSignaling();

END_IF

]]></ST>
      </Implementation>
    </Method>
    <Property Name="PowerAvailable" Id="{5893323d-bea0-0585-21c4-29a239c8904a}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerAvailable : LREAL]]></Declaration>
      <Get Name="Get" Id="{3cea5e68-294e-0b41-2699-f70888d00ff2}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerAvailable := _powerAvailable;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="SystemActive" Id="{65737fcc-d645-461d-af8b-4d3900478c50}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY SystemActive : BOOL]]></Declaration>
      <Get Name="Get" Id="{32aaad53-cf7f-4d48-b5bc-3573230265a0}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[SystemActive := _systemActive;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerLimit" Id="{6dec4910-344c-0ba3-0c01-a7bf0383d91f}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerLimit : LREAL]]></Declaration>
      <Get Name="Get" Id="{3d83cf91-c20a-0cb8-2098-5ab2c9088e0c}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerLimit := _powerLimit;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerLimitCriticalReached" Id="{aa16695b-1716-0087-03f6-416a16c517de}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerLimitCriticalReached : BOOL]]></Declaration>
      <Get Name="Get" Id="{72b374ca-3314-026b-157f-6600afbf042b}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerLimitCriticalReached := _powerLimitCriticalReached;
]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerRegenUsage" Id="{c6f72e85-0e5a-0567-0691-6584ad6fe7d3}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerRegenUsage : LREAL]]></Declaration>
      <Set Name="Set" Id="{1eff4645-4898-470c-b452-e357bd054290}">
        <Declaration><![CDATA[
VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[_powerRegenUsage := PowerRegenUsage;]]></ST>
        </Implementation>
      </Set>
      <Get Name="Get" Id="{26772c49-30a6-05ad-0189-d4e8a53a82ea}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenUsage := _powerRegenUsage ;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="StopPowerUsage" Id="{cc8eedf9-ef3f-0d26-0b76-f5c74b2124a1}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY StopPowerUsage : BOOL]]></Declaration>
      <Get Name="Get" Id="{ba242211-bdac-000b-3d59-3001b4a0e11b}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[StopPowerUsage := _stopPowerUsage; ]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerRegenAvailable" Id="{d8c9f07e-c720-0ba5-3ee5-b85a1debf97a}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerRegenAvailable : LREAL]]></Declaration>
      <Get Name="Get" Id="{fc21b7e9-6296-0306-3b80-a9a3d7eb6c2d}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerRegenAvailable := _powerRegenAvailable;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="HabourMode" Id="{dc5dfc46-ceb9-0748-3e8b-905f61234d04}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY HabourMode : BOOL]]></Declaration>
      <Get Name="Get" Id="{6c8d74dc-390f-0cb8-15b7-7741c2cf6d33}">
        <Declaration><![CDATA[]]></Declaration>
        <Implementation>
          <ST><![CDATA[HabourMode := _habourMode;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerLimitCritical" Id="{eb791b17-f551-0002-3e2b-4629bf2f788c}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PowerLimitCritical : LREAL]]></Declaration>
      <Get Name="Get" Id="{20a15f43-ebf4-026b-19eb-8d0891996263}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerLimitCritical := _powerLimit;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="PowerUsage" Id="{f1058eb9-e166-07e1-1d6b-aa2cae3487e4}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
PROPERTY PowerUsage : LREAL]]></Declaration>
      <Set Name="Set" Id="{8b10c9f7-a6d6-48ab-a6e3-47c2f5c60714}">
        <Declaration><![CDATA[
VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[_powerUsage := PowerUsage;]]></ST>
        </Implementation>
      </Set>
      <Get Name="Get" Id="{e8a619c0-97dc-0db4-026b-a6f85b5032b8}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[PowerUsage := _powerUsage;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Method Name="_calcLimitSignaling" Id="{f3e72b07-63af-094c-370e-2793b289e870}">
      <Declaration><![CDATA[METHOD PRIVATE _calcLimitSignaling : BOOL
VAR_INST
	powerLimitTimer						: TON;
	powerLimitCriticalTimer				: TON;
	powerRegenLimitTimer				: TON;
	powerRegenCriticalTimer				: TON;	
END_VAR]]></Declaration>
      <Implementation>
        <ST><![CDATA[powerLimitTimer(	IN:= (_powerLimit - _powerUsage) < 0.0, 
					PT:=config.filterTime
);

powerLimitCriticalTimer(IN:= (_powerLimitCritical - _powerUsage) < 0.0, 
							PT:=config.filterTime
);

powerRegenLimitTimer(IN:= (_powerRegenLimit - _powerRegenUsage) < 0.0, 
						PT:=config.filterTime
);

powerRegenCriticalTimer(	IN:= (_powerRegenCritical - _powerRegenUsage) < 0.0, 
							PT:=config.filterTime
);

_powerLimitReached 				:= powerLimitTimer.Q;
_powerLimitCriticalReached 		:= powerLimitCriticalTimer.Q;
_powerRegenLimitReached			:= powerRegenLimitTimer.Q;
_powerRegenCriticalReached		:= powerRegenCriticalTimer.Q;

_stopPowerUsage					:= _powerLimitCriticalReached OR _powerRegenCriticalReached;

_habourMode := IO_In.PowerHabourMode;



]]></ST>
      </Implementation>
    </Method>
  </POU>
</TcPlcObject>