Array Shifting

klawson2012

Member
Join Date
Jan 2013
Location
Mass.
Posts
9
Hi,

I am using RSLogix 5000 V19 and I am trying to figure out how to shift reals in an array. Currently, I setup a flip-flop that increments a counter which increments the stored location in the array. This gets a little hairy when I get to the final location in the array because I then have to reset the counter back to zero and data is lost for one of the counts. I found some notes on the FIFO process but nothing has really helped me much.

Anyone have suggestions on how to create a rolling array of reals?

Thanks.
 
So each time you want to shift, you move your array "up" by one location, then store the latest value in element zero? Does that sound about right?
 
Yea that is what I would like to do. I haven't been able to get anything to work so I went with the flip-flop an I store the data incrementally. Ideally I want to have something that I can store the date into location zero and have the data shift each time to the next location
 
Put the following on a rung and check it out.

Code:
XIC toggle ONS oneshot COP array1[0] Array2[1] 9 MOV NewValue Array2[0] COP Array2[0] array1[0] 9

The basic idea is copy your rolling array starting at element 0 into a temporary array at element 1 for whatever length you are keeping track of (be sure not to make the length longer than your array size). Then you write your new value to temporary array element 0. Then copy your temporary array starting at element zero back to your original array.

This will shift your arrays "up" by one. Someone here will have a more novel idea I'm sure.

(my example, both arrays were 10 real's and the "NewValue" is a single real)
 
You will want to use better names than I and be more consistent with naming! I had one array name that was all lower case and another that started with a capitol. For shame...
 
There are a number of ways to do it. The suggestion above would work. Most recently, I ran a loop:

LBL(SHIFT)MOV(tArray[40-IDX2-1],tArray[40-IDX2]);
[ADD(1,IDX2,IDX2),LES(IDX2,40)JMP(SHIFT)];
MOV(tValue,tArray[0]);
 
That looks good, MusicGuy. I think that is the smart way to first shift the array UP, retaining all the old values at 1 position higher than before, but creating a vacant spot at Position 0, then move the new value to the vacant Position 0.
 
The smartest way is to make the array work "backwards" with the newest data at the highest element, and the oldest data at element zero.

For an array of size 20 elements, the shift is performed with a single COP ....

COP Array[1] Array[0] 19

Then to store the new data ...

MOV Data Array[19]



If you can't work with the data array oldest first, then you can still do the shifting, but you will need another "scratchpad" array, and two COP instructions ...

COP Array[0] Scratchpad [1] 19
COP Scratchpad[0] Array[1] 19

Then put your new data in Array[0]


If you wan't to make the whole process synchronous, so that the array can't be read by another machine while it is being copied (remember the system overhead time-slice that deals with communications!), then the most bomb-proof method is as follows ...

CPS Array[0] Scratchpad [1] 19
MOV NewData Scratchpad [0]
CPS Scratchpad[0] Array[0] 20

Note that I have used CPS instead of COP - CPS is the same as COP, but can't be interrupted while it is executing.
 
Last edited:
Check out The FAL instruction. Use it in the Incremental mode. It works with REAL and it will roll over once it reaches the end of the Array.
 
Using FAL in incremental mode on a 200 element array would require 200 scans of the logic to shift the array. I don't think that is what the op wanted.

The solution I provided above works well, and can't be bettered in terms of efficiency and robustness. I even tested it... see pic

2013-12-07_004322.jpg
 
Daba, I recognize your expertise and enjoy reading your explanations.
I meant no offense to your suggestion. I was just offering another way to shift data thru an Array With the FAL instruction.
 
Daba, I recognize your expertise and enjoy reading your explanations.
I meant no offense to your suggestion. I was just offering another way to shift data thru an Array With the FAL instruction.

I wasn't offended, just thought the OP wanted a single-scan solution 🍺
 
No help from me for the OP srry. I just wanted to state that I have a lot to learn by just reading the replies.. lol.
 
Why not implement a circular queue? Then one doesn't waste time moving data.

Perfectly valid solution, although the indirection needed might make it less transparent, to the inexperienced, what is being achieved. And having the data stored absolutely in a historical manner makes it easy if you wanted to, say, plot a trend, or tabulate the data on a HMI or SCADA.

To determine how much time is "being wasted", I set up a FOR instruction to call a routine a lot of times. The routine contained one CPS instruction, to copy a 20-element REAL array.

With the iteration set at 20,000 my scan time increased by approximately 220 mS. After removing the CPS from the called routine the scan time reduced to 55 mS.

This rough and ready test tells me that 20,000 CPS executions took approx. 165 mS, so each execution 8.25 uS.

This test was conducted on an L62, the L7x controllers are even faster.

On balance I would choose the copy-shift method, and not be concerned about the time it takes to execute.
 

Similar Topics

Greetings, I am trying shift data in an array (such as Array[0] is shifted into Array[1], Array[1] is shifted into Array[2]......all...
Replies
10
Views
362
Hello Guys, I think I need to use SCL for the following task but am very weak using SCL in tia portal. I have results history data block. It is...
Replies
14
Views
3,041
Hey guys, What do you suggest for doing a shift of dints within an array like this? Thanks, Kevin
Replies
12
Views
2,585
Not sure if this is possible but thought I would put it out there. Using Rockwell Studio 5000, is it possible to shift the address of an array...
Replies
7
Views
2,096
Gents, Trying to shift and average 10 integer array values. First issue is shifting every value in the array forward one spot. 0..8 becomes...
Replies
5
Views
1,934
Back
Top Bottom