Go to Post I'm not overdosed, I'm not even addicted. I just choose to never, ever, ever leave FIRST. - SL8 [more]
Home
Go Back   Chief Delphi > Technical > Programming > NI LabVIEW
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 7 votes, 4.86 average. Display Modes
  #1   Spotlight this post!  
Unread 08-03-2011, 17:06
siggy2xc siggy2xc is offline
Registered User
AKA: Tyler Siegrist
FRC #1756 (Argos)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2009
Location: peoria
Posts: 70
siggy2xc is an unknown quantity at this point
Run something only once in a while loop?

What is the best way to run something only once while in a while loop? The method I am currently using uses a global variable but id like to find a way that does not require a global or a local variable.
Reply With Quote
  #2   Spotlight this post!  
Unread 08-03-2011, 17:51
Vikesrock's Avatar
Vikesrock Vikesrock is offline
Team 2175 Founder
AKA: Kevin O'Connor
no team
Team Role: Engineer
 
Join Date: Mar 2006
Rookie Year: 2007
Location: Manchester, NH
Posts: 3,305
Vikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond repute
Send a message via AIM to Vikesrock Send a message via MSN to Vikesrock Send a message via Yahoo to Vikesrock
Re: Run something only once in a while loop?

So if I understand correctly you want some piece of code inside a while loop to run only the first time the loop runs?

This can be accomplished by placing the desired code inside a case structure that is fed by a comparison with the out put of the little blue "i" block in the corner. This block keeps track of the number of loop iterations. I can't recall off hand whether it starts at 0 or 1 (I think 1) but you should be able to trigger your case structure by doing an "equal to" on the output of.

Let me know whether that made any sense or not
__________________


2007 Wisconsin Regional Highest Rookie Seed & Regional Finalists (Thanks 930 & 2039)
2008 MN Regional Semifinalists (Thanks 2472 & 1756)
2009 Northstar Regional Semifinalists (Thanks 171 & 525)
Reply With Quote
  #3   Spotlight this post!  
Unread 08-03-2011, 17:54
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,563
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Run something only once in a while loop?

You can also use the first call function: http://zone.ni.com/reference/en-XX/h...ng/first_call/
Reply With Quote
  #4   Spotlight this post!  
Unread 08-03-2011, 17:56
siggy2xc siggy2xc is offline
Registered User
AKA: Tyler Siegrist
FRC #1756 (Argos)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2009
Location: peoria
Posts: 70
siggy2xc is an unknown quantity at this point
Re: Run something only once in a while loop?

Thanks! never knew that. But it starts at 0 for future reference
Reply With Quote
  #5   Spotlight this post!  
Unread 06-03-2013, 17:32
random_variable random_variable is offline
Registered User
no team
 
Join Date: Mar 2013
Location: Washington
Posts: 1
random_variable is an unknown quantity at this point
Re: Run something only once in a while loop?

I think something like this may be of interest to you. After I start the program, no random number appears. When I 'Begin Trial' a single random number appears. I did initialize the value of 'Boolean' to TRUE before the while loop executes.

Attached Files
File Type: vi sasasa.vi (8.7 KB, 185 views)
Reply With Quote
  #6   Spotlight this post!  
Unread 06-03-2013, 20:16
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Run something only once in a while loop?

Yes, that's pretty much how Tyler was doing it in the first place. He was asking how to do it without a global/local variable.

Two years ago.



I think the "First Call?" function is the appropriate tool.
Reply With Quote
  #7   Spotlight this post!  
Unread 14-03-2013, 18:12
NeatNit's Avatar
NeatNit NeatNit is offline
Alumni Programmer
FRC #1577 (Steampunk)
Team Role: Teacher
 
Join Date: Sep 2011
Rookie Year: 2011
Location: Israel
Posts: 60
NeatNit is an unknown quantity at this point
Re: Run something only once in a while loop?

I believe First Call is a very specific function with a very important quirk to keep in mind. If said while loop is inside of a bigger loop, then the second time it is called, should the code run? With First Call, it will not run. With iteration check, it will run, since that is reset every time the loop is started.

There is also a bunch of LabVIEW quirks, if you run your code through your PC for debugging after some code has already run on the robot, you can't be sure whether First Call will reset or not, as LabVIEW seems to save the most annoyingly unpredictable bits of data when you do things like that.

First Call is a deceivingly simple yet advanced function and should not be introduced to inexperienced LabVIEW programmers.

Last edited by NeatNit : 14-03-2013 at 18:17.
Reply With Quote
  #8   Spotlight this post!  
Unread 15-03-2013, 07:10
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Run something only once in a while loop?

I agree that first call is an advanced concept, but I wouldn't call it quirky. The documentation is pretty explicit "The First Call? function returns TRUE only the first time you call it after you click the Run button." There is a bit more in the details. If you find a case that doesn't fit the documentation, it is time for something to change, either the docs or the product.

