LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is Data-dependency the only thing to stop while loops from running simultaniously?

Solved!
Go to solution

I'm writing some code to turn two stepper motors with various functions. This is my first program, so I'm sure it's all terrible, but I'm very interested in getting this to work. I've checked for data depenecy, and followed some suggestions to use local variables to fix the issue. This didn't work, so now I'm trying to figure this out. 

 

I know that all functions were capable of running simultaniously, but after some changes (notably putting the whole thing in a while loop to keep the program running until someone hits the exit button), no functions can currently work simultaniously. 

 

I'll post my code in case anyone has the time to look at it (I'd really appreciate it if someone did), but I'm also looking for other issues which might be the source of the problem. 

 

Really, any help is greatly appreciated.

 

Thanks in advanced

0 Kudos
Message 1 of 16
(4,196 Views)

Oh boy, there are many many problems 🙂 But it is good, large room to improve and learn! 🙂

Just some points:

  1. You have experience in other text-based programming language? Do not program as you would in a text based language. Think of dataflow!
  2. Way too large block diagram! Difficult to debug and understand. Your block diagram should fit into a normal screen. Use subVIs!
  3. Do not use the (stacked) sequence structure! This is an object which you rarely need (usually only for benchmarking code)! The main problem is that, it violates dataflow (takes execution order out of normal dataflow)!
  4. Learn how to design and program STATE MACHINE!
  5. Avoid overusing local variables! Many possible race conditions you may create in your code!

I really recommend you to learn LabVIEW, start with the Core 1 and Core 2 course. Here you can find info about options to reach free teaching material:

https://forums.ni.com/t5/Community-Documents/Unofficial-Forum-Rules-and-Guidelines/ta-p/3536495 (under section: "looking for free training")

 

Have a look at this also:

http://www.ni.com/newsletter/51735/en/ 

 

edit: State machine example:

https://www.ni.com/en/support/documentation/supplemental/16/simple-state-machine-template-documentat...

 

edit2: this is a link you can find from one of the above, but in case you miss it:

http://www.ni.com/newsletter/51770/en/ 

 

0 Kudos
Message 2 of 16
(4,147 Views)

Some additional comments:

 

  1. Read up on event structures. This will allow you to write more compact code to respond to the buttons.
  2. SubVIs are definitely your friends.
  3. It's hard to tell from the code, but most likely, your assertion that "no functions can currently work simultaniously" is not actually true. The most likely source of your problem is that the external while loop isn't iterating because it's waiting for the two internal ones (which are running) to finish. You can see things like this by enabling the execution highlighting button. As Blokk suggested, this is a fairly basic point of dataflow.

___________________
Try to take over the world!
0 Kudos
Message 3 of 16
(4,119 Views)
The code needs to be finished fairly soon. The lab needs it done sooner rather than later, so time for a grandious re-write probably isnt in the cards, as much as id like to do it all correctly.

Sub VIs do sound like a big improvement, and a much faster fix.

As far as the outside loop waiting for the inside one(s) to finish, I agree that this is probably the source of the problem. However, im 90% sure the loop never starts. When clicking the GO button, the first thing to happen in the loop is to reset the button, but that never happens. Leading me to believe that the loop never starts
0 Kudos
Message 4 of 16
(4,020 Views)

In my opinion subVIs only will not fix this enough. I understand time is often critical, but many people do not consider how much time they will lose when the software does not work as it should during its usage. In the end, you may lose more time due to an immature software than win by finish it "fast".

 

edit: I really would like to help, but due to the size of the VI, it is not something I could clean up in a few minutes or even hours, specially since I do not know the task of the code... 19 frames in a stacked sequence, many hidden controls used as variables instead of using wires and data flow, this code is over which i can consider refactorable... Smiley Frustrated

But for sure we could help to guide you through to rewrite the code. Learn how to program a state machine at least, it is pretty easy. I believe, here you need to use a state machine, probably with an Event structure...

