State Machines - my brain won't do what I want it to
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
Most of the FSMs I've written were for low level communications protocols (think HDLC/X.25, SDLC/SNA). Never did implement the new-fangled TCP/IP. ;P One interesting side-effect of implementing some of the older ones (Bisync flavours, anyone?) was proving that the protocols as documented were incomplete. They needed a catch-all state "Human intervention required". Maybe a toy poll/response protocol? Two interacting FSMs, one for master, one for slave. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
I've really done it. Ever since I wrote my GLR parser generator, which can create parsers that even parse human language I have run out of things to code. I jumped the shark. Now I've been slumming it writing small tips instead of whole articles.
Real programmers use butterflies
Make a tool to create languages...
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
There are quite a few real-world examples if you think in overview what a FSM requires: 1) Inputs that brought the machine to this state 2) The current state of the machine and the outputs that arriving there generated 3) The new state the machine will have as a response to new inputs So: navigating a vehicle on an small island could be modeled as an FSM. A drinks vending machine. Traffic lights (sooooo simple, very small set of states). Voting systems (could get humorous). Good luck!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Most of the FSMs I've written were for low level communications protocols (think HDLC/X.25, SDLC/SNA). Never did implement the new-fangled TCP/IP. ;P One interesting side-effect of implementing some of the older ones (Bisync flavours, anyone?) was proving that the protocols as documented were incomplete. They needed a catch-all state "Human intervention required". Maybe a toy poll/response protocol? Two interacting FSMs, one for master, one for slave. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
That's an interesting idea. I'll consider it. It also made me think of another idea involving an asynchronous implementation of an HTTP request/response cycle.
Real programmers use butterflies
-
Make a tool to create languages...
I've already created a lot of tools for building compiler front-ends. I even developed my own language (actually a subset of C#) for reasons. This however, would be too complicated for the examples I intend to present.
Real programmers use butterflies
-
I've already created a lot of tools for building compiler front-ends. I even developed my own language (actually a subset of C#) for reasons. This however, would be too complicated for the examples I intend to present.
Real programmers use butterflies
You said you were bored. Think greater! A program for generating any language! :laugh: An 'inverse-parser' if you will! It could spit out anything, and each syntax could take experts hundreds of years to decipher (or you could make that your next project!) :laugh: :laugh:
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
There was a QA a while ago about 'decoding' some data - the eventual solution was proposed using a RegEx - I was in two minds about it, I personally would have used a state-machine, or maybe it was a 'borderline case' (as opposed to me, 'nut case')
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
honey the codewitch wrote:
a simple example of a state machine that is real world at all
Do you own a car ? Think about any car part that is based on electronics or electrical actors -> It runs a state machine. Embedded is full of state machines. Actually, state machines are the AI of embedded world.
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
An example that could grow into something non-trivial, but that everyone understands, is a set of four traffic lights.
Robust Services Core | Software Techniques for Lemmings | Articles
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
If you can't think of a real world example then maybe it's not something people particularly need to know about?
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
When I hear "state machine" I think of games. Maybe you could write something about that? Also, don't know if I used it right, but I once used a state machine for order processing. The order could go from "ordered" to "paid" and "paid" to shipped, but never "ordered" to "shipped", or something like that. It was a bit more complicated than that, but it's been a while so I don't remember, but it was something like that.
Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...
-
How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...
That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.
-
That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.
Yep - needs rules real quick. Always going to the closest floor request would not be a good idea, e.g.
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
Reading input devices like a mouse, keyboard, joystick - without a fancy event based framework is a good example. Given that the hardware only gives you the state of pressed/not-pressed events like click, tap, double-click, etc. require and abstract state machine on top of the state of the physical buttons, and best of all most beginner programmers are going to understand the examples without needing any other domain knowledge.
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
Way back in the stone-age, I a state machine and a timer interrupt to implement a Serial port on a micro controller. Due to the efficiency of the state machine, I was able to handle 9600 baud in the background while the software handled its main task, probably driving a printer. I think that was state driven as well. It’s amazing what you could do in lees than 4K ROM and 120 byes of Ram, including stack. One of the big advantage of some state machine implementation is they take very little RAM.
"Time flies like an arrow. Fruit flies like a banana."
-
How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...
Tim Deveaux wrote:
How about an elevator.
I second that: if my memory serves me well, Knuth used it as an example in TAOCP (The Art of Computer Programming) and he didn't use FSMs (I'll have to go review that). If Knuth himself used it, it must be good. Mircea
-
I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr
Real programmers use butterflies
There's a bunch of articles on state machines already here on CP. Games, like Hunt the Wumpus - Wikipedia[^] are perfect examples for using a state machine. BTW, I actually knew Gregory Yob, he was very much a mentor to me in my early 20's.
Latest Articles:
Abusing Extension Methods, Null Continuation, and Null Coalescence Operators -
That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.
Speaking of elevators, I've always wanted to ask if repeated button pushes get precedence. It would explain why when I'm standing there and the button is already lit, others feel the need to press it again, sometimes repeatedly. Do they know something I don't?
"Go forth into the source" - Neal Morse