Filling an array in Beckhoff Twincat

matt_sd

Member
Join Date
Jan 2007
Location
Australia
Posts
96
Hello All,

I am trying to do something quite simple but not use to some of Twincat's instructions/syntax.

I am programming in Structured Text and want to fill an array with say raw vibration data and then calculate the RMS over 200 samples of that data.

I have created an array:-

******************************************

VAR
raw_vib_data:real; (* Raw vibration Data *)
vib_array: ARRAY[1..200] OF REAL; (* Array to hold 200 samples of raw vibration data *)
END_VAR


in the program file:-

vib_array[1]:=raw_vib_data; (* At this point in time copy the current raw data into the 1st field of the array *)

****************************************

Now say the program is called 10 ms, I want to shift all the array data up 1(i.e content of 3 go into 4,2 go into 3, 1 go into 2 etc, so on the next scan I can fill data cell 1 up again etc.

I have done this with ease in the past when using Rockwell control logix FFL/FFU FIFO Load/unload instructions but Twincat doesn't have this. I have noticed there is a rotate left/right that I thought may allow me to rotate the array data but this is a bit rotate and cant be used with an array of REALS.

I know how to do the rest of it I just cant get the load/unload of the array working.

Any help appreciated.

Thanks Matt
 
Last edited:
There was an almost similar thread about array FIFO in S7 SCL not long ago.

Anyway, you have to program the pushing of the registers yourself.

Code:
VAR 
  raw_vib_data : real ; 
  vib_array: ARRAY[1..200] OF REAL ; 
  index : INT ;
END_VAR
 
//this is the pushing part 
FOR index:= 200 TO 2 BY -1 DO
  vib_array[index] := vib_array[index-1] ;
END_FOR ;
//this is where the first array position is filled
vib_array[1]:=raw_vib_data ;

An alternative method is to not push all the registers, but instead move the point where you insert the new data. This is more efficient regarding execution of the code.
To calculate the RMS value of all the data it is irrelevant that the data does not start and end at 1 and 200 respectively.
Disadvantage is that if you look at the array data online, it is more confusing because the end and start is some place in the middle of the array:

Code:
VAR 
  raw_vib_data:real; 
  vib_array: ARRAY[1..200] OF REAL; 
  index : INT ;
END_VAR
 
IF index < 200 THEN 
  index := index + 1 ;
ELSE
  index := 1 ;
END_IF ;
vib_array[index]:=raw_vib_data ;

NB: I know S7 SCL pretty well. IEC ST should be similar, so I think the code is readable to you.
 
Good morning L D[AR2,P#0.0],

wow that was a quick response!

ok, so every sample I will have to something like this:

pointer_pos:int; (* Pointer position*)
pointer_1:int; (*pointer +1*)

****** Program executed every sample****

vib_array[0]:=raw_vib_data;


pointer_pos:=199; (*Initalise*)
pointer_1:=200; (*Initalise*)

while pointer >= 1 do
vib_array[pointer_1] :=vib_array[pointer_pos];
pointer_pos:=pointer_pos - 1;
pointer_1:=pointer_pos + 1;
end_while



Just seems quite long winded - this is the only/best way and there is not a simpler option?
Suppose I'm use to Allan Bradley where many of the instructions are created and do this behind the scenes.

Thanks Matt
 
Hello JesperMP,

just saw your reply after posting mine. Thanks for that. Yes moving the pointer sounds the way to go to make the code more efficient and will be adequate for an RMS calculation where I dont need history

Thanks - Beckhoff Twincat is certainly improving my programming skills - you don't need many skills with Rockwell products - its all done for you ;).

I'm starting to build a library by creating many function blocks as well for various things like this.

Regards

Matt
 

Similar Topics

I created a 13x2 array called Rack_Parts in RS5000. What is the correct syntax so I can MOV inually put in: Rack_Parts(Rack_Number...
Replies
6
Views
2,438
Hello guys! I'm new to STEP7, so could you, please, help me with the following problem I face: I need to write values from a current sensor into...
Replies
8
Views
2,884
Hey guys, We are facing an issue, Krones hotfill line, around 10-15 fillers out of 110 are overfilling the bottle. Please help us what can...
Replies
13
Views
523
Hi, I am just exploring the options to replace an outdated filling valve controller for a can filler. It has 78 filling valves and runs at max...
Replies
15
Views
4,523
Hi guys I am hoping you can help me. I want to use an Allen Bradley HMI and PLC to record information every time a Cask is filled to an SD drive...
Replies
8
Views
3,220
Back
Top Bottom