Files
Uniper_PLC/PLC/POUs/Sunspec/FB_PowerSupplySunspec.TcPOU
2024-01-04 15:15:26 +01:00

628 lines
17 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
<POU Name="FB_PowerSupplySunspec" Id="{a826dd09-442c-45c5-8ae3-9b71f293003c}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_PowerSupplySunspec
VAR_INPUT
sInverterIPAddr : STRING;
xEnable : BOOL;
rPower : REAL;
xReset : BOOL;
rMaxBattPower : REAL := 24_000; // 24kW
END_VAR
VAR_OUTPUT
xCloseDCRelais AT %Q*: BOOL;
rActDCCurrent : REAL;
rActDCVoltage : REAL;
xError : BOOL;
xActive : BOOL;
END_VAR
VAR
// Current state
_iState : INT := 0;
// FB for reading Modbus holding registers
_fbReadRegister : FB_MBReadRegs;
// FB for writing Modbus holding registers
_fbWriteRegister : FB_MBWriteRegs;
// Timer for checking if the inverter started in a reasonable amount of time
_tonInverterStartup : TON;
// converter max power scaling factor
_iWMaxSF : INT;
// Unscaled converter max power
_iWMax : UINT;
// Scaled converter max power
_rWMax : REAL;
// Unscaled limit for converter power
_iWMaxLimPct : INT;
// Scaling factor for power limiting
_iWMaxLimPctSF : INT;
// Unscaled maximum power from type label
_iWRTGSF : INT;
// Scaling for maximum power from type label
_rWRTGScaling : REAL;
// Current state of the inverters internal statemachine
_uiInverterState : UINT;
// Last written power to the inverter
_rOldPower : REAL;
// Value to enable or dissable the Power limiting feature
_uiMaxLimEn : UINT;
// Value for commanding the target state of the inverter
_uiPCSSetOperation : UINT;
// Holds the state number in which an error occured
_iErrorInState : INT;
// Time for polling for current dc values and check for inverter error
_timPollingDelay : TIME := T#250MS;
// Timer for polling of current values
_tonPollingTimer : TON;
// Current DC values (DCA, DCA_SF, DCV, DCV_SF) in word array for efficient modbus reading
_awCurrentDCValues : ARRAY[0..3] OF WORD;
// Inverter error bits
_dwErrorBits : DWORD;
// Inverter reset errors command
_uiResetInverter : UINT := 1;
END_VAR
VAR CONSTANT
// Inverter statemachine status register
// Size 1, enum16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
STATUS_REGISTER : WORD := 40108;
// Throttled power register
// Size 1, int16 (Range = -32767 .. 32767, Not implemented 0x8000)
W_MAX_LIM_PCT_REGISTER : WORD := 40187;
// Throttled power register scaling factor
// Size 1, sunssf (int16) (Range = -10 .. 10, Not implemented 0x8000)
W_MAX_LIM_PCT_SF_REGISTER : WORD := 40205;
// Control register to enable and dissable if the power throttleing should be active
// Size1, enum16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
W_MAX_LIM_EN_REGISTER : WORD := 40191;
// Register to reset latched alarms in the inverter
// Size 1, uint16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
PCS_ALARM_RESET_REGISTER : WORD := 40230;
// Control register to set the target state of the inverters state machine
// Size 1, enum16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
PCS_SET_OPERATION_REGISTER : WORD := 40231;
// Maximum inverter output power
// Size 1, uint16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
W_MAX_REGISTER : WORD := 40152;
// Maximum inverter output power scaling factor
// Size 1, sunssf (int16) (Range = -10 .. 10, Not implemented 0x8000)
W_MAX_SF_REGISTER : WORD := 40172;
// Maximum inverter output power from type label
// Size 1, uint16 (Range = 0 .. 65534, Not implemented = 0xFFFF)
W_RTG_REGISTER : WORD := 40125;
// Maximum inverter output power from type label scaling factor
// Size 1, sunssf (int16) (Range = -10 .. 10, Not implemented 0x8000)
W_RTG_SF_REGISTER : WORD := 40126;
// Start of register with the current dc values
// Size 4
DC_VALUES_START_REGISTER : WORD := 40097;
// Error bits register
// Size 2
EVT_1_REGISTER : WORD := 40110;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// Clamp rPower to maximum allowed power
IF (rPower > rMaxBattPower) THEN
rPower := rMaxBattPower;
END_IF
IF (rPower < -rMaxBattPower) THEN
rPower := -rMaxBattPower;
END_IF
// State machine
CASE _iState OF
0: // Off
// If enable and INTLK Ok
IF xEnable THEN
_iState := 10;
xCloseDCRelais := TRUE;
END_IF
10: // Wait for inverter to be online and in state STANDBY(8)
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= STATUS_REGISTER,
cbLength:= SIZEOF(_uiInverterState),
pDestAddr:= ADR(_uiInverterState),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error and the state is STANDBY(8) then continue
IF NOT _fbReadRegister.bError AND _uiInverterState = 8 THEN
_iState := 20;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
20: // Read inverter max power scaling
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= W_MAX_SF_REGISTER,
cbLength:= SIZEOF(_iWMaxSF),
pDestAddr:= ADR(_iWMaxSF),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error then continue
IF NOT _fbReadRegister.bError THEN
_iState := 25;
// Check for valid value
IF (_iWMaxSF < -10) OR (_iWMaxSF > 10) OR (_iWMaxSF = 16#8000) THEN
// Goto error state
_iState := 1000;
END_IF
ELSE
xError := TRUE;
// Goto error state
_iState := 1000;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
25: // Read inverter Max power limit scaling
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= W_MAX_LIM_PCT_SF_REGISTER,
cbLength:= SIZEOF(_iWMaxLimPctSF),
pDestAddr:= ADR(_iWMaxLimPctSF),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error then continue
IF NOT _fbReadRegister.bError THEN
_iState := 30;
// Check for valid value
IF (_iWMaxLimPctSF < -10) OR (_iWMaxLimPctSF > 10) OR (_iWMaxLimPctSF = 16#8000) THEN
// Goto error state
_iState := 1000;
END_IF
ELSE
xError := TRUE;
// Goto error state
_iState := 1000;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
30: // Read inverter max power
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= W_MAX_REGISTER,
cbLength:= SIZEOF(_iWMax),
pDestAddr:= ADR(_iWMax),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error then continue
IF NOT _fbReadRegister.bError THEN
_iState := 40;
// Reading a register with scaling factor = value * 10^SF
_rWMax := _iWMax * EXPT(10,_iWMaxSF);
// Calculate power to write to register
_iWMaxLimPct := LREAL_TO_INT(rPower * EXPT(10,_iWMaxLimPctSF) / _rWMax);
ELSE
xError := TRUE;
// Goto error state
_iState := 1000;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
40: // Set power limit
_iErrorInState := _iState;
_fbWriteRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= W_MAX_LIM_PCT_REGISTER,
cbLength:= SIZEOF(_iWMaxLimPct),
pSrcAddr:= ADR(_iWMaxLimPct),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> );
// If writing modbus register is done
IF NOT _fbWriteRegister.bBusy THEN
// And there is no error, then continue
IF NOT _fbWriteRegister.bError THEN
_iState := 50;
_rOldPower := rPower;
_uiMaxLimEn := 1;
ELSE
xError := TRUE;
// Goto error state
_iState := 1000;
END_IF
_fbWriteRegister(bExecute := FALSE);
END_IF
50: // Enable Power limiting (THROTTLED)
_iErrorInState := _iState;
_fbWriteRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= W_MAX_LIM_EN_REGISTER,
cbLength:= SIZEOF(_uiMaxLimEn),
pSrcAddr:= ADR(_uiMaxLimEn),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> );
// If writing modbus register is done
IF NOT _fbWriteRegister.bBusy THEN
// And there is no error, then continue
IF NOT _fbWriteRegister.bError THEN
_iState := 60;
_uiPCSSetOperation := 1;
ELSE
xError := TRUE;
// Goto error state
_iState := 1000;
END_IF
_fbWriteRegister(bExecute := FALSE);
END_IF
60: // Switch to THROTTLED mode
_iErrorInState := _iState;
_fbWriteRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= PCS_SET_OPERATION_REGISTER,
cbLength:= SIZEOF(_uiPCSSetOperation),
pSrcAddr:= ADR(_uiPCSSetOperation),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> );
// If writing modbus register is done
IF NOT _fbWriteRegister.bBusy THEN
// And there is no error, then continue
IF NOT _fbWriteRegister.bError THEN
_iState := 65;
ELSE
// Goto error state
_iState := 1000;
END_IF
_fbWriteRegister(bExecute := FALSE);
END_IF
65: // Wait for error polling timer
_tonPollingTimer(IN := TRUE, PT := _timPollingDelay);
IF _tonPollingTimer.Q THEN
_tonPollingTimer(IN := FALSE);
_iState := 70;
ELSIF ABS(rPower - _rOldPower) > 0.1 THEN
_tonPollingTimer(IN := FALSE);
// If power has ben changed, goto set power limit mode
_iState := 40;
END_IF
// check if inverter should shut down
IF NOT xEnable THEN
_uiPCSSetOperation := 3;
// Goto shutdown sequence
_iState := 200;
END_IF
70: // Enabled, check for error
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= STATUS_REGISTER,
cbLength:= SIZEOF(_uiInverterState),
pDestAddr:= ADR(_uiInverterState),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error and the converter has no error continue
IF NOT _fbReadRegister.bError AND (_uiInverterState <> 7) THEN
_iState := 80;
IF (_uiInverterState = 4) OR (_uiInverterState = 5) THEN
xActive := TRUE;
ELSE
xActive := FALSE;
END_IF
ELSE
xError := TRUE;
// Read error register
_iState := 990;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
80: // Read current DC values
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 4,
nMBAddr:= DC_VALUES_START_REGISTER,
cbLength:= SIZEOF(_awCurrentDCValues),
pDestAddr:= ADR(_awCurrentDCValues),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error and the converter has no error continue
IF NOT _fbReadRegister.bError THEN
_iState := 65;
rActDCCurrent := WORD_TO_UINT(_awCurrentDCValues[0]) * EXPT(10,WORD_TO_INT(_awCurrentDCValues[1]));
rActDCVoltage := WORD_TO_UINT(_awCurrentDCValues[2]) * EXPT(10,WORD_TO_INT(_awCurrentDCValues[3]));
ELSE
// Read error register
_iState := 1000;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
200: // Shutdown sequence
_iErrorInState := _iState;
_fbWriteRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= PCS_SET_OPERATION_REGISTER,
cbLength:= SIZEOF(_uiPCSSetOperation),
pSrcAddr:= ADR(_uiPCSSetOperation),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> );
// If writing modbus register is done
IF NOT _fbWriteRegister.bBusy THEN
// And there is no error, then continue
IF NOT _fbWriteRegister.bError THEN
_iState := 210;
rActDCCurrent := 0.0;
rActDCVoltage := 0.0;
ELSE
// Goto error state
_iState := 1000;
END_IF
_fbWriteRegister(bExecute := FALSE);
END_IF
210: // Wait for poll timer to
_tonPollingTimer(IN := TRUE, PT := _timPollingDelay);
IF _tonPollingTimer.Q THEN
_tonPollingTimer(IN := FALSE);
_iState := 220;
END_IF
220: // Poll and wait for standby state
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= STATUS_REGISTER,
cbLength:= SIZEOF(_uiInverterState),
pDestAddr:= ADR(_uiInverterState),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error and the converter has no error continue
IF NOT _fbReadRegister.bError THEN
_iState := 0;
xActive := FALSE;
xCloseDCRelais := FALSE;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
990: // Read error register
_iErrorInState := _iState;
_fbReadRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 2,
nMBAddr:= EVT_1_REGISTER,
cbLength:= SIZEOF(_dwErrorBits),
pDestAddr:= ADR(_dwErrorBits),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> ,
cbRead=> );
// Check if reading mudbus register is done
IF NOT _fbReadRegister.bBusy THEN
// If there was no error and the converter has no error continue
IF NOT _fbReadRegister.bError THEN
_iState := 1000;
END_IF
_fbReadRegister(bExecute := FALSE);
END_IF
1000: // Error state, wait for reset
IF xReset AND (NOT xEnable) THEN
_iState := 1010;
END_IF
1010: // Try to clear all latched events
_fbWriteRegister(
sIPAddr:= sInverterIPAddr,
nTCPPort:= 502,
nUnitID:= 16#FF, // 16#FF for Modbus TCP
nQuantity:= 1,
nMBAddr:= PCS_ALARM_RESET_REGISTER,
cbLength:= SIZEOF(_uiResetInverter),
pSrcAddr:= ADR(_uiResetInverter),
bExecute:= TRUE,
tTimeout:= T#5S,
bBusy=> ,
bError=> ,
nErrId=> );
IF NOT _fbWriteRegister.bBusy THEN
_iState := 0;
_fbWriteRegister(bExecute := FALSE);
END_IF
END_CASE]]></ST>
</Implementation>
<LineIds Name="FB_PowerSupplySunspec">
<LineId Id="774" Count="70" />
<LineId Id="1171" Count="0" />
<LineId Id="845" Count="33" />
<LineId Id="1172" Count="0" />
<LineId Id="879" Count="33" />
<LineId Id="1173" Count="0" />
<LineId Id="913" Count="29" />
<LineId Id="1174" Count="0" />
<LineId Id="943" Count="28" />
<LineId Id="1175" Count="0" />
<LineId Id="972" Count="46" />
<LineId Id="1127" Count="0" />
<LineId Id="1019" Count="25" />
<LineId Id="1097" Count="1" />
<LineId Id="1100" Count="1" />
<LineId Id="1099" Count="0" />
<LineId Id="1045" Count="0" />
<LineId Id="1176" Count="0" />
<LineId Id="1046" Count="6" />
<LineId Id="1063" Count="20" />
<LineId Id="1090" Count="0" />
<LineId Id="1092" Count="0" />
<LineId Id="1084" Count="4" />
<LineId Id="1062" Count="0" />
<LineId Id="1093" Count="1" />
<LineId Id="1102" Count="19" />
<LineId Id="1166" Count="1" />
<LineId Id="1122" Count="4" />
<LineId Id="1054" Count="0" />
<LineId Id="1295" Count="4" />
<LineId Id="1128" Count="0" />
<LineId Id="1300" Count="1" />
<LineId Id="1129" Count="0" />
<LineId Id="1132" Count="20" />
<LineId Id="1163" Count="0" />
<LineId Id="1294" Count="0" />
<LineId Id="1161" Count="1" />
<LineId Id="1130" Count="1" />
<LineId Id="1055" Count="0" />
<LineId Id="1177" Count="20" />
<LineId Id="1199" Count="1" />
<LineId Id="1169" Count="0" />
<LineId Id="1265" Count="2" />
<LineId Id="1269" Count="3" />
<LineId Id="1274" Count="11" />
<LineId Id="1273" Count="0" />
<LineId Id="1289" Count="1" />
<LineId Id="1293" Count="0" />
<LineId Id="1291" Count="1" />
<LineId Id="1056" Count="0" />
<LineId Id="12" Count="0" />
</LineIds>
</POU>
</TcPlcObject>