LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stop the application when the desired variable is reached

Solved!
Go to solution

I'm working with MyRIO to move a linear motor and when I press STOP the motor will return to the initial position with high velocity. I want to change that and set a slower PWM so until it reaches that position it moves with a slow velocity. I have thought to create a case structure to check if the actual value is equal to the desired one and only if they are equal the while loop will stop, but I don't know how I can implement it. 

0 Kudos
Message 1 of 15
(1,206 Views)

Hi luna,

 


@luna30 wrote:

I'm working with MyRIO to move a linear motor and when I press STOP the motor will return to the initial position with high velocity. I want to change that and set a slower PWM so until it reaches that position it moves with a slow velocity. I have thought to create a case structure to check if the actual value is equal to the desired one and only if they are equal the while loop will stop, but I don't know how I can implement it. 


Which "STOP" do you use?

Do you use the red circle in the VI menu bar? Don't do that!

 

In general you can use a simple statemachine for tasks like yours: when there is an user action ("press a stop button") then the statemachine calls a certain state ("move stage slowly to initial position")…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 15
(1,179 Views)

Yes I actually use the stop button. How can I modify it?

0 Kudos
Message 3 of 15
(1,168 Views)

Hello, Luna.

 

     Thank you for attaching your VI.  Also, thank you (from both me and @GerdW) for using LabVIEW 2019, which we both can read!

 

     Some comments:

  1. Congratulations on using the Error line "almost everywhere" to serialize all the VIs and functions in your State Machine.
  2. The previous point (#1) means that you can (and absolutely should) get rid of the Framed Sequence structure.  Until you start programming the FPGA in your myRIO, you will probably never have a use for this awkward, visually-unappealing, and rarely useful structure (unless you never use the Error line properly).
  3. One disadvantage of the Framed Sequence is that it makes it too easy to trace wires that you haven't coded to run mostly horizontally, particularly when crossing boundaries (like the edges of While, Case, and For structures.  I was trying to figure out where the lowest Array input into the outer Frame was coming from, and realized it must have been "Waveform Volume".
  4. Try to keep your code "neat", "compact", and with wires that mostly run purely horizontally.  This can (and should) reduce the size of your Block Diagram so that you can fit it all on a Laptop screen and "see all the pieces" at the same time.
  5. Do you know about Event Structures?  You want to turn your Main Loop into a State Machine (which is basically a While Loop wrapped around a Case Structure, where the Case has "generic processing steps" like "Initialize", "Main Loop", and "Shut Down".  Above it, you put an Event Structure that holds the Front Panel "Do It Now" controls such as your Stop Button.  The While Loop (which is the State Machine) "decides what to do" by getting a series of "commands", usually from a Queue (or, if you are a Channel Wire enthusiast as I am, from a Messenger Channel) telling it what State to do next.
  6. LabVIEW ships with a Project Template for their Queued Message Handler which acts like a State Machine with an Event Structure, as mentioned above.  I made a simpler example using a Channel Message Handler that I posted on the Forums a while ago.  I'm going to look for it and will post it again, if I find it.

Bob Schor

Message 4 of 15
(1,146 Views)

Hi Luna,

 


@luna30 wrote:

Yes I actually use the stop button. How can I modify it?


I hope you mean that button labelled stop on the frontpanel of your VI…

 

When you want to move your stage "slowly" then you should implement a ramp on shutdown, starting from actual position down to "initial" position. Right now you simply set a fixed PWM duty upon stop…

 

Btw. I would suggest not to use event structures for your myRIO code. The myRIO runs headless, so it also has no frontpanel where a user can create user action events…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 15
(1,131 Views)

ok, if I use the ramp function, how do I link it to the stop button so that the movement of the motor can stop in the desired position? 

0 Kudos
Message 6 of 15
(1,121 Views)

Found it.  I'd posted it as a LabVIEW 2021 routine, but I've "Saved for Previous version" in LabVIEW 2019.  I forgot that it shows quite a lot of things that I might or might not have explained in my earlier posting, so I'll do a quick "walkthrough" here.  You do not need to implement all of the "features" here -- just get the Event Loop and the Queued (or Messenger-Channeled) State Machine implemented.

 

Here's a picture (I'll attach the Project as a Zipped file, unless I forget ...).

CMH Demo with Stream FP.png

 

This is the Front Panel of this routine.  It has a Cluster of Front Panel controls that includes a "Run/Pause" slider switch and a Stop Button.  It also contains a sub-Panel window that will be "filled" (as you can see) by a sub-VI that "does all the work and makes a nice display".

 

Channel Message Handler DemoChannel Message Handler Demo

 

This is the Block Diagram.  On top is the Event Structure, showing what happens when you change one of the Controls on "FP Controls" -- it "packages" the Control 's "New Value" and the State, "Get Controls", we want to run (and use the data we passed in).

 

The larger loop below it is the Channel Message Handler loop masquerading as a Channel State Machine.  The "pink pile" running around the Block Diagram is a Messenger Channel Wire ("pipe") carrying a Cluster (pink) -- anything put into the Pipe (done by "UTIL Next State" seen in the Event Loop) "instantaneously" appears inside the VI "Read State" that contains the Channel Messenger Reader, where it comes out as two wires, the top being the State and the bottom (a Variant) being whatever you wired to the second input of "Next State".

 

There's something that might be unfamiliar in the Initialize State, which shows me putting a reference to a sub-VI called "LOOP Show and Save" into a Front Panel object called a sub-Panel.  What this does is it displays the Front Panel of "LOOP Show and Save" (which produces the graph shown on the first Figure, above) whenever the Loop is running.

 

But where is the loop?  See the Orange (meaning, of course, Dbl data of some kind) flowing in a (Stream) Channel Wire to a sub-VI whose icon resembles a While Loop?  That's LOOP Show and Save.  When you open the attached Project and look, you'll see that this gets data from "somewhere" (it's manufactured in the "Run" State of the State Machine) and is sent to LOOP Show and Save by a Stream Channel (which is like a Queue on Steroids).

 

There are more things to discover here, including the Error Handler that's the final VI inside the State Machine. It basically "traps" any Error on the Error Line, calling the State called (naturally) "Error", which simply puts the contents of the Error Line entering the Error Handler on the "bottom" Error Line, and then calling the Exit State.

 

Exit does three things.  First, it wires "True" to the Stop control of the State Machine, stopping it.  Second, it sends a Stream Packet to LOOP Show and Save, with two flags set that tell "Show and Save that (a) it should ignore any data that it send, and (b) this is the "last element" it will be sending, allowing LOOP Show and Save to exit.  So that's two of the three While Loops stopped.  How about the Event Loop?  Here I use another type of Channel Wire, an Event Messenger, to send a Boolean "True" which triggers a <channel value> Case that wires True to the Event Loop's Stop.

 

Sorry, that's a lot of stuff, more than you may need now.  But I had this example and could post it quickly.  The key lessons are creating a State Machine based on a While Loop around a Case Statement that "names the States" you want to use (please don't say "Oregon") and an Event Loop that can monitor (and deliver, via a Queue or Messenger Channel Wire) Front Panel commands.

 

Bob Schor

 

P.S. -- it looks like I originally posted this about a year ago ...

0 Kudos
Message 7 of 15
(1,116 Views)

Hi luna,

 

as suggested before: by implementing a proper statemachine.

The "shutdown" is just another state, selected by the stop button…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 15
(1,116 Views)

Hi, I tried as suggested to implement a proper state machine with start, process and stop but the problem remains... I'm not able to shut down the program when I want. What do I do wrong?

 

0 Kudos
Message 9 of 15
(1,062 Views)
Solution
Accepted by topic author luna30

Hi luna,

 


@luna30 wrote:

I'm not able to shut down the program when I want. What do I do wrong?


There is no code in the "Stop" state to gracefully position your stage…

 

Suggestion:

Move the content of the sequence frame into your "Stop" state…

(In my assumption you need to change the duty parameter from last value (in shift register) to a known end value to move the stage to a defined position. Adapt as needed…)

 

Why don't you cleanup the block diagram (aka "your code") to make it more readable???

Btw. the "Sample time [ms]" is still nonsense as you use the "Count (mSec)" to set the iteration rate of the loop! The PID either needs to get the very same value as "Count (mSec)" - or simply leave that input unwired…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 15
(1,055 Views)