I am very new on Labiew. I made a vi as attached to drive a syringe pump and rotary valve, and it worked fine. what it does is to send command each time I click 'pump' or 'valve' button.
But now, I want to run a series of commands automatically, for instance, command #1 to pump, #2 to valve, #3 to pump, #4 to pump, etc. total number of commands will be around 10. I wonder what is the best ways to realize it.
Thanks a lot
Solved! Go to Solution.
Make a cluster (a typedef) that for instance contains info on time and type of command (I supposed you would want to time the different commands)
Make an array of these "command clusters" (or a queue, read up on QMH) and feed it to a state machine that issues the commands at the right time.
If you are new to any of these concepts, read up on the free documentation before you start:
By the way, you do not need the outer while loop. Instead, make the stop button a case in your event structure and wire the stop terminal to be true only in the stop button case.
You can do several things:
1. Make all the code in the "Pump" event case into a SubVI. Do the same for all the code in the "Valve" event case. This can be done quickly by highlighting all the code and selecting from the menu Edit>>Create SubVI. If the connector pane has been set up to your liking, in a new VI, you can now string together these SubVIs, but still have a VI with an interactive UI as before.
2. Use the functions in the Events pallette to create user events. User events allow you to programmatically trigger (without UI intervention) cases within the event structure. With this in mind, the same event case could be configured for a UI event and a user event. Equipped with the user event refnum, you can then "fire" off the event by calling the Generate User Event function. Consult the LabVIEW documentation for information on user events in the event structure.
3. Follow the Queued State Machine design pattern as suggested by peterhult, which is probably the preferred method by seasoned LabVIEW folks.
Thank you very much for you reply.
I was able to build a state machine and queue the command, but am currently having trouble to excute the command at right time. Basically I could easily miss a command.
I wonder if I could get help from you on this regard.
In your Queued State Machine, in addition to enqueuing the state to execute, you can also include the time to wait before moving on to the next state.
This can be done by changing the data type of your queue to a cluster that includes a numeric that represents the wait time. When you enqueue, bundle the state's name and time. On dequeue, unbundle.
If your code is written in a sequential manner rather than in parallel loops, you can use wires through shift registers to pass the wait time between states.
Alternatively, you can create a state machine case that is dedicated only to wait. Between enqueues of commands, enqueue this "Wait" state and its delay value.
Thank you very much for your reply.
I am trying to figure out the first method.
When you say string together the subVIs do you mean use a flat sequence to lay out all the command?
I usually do something like this.
It's always a good idea to post the latest version of your VI with every new question you have so we can make suggestions.
Using a flat sequence structure is frowned upon nowadays.
You'll want to have an error-in and error-out cluster on the front panels of your SubVIs, so that the error cluster flows from one VI to the next. This enforces the dataflow execution sequence of the code in addition to picking up any errors that occur along the way. These error clusters usually are on the bottom left and bottom right of the connector pane.
Look at the way NI's functions are built for guidance. The File I/O functions and example code is a good starting place.
Here is my latest version using state machine. I could not get the timing right...
basically, the next command started before the previous command finish, or several command was simply skiped...