From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Water tank valve control with Plots and Twists

Solved!
Go to solution

Greetings everyone!

 

This is my frist real project. And I'm loving this program so far!
I do have a bit of a problem. Here it goes:

The university project that was given to us is to simulate a water tank filling(Input valve OPEN/CLOSED) and empyting(Output valve range from 0-100%), but with a few twists. It's done by controling the flow by the formula : flow[%] = valve position[%]*current water height[%].(The output valve takes values in percent from 0 to 100% giving digital output voltage of 0 volts for closed(0%) or 5 vols for open(100%), the strange part is the digital output  can have 0 or 1 value so the valve position is "simulated". And the valve feeding the water(input valve) can be open or closed(so 0 or 5 volts)

-Sequences cannot be used(Only the one for initialization/cleanup can be used)

-When the water level reaches 10% of max height, the LED turns on, the output valve closes, and the input valve opens. The LED is on untill the button is pressed. The output valve can't be open untill the water height is greater than 50%.

-When the water level reaches 90% of max height, the LED turns onthe input valve opens. The LED is on untill the button is pressed. The output valve can't be open untill the water height is less than 50%.

-There are two mode selectios:

      Control by hand: The person sets the valve position in % and the flow is calculated by the formula and displayed on the screen.

      Automated: At 2 second intervals the program sets the valve position  so the flow is constant that is set by the person.

