01-13-2009 09:31 AM
I have a vi that zero's my voltage signal. The first time I run the vi the shift register has a zero value which is what I want. The second time I run the vi the shift register has the previous values in it, this will not let me zero correctly. How do I initialize the shift register only when I run the vi. I have tried setting the shift register to a zero constant but this returns a zero every time the value loops.
Thanks,
joew
01-13-2009 09:40 AM
A shiftregister keeps values written to it in the previous iteration. Therefore if you have written a value other than 0, it will keep that value until you write the next value to it.
If you load a VI containing a shiftregister, LV will initialize it to its default datavalue (0 for numeric, "" for string, NULL for references and FALSE for boolean). Running the VI will pass on that value in iteration 0. If you do not write a new value to it in this iteration, the next iteration will also pass on the default value....
If you initialize the shiftregister, you will write a new value into the shiftregister just before executing the first iteration (0), and therefore will get the value you have just written in iteration 0.
If the VI is stopped and started again, LV will still have the memory of the shiftregister in it's dataspace and therefore read the value written in the last iteration of the previous VIs execution. Initializing the shiftregister in your VI will prevent the VI from reading out-of-date data.
hope this helps,
Norbert
01-13-2009 09:50 AM - edited 01-13-2009 09:52 AM
I'm not sure I understand correctly what you mean by "How do I initialize the shift register only when I run the vi".
A common way to work with such functional globals (or action engines) is to have different methods. One of the could be to init the VI, one to set a value, one to read a value, ...
Then you just call the VI with the init method when your application starts.
I'd recommend to read Ben's thread on Action Engines. It's an excellent resource on that topic.
Hope this helps,
Daniel
01-13-2009 10:39 AM - edited 01-13-2009 10:39 AM
The title of your question is quite clear however the following question is not.
How about this:

Ton
01-14-2009 02:35 AM - edited 01-14-2009 02:36 AM
Ton,
i am sorry to say that your screenshot is a kind of Goldberg 😉
This VI does the same (i just used Add instead of Increment since Increment would use double here):
So your VI makes only sense if your initvalue is != 0....
Norbert
01-14-2009 03:56 AM
To initialize a shift register i usually connect do it from the "outside", just like you mention you do with a constant.
The shift registers idea is to pass the data to the next loop iteration, so unless you modify the data inside the loop it'll always stay at the previously initialized value.
(maybe i misunderstood)
What do you use the shift register for, maybe it's not needed at all?
/Y
01-14-2009 04:37 AM
I'm sorry Norbert, but you are wrong and Ton is right.
When you execute the vi in the development environment, the shift register is not re-initialized when the execution is stopped and restarted.
So, Ton's code is necessary in order to restart with a proper null (or whatever needed) value in the sr, because the "First Call?" node issues a true every time the execution is restarted.
01-14-2009 04:42 AM
Paolo,
please review my screenshot: Do i init the shiftregister??
What does a (none initialized) shiftregister pass in the first iteration (i==0) in the first execution of the VI after loading it into memory????
So if Ton is passing a numeric 0 (boolean would be FALSE, Reference NULL, string an empty string), it does exactly the same.
If Ton passes something different, the select and "first call" would be neccessary because it would change the value of the shiftregister in the first call after the VIs loading process.....
Norbert
01-14-2009 06:14 AM
If you stop the VI I wrote and you rerun the VI the value be resetted.
The code I have written has no meaning (as Norbert noticed), it is the technique of the 'FirstRun?' and the case selector and the default value that matter.
Ton
01-14-2009 07:49 AM
I’m struggling here to understand the requirements here, but as I see it, what Yamaeda has done is what is needed. Now if joew has tried this and, as he says, “but this returns a zero every time the value loops” maybe he has miss-wired the connection and wired zero inside the loop through a normal tunnel which is hidden under the shift register arrow.