11-09-2023 05:51 AM
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.
Solved! Go to Solution.
11-09-2023 06:56 AM
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")…
11-09-2023 07:18 AM
Yes I actually use the stop button. How can I modify it?
11-09-2023 07:55 AM
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:
Bob Schor
11-09-2023 08:09 AM
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…
11-09-2023 08:38 AM
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?
11-09-2023 08:52 AM
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 ...).
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".
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 ...
11-09-2023 08:52 AM
11-10-2023 04:19 AM
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?
11-10-2023 04:38 AM - edited 11-10-2023 04:43 AM
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…