06-25-2010 06:32 PM
So here is the story,
I wrote a nice program for some data acquisition, BUT it is very slow when running. Basically, the program changes the current (ramp up and ramp down) and reads the temperature (voltage) after every increase/decrease. I used stacked sequences (2 embedded) to control the data flow, and I used local variables to write the data to a matrix and to keep track of the current parameters. These local variables are probably making the program to slow down. So I tried to avoid them, but I noticed stacked sequences only gives out its values after all sequences are done. The option I have is to change them to flat sequences, but this is just a mess and they take way too much space. So there must be a better way to get the data out of the stacked sequence? Or to control the data flow in another way, I just don't know how yet...
So in short, this picture is basically what I am doing. All I want now is to get read-out 1 and 3 into an array every time that for-loop runs AND the same for 2 and 4 in the SAME array. The array will then be used to plot in an xy graph and to store the data in a file.
I have uploaded the original program as well, as it is right now. Any other suggestions are always welcome.
Thanks,
Kristof
06-25-2010 07:36 PM
Don't use stacked sequences. Don't used flat sequences. Just don't use sequences at all. You have just run into one of the many disadvantages of sequences. Use data flow to control execution. One thing newbies just don't understand is that data flow can be used to control execution flow, and is the preferred method by far. Just remember that any function or loop or subvi will not start to execute until all of its inputs are present. That is it. If you want loop1 to execute before subvi2, then wire an output of loop1 to an input of subvi2.
You would do best to learn about state machines. Search for examples. Your stacked sequence could be easily converted to a state machine. You can take outputs out of any state. Your first sequence frame could be your first state, second sequence frame can be the next state, and so on. Once you learn the state machine architecture, you should never go back to using stacked or flat sequences.
Also, avoid the use of local variables. You can use shift registers with state machines to hold variable values. I have attached an example of a state machine for you to get started. The picture you posted is too small to see. Post your code instead.
06-26-2010 02:05 PM
Hi Kristof,
you uploaded a very special VI
- It's quite interesting to see all those semaphore operation to avoid race conditions due to heavy overuse of local variables (the disease is called "Localitis" here in the forum)
- how does the program stop? Do you ever stop the program? (At the moment that could be tricky due to loops running forever...)
- Please think about using shift registers and/or FGVs in your code.
- And please take tbob's advice on never using sequence structures...
- Before uploading a VI to the forum you should also remove any fancy background pictures. For your VI it takes ~95% of the filesize!
- And please remove all those semi-Rube-Goldberg operations (like multiplying with -1 or comparing with zero) and replace them with the proper LabVIEW primitives (like "-x" function or "=0" comparison) to clean up your VI.
06-26-2010 05:01 PM
kristofparedis wrote:I wrote a nice program for some data acquisition,
No, a huge color image on the front panel does not make a VI "nice", it just makes it almost 3MB for no good reason at all. 🙂
You should spend more time on function instead of form. Everything that was said above is true.
One culprit for the slowdown are the three (or more) small loop "reset current to zero", "clear", and "save file" and the big loop in idle mode. None of these need to execute in parallel, so you can stuff it all in one loop containing a reasonable wait.
Since they do not currently have a small wait, they will grab the CPU for extended periods of time, spinning like crazy, starving all other loops.
The rest of the code is just a mess, with tons of unneeded locals and sequences. I am sure the same program could be written with 20% of the code with a diagram that comfortably fits on a typical screen. Try it! 😄
06-26-2010 07:09 PM
Thanks for the info guys,
I am indeed new to Labview (like 2 weeks) and when i said nice i was being ironic.... i know it could be way better... but you got to start somewhere...
anyway, that ugly thing works... now I need to clean it up indeed and when I tried getting rid of all the locals I ran into the variables-don't-leave-the-sequence problem and that's how I got here.
06-26-2010 07:17 PM
Your homework for tonight is to eliminate all the sequences and all the local variables. Think state machine.
06-26-2010 10:37 PM
@altenbach wrote:
kristofparedis wrote:I wrote a nice program for some data acquisition,
No, a huge color image on the front panel does not make a VI "nice", it just makes it almost 3MB for no good reason at all. 🙂
Reminds of the one time at my previous job when we interviewed a candidate and his VI front panels has all sorts of textured backgrounds. One was of wood paneling. On a VI. Yeah. Horrible code, but hey, it had wood panels!
To OP: Don't fall into this trap. It's alluring, but so are sirens.
06-27-2010 02:47 AM
06-27-2010 09:03 AM
I was oh so close to saying something similar.
06-27-2010 09:59 PM
I decided to give it a go with state machines, from scratch. The VI is attached. I got rid of all of my locals except for one and all the sequences are gone now. (the effective read-out functions etc. are replaced by random number generators).I hope this is better coding
My comments/questions:
- As you can see I have still one local left, because I basically ran into the same problem as before: I cannot get the data out of the for-loop to plot them in real time. It stacks them in an array, but they don't go further untill the for loop is done. (why is this such a problem in labview anyway???)
- secondly if you run the VI you can see the 'data' and 'output array' in the front panel. By clicking the index next to that, you can browse through all the previous values of this array. Why is this? When I check in the block value it says it should be a 1D array, yet I see 'n' 1D arrays in the front panel
Thanks a lot guys!
(oh and I got rid of the background :-p)