03-03-2011 05:51 PM
I need help resetting my loop iterations back to 0. For the most part it works fine but has one bug. Some background on what I am trying to do. Basically, i'm controlling a motor with a brake on it. On the brake there is a manual overide that I am monitoring with a proximity sensor. If the magnetic field is broken it stops the VI with the iteration it stopped at. When I press the start button, the iteration continues. The VI stops when iterations equal the Set # of Cycles control value. I wanted the option of resetting the iteration back to 0 if I wanted at any point, so I placed a reset button in the VI. This works fine with one the one bug. This bug is if for some reason the iteration count is higher than the Set # of Cycles count, the VI will continue to run. This is where I need help, as i'm not sure how to go about this other than faking it by setting the min threshold at a high number, forcing the operator to reset the counter. Attached is a peg of the area of code. Any help or suggestions are greatly appreciated. Thank You!
Solved! Go to Solution.
03-03-2011 07:40 PM - edited 03-03-2011 07:41 PM
If the iteration count is higher than set number cycles it will continue to run because you are checking to see if the values are equal. You could change that to greater or equal.
03-03-2011 10:05 PM
As mentioned by Steve, replace "equlas' with "greater than or equal to"
Guru
03-04-2011 06:57 AM
Hello Steve, Yes, everything worked well before putting the Reset button in. Everything works well now with the exception of this small glitch. Why, replace with "greater than or equal" if I want VI to stop when "cycles" equals "Set # of cycles" ? I'll try it and let you know. I do know that changing the "Set # of Cycles" to I32 makes no difference. The VI is much larger than the image and there is a start button, it is out of view. The LabView run button puts VI in an "idle" state and does not run until I press the start button. Thanks for the reply.
03-04-2011 09:37 AM
@swins wrote:
The LabView run button puts VI in an "idle" state and does not run until I press the start button. Thanks for the reply.
Many people write their programs such that they exit the main loop when the program is done. Then they press the LabVIEW run button to run the application again. Even worse they have no main loop and press the run continuous button. Both are wrong and the latter much more so.
Sorry I assumed you were doing that but without seeing more code I could only guess.
Changing the equal to greater or equal would be more of a debugging step. Does the loop that you show continue to run or just the rest of your application? If the loop keeps running then the output of the compound or is always false meaning that the output of the equals is never true.
Did you put probes on the output of the tunnel connected to the loop stop terminal and the iterations shift register? Also put probes on the three terminals of the equals and turn on highlight execution.
03-04-2011 10:09 AM
Hi swins,
Under what conditions does your iteration count get larger than your "set # of cycles?" If you set that set # of cycles less than the iteration count while the vi is running and the iteration count has already exceeded that number then it is not possible to stop the loop when the # of iterations equals that only greater. Or am I misunderstanding you?
Guru is correct. You need to change the "equals to" to a "greater than or equals to."
Also, if you eliminated the case where you "set # of cycles" was set lower than the iteration count during run time, you would definitely want to change the "set # of cycles" to an I32 data type. Otherwise there are problems with the equals to block pertaining to how data types are stored in memory. See this link if you are interested in learning more: Wikipedia: Double precision floating-point format
Hope this helps! Have a great day,
Chris V
03-04-2011 10:54 AM
Hello Chris,
Thanks for your reply. I can give you one instance. If Set # of Cycles is set to 0 and and iterations is 0, when Start button is pressed it will start to cycle then stop because iterations equal Set # of iterations. If I pres the Start button again without pressing the Reset button VI will sart again and cycle past 0 and keep going until I press the Stop button. I kind of understand why it is doing this because of the +1 on the shift register but don't understand how to correct it.
03-04-2011 11:36 AM
Hi swins,
Okay, I see what's going on! It appears from your screenshot that you have unintentionally created a functional global with your iteration counter. By not initializing your shift register, it is retaining the last value from one run of the program to the next. So if you run it once and stop, the next time your program starts it will start at 1 and then increment to 2. You should be able to fix the problem by wiring a zero constant to that shift register. For more information on functional globals check out this article on the community site: Basic Functional Global Variable Example
Have a great day,
Chris V
03-04-2011 01:02 PM - edited 03-04-2011 01:03 PM
@Chris V wrote:
Hi swins,
Okay, I see what's going on!
Good point. I thought all swins added before it broke was the select prim and reset button and that the shift register was already there.
Swins, if that is not the problem is it possible to post an image of the loop before you modified it? I am willing to bet (but not cash or anything) that the select prim was already there and the stop button was wired to the selector which is why it got reset. And I am also willing to bet (but not cash or anything) that if you wire a 0 to the input of the shift register all will be well.
If that is it make sure to mark Chris's reply as accepted solution.
03-04-2011 02:36 PM
Hi Chris,
I'm sorry to say that placing a zero constant on the shift register did not work. By doing this, now every time I press start, iteration starts at 1. What I did do by your earlier post is change the Set # of Cycles to an I32 data type and change the equal sign to a greater than/equal to. This works as expected, if I start vi with iteration higher that set # of cycles, VI will run 1 cycle then stop. This is acceptable. I want to thank you for all your help and insight. I knew I was close but phenomena happened when I tried to add "reset" logic.