LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Initialization and de-initialization

I am going to give you a simple example of a State Machine. Maybe this will give you some ideas on how to refactor your code to a proper program architecture.

 

First we initialize a WT1600 power analyzer

S1Capture.PNG 

Next we initialize a WT230 power analyzer

s2Capture.PNG

 

Next we open our data file

S3Capture.PNG

 

Then we take a measurement

s4Capture.PNG

 

Then we go to the Timer state and wait for time to elapse

s5Capture.PNG

 

This is the part I want you to understand since your first question had to do with error handling.

es1Capture.PNG

 

Where the instruments are "De-initialized" and the data file is closed

s6Capture.PNG

An error message is displayed and the program exits after the error is acknowledged.

 

Now I did not go into a lot of detail or show every single state as I wanted to keep it simple.

 

But if you have any questions feel free to ask.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 11 of 39
(1,002 Views)

your example of State machine is not helpful. Wt1600 initialization takes resources at first vi in line and there isn't another vi which takes resources.

let's look your Error state. If error would happen in WT230 close then WT1600 close and file close will not do anything and resources will not be clear! This topic is really about this!!!

 

your program is pretty simple and you use single top vi. in anyhow, state machine approach has few issues

1. all items, like wt1600, are not visible at glance. programmer needs to click multiply times to find out

2. programmer should keep all information in memory. let's say program has 100 items, initialization order is hidden and it is easy to miss initialization of couple items

3. debugging requires many extra clicking

 

in anyhow, state machine or not is preference of each person.

 

to give you chance to think a little instead blind follow pattern 😉 i placed couple block diagrams

1. Wash initialization vi. Wash uses 2 valves and 3 stages. The stages are shared with other items. It shows why there is extra reference (I created topic but nobody wasn't able to improve)

image.png

2. Wash has task which work with top vi and multiple other tasks. It shows how to simplify top vi instead of put everything in "State machine" 😉

Vasilich2004_0-1632944543389.png

 

if i would follow your approach my top vi will have few hundred states 😉

0 Kudos
Message 12 of 39
(984 Views)

To alterbach

1. Yes, you are correct about "You are probably talking about controls/indicators"

 

2. No, you are incorrect about "Wires will never be long if the diagram is kept at a reasonable size and has a clean architecture, so focus on that."

 

Architecture is trade!

 

Your suggestions (which I see time to time) are mostly useful in case of "hidden" LabVIEW knowledge but not always. I think you tried to help to avoid additional reference indicator where nobody could help.

In case of complex questions I have feeling that I mostly didn't get good suggestions in LabVIEW forum.

 

 

0 Kudos
Message 13 of 39
(976 Views)

@Vasilich2004 wrote:

2. No, you are incorrect about "Wires will never be long if the diagram is kept at a reasonable size and has a clean architecture, so focus on that."


The longest horizontal wire segment can never be longer than the width of the diagram. 😄

 

With a wire, you always know where the data is coming from, where it last got updated, and where it is used downstream. With local variables, you can never be sure unless you sprinkle an unhealthy amount of sequence structures everywhere. Besides, you get extra data copies in memory.

 

(I typically keep most process data in a cluster held in a shift register and updated using bundle by name. One wire, unlimited number of "variables" and self documenting as long as the cluster elements are named well. If a function need parts for processing, I unbundle by name.)

0 Kudos
Message 14 of 39
(970 Views)

@Vasilich2004 wrote:

your example of State machine is not helpful.


Your reply (and your block diagram) tells me that you do not understand data flow programming.

 

I suggest you take a few of the LabVIEW training courses.

 

Good luck, I am done here.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 15 of 39
(965 Views)

@Vasilich2004 wrote:

To alterbach

1. Yes, you are correct about "You are probably talking about controls/indicators"

 

2. No, you are incorrect about "Wires will never be long if the diagram is kept at a reasonable size and has a clean architecture, so focus on that."

 

Architecture is trade!

 

Your suggestions (which I see time to time) are mostly useful in case of "hidden" LabVIEW knowledge but not always. I think you tried to help to avoid additional reference indicator where nobody could help.

In case of complex questions I have feeling that I mostly didn't get good suggestions in LabVIEW forum.

 

 


Based on your replies in this thread I don't think you are really open to advice. State machines are a fundamental construct in programming. Not just LabVIEW but other languages as well. This is especially true in the embedded world. You ask for ways to improve your code and basically reject all the advice given. Instead of looking at the simplified state machine example posted here and seeing how it can be used to improve your code you actively tried to explain to everyone why state machines are not useful. Something that is very far from the truth. Virtually every top level VI I write is a state machine. I also don't use local variables. I work on some very large projects as well. The people giving you advice are very experienced LabVIEW programmers. Rather than argue against the advice given you try to be open to it and see how it can improve your coding.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 16 of 39
(958 Views)
0 Kudos
Message 17 of 39
(945 Views)

Actually I have one more thing to add: 

 

If you think a State Machine is hard to understand or troubleshoot. Just wait until you are handed some code that looks like this and told to fix it.

 

JustWrongCapture.PNG

 

I literally had to spread this stupid block diagram out over THREE 27 inch monitors to see it all and screen capture it.

 

THIS is the direction you are heading and THIS is why we are trying to tell you now that you are doing it wrong.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 18 of 39
(942 Views)

i use shift register as minimum as possible. to reach that i use object-oriented approach (but LabVIEW object-oriented vi). of course, i use clusters but again those are trade!

 

examples of good usages of shift registers and clusters 😋

Vasilich2004_0-1632949168266.png

 

 

0 Kudos
Message 19 of 39
(936 Views)

So now you are saying that clusters and shift registers are bad. What part of the language is acceptable to you?



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 20 of 39
(931 Views)