03-04-2010 06:05 PM
Hi
Just a quick question to get me out of a hole..... I am using local variable to control while loops, and based on a counter value, it turns it on or off. When i probe the value of the actual boolean, it comes up not exectuted, but the local indicator value is true???? is there any way to change this?? i need to to remain false, UNTIL it goes true!
Cheers
Peter
Solved! Go to Solution.
03-04-2010 07:01 PM
What do you mean by the "local indicator"?
The local variable is a reference to the memory location of a front panel indicator or control. That indicator is going to have a value even if no code has executed yet.
If this doesn't answer your question, please post your VI so we can see what you are trying to do. With your description, I wonder if you are causing yourself some race condition problems.
03-04-2010 07:25 PM - edited 03-04-2010 07:26 PM
Hi,
I am attempting to stop the while loop containing the counter. The while loop should stay running until the counter value saifies the parameters of the position vI, which then should turn the two variables Xstop2 and Ystop2 true, (it does this ok) and that in turn turns the Boolean icon Ystop3 true (it does), but the local variable [YStop 3] in the counter while loop is true all the way alsong, regardless of whether the counter has satisified the contidtions of the position VI? (this is stopping the counter straight away, and the VI will never leave this situation then)
03-04-2010 08:02 PM
Hi,
Indeed of usually local variables, try using a queue instead. You may have a race condition going in your program. If not, for the boolean that you said is always true, check to see is it default value true or not. If it is, you need a boolean with default value of false instead.
Yik
03-04-2010 08:22 PM
I asked you to post your VI, not a screen shot where half the code is cutoff.
You almost certainly have a race condition going on. That means your logic is uncontrolled because it is dependent on what code is executing first which you have no control over.
For instance, the local variable of Y-stop3 is getting read before a value gets written to it, which means it will have either the default value from when the VI was opened, or the value left in there from the last time it was run.
(PS. Don't forget to wire your error wires on the DAQmx functions.)
03-05-2010 08:12 AM
I think that "Ravens" has hit the nail on the head. Using unprotected shared resources (like local variables) in parallel will open the door to race conditions... the bane of any programmers life!
Here are some useful resources which further explain this concept.
Using a Local, Global, or Shared Variable in Parallel Loops Can Cause Race Conditions
http://digital.ni.com/public.nsf/allkb/4F3CC921B4179F9F86256A3B0045CE2D
Tutorial: Local Variable, Global Variable, and Race Conditions
http://zone.ni.com/devzone/cda/tut/p/id/7585
The first link also mentions the producer/consumer articeture and the semaphore. Both great solutions. However, correctly sequencing your source code (using things like error clusters) will help prevent this issue.
Further more, I wonder if you are initialising your variables??
You should write a value of false to the variable before you enter the main body of your code. This ensure that the first time you read from the variable it will return a known value (false). If you dont do this, the variable will return what ever was the last value written to it.
Hope this has been useful.
03-05-2010 09:20 AM
03-05-2010 09:27 AM - edited 03-05-2010 09:28 AM
Rich,
Any quick way to write code to initialise the variables, withour resetting the whole program?
Rgds
Peter
03-05-2010 09:28 AM
Mark Yedinak wrote:
I would also highly recommend you take a serious look at your program architecture and if nothing else consider using a state machine. From the screen shot you posted your application is using a 31 frame stacked sequence structure.
I didn't even notice that! But I did see that the user needs to download and install some Windows updates.
03-05-2010 09:33 AM
newbie09 wrote:Rich,
Any quick way to write code to initialise the variables, withour resetting the whole program?
Rgds
Peter
Message Edited by newbie09 on 03-05-2010 09:28 AM
There is but I am hesitant to tell you only because by doing so it would only enable bad programming habits. You need to really look at your code and avoid the use of all the local variables as well as the overuse of the sequence structures.
Being nice though you can simply write a value to another copy of the local variable or you could use a method (Invoke node) for the control/indicator and use the method "Reinitialize to Default".