S7-scl problem

Mstr

Member
Join Date
Jun 2010
Location
NY
Posts
13
I developed DMC (Dynamic Matrix Control) controller in S7-SCL. The problem is: my controller seems to ignore the setpoint, and it is reading always the same output. My setpoint is, for example, 35% of flowmeter, and it's always reading 83% (for any setpoint i give him). I dont't have any idea why.
This controller is similar, and more advanced, to PID, and PID is called from OB35.
Is it possible that it's not working properly because i'm calling FB from OB1?
I tried calling it from, OB32 and OB35, but i dont have the real system to try it.
Can you give me any idea why this is happening?

Thank you in advance.
 
I developed DMC (Dynamic Matrix Control) controller in S7-SCL. The problem is: my controller seems to ignore the setpoint, and it is reading always the same output. My setpoint is, for example, 35% of flowmeter, and it's always reading 83% (for any setpoint i give him). I dont't have any idea why.
This controller is similar, and more advanced, to PID, and PID is called from OB35.
Is it possible that it's not working properly because i'm calling FB from OB1?
I tried calling it from, OB32 and OB35, but i dont have the real system to try it.
Can you give me any idea why this is happening?

Thank you in advance.


Hello Mstr;
We would need to see your code to really understand what is going on.
This reminds me of the standard FB41 (Continuous PID control block) that all Step 7 newbies face: either it does not initiate properly, or it gets stuck in Manual mode (and therefore does not apply the math to the sensed error signal): in both cases the output is frozen.
Hope this helps,
Daniel Chartier
 
I developed DMC (Dynamic Matrix Control) controller in S7-SCL. The problem is: my controller seems to ignore the setpoint, and it is reading always the same output. My setpoint is, for example, 35% of flowmeter, and it's always reading 83% (for any setpoint i give him). I dont't have any idea why.
This controller is similar, and more advanced, to PID, and PID is called from OB35.
Is it possible that it's not working properly because i'm calling FB from OB1?
I tried calling it from, OB32 and OB35, but i dont have the real system to try it.
Can you give me any idea why this is happening?

Have I got this right? You developed this advanced program and you don't know why it doesn't work.(y)

Did you design it to wotk being called from a timed interrupt or not ?
 
Code:
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_reponse:ARRAY [1..30] OF REAL;
      Input, Control_variableReal,Control_variable,Sum,Flow,Wp,L:REAL;
      i,j,n,y,Control_variableInt: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 
    Pump:=1;
ELSE
    Pump:=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

Thank you Daniel, that is exactly what happend but, i still don't know how can fix this. This is my code, hope you can see where i'm mistaking.

L D[AR2,P#0.0]: Yes, you got it right :). I wrote the code at home and i understant theory, and then i went to laboratory on my University to run it on real system (on PLCSim i couldn't see that it is stuck) and my mentor and i didn't have any idea why. It was called from OB1, in this code i changed it.
I hope someone here with expirience can see the problem.
 
Please post the code as you compiled it - the code you have posted contains typos and missing declarations....
 
Code:
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

Sorry, i changed variable names to be understandable to not just me. This is the code.
 
Vector "Ref: ARRAY [1..10] OF REAL:=10(20);" is the setpoint, and
"Om:=L*Om[i-1]+(1 -L)*Ref;" is for reference trajectory. You don't need to set it manually. In this code is 20% because i set various values hoping it would work. It's not important to me what is the setpoint value, i just need it to work by it, but i always get 80-83% no matther what i set to vector Ref values.
 
Thank you. My graph in winCC looks something like this, but i need PIW256 (Flow) to be close to setpoint, and follow my reference trajecory, and i am not getting this.
 
Last edited:
No, not deliberaltely :(. How can i know (calculate) sample time? And, when i calculate it, does it mean that i need to use timed interrupt OB with the time which is close to my sample time?
 
It was 1 second. Does this mean to call FB1 from OB32?
Thank you wery much for helping.
 
It would make sense to do so but nothing I've read about DMC can confirm this. Which CPU are you using ? You can set the interrupt time of OB32 in the h/w config.
 

Similar Topics

Guys Cant remember syntax for an if statment like this in studio 5000 Can someone help If ( some_tag == false) then do something end_if I can...
Replies
4
Views
1,383
Hello All, I am new to programming with Tia Portal and I am having difficulty with something that we have found "easy" in other programming...
Replies
3
Views
2,334
FUNCTION_BLOCK FB283 VAR_INPUT DB_number: BLOCK_DB; add : int; END_VAR VAR_OUTPUT out:INT; END_VAR VAR end_var BEGIN...
Replies
2
Views
3,166
Dear all, I don't know why the above section (written in SCL) switches the CPU to stop mode. When I delete the two lines highlighted with...
Replies
14
Views
2,860
The following code does some weird things with my outputs... is it just like in Lad, or stl not permitted to use outputs more the once ?? And if...
Replies
14
Views
4,952
Back
Top Bottom