Tip:
Highlight text to annotate it
X
In our last episode we took a look at implementing error handling in our state machine and we
asked a question at the end: if, for instance, I’m going to notify the user of whether
a test passed, how does the notify VI know which test passed? It may be values for the
test if the tests occur in other states. In other words, how do we get data between states?
Within a state it’s pretty easy, we just pass the data to whatever handles it, but
getting it from this state over to this state will require passing that data between loop
iterations. Are your ears perking up? Because that’s what a shift register is for and
that’s what we’re going to use. In this case let’s say I want to pass the data regarding
the basic test from this state to the notify state. Well I’ll right click and create
a shift register. Now what should I put in here? Well this “passed” is a boolean,
so I could go and wire this boolean into here, but then what about the data for the advanced
test? I can’t use the same shift register because that’ll be a different value so
I’d have to create another one and then what if I also wanted the actual values for
the test? Maybe some voltage values, maybe for both tests. Well now then I have four
different shift registers sitting here and pulling them off, and as you can imagine as
my application scales I’d have a bunch of these, that’s inconvenient. So what I really
want is one shift register that holds all of those and so we’ll keep them in a cluster.
We’ll go back here, get rid of this and instead the data is going to more closely
reflect what’s in here.
Now right here we’ve kept the data pretty simple: numeric and boolean and we haven’t
even been terribly consistent about the labels, so let’s change that. What we really want
is an instance of a type definition in all cases, so I’ll right click on the border
and make this a type def, and open up the type def, save it, keep the same prefix and
choose “data.” Let’s make this a little prettier, expand this a bit. Let’s say the
pieces of data we want are this: four simple pieces, this voltage for each test and then
whether that test passed or failed. So we’ll call this “basic voltage” and this “basic
passed.” A line across the top and make a copy. Ctrl and shift, keeps them on the
same axis and we say “advanced voltage” and “advanced passed,” save that and we’ll
look at the order of the controls 0, 1, 2, 3, that looks good to me. So now this is an
instance of the type def and it got a little bigger so we’ll move it over a bit here,
rearrange some things. This also should be an instance of the type def. We don’t want
to make a new one of course we’re just going to right click on it and replace it with the
one we just made that way it’s still connected up, it’s still in the block diagram, much
easier. Pull this over here, line these up quickly, same here, gorgeous. Wait, not yet
gorgeous, now, okay. So I’ll close that out, this now is an instance of that type
def and of course we want it going into here, there we go, and now this will be an instance
of the type def too. So I can create a constant at the beginning of runtime in order to re-initialize
the values in that cluster, very nice. I can even put a label here, it’s good programming
practice, we’ll just change it to “data” and make it a little smaller, we don’t need
to see all of it and now any other cases that need to access that, we’ll just pull it
from there.
Now I’ll obviously need to go to the “advanced” and do the same thing as I did before. I’d
pretty it up but in the light of time, that’s good enough. In we go, and out and finally
the notify state, same thing. Obviously we still have this hollow tunnel so we’ll have
to decide what to do in the other cases like “initialize” and “idle.” Well in “idle”
we won’t have to worry because we’re not changing that data or even hauling it anywhere
so I’ll just pass it directly through. So it’s in “initialize” or “error shutdown.”
Both of these use the un-type def data so I won’t make you sit through that, I’ll
go ahead and change that right now. Okay all done, all of these now have the updated type
defs. So I’ll wire them in and out in all cases. Solid tunnel here means we have all
cases taken care of.