Greg McKaskle
Reply With Quote
  #9   Spotlight this post!  
Unread 15-03-2013, 07:19
NeatNit's Avatar
NeatNit NeatNit is offline
Alumni Programmer
FRC #1577 (Steampunk)
Team Role: Teacher
 
Join Date: Sep 2011
Rookie Year: 2011
Location: Israel
Posts: 60
NeatNit is an unknown quantity at this point
Re: Run something only once in a while loop?

Well, just think about how many times feedback nodes reserved information you didn't want them to, or wouldn't expect them to. First Call would do the same thing.
Reply With Quote
  #10   Spotlight this post!  
Unread 16-03-2013, 10:40
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Run something only once in a while loop?

The explicit purpose of the feedback node is to retain information for another run of that diagram expression. The complication is specifying what the lifetime is for the retained state data -- when is it to be initialized and with what value. The initializer terminal on the feedback node and the left side of the shift register are there to specify the value and timing of the initialization. Perhaps it doesn't work the way your intuition says it should, and I'd like to hear more about that, but it seems pretty complete, useful, and teachable. I'd like to hear the cases you are having issues with.

Greg McKaskle
Reply With Quote
  #11   Spotlight this post!  
Unread 16-03-2013, 16:55
NeatNit's Avatar
NeatNit NeatNit is offline
Alumni Programmer
FRC #1577 (Steampunk)
Team Role: Teacher
 
Join Date: Sep 2011
Rookie Year: 2011
Location: Israel
Posts: 60
NeatNit is an unknown quantity at this point
Re: Run something only once in a while loop?

Quote:
Originally Posted by Greg McKaskle View Post
The explicit purpose of the feedback node is to retain information for another run of that diagram expression. The complication is specifying what the lifetime is for the retained state data -- when is it to be initialized and with what value. The initializer terminal on the feedback node and the left side of the shift register are there to specify the value and timing of the initialization. Perhaps it doesn't work the way your intuition says it should, and I'd like to hear more about that, but it seems pretty complete, useful, and teachable. I'd like to hear the cases you are having issues with.

Greg McKaskle
Well to put it short I am having problems predicting when feedback nodes get initialized, especially when teaching about them. They seem to be very determined in retaining information always always, and sometimes even between totally separate runs. Shift registers are completely predictable - they get initiated when the loop is started. But I've seen some generally nice code by rookie programmers get slaughtered by this. Stopping, editing code, and running it again, feedback nodes actually retained information from previous runs. I find it difficult to teach people "when to use what", because they're not going to accomplish anything if they don't learn these kinds of things the way they understand them, and with feedback node and shift registers being so similar, it's very hard for me to just tell someone "forget it, feedback node's broken, switch to shift register".
Reply With Quote
  #12   Spotlight this post!  
Unread 16-03-2013, 20:09
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Run something only once in a while loop?

I agree that you need to have the correct concept and trust your code to work.

I'd suggest that you build very simple programs, perhaps charting them. This will hopefully help with the teaching.

By the way, it is certainly allowed for code to retain values across runs. This is intentional so that it can better represent external hardware and other processes. This is the behavior of the uninitialized shift register and the feedback node with no initializer.

Greg McKaskle
Reply With Quote
  #13   Spotlight this post!  
Unread 17-03-2013, 16:59
NeatNit's Avatar
NeatNit NeatNit is offline
Alumni Programmer
FRC #1577 (Steampunk)
Team Role: Teacher
 
Join Date: Sep 2011
Rookie Year: 2011
Location: Israel
Posts: 60
NeatNit is an unknown quantity at this point
Re: Run something only once in a while loop?

Something I can't test right now - many inexperienced LabVIEW programmers like to use Run Continuously (or whatever it was called, the loopy button next to the normal run button) for testing their loop code. How does First Call react to that? For the record I intend never to teach about this button ever again, just because of the confusion it causes, and the horrifying habit of pressing it always instead of normal run.
Reply With Quote
  #14   Spotlight this post!  
Unread 17-03-2013, 18:41
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Run something only once in a while loop?

It works the same as pressing run immediately after the VI finishes. It inits between each run of the diagram.

Greg McKaskle
Reply With Quote
  #15   Spotlight this post!  
Unread 23-03-2013, 18:29
NeatNit's Avatar
NeatNit NeatNit is offline
Alumni Programmer
FRC #1577 (Steampunk)
Team Role: Teacher
 
Join Date: Sep 2011
Rookie Year: 2011
Location: Israel
Posts: 60
NeatNit is an unknown quantity at this point
Re: Run something only once in a while loop?

I know how it works, but when someone is new to LabVIEW, the first tasks for teaching them are mostly input, linear calculations, and output. To test this we use the Run Continuously button, so we can see how the output responds to our input immediately. This causes the hard-to-break habit for new programmers to always use the Run Continuously button instead of the Run button.

$@#$@#$@#$@# annoying.


Edit: lol, that looks a lot more angry than the original xD
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:27.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi