How to store Variable value in each PLC cycle in ST

Akgaurihar

Member
Join Date
Apr 2020
Location
xxx
Posts
9
Hello,

I have a variable that contains the velocity value. I have to store the velocity value in each PLC cycle and have to do further calculations. Is it possible to save 20 entries in an array and rotate it, always new entries will be added and the older ones will be deleted? Somebody can help with the code example in Structured Text?
 
Code:
FOR i:= 19 TO 1 BY -1 DO
  arVelocity[i] :=  arVelocity[i-1];
END_FOR
arVelocity[0] := rVelocity;
This will push newest entry to index[0] and shift towards index 19.
 
An alternate but equivalent approach, using a circular array.


(initialize both next_pos and N to zero)


Code:
IF next_pos < N THEN
  <remove value at arVelocity[next_pos] from calculation result>
END_IF

arVelocity[next_pos] := rVelocity;

next_pos := next_pos + 1;

IF N < next_pos THEN
  N := next_pos;
END_IF

<add value in rVelocity to calculation result; N is array size>

IF next_pos > 19 THEN
  next_pos := 0;
END_IF
This is more complicated than the shift algorithm described in post #2, but it moves less data around, because it leaves the data in place and moves the index over the data, where post #2 leaves the index in place (=0) and moves the data over the index.
 
True, but a measly 20 indices in a for loop shouldn't have any impact on performance, and I have always preferred having the latest entry in a fixed position for online viewing :)
 
Code:
FOR i:= 19 TO 1 BY -1 DO
  arVelocity[i] :=  arVelocity[i-1];
END_FOR
arVelocity[0] := rVelocity;
This will push newest entry to index[0] and shift towards index 19.


HI! Thank you for quick help. Being a beginner in programming, need little more help. Could you please check the following code.



Code:
VAR
 Vel_L_Array : ARRAY[1..10] OF INT;
 i : int;
END_VAR


FOR i:= 10 TO 1 BY -1 DO
  Vel_L_Array[i] :=  Vel_L_Array[1];
END_FOR;
Vel_L_Array[1] := Velocity_L;

Velocity_L is the variable giving me the value from the drive.
And now I want to access 10 values from the array for some arithmetic calculation. I hope I'm on right track.
 
To access the "10" arrays you could do the same loop for example if you wanted to find an average over the ten readings then in the loop add the 10 and after the loop divide it by 10
AverageVal := 0; //Clear the average register
for I = 1 to 10
AverageVal := Myarray; //Add the 10 readings
End_For
AvarageVal := AverageVal / 10; //Divide to find the average


forgive me if my ST may be incorrect, but feel lucky as I went back to my pascal days & put begin & end in first time 🔨
 
HI! Thank you for quick help. Being a beginner in programming, need little more help. Could you please check the following code.



Code:
VAR
 Vel_L_Array : ARRAY[1..10] OF INT;
 i : int;
END_VAR


FOR i:= 10 TO 1 BY -1 DO               <<<<<FIX THIS A
  Vel_L_Array[i] :=  Vel_L_Array[1];   <<<<<FIX THIS B
END_FOR;
Vel_L_Array[1] := Velocity_L;
Velocity_L is the variable giving me the value from the drive.
And now I want to access 10 values from the array for some arithmetic calculation. I hope I'm on right track.




You are close:

  • Where it says [<<<<<FIX THIS A] change [TO 1] to [TO 2]
  • Where it says [<<<<<FIX THIS B] change [1] to [i-1]
 
Sorry I missed a crucial bit
AverageVal := 0; //Clear the average register
for I = 0 to 9
AverageVal := AverageVal + Myarray; //Add the 10 readings forgot the accumulated value doh.
End_For
AvarageVal := AverageVal / 10; //Divide to find the average
 
Last edited:
If you are going to perform a true average be careful of data type and result overflow. The result of adding ten 16-bit integers that presumably span the range of a 16-bit integer will NOT fit back into a 16-bit integer.

Keith
 
I thought that as well. But they seemed to be having so much fun. I figured who am I to get in the way of their joy.

Keith

It's not much help though and not what the OP asked.

He had the answer at #2. He may as well watched kitten videos on you tube after that.
 

Similar Topics

I'm running into a problem when trying to use the RSLinx Backup Restore Utility where it throws an error when it tries to restore a backup. The...
Replies
6
Views
571
Hi y'all I have a KTP400 connected to a s7-1200. Now i have on my screen the encoder value which comes from my SEW movitrac. under there I have...
Replies
2
Views
387
This was asked in another forum and taken down due to being a 'backdoor', but this has useful info that might be useful to someone in a pinch. I'm...
Replies
6
Views
608
Hello all, I'm using a 5069-L330ER in a project and I need to essentially capture some data that will be shown as a trend on a screen. The data...
Replies
9
Views
962
Hi all, I am working with an IO link sensor (measuring current across motor)and need to read in and store the sensor values for 5 seconds, then...
Replies
13
Views
1,213
Back
Top Bottom