05-22-2019 04:50 AM
Hi everyone,
I'm new to Labview and I would like your opinion on my VI, to improve myself and better understand how Labview works.
I use a distance sensor (a led + a photodiode) that I first need to calibrate at two distances (0mm and 0.5mm). I use Labview NXG to acquire voltage across a shunt resistor. This calibration is then used to process the distance between the photodiode and my object.
I used an event loop to retrieve the high and low value and "store" these values with the help of shift registers. Once the calibration is done, the third event structure continuously acquires new data and process it to display the distance. on a chart.
What can I improve ? Did I make any mistake ?
Thanks for your help.
05-22-2019 07:24 AM
Does it work?
Your outer loop does not have any purpose and your stop terminal belongs into the inner loop (and have an event for the stop button)
(You don't seem to understand dataflow yet. Once the inner loop executes, the inner stop terminal will only see what the terminal was when the inner loop started, and will probably never stop, even if you press the stop button later. For the stop terminal to be read during the inner loop, it needs to be in the inner loop. Similarly, in the "start measurement" event, you are entering a while loop that can never complete, permanently trapping the code and preventing the event case to ever complete. Not good!)
05-23-2019 01:58 AM - edited 05-23-2019 01:59 AM
Hi altenbach and thanks for your reply.
The VI works, except for the stop button. I modified my VI on your advice and the stop button now works. Is it better ?
I will now take care to always provide an escape condition for my while loops.
05-23-2019 03:50 AM
Dear Taratataaa,
I see your concept, it makes sense and in principle it should work now.
The problem is with robustness: if the user forgets to click the calibrate button first, and starts the measurement instead,
you have an unintended situation, expectedly a division by 0 in the inner while loop. Stopping both loops with one stop button is not elegant either.
My proposal is to use the event based state machine design pattern for this task. Possible states: Init (disabling the start button and calibrate high button, creating main measurement task)), Wait for User Action (event structure included in this case), Calibrate Low (enabling Cal. High and Disabling Cal. Low), Calibrate High (disabling Cal. High and enabling Start), Measurement Running (only Start and Stop buttons are enabled), Stop State (clearing the main measurement task).
At the DAQmx Timing VI set "Finite Samples", so you do not need the nested while loop.
The state machine template you will find in LabVIEW under "Project>>Create Project".
Best Regards,
NI TSE
05-23-2019
03:51 AM
- last edited on
05-13-2025
01:30 PM
by
Content Cleaner
05-23-2019 07:46 AM
Hi Akos_43,
Thanks for the advice and the tutorial link, I'm implementing a state machine to make the VI more robust. I have 2 questions :
- how do you hide a button regarding of the current state as you suggested ?
- I created the acquire task in the Start state as suggested, but I have issue by doing this way. I have an error in the High Calibration state, I don't understand why.
Is it best to create a task and terminate it in the same state (so 3 different task for low cal, high cal and run states) or to create one task for all the FSM in the start state and use the same all the time until the stop state, where I terminate it ?
05-23-2019 08:19 AM
Hi Taratataaa,
to answer your questions:
"how do you hide a button regarding of the current state as you suggested ?": Only possible in LabVIEW, not in DAQExpress unfortunately. Right Click on the button and Create>>Proberty Node, and use the "Disabled" property.
"- I created the acquire task in the Start state as suggested, but I have issue by doing this way. I have an error in the High Calibration state, I don't understand why." What kind of error do you have here exactly?
"Is it best to create a task and terminate it in the same state (so 3 different task for low cal, high cal and run states) or to create one task for all the FSM in the start state and use the same all the time until the stop state, where I terminate it ?" Both way should work.
I see a problem in your code: you start the task in all the three cases (Hi Cal, Lo Cal, Run), but do not stop it there. Either start the task in the Start state only, or stop them in each case where you start it instead. Main principle is, that one shouldn't/cannot start an already running task.
Best Regards,
NI TSE
05-24-2019 08:39 AM
Thanks for your answers.
As it is now working, I now want to implement a 2-output PWM generator + write on a digital output. I have modified an example for PWM I found online but I'm unable to make it work for two separated channels. How do I specify this ? Should I create a DAQtask for the PWM and another for the digital write ? What's the philosophy behind the DAQ task ? Is it "a hardware unit = a task" or "an IO type = a task" ?
Thanks for your help !
05-24-2019 08:55 AM - edited 05-24-2019 08:55 AM
Here is what I did : 2 counter tasks (one for each counter of my USB-6215) using one different counter each, and one digital output task. Is this efficient ? Is there a way to merge the 2 counter tasks ?
Thank you.
05-24-2019 09:01 AM
Hi Taratataaa,
you need to make a reference of the button in NXG, and connect it to a Property Node.
(Ctrl-Space, type property node). Then select the Disabled property.
I am glad I could help.
Best Regards,
NI TSE