-(Haven't gottern here yet)When the ESD(Emergency shutdown switch) is pressed both the input and the output valves are closed. The input valve can't be oppened untill the RESET_IN button is pressed. If the program is in "Control by hand mode" the output valve can't be opened untill the RESET_OUT button is pressed. In case the output valve is in "Automated" mode, it won't start working untill the RESET_OUT button is pressed.

 

My main problem here is the fact i can't use sequences, and nested while loops won't work. The data aqusition stops untill the inner loop completes and i need both working. Especially for the 50% rule.

I've been bashing my head for a few days and still couldn't come up with a solution.

I've attached the VI.

Any tips are more than welcome!

 

For now analog input/digital outputs are a simulation(When the time comes the USB 6008 hardware will be used).

0 Kudos
Message 1 of 6
(4,784 Views)
Solution
Accepted by topic author TheSeeker92

There are things there that confuse me (like whether the valves are percentage, 0% to 100%, or digital, 0 or 1), but never mind, that's a detail.

 

Consider the task, and consider what you want to do, and what "states" the system can be in.  [Do this with pencil and paper, don't open LabVIEW yet ...].  Your problem description tells you something about transitioning between one state and another.  So here's a thought exercise:

 

Suppose you are in some "state".  Let the system "do its thing" for a second, which might involve some water flow.  At the end of the second, re-evaluate your state -- did it change?  Keep doing this ...

 

This model of "doing something" is called a State Machine, and LabVIEW has a number of very good examples and tutorials on creating and using them.  The important things to do before doing your coding is to (with pencil and paper) decide on your States, decide on the transition rules, and then you are ready to code.

 

There are various ways to build a State Machine in LabVIEW, but the basic idea is a While loop (to keep things moving along, a Shift Register that holds the current State, usually represented as an Enumerated Type whose values are simply the names of your States (makes for a very mnemonic routine), and a Case Statement whose input is the State from the Shift Register that contains the code for carrying out that particular State, including implementing any Transition Rules (which can result in a new State being put on the Shift Register as it leaves the Case Statement).

 

A great exercise.  Strive to keep your Block Diagram on a single laptop screen by making use of sub-VIs to do drudgery (like handling the changing water level based on the current water level and the "states" (here meaning "values") of the two valves).  Once you understand the State Machine "Idea", you should be able to write the code for this problem rather easily.

 

Bob Schor

 

 

Message 2 of 6
(4,764 Views)

First off, i would like to thank you so much!!! I couldn't have anticipated a more elaborate and a better written answer. 

 

About the valves: They are represented as digital outputs, on a USB 6008 board they're wired to a 220ohm resistor and a diode(ON(5V) is valve open and OFF(0) is valve closed). But the second valve, the output valve, can take a range from 0% to 100%, as in - it can be 50% open or 30% open(And based on that, the flow is calculated), but that is only used with the simulation, the hardware itself only cares if its open or closed. As far as i understood(and i hope i understood right), because the digital outs can't have other values than 0 or 5V, and it's stated that the valves must be wired to digital outputs.

 

So the idea is i should scrape my current design and start looking up the State Machine concept, visualize it, make a concept on paper and after its all clear i shoul put it to work in labview?

 

One more queston, are there any functions, variables(we haven't done local variables, but it seems to me they should be used here) i should take a look at for this specific problem? 

 

Oh, and one more, how can i take samples from DAQ read? Should i put it in every case or is there a way to have only one feeding the signal? Because it's obvious i can't have nested while loops, and if i make two, the signal flow is interrupted.

 

Once again, thank you so much!

0 Kudos
Message 3 of 6
(4,751 Views)

 

So the idea is i should scrape my current design and start looking up the State Machine concept, visualize it, make a concept on paper and after its all clear i shoul put it to work in labview?

 

Yes, that's my suggestion.  Look at "State Machine Fundamentals" in the Examples (Help, Find Examples) to get started.

 

One more queston, are there any functions, variables(we haven't done local variables, but it seems to me they should be used here) i should take a look at for this specific problem? 

 

Since you are just getting started, here is a good "rule" for now -- never use local variables.  That may sound strange, as they do have their place, but they are probably one of the more "abused/misused" items in LabVIEW.  What you should pay attention to is the use of Shift Registers (as local memory) in While loops.  Do you see how you could use a Shift Register to create a Case block that would execute only when a Boolean control went from True to False?

 

Oh, and one more, how can i take samples from DAQ read? Should i put it in every case or is there a way to have only one feeding the signal? Because it's obvious i can't have nested while loops, and if i make two, the signal flow is interrupted.

 

When you are ready to program your I/O, read this excellent NI White Paper on DAQmx.  Note that you will have the initialization "stuff" before entering your While loop, the acquisition stuff inside the While loop, and the closing stuff after exiting the While loop.

 

And while you are correct that you can't, here, have nested While loops, you should shortly learn that one of LabVIEW's most powerful features is to have parallel While loops, which actually execute in parallel, simultaneously (by "NI Magic").

 

Once again, thank you so much!

 

Pleased to be of service.


See comments inserted, above.

 

Bob Schor

0 Kudos
Message 4 of 6
(4,728 Views)

Greetings again!

I hope i can post even after i checked the soulution(It put me on the right track so it is the solution).

I'm almost done thanks to your help Bob, i just have a question.
I'm having trouble making the diodes and the button work(The last requrement and the on button press disable the warning). The explanation is in the post describing the problem. I've tried a few things but i just can't wrap my head around it.
I've attached the vi and I've made ctl's for the enumerates so I'll attach a zip.

0 Kudos
Message 5 of 6
(4,638 Views)

I'm not sure I can figure out what you are trying to do, but here are some suggestions (in no particular order):

  • The two "OK" buttons on the Front Panel have no labels!  I didn't know that it was possible to create an unlabeled Control, but it is certainly a very bad idea.  For one thing, you can't tell the two Controls apart on the Block Diagram!  Give every control (including Enums!) a label (LabVIEW does this for you automatically, but you'll always want to replace the default LabVIEW name with a "meaningful" name, like "Low Warning Enable", or "Level State" (for the Enum).
  • Try simplifying your "range" logic.  The top Case, which seems to be determining "Level State" based on the Tank value and the previous Level State, is not very easy to follow.  In many cases, I find a Case Statement easier to quickly understand than a Select, particular than two Selects.  The High and Low cases are really easy -- if it crosses 50, then output Normal, else output what was there before.  The "Normal" case is a little trickier.  Here I used a "In Range and Coerce" function to test if it is Normal (between 10 and 90).  If not, then if it is greater than or equal to 90, that's the answer, otherwise it must be less than or equal to 10.  See the snippet.

Tank 1.png

  • From looking at your code, it is not intuitive what the lower loop is doing.  Better labeling (what is s? t:f coming from?) would help.
  • Is this loop supposed to be "clocked"?  I see a 2 second elapsed time function inside the lower Case, a 50 msec Wait inside the outer loop, an some DAQ Assistants whose purpose I don't know (and don't know if they also contain a clock).  Having two timers, particularly if you don't need them, needlessly complicates things.  Try to get everything on the same clock.  Also strive for simplification of the logic for the lower loop.  You are clearly trying to constrain the value in the Shift Register (suggestion -- label the Wire, or put a Free Label near it to identify what the (blue) wire represents), maybe between something and 100, a perfect task for the In Range and Coerce (here you want to use Coerce).

My suspicion is that if you clean up your own code (do you know about the Block Diagram Cleanup?  The little Broom icon in the Tool Bar?  Hint -- before you use it, save your VI, just in case you don't like what the Cleanup does to your diagram ...) following some of my suggestions, you will be able to figure out what is going wrong, especially since you have a much better grasp of what you are trying to do than I.

 

Bob Schor

0 Kudos
Message 6 of 6
(4,608 Views)