LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help avoiding sequences (possibly causing serial write bug)

I have written a program in LabView 2014 that steps a delay line motor forward (through serial commands), and then records a spectrum from an ocean optics spectrometer (HR2000) using the ocean optics subVIs. The issue is that the stepping and recording occur within the same for-loop and I am seeing the serial delay line step several times before the spectrometer records 1 spectrum i.e. the spectrometer will only record nonzero data every 2 or 3 serial commands even though there is 1 serial write and 1 spectrum grab within the for loop.

 

After doing a little research it seems that I have committed a sin by using flat sequences. So two questions: 1) Is my bug being caused by the usage of the flat sequences (and if not what is causing it). 2) How should I rewrite my code using best coding practices (avoiding the usage of sequences).

0 Kudos
Message 1 of 6
(2,243 Views)

First of all, no, the sequence file isn't causing the issue.  You didn't include the critical subVI that needs to be loaded, so I'm making a (probably correct) guess that the ? code inside the FOR loop contains the code to grab the data, and that it ignores any attempts to do another grab while one is already in progress, returning no data.

 

Now, while the sequence structure isn't causing the issue itself, it's the issue of not understanding why it isn't necessary that IS (partly) causing your issue!  Read up on Dataflow Basics.  The main takeaway from that tutorial is: Remember that a node executes only when data is available at all of its input terminals and supplies data to the output terminals only when the node finishes execution.  Your issue is very well described in figure 2 of that tutorial.  The way you have it set up, you cannot know whether the serial write or the data grab will happen first.  If you can grasp that one italicized sentence above and be able to apply it ruthlessly to your code, you will see why you shouldn't need any sequence structures ever.  (Technically, if the only way to enforce dataflow is by using a sequence structure, you can always stick that code in a subVI instead, although most LV developers would not frown upon you if you were "lazy" and didn't do that.)

 

So, what you should have learned from the tutorial is that you have to wire your data grabber subVI such that, by applying that sentence above, it is forced to execute AFTER the serial write.

 

The other issues, not related to dataflow, is that you have to figure out how to a) stop the grabber from grabbing before the motor has had a chance to complete its mission (bet you didn't even know you could have that issue, but whatever it is that the motor is supposed to do, it's not going to happen instantaneously), and b) make the grabber subVI wait until the grab is complete before exiting.

 

Hope that helps.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 6
(2,220 Views)

Thanks for your response! This makes sense and I think I can see how to wire things up differently so that I can do away with the sequence structures just by more strategically placing my wires.

 

One more follow up: Will the for-loop move on to the next iteration before the spectrum grabber finishes? I was under the impression that the for-loop shouldn't advance until everything inside it has finished. If that is the case, then there should never be a situation where the grabber would need to ignore another grab because it should already have finished.

Message 3 of 6
(2,200 Views)

@ljh48332 wrote:

Thanks for your response! This makes sense and I think I can see how to wire things up differently so that I can do away with the sequence structures just by more strategically placing my wires.

 

One more follow up: Will the for-loop move on to the next iteration before the spectrum grabber finishes? I was under the impression that the for-loop shouldn't advance until everything inside it has finished. If that is the case, then there should never be a situation where the grabber would need to ignore another grab because it should already have finished.


Your impression is correct - that's dataflow!  I did so well explaining dataflow, then didn't follow it.  So now you know how to make the grab happen after the motor command.  Now you have to figure out how to make it happen after the motor COMPLETES its command.  It could mean a hard-coded wait in between the two, but that is inelegant, not very robust, and usually implies an incomplete understanding of how to communicate with the thing on the other end.  See if there is some kind of feedback from the motor that tells you when it has completed its task, or if you can somehow query the motor to see whether it has completed its task or not.  If there isn't, you may have to go with the hard-coded wait.

 

Oh and I gave you kudos for showing that you have taken seriously the task to understand dataflow!

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 6
(2,194 Views)

The Flat Sequence structure itself is not the sin you committed here.

 

No, your sin is not using a proper program architecture. I understand you are new to this and National Instruments has done you a disservice by making it look like your programs should all be OBL (One Big Loop) architectures.

 

OBL works for the tiny little demo and tutorials but in the real world it rarely works, and when it does if left unchecked, you end up with something like this.

OBL.PNG

(I could not even see the whole thing when spread across two monitors)

 

I suggest you look closely at the simple state machine architecture for this project.

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 5 of 6
(2,175 Views)

Everything in the For loop must complete before it will execute the next iteration of the For loop.



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 6 of 6
(2,170 Views)