10-15-2018 05:30 AM
I'm making a relatively simple .vi to step a motor through a number of speeds. It's my first attempt at making a state machine architecture, I had previously been working from an example provided by the motor manufacturer that was a timed sequence.
I am trying to add a button to my vi that forces the state to "Disabled" to stop the motor, but I'm having trouble putting it somewhere where the reaction is fast enough. It's not supposed to be an emergency stop so it doesn't need to be completely instant - there's a hardware button for that - but it's more of a "oh balls, I had the wrong option selected" kind of thing.
Outside of the main loop it obviously doesn't get read after the program starts. Within the main loop but outside the case structure also doesn't seem to work, and within the case structure I thought of putting it in the "Spin" state but that takes 5s to respond due to the wait function.
Solved! Go to Solution.
10-15-2018 06:46 AM
Your states take too long. You should not have "long" waits in your states. You are allowed to repeat states until an amount of time has elapsed. Then you can react to the button press a lot faster.
10-15-2018 08:32 AM
I see, so do you think it would be better to split my current "Spin" state so I have one state that sends the message to the motor to start spinning, and then move to a new "Spinning" state that I repeat as necessary?
How would I best repeat the state for a set time? Would you have a Wait function that is a fraction of the desired time and an iteration counter that increments until it reaches the total time divided by the wait time?
10-15-2018 08:43 AM
Take a look at queues. Basically, you want one "master loop" that queues messages to a "slave" loop and when you press the stop button it queues the state in front of the queue, forcing it to execute first. It will be easier to synchronize and program than a traditional state machine.
10-15-2018 08:43 AM
@Martin_N wrote:
How would I best repeat the state for a set time? Would you have a Wait function that is a fraction of the desired time and an iteration counter that increments until it reaches the total time divided by the wait time?
Use the Elapsed Time express VI. You will want to keep the reset in a shift register so that you only reset it on the first iteration of the spin.
10-18-2018 10:18 AM
Thanks, that was really helpful. It solved that particular issue and helped me to come up with ways to do some other things too.
10-18-2018 10:40 AM - edited 10-18-2018 10:42 AM
I am glad you solved your problem, but there are quite a few other things that require attention that will help you in the future. Here are some:
10-18-2018 10:43 AM
Thanks for your comment, all of those issues have already been fixed. The VI I have now is considerably improved from what I originally posted. I'm now into a whole new world of fun - CAN