08-30-2006 10:33 AM
08-30-2006 10:38 AM
Hi Tony,
This is expected... It is not a bug.
You have to initialize the shift register before the loop, especially, if you do not force an initialization within the loop. Otherwise, you will not be able to predict the outcome. Furthermore, if you create a sub-vi which is called more than once, it will retain the last value as the starting point, which is unlikely to be the behaviour that you would want.
Simply initialize the shift register to the left of the loop. A typical method is to wire a constant of 0 (or array of zeros).
RayR
08-30-2006 01:39 PM
08-30-2006 01:54 PM
08-30-2006 02:12 PM
I like what Jaegen proposed.
If it's the first time that the vi runs, it will do the initialization.
However, you mentionned this: " I want to preserve the results so I do not want to initialize the shift register. "
You'll have to define that behaviour. Do you mean each time it is called, you want it to use the last values? However, you would want to have the initial values to be predictable, right? And what about if the vi is closed & re-opened or something obscure occurred in between (not that it ever happens, right? 😉 )
You may want to read an ini file that contains the "first-run values" and could / would store the last values for each time the sub-vi (or vi?) is called.
08-30-2006 02:21 PM
08-31-2006 08:40 AM
@TonyFeng wrote:
The interesting thing is I did not expect to get empty for an ADD function. I think for an ADD function, logically, something ADD nothing = something, not nothing makes sense.
Actually, when you do not initialize the shift register, it's contents are not "nothing" it is whatever residue is in that memory location. If you're lucky the value is zero (0), so "something" + 0 = "something", eg: 6 + 0 = 6.
Let's say "something" = 6:
If the residue in memory was 623458723647823746827364273655 + 6 = 623458723647823746827364273661, which is an unexpected result.
I'm illustrating this to show that you are not adding blank, thus resulting in blank... There is a value in memory. Which is why it is always important to initialize values..
RayR
08-31-2006 10:54 AM
We seem to need to clarify things ...
If I open a new blank VI, and create a for loop with an array constant wired to its right-hand-side but nothing wired to its left-hand-side, when I first run it the value out of the left-hand-side of the shift register will always be an empty array. And if I close and re-open the VI (as long as it's not somehow retained in memory on the clip board ), the first time I run it, the left-hand-side will always be empty again. Also, if the operations I do on the array do nothing to overcome the fact that it's empty (like only adding it to another array), and therefore an empty array always gets written to the right-hand-side of the shift register, the left-hand-side will always be empty.
However, if the shift register is uninitialized, but somehow a non-empty array gets written to the right-hand-side, then between runs of the VI while it's held in memory (either separate clicks of the "run" button, or separate runs of the VI as a sub-VI), the shift register will retain whatever value got written to its right-hand-side. This means that the data may persist from one run of your program to another, which may not be desired.
Hopefully this isn't too muddy ...
Jaegen
08-31-2006 01:16 PM
Jaegen,
I'm reading these posts at lightening speed during short breaks 😉
You got it!
When you close a vi and re-open it. The FIRST time you run the vi, the non-initialized array or value for a shift register will initialize itself to the default value for the given type.
The NEXT time you run it, it will (may?) initialize itself to whatever value remained in memory (the residue in memory that I mentionned earlier).
Uninitialized shift registers are not a good idea.. 🙂
RayR
08-31-2006 01:20 PM
Unless you initialize them inside the loop of course.
@JoeLabView wrote:
... Uninitialized shift registers are not a good idea.. 🙂