Pages: [1] 2   Go Down
Author Topic: Finite State Machine Using Switch Statement.  (Read 6675 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello,
I'm trying to learn about Finite State Machine,I understand the concept but i seriously can't see how to implement it in my programming. Currently I'm looking for a project that could help me learn about Finite State Machine. In my humblest opinion, Finite State Machine could be control by using Switch Statement, but i may be wrong. Could you help me in this matter?

so my Question are:
1) What would be a good project for me to learn Finite State Machine.
    I would like it if the project allow me to interface between computer and arduino.
    I would also like if the project involve some hardware interfacing eg, motor
   (i guess this would be the wrong place to ask for this question)
2) How to implement the Finite State Machine in my project?
3) Would Switch be a better solution for implementing Finite State Machine
4) Could i do a nested switch to make the programming part simpler?
5) If so i would like to seek some guidance in the process of making this project a success.

edit:
6)would calling up a function also make the programming part simpler?
« Last Edit: December 14, 2012, 10:25:20 pm by ash901226 » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You say you are comfortable with the FSM concept. In order to implement one in code, you will also of course need to be comfortable with the programming language (C++), but just as importantly, be able to draw state transition diagrams and state transition tables. Drawing a diagram and/or a table is essential before starting to write code. So are you good on all that?

A switch statement is a commonly used structure to implement a FSM. Nested switches can work for more complex problems, but I'd start out simple. Functions can always simplify things as well but that's a general thing and not specific to FSMs.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Germany
Offline Offline
Edison Member
*
Karma: 132
Posts: 1441
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I fully agree to the previous post. Additionally i can give some examples including state diagram and code.

My none-Arduino project to drive several power LEDs includes the user interface as state machine here http://code.google.com/p/tinyledlight/. The state machine is implemented with switch case statements and is available here: http://code.google.com/p/tinyledlight/source/browse/v2/tiny_led_light_v2.c

State machine for a rotary encoder software, part of m2tklib for Arduino. Sketch and state diagram are available here: http://code.google.com/p/m2tklib/wiki/rotenc. The state machine is implemented as "next step" lookup table.

Oliver
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i see the task that im tying is way over my knowledge, urm maybe i should try and learn more or learn while doing it?
yes i am comfortable with to draw state transition diagrams and state transition tables but C++ is something i am still lacking but i really do want to learn this well
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is there any simple project that i could do to enhance my understanding of Finite State Machine?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

during my search i had found some interesting site related to this matter
http://www.ni.com/white-paper/3024/en
http://www.mcgurrin.com/robots/?p=54
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the more i search and trying to understand what is Finite State Machine, the more i understand and the more i realized how much i need to know when it come to programming . however i cant help but to see the similarity between Finite State Machine with the step sequence that i use to used when programming PLC
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if(state=="try"){ //do actions here }
if(state=="it"){ //  ....}


basically it be done like that.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks. i didnt thought of that
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Switch and functions will certainly help you to build a state machine. Sometimes your tests and state transitions are simple enough that they can go directly in the switch. I like to keep loop small enough (like any other function) that it can be read without scrolling though, so generally, I'll call a function that handles each state from the (then) simple switch. Here's an example I posted a while back that illustrates both:

http://arduino.cc/forum/index.php/topic,106335.0.html

Sometimes, the system is so simple that a state machine is hardly worth it, but it still makes a nice framework. Also, if functionality is added later, you don't find yourself cursing that you should have used the concept from the start. This one shows this:

http://arduino.cc/forum/index.php/topic,59511.0.html

Here's another example, pretty pure, one function to handle each state:

http://arduino.cc/forum/index.php/topic,71867.0.html.

Repeating Jack's advice, if you use this switch and function call model, the state machine practically writes itself - as long as you have specified your states and possible transitions up front.

Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My none-Arduino project to drive several power LEDs includes the user interface as state machine here http://code.google.com/p/tinyledlight/. The state machine is implemented with switch case statements and is available here: http://code.google.com/p/tinyledlight/source/browse/v2/tiny_led_light_v2.c

Nice state diagrams! What did you use to draw them?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the more i search and trying to understand what is Finite State Machine, the more i understand and the more i realized how much i need to know when it come to programming . however i cant help but to see the similarity between Finite State Machine with the step sequence that i use to used when programming PLC

The first job then would be getting comfortable with C/C++ then. OO coding isn't needed to do decent FSMs. Start with something simple, three or four states at most. Being able to understand the problem at hand and visualize it in terms of states can take a little practice, but usually comes pretty quickly.

Lots of examples are available, several have been offered in this thread. A lot of good FSM exercises can be done with an Arduino, some switches to represent inputs, and LEDs to represent outputs. I'll offer one more at the risk of giving you more examples than you can enjoy, it's an implementation of the common garage-door controller project that a lot of folks have done. At this point it only senses the state of the door and lights up some LEDs accordingly, and just needs two switches and some LEDs to implement. I think it has four or five states at most. Not much code, and I also have state diagrams and tables to go with.

You can think of other examples and invent your own exercises. Lots of things in everyday life can be seen as FSMs. Appliances like washing machines go through a series of states and control valves and pumps. Traffic signals and crosswalks. See Wikipedia's article for a simple FSM representing a turnstile.

I've never programmed a PLC, but yes I wouldn't be surprised if there were a lot of similarities, so you probably have some good background there in thinking about the inputs, outputs, and states that represent a system.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes thank you. The more i study what is Finite State Machine, the more i realize that most of its logic had been teach to me by my sensei. I actually use it b4 during my time working in the automation industries which actually give me some credential as a programmer back then, but that was only in ladder diagram and PLC. And yes as we speak i actually have been trying various task that i used to do while i was working. wow this really open up my eye to a whole new work of possibilities. I mean b4 i uses to think that PLC and arduino style programming have very little similarities but now, WOW!!! and i really mean WOW!!!!! all this actually click in my head rite now.
for instant now i can see how i could actually program a whole machine with this knowledge alone. 
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Understanding the FSM is a bit like finally getting the blink without delay example. Once you see it, there's very much a feeling of "Is that it?". Before that though, it seems baffling, especially in the abstract. Examples help - a lot.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

one thing about PLC is that every thing is represented with HIGH and LOW. which really help. with arduino the possibility to represent something as HIGH and LOW and everything in between, WOW now everything i learn from my sensei could finally put to use. He thought me about Fuzzy logic and Sequence step, timing diagram. then he thought me some automation and finally b4 i when to work he thought me about Finite State Machine, although at the time he did not tell me its name just that it is an advance form of Sequence step. but now everything make sense now. So Thank you all for actually making me realize that my sensei had thought me all of this all along. Urm you know suddenly i have the confident in me to actually do any advance work with arduino.
Again i cant stress enough how much I'm Thanking all of you.... thank you thank you thank you ......
Logged

Pages: [1] 2   Go Up
Jump to: