LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

hardware automation sequencing (switch-function inside while-loop)

Hello,

 

as an experienced C programmer, I have been somewhat puzzled in sequencing functions in LabVIEW for quite some time. 

 

 

Here's a pseudo C code logic

//header

function one(var x);

function two(var y);

function three(var z);

Variable A;

 

void main(){

while(true){ //ALL active hardware runs within this while

if(button=true){

one(A);

if(one.Complete=true){

two(A);

if(two.Complete=true){

three(A); button=false;

}}}}}//

 

function one(var B){

B=A.doSomething();

Boolean Complete;

}

function two~~three~~

 

 

The point is, that I want to have multiple functions which can be called from the main while loop. However, I found that the function MUST be built within the while loop in order to be interactive, which means it is operated by a switch and the end of the switch sequence is turning off the switch. Also, I cannot make the individual function to be a sub-vi, because it must handle global variable(s) which also must be handled in proper sequence.

 

I want to run 3 co-dependent functions,  A, B then C, in order. Could someone show me the "proper" way to do it please?

0 Kudos
Message 1 of 14
(4,666 Views)

Try to "forget" C programming a little bit when you deal with LabVIEW. I have seen codes from many collegues (they have lots of text based programming language background), when they tried to implement their idea in LabVIEW by using C "thinking". The end was always a disaster, full of local and global variables (did I mention, I think I did not even need to use global variables in my last ~3 years of LabVIEW programming? 🙂 ), un-abortable long lasting flat sequence structures (do NOT use sequence structures), undebuggable code, etc... I am a bit worried about you mention you use global variables...reason? (there are certain valid usage conditions, but often they are undesirable...)

 

As I see you are in a similar vulnarable phase during your LabVIEW learning curve, so I would like to bring your attention to some useful documents to read and understand:

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

 

What you describe as an algorithm, could be easily done using State Machine(s) in LabVIEW. Also you need to learn and understand different communication and synchronisation tools in LabVIEW, like Notifiers, Queues, Event Structures (Dynamic User Events), etc...

Here is a very simple example (not really used in this form, but helps to understand the idea behind):

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

There are many different implementations, google for state machine expr. Also, you can check the one shipped with LabVIEW: go to "create new project", and chose "Simple State Machine".

 

EDIT: reading again your pseudo-code, I could imagine something like an Event structure driven pattern. You would have a main While loop where you capture user commands, and you could initiate (or even abort on the fly, etc...) the 3 parallel sequences which are 3 other While loops including state machine logic (shift register). You can use Queues or Dynamic User Events to communicate between the 4 While loops. In case of User Events, I would put the 3 sub functions into subVIs (try to avoid using more than 1 Event structure in "one block diagram").

 

Edit2:

if you can show a little bit more details, like the required steps and HW functions (what kind of interface? RS232, GPIB, ...?) for one single sequence, I might have time later to put together an example for you...

0 Kudos
Message 2 of 14
(4,634 Views)

Hi SJLPHI,

 

in addition to Blokk's comment:

 

Your code with some indentations:

while(true){ //ALL active hardware runs within this while
  if(button=true){
    one(A);
  if(one.Complete=true){
    two(A);
  if(two.Complete=true){
    three(A); button=false;
}}}}}//

Let's pick parts from your code:

while(true){ //ALL active hardware runs within this while
}

That's a simple WHILE loop set to run forever…

 

if(button=true){
}

That's a simple case structure with a button connected to the case selector.

 

one(A);
if(one.Complete=true){
  two(A);
}

You call a function aka subVI with a parameter A. Then you check the output of that subVI: why do you do this when the function ALWAYS returns the "Complete" condition??? In LabVIEW you don't need to check that parameter as DATAFLOW (using error wires) will enforce the correct order of calling your subVIs!

 

button=false;

Here you reset the button to FALSE.

In LabVIEW you would set the button switch mode to "Latch…" so you don't need any extra code to reset the button!

 

I have been somewhat puzzled in sequencing functions in LabVIEW for quite some time.

That's the main point of your post and it is answered by THINK DATAFLOW!

Read the LabVIEW help an the term "DATAFLOW", go through the basics examples and beginner courses on NI website!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 14
(4,623 Views)

Yep, read as above, I gave a bit too much info 🙂 
We should examine the actual functions which you need to implement. Anyway, the best way if you start to put together your VI(s), and show them here (snippet or attached VI, not screenshot pls!). So we can help with the design, and also we will see better what is your goal here...

0 Kudos
Message 4 of 14
(4,616 Views)

Hi SJLPHI,

 

Also, I cannot make the individual function to be a sub-vi, because it must handle global variable(s) which also must be handled in proper sequence. I want to run 3 co-dependent functions,  A, B then C, in order. Could someone show me the "proper" way to do it please?

So there are 3 functions, which must be called in a certain order (one after the other).

But these 3 function are "co-dependent"? Why should there be a problem, when subVIs are called in a certain order, but still have access to global variables?

 

You really need to explain your problem with more details!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 14
(4,610 Views)

GerdW,

 

Yes, the "IF statements" are replaced by case structures. The switch responsible fore the case structure is set to "switch when pressed". Basically each case structure is set to turn itself off once its purpose has been fulfilled. I disable "Latch" operation, because I cannot set a latch to be true/false.

 

Also, this is what's going on. Inside the while loop, Say function A handles signal a, and function B handles signal b. As a is being incremented, it takes something away from b, and as b is being incremented vise versa, and at each cycle of the while loop, there is a signal c, which calculates the difference betwen a and b then updates status.

 

In real example,

 

c would be "Mask byte"

11110000, the last 4 bytes signify what the hardware should do

b would be "Direction singal"

Mask byte Or 11111010, setting the 2nd and 4th bit to be 1 if not alredy one.

a would be "pulse"

Mask byte Or 11110101

 

At the end of sequence

Mask byte and 11110000 to reset the whole thing.

 

So Mask byte is being tossed around here, it can be global variable yes, but there is a function called tracer.

Tracer checks the number of pulse at which direction that mask byte was used, and logs it to a graph. Which means, that it has to "lively" log individual data from each function.

 

As my VI got larger, now I use the above function to set how many pulses at which direction to take in order to get where I wish to be.

0 Kudos
Message 6 of 14
(4,595 Views)

Hi SJLPHI,

 

your description reminds me of a state machine.

Do you know how state machines are done in LabVIEW?

 

Hey, wait: didn't Blokk mantion state machines in the morning? Did you even consider them by now?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 14
(4,576 Views)

I will give that a go, and return here.

 

It's been fruastrating because I spent so far 2 months trying to get this done:

 

if A<=5

and B is pressed

if I ask the machine to go

1->2->3

instead of

1->3

 

the machine would either go to 1->2->2+->2++, basically it's a safety measure and 2 is only a higher value than 1

or it would simply ignore 2 and go from 1 to 3.

 

-SJL

0 Kudos
Message 8 of 14
(4,562 Views)

Do you have active SSP or Academic site licence, etc? If so, you can access the online training material from NI, called Core 1-2-3, etc...

It would really help you to familiarize yourself with different basic LV concepts...

0 Kudos
Message 9 of 14
(4,553 Views)

I'm not familiar with either,

 

I am a full time university student, and one of the LabView was registered under my e-mail account over the summer.

 

I'd appreciate proper training material, if I had the time.

 

-SJL

0 Kudos
Message 10 of 14
(4,538 Views)