First create a flow chart what your code needs to do, what are the tasks, and the transitions between states + conditions. After this you can program a proper state machine.

 

0 Kudos
Message 5 of 16
(4,007 Views)

I completely agree with Blokk.

 

Having councilled colleagues with similar poorly-structured code to "Start Over" (and to first write down what they want to do, and not to worry about how they need to do it) and seen this advice ignored "because I need this quickly", when a few weeks have gone by and it is still not working, I have occasionally Started Over on their code, and after a few days, had something that "mostly" worked (and was much easier to debug) and frequently worked much better (i.e. was faster, or permitted 24 parallel processes instead of 7, etc.).  The "catch", of course, is you need to "give up" on such things as Stacked Frames, Block Diagrams that span multiple monitors, code that has more Wires than Sub-VIs + functions (that's a measure that I just made up, but some of the more atrocious code I've seen has wires running everywhere, crossing up and down, across 3-4 monitors, and connecting only a handful of functions and no user-written sub-VIs).

 

An Investment of a Week now can save you months down the road ...

 

Bob Schor

0 Kudos
Message 6 of 16
(3,994 Views)
0 Kudos
Message 7 of 16
(3,989 Views)

To answer a previous question, yes, I have a good bit of experience with text-based programming languages. I'm sure it shows.

 

So just use a state machine for the initialization function intstead of using the 19 long stacked sequences?

 

I'm a bit confused about how to do wait timers in a data flow scenario, unless you maybe do a while loop sub-vi with a case statement inside, and have the iteration variable (i) and a division with remander (mod) to select what to do during the while loop?

 

For instance my sweep function has 4 basic things inside of it, a move left, a wait, a move right, and then another right. I would want to do a mod4 function on the iteration variable. Make a while loop, using the event handler, then where mod4 produces a 1, move left, when mod4 produces a 2, move right, etc...

 

Another quesiton about state machines. Do you suggest that I put the whole thing into a state machine? Looking at them, it seems like it would require mulitple states to exist simultaniously. Which doesn't seem to be the way any examples I've looked at work. They each have 1 user imput, which then changes the state to something else. I would want at minimum 2 states to exist simultaniously if i'm thinking of states as calls to a function. (Which again, might not be the correct way to think about it) 

 

 edit: Also - i'm also 95% sure that you're right about the while loop waiting until the inside loop finishes. I'll select one button go button, then that while loop starts. Clicking another go button would do something, except that the outside while loop has already decided that it doesn't need to run that function (since at the beginning, it was turned off), thus it's waiting for the inside loop to finish before it repeats the check to see which functions need to run. Is that what you meant?

 

 

0 Kudos
Message 8 of 16
(3,959 Views)

Here's an Idea (not necessarily the Best Idea) for adding a Wait to a State Machine -- make a State called (are you ready?) Wait.  Create two Shift Registers, one with an I32 with a label called "Delay" and a second labelled "State After Delay" for the State after the Delay.  Now, if State 3 is supposed to be followed by a 4 second delay, followed by State 4, before leaving State 3 wire 3000 (3 seconds) into Delay, State 4 into State After Delay, and make the Next State "Wait".  You will enter the Wait State, wire the Delay into a Wait (ms) function and wire the State After Delay to Next State.  You'll wait for the Delay Time, then do what you were supposed to do after the Next State.  Note that the Wait State is very flexible -- it can wait for any amount of time (you decide) and do any subsequent State (again, you decide).

 

Bob Schor

0 Kudos
Message 9 of 16
(3,935 Views)

When someone is new to LabVIEW, it's not ideal to give them broken tools.  Why would you ever suggest he setup something that will prevent his code from responding to user input for 3 seconds?

 

That's twice you've suggested this behavior today.  Please spend more time with the wait functions before suggesting how to use them =/

0 Kudos
Message 10 of 16
(3,905 Views)