TYPE Matrix
STRUCT
RowNumber: INT: = 0;
ColumnNumber: INT: = 0;
Element: ARRAY [1..10,1..10] OF REAL;
END_STRUCT
END_TYPE
FUNCTION_BLOCK FB1
VAR
A, B, C : Matrix;
Start: BOOL;
K, Fr :ARRAY [1..10]OF REAL;
Om :ARRAY [1..10]OF REAL:=10(0);
Past_measured_flow :ARRAY [1..20]OF REAL:=10(0);
Past_measured_flow_temp:ARRAY [1..20]OF REAL:=10(0);
S_response:ARRAY [1..30] OF REAL;
Input,Control_variableReal,Control_variable,MeasuredValue,Sum,Flow,Wp,L:REAL;
i,j,n,y,Control_variableInt,MeasuredValueInt:INT;
Ref: ARRAY [1..10] OF REAL:=10(20);
END_VAR
A.RowNumber:= 5;
A.ColumnNumber:= 5;
A.Element[1,1]:= 0.0012;
A.Element[1,2]:=-0.0018;
A.Element[1,3]:=0.0007;
A.Element[1,4]:=-0.0001;
A.Element[1,5]:=-0;
A.Element[2,1]:=-0.0018;
A.Element[2,2]:= 0.0040;
A.Element[2,3]:=-0.0030;
A.Element[2,3]:=0.0009;
A.Element[2,4]:=-0.0001;
A.Element[3,1]:=0.0007;
A.Element[3,2]:=-0.0030;
A.Element[3,3]:= 0.0044;
A.Element[3,4]:=-0.0030;
A.Element[3,5]:=0.0008;
A.Element[4,1]:=-0.0001;
A.Element[4,2]:=0.0009;
A.Element[4,3]:=-0.0030;
A.Element[4,4]:= 0.00041;
A.Element[4,5]:=-0.0020;
A.Element[5,1]:=-0.0000;
A.Element[5,2]:=-0.0001;
A.Element[5,3]:=0.0008;
A.Element[5,4]:=-0.0020;
A.Element[5,5]:= 0.0016;
B.RowNumber:=5;
B.ColumnNumber:=10;
B.Element[1,1]:= 0;
B.Element[1,2]:= 4.6240;
B.Element[1,3]:= 18.4970;
B.Element[1,4]:= 47.3990 ;
B.Element[1,5]:= 47.9770;
B.Element[1,6]:= 47.3990;
B.Element[1,7]:= 47.3990;
B.Element[1,8]:= 47.3990;
B.Element[1,9]:= 46.8210;
B.Element[1,10]:=47.3990;
B.Element[2,1]:= 0;
B.Element[2,2]:= 0;
B.Element[2,3]:= 4.6240;
B.Element[2,4]:= 18.4970;
B.Element[2,5]:= 47.3990;
B.Element[2,6]:= 47.9770;
B.Element[2,7]:= 47.3990;
B.Element[2,8]:= 47.3990;
B.Element[2,9]:= 47.3990;
B.Element[2,10]:=46.8210;
B.Element[3,1]:= 0;
B.Element[3,2]:= 0;
B.Element[3,3]:= 0;
B.Element[3,4]:=4.6240;
B.Element[3,5]:= 18.4970;
B.Element[3,6]:= 47.3990;
B.Element[3,7]:= 47.9770;
B.Element[3,8]:= 47.3990;
B.Element[3,9]:= 47.3990;
B.Element[3,10]:=47.3990;
B.Element[4,1]:= 0;
B.Element[4,2]:= 0;
B.Element[4,3]:= 0;
B.Element[4,4]:= 0;
B.Element[4,5]:= 4.6240;
B.Element[4,6]:= 18.4970;
B.Element[4,7]:= 47.3990;
B.Element[4,8]:= 47.9770;
B.Element[4,9]:= 47.3990;
B.Element[4,10]:=47.3990;
B.Element[5,1]:= 0;
B.Element[5,2]:= 0;
B.Element[5,3]:= 0;
B.Element[5,4]:= 0;
B.Element[5,5]:= 0;
B.Element[5,6]:=4.6240;
B.Element[5,7]:= 18.4970;
B.Element[5,8]:= 47.3990;
B.Element[5,9]:= 47.9770;
B.Element[5,10]:=47.3990;
C.RowNumber:=5;
C.ColumnNumber:=10;
S_response[1]:=0;
S_response[2]:=4.6240;
S_response[3]:=18.4970;
S_response[4]:=47.3990;
S_response[5]:=47.9770;
S_response[6]:=47.3990;
S_response[7]:=47.3990;
S_response[8]:=47.3990;
S_response[9]:=46.8210;
S_response[10]:=47.3990;
S_response[11]:=46.8210;
S_response[12]:=46.8210;
S_response[13]:=46.8210;
S_response[14]:=46.8210;
S_response[15]:=47.3990;
S_response[16]:=46.8210;
S_response[17]:=46.8210;
S_response[18]:=47.3990;
S_response[19]:=47.3990;
S_response[20]:=47.3990;
S_response[21]:=47.9770;
S_response[22]:=46.8210;
S_response[23]:=47.3990;
S_response[24]:=46.2430;
S_response[25]:=46.8210;
S_response[26]:=46.8210;
S_response[27]:=46.8210;
S_response[28]:=46.8210;
S_response[29]:=46.2430;
S_response[30]:=46.2430;
/// ///////////////////////////////////////////////////////////////////
IF Start=1 THEN
Q124.0:=1;
ELSE
Q124.0:=0;
END_IF;
//////////////////////////////////////////////////////////////////////
//calculating system model matrices, A & B im getting from matlab
//with eigenvalue decomposition of dynamic matrix
////////////////////////////////////////////////////////////////////
FOR i:= 1 TO 5 BY 1 DO
FOR j:=1 TO 10 BY 1 DO
Sum:=0;
FOR y:=1 TO 5 BY 1 DO
Sum:=Sum + A.Element[i,y]*B.Element[y,j];
C.Element[i,j]:=Sum;
END_FOR;
END_FOR;
END_FOR;
FOR n:=1 TO 10 BY 1 DO
K[n]:=C.Element[1,n];
END_FOR;
//;
///////////////////////////////////////////////////////////
//measured value for the next step
////////////////////////////////////////////////////////////
MeasuredValueInt:=WORD_TO_INT(PIW256);
MeasuredValue:=INT_TO_REAL(MeasuredValueInt);
Flow:=((MeasuredValue-INT_TO_REAL(5504))*INT_TO_REAL(100))/INT_TO_REAL(22144);
/////////////////////////////////////////////////////////////
//calculations for the free response of the system
//////////////////////////////////////////////////////////////
Wp:=Flow;
L:=0.5; //control gain
Om[1]:=L*Wp+(1 -L)*Ref[1];
FOR i:=2 TO 10 BY 1 DO
Om[i]:=L*Om[i-1]+(1 -L)*Ref[i]; //reference trajectory
END_FOR;
FOR i:=1 TO 10 BY 1 DO
Sum:=0;
FOR j:=1 TO 20 BY 1 DO
Sum:=Sum+(S_response[i+j]-S_response[j])*Past_measured_flow_temp[j];
END_FOR;
Fr[i]:=Sum+Flow; //free response
END_FOR;
//////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
Sum:=0;
FOR i:=1 TO 10 BY 1 DO
Sum:=Sum+(K[i]*(Om[i]-Fr[i])); //input for the next step
Input:=Sum;
END_FOR;
//////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
FOR i:=1 TO 19 BY 1 DO
Past_measured_flow_temp[i+1]:=Past_measured_flow[i];
END_FOR;
Past_measured_flow_temp[1]:=Input;
Control_variable:=Control_variable+Past_measured_flow[1]; //next input
Past_measured_flow:=Past_measured_flow_temp; //past inputs vector for the next step
//////////////////////////////////////////////////////////////
Control_variableReal:=((Control_variable*INT_TO_REAL(22144))/INT_TO_REAL(100))+INT_TO_REAL(5504);
Control_variableInt:=REAL_TO_INT(Control_variableReal);
PQW256:=INT_TO_WORD(Control_variableInt);
//////////////////////////////////////////////////////////////
END_FUNCTION_BLOCK
//---------------------------------------------------------------------------
DATA_BLOCK DB1 FB1
BEGIN
END_DATA_BLOCK
//---------------------------------------------------------------------------
ORGANIZATION_BLOCK OB32 //this was OB1 in my attempt to run it
VAR_TEMP
// Reserved
Info: ARRAY[0..19] OF BYTE;
// Temporary Variables
END_VAR
// Instructions
FB1.DB1();
;
END_ORGANIZATION_BLOCK