From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stopping state machine, and questions about JKI State Machine

Solved!
Go to solution

I am almost done building my first state machine for some calibration processes. I am sure there has to be an easy way to do this but I cannot figure it out... I would like to be able to stop the state machine with a stop button in case the user want's to abort the calibration. I tried to search for a way to do this, but am not finding much. 

 

I did however find several well known members telling people to review the simple state machine example and/or the JKI state machine. Well, I reviewed the simple and downloaded/installed the JKI state machine. I'll get back to that in a minute. 

 

My issue with just putting a stop button in the while loop to stop its execution is that some of my calibration VI's run for quite a while. So, even if I put a stop button, I might have to wait for the current state to finish running the couple hundred iterations of reading sensor data. I would like to find a way to hit the stop button and end execution immediately, not 3-5 minutes later. 

 

I am using the basic simple state machine concept of a while loop with a case structure inside. My VIs are in each case/state. The VIs have stop buttons wired to their connector pane to stop their execution immediately, but since they are in the case statement they don't receive the stop until after the case has completed and moving to the next state, minutes later. Is there an interrupt or something that I can use?

 

In my searching I came across the JKI state machine. It seems way over my head, but I would like to learn more about it. What are the advantages of the JKI state machine over the simple state machine I learned about in core1 and am using now? Are there any good examples out there that I could download to look at, or maybe someone has one they could post? The github file listed on the JKI website has an examples folder as well as a few other VI examples but they are broken missing files and cannot be executed. Has anyone taken JKI state machine course, and is it worth it? 

0 Kudos
Message 1 of 9
(2,524 Views)

It is so much easier for us to understand your issues when you attach all of your relevant code.  I assume have all of the relevant VIs in a single folder (particularly if you are using LabVIEW Project).  Compress that folder and attach the resulting .zip file.  I almost guarantee helpful responses.

 

Bob Schor

0 Kudos
Message 2 of 9
(2,511 Views)
Solution
Accepted by topic author TheWolfmansBrother

So your problem is that your calibration VI's are blocking. There are a few ways to abort a subVI externally, but they're not good options (it's more of a "yank the power cord" option).

 

You will need to rewrite them to have an external communication channel. You can do this with channel wires, user events, queues, notifiers, and a few other ways.

 

I'd suggest just using a notifier. Create a notifier with a Boolean constant as its type. On your calibration front panel, add a control for that notifier's reference. Code your internal VI so that inside its loop it's checking for a notification received on that reference. The main VI will then need to send a Notification on this reference to the subVI to tell it to Abort. Unfortunately you can't do this with your current structure of a single loop.

 

In your main VI, you will need to break up your code into a GUI loop and a "Do stuff" loop. There's a great example of this in regular LabVIEW- click Create Project then the Queued Message Handler. It's a good place to start learning about communication between multiple loops.

Message 3 of 9
(2,481 Views)

@ 

 

My basic Vi is a bit different, but more or less the same. I take a certain
number of samples from a sensor reading (the rand in this case), there are
multiple different data types in my real VI. Stability is checked with StdD.
Theres some other code not necessary that ensures the 100 point sample is
stable. The while loop finishes once the 100 point sample is stable, max
number of iterations is reached, or the stop button is pushed.

The states that i have (the meat and bones) in my machine:
1. check stability
2. read sensor to be calibrated and a standard calibrated sensor create averages
of stable readings for calibration points/verification
3. perfrom calibartion calculations with average stable points
4. apply calibration
5. compare standard sensor to uut check that reading deviations are within
tolerance
6 end

theres other states that initialize,clear values, set up parameters in the
sensors, let you know that fails occured, ask if you want to retry,
but there is really no need for all that for the demonstartion and my
original question on how to stop the machine while its in a lengthy
while loop

 

 

 

0 Kudos
Message 4 of 9
(2,415 Views)

idk  why my last message was marked as spam and taken down....

 

I made a fake state machine, I am unable to post my real project for IP reasons. The fake one is enough to give a basic demonstration of how the real one works. The real one is larger with more complex states but the fake one should suffice to help answer my question. 

 

So the notifier I will have to find some examples for. 

"Create a notifier with a Boolean constant as its type" this is in the state machine?

 

I'm looking at that Queued message handler its likely going to take me a bit to wrap my head around. 

0 Kudos
Message 5 of 9
(2,427 Views)

It's a little messy, but I was able to achieve what I wanted with the notifiers and the second loop. I only implemented it in the fake program for now. I think I probably did it a little differently than you were trying to describe. I'm attaching the updated working solution if you or anyone else wants to critique. 

 

Thanks for the help!

0 Kudos
Message 6 of 9
(2,396 Views)

Good on you for uploading minimum viable sample code! Lots of people won't do that 🙂

 

So a couple critiques, but you're almost there with the notifier.

 

1- Don't hide labels on your diagram terminals, it makes it much harder to read

2- You use an event structure to start the thing... why do you poll the Stop button? Use another Event Structure 🙂

3- Notifier wires are "by reference" which means you don't have to "chain" them. Anything operating on a notifier reference works on all notifiers branched from that wire.

4- In your "fake loop" you set the Timeout value for the notifier to -1, which means it waits *forever* for a notification. Change that to 0 for it to check once, then move on if there isn't one.

5- You don't need a second Wait on Notification in your main loop.

6- The Time Delay can just go anywhere in the fake stability loop, I wouldn't recommend placing it inline with anything else.

 

I made those changes and cleaned up the code a bit. I didn't look through your state machine logic and try to run the code thoroughly... I don't have a ton of time right now to deep dive into it, but this should get you started. Take a look at the attached version and see if it makes sense.

Message 7 of 9
(2,383 Views)

I will try to implement the suggested changes. Unfortunately, I am on LabVIEW 17, and the project you attached is on a newer version...

0 Kudos
Message 8 of 9
(2,332 Views)

Sorry about that. Version 17 files attached.

0 Kudos
Message 9 of 9
(2,283 Views)