07-31-2013 03:56 AM
Hi.
I want to realize an exercize to practice on the state machines in labview.
Attached there is the VI I made. It controls the movement of a Bus. I have two trafficlights, the first one allows the Bus to exit, the second one is for the normal traffic. So while the Bus is in the parking, his trafficlight is Red all the time, while the other one works normally shifting from green to red. Once the Bus starts to move, his trafficlight goes to Green and the other one goes to red.
My questions are:
1- if this VI could be consider a valid exercize for the state machines, in the way I have made it.
2- if in the "Bus Moving" of the Case structure is it possible to remove the "for cicle" in somehow with a timer that waits for a certain number of seconds. I tried with the "wait pattern" with the idea to do a comparison between his output and the reach of the input I gave to him.... but I saw that it will never give me the same output since it take in count the ms timer value, so as it is written in the help: if millisecond timer value is 112 ms and milliseconds to wait is 10 ms, the VI finishes when millisecond timer value equals 122 ms.
Any tips?
Thank you in advance
Solved! Go to Solution.
07-31-2013 04:04 AM - edited 07-31-2013 04:11 AM
Honestly, i recommend you to take a step back.
DO NOT USE LABVIEW at this stage.
The point is that your algorithm still shows that you are simply going the try-and-error way to learn programming. Programming is about planning. About thinking *what* is *when* to do.
A graphical representation of this is the state diagram.
So please pick up pen'n'paper and draw a state diagram for your task "traffic light control". Include additional states (and transitions) to handle errors and to stop the application "early" (Stop button on interface!).
Once you have a well defined (and documented) state diagram, you are ready to start working with LV. You will find out that such a diagram is transferred to LV quite easily by using the state machine architecture.
hope this helps,
Norbert
EDIT: You really improved compared to your first postings of code. You are now using a basic state machine (even if states are still not defined well for the task) and use enums and stuff. Pretty good improvement. Next steps besides the "planning" recommendation are:
- Try to keep wiring simple (no unnecessary edges and stuff)
- Put some documentation in the blockdiagram
- Try to avoid variables (hint: put terminals outside the case structure still within the loop)
- Try to avoid sequence structures as you should replace each sequence frame with a state
- For purpose of international support i recommend to use english as development language. You can use localized CAPTIONS on the frontpanel if you require a localized UI.
07-31-2013 04:22 AM - edited 07-31-2013 04:32 AM
Yea I'm sorry for the italian in the Front Pannel I forgot to translate it.
Can you give me more details (maybe with an easy example if it is possible) on these two points:
- Try to avoid variables (hint: put terminals outside the case structure still within the loop)
- Try to avoid sequence structures as you should replace each sequence frame with a state
and about the other question I asked the "For loop" is the only option, am I right?
EDIT: with the second point you mean remove the structures and create other states in the states machine?is this that you mean?
Thank you
07-31-2013 06:03 AM
Regarding the sequence structure, your EDIT is correct. Please replace them with appropriate cases in your state machine. Your state transition code has to reflect the possible transitions of your state diagram to generate the correct sequence of LED turning on/off.
As most of your controls are indicator, moving them out of the case structure enables you to write values each iteration without the need of variables. The disadvantage is that you HAVE to write new values in each iteration.
But you already know the correct way to "preserve data from a previous iteration": shift register.
So, the code would look similar to this:
hope this helps,
Norbert
07-31-2013 07:01 AM
Oh well it is all clear now.
I'll work on these tips. Thank you ![]()