Finite state machine development.

Go To Last Post
128 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike32217 wrote:
Not to toot my own horn but you might want to also look at an article I wrote;  Finite State Machine for embedded systems

Kartman wrote:
About the only nit-pick I had was your use of external interrupts for switches.

 

For the purposes of the article I must disagree. The article missed out much of the scaffolding for his state machine but was already too long. I couldn't find the CTransition or CFiniteStateMachine Class for example.

 

As written, the Keyboard ISR is simply a source of events, where even g_millis is a distraction. To add a traditional keyboard scanner, would have been overkill here.

 

That aside.

  1. In the code,  I could easily find the defined states of the FSM because they are clearly defined and instantiated, but had trouble finding the defined events; I suppose they are SPEED_MASK & STOP_MASK; but was there an enumeration missing ?
  2. There is a missing transition: Pressing STOP in IDLE mode. what would the code have done there ? I couldn't tell.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom  Did you download the code? The scaffolding is there, I just didn't put all the code in the article.

 

The way I did it was only one way it could have been done, there is always a better way.

 

 

 

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh thanks for following up - I'll download and check it out.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I seem to remember where someone had something like:

seconds_is_1_state

seconds_is_2_state

seconds_is_3_state

..

..

seconds_is_59_state

They seemed bent on having as many states as possible! 

Well, I suppose that could be a state machine

I'd like to see their state machine for an odometer.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An odometer is a case where there MIGHT be a separate state machine for each wheel.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

I'd like to see their state machine for an odometer.

I don't think a mechanical odometer is a finite state machine because of the myriad of states while the drive wheel revovles between digits.

A digital odometer may internally count in feet or meters but at least that becomes finite.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I have created two state diagram for one system.

System do the two tasks

 

Blink LED every 1000ms

Turn on LED when user turn on the switch

 

Task one take 1000ms

Task two take 80 ms 

 

I want to make state diagram in which Led will blink continuously at 1000ms but it should be stop when user turn on the switch.  

 

How to convert both diagram into one state diagram?

 

 

 

 

Last Edited: Fri. Aug 14, 2020 - 10:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You really are starting at the wrong end of things!

 

First define your states:

switch on

switch off

light on

light off

 

we can get rid of switch on as light on/off means the switch is on.

 

This leaves us three states. The rest should be simple.

 

 

 

As for creating a state machine that you would code to solve that problem, then that might be quite different as you might want to poll the switch at a iven rate, but derive you flash timing from that rate. So you'd need a counter.

Last Edited: Fri. Aug 14, 2020 - 10:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I don't think a mechanical odometer is a finite state machine because of the myriad of states while the drive wheel revovles between digits.

That's a good point about an analog odometer!

 

I think you see my point exactly with an odometer

some fools would try to set up these states:

miles_1_state

miles_2_state

...

miles_9999_state

that is NOT what you want to do, (even though technically, they could be considered different states).

My point is it is important to define the perspective of state correctly, or you will create a quick mess of things.

In this case, it would be more of a pulse debouncer state machine connected to a counter, somewhat like a 1000 count encoder

 

 

 

 

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Djsarkar wrote:
How to convert both diagram into one state diagram?
If they are two separate tasks what makes you think they can be combined into one overall stater diagram/machine.

 

I suppose you could join the two diagrams with something like:

 

 

EDIT: Oh wait a minute are you talking about the same (flashing) light? So it's either off, flashing on or flashing off isn't it?

 

Last Edited: Fri. Aug 14, 2020 - 11:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 

Djsarkar wrote:
How to convert both diagram into one state diagram?
If they are two separate tasks what makes you think they can be combined into one overall stater diagram/machine.

 

EDIT: Oh wait a minute are you talking about the same (flashing) light? So it's either off, flashing on or flashing off isn't it?

 

 In terms of programming, LED blinking at every 1 second in main loop when user turn on switch means external interrupt will generate and in interttupt routine LED should be off

Last Edited: Fri. Aug 14, 2020 - 11:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't understand a word of that. But this is the thing - you need to start with a clear description of exactly what it is you are trying to achieve. Don't try to implement or even get to the level of state description until you have a very clear explanation/picture of what you intend the thing to to. From that you add increasing detail until at some stage you reach the level of state description and even further on you reach the point of actual implementation. Don't be tempted to "jump ahead".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mike,

 

In reference to your post

N.Winterbottom  Did you download the code? The scaffolding is there, I just didn't put all the code in the article.

The way I did it was only one way it could have been done, there is always a better way.

 

Do I have to login into jaxcoder in order to download your fms.zip.  I searched the jaxcoder article for the word 'download' and the zip file's name and did not find any link

 

Alan

Last Edited: Fri. Aug 14, 2020 - 12:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

allano

 

In the upper right corner of page there's a box "Available Downloads" click on the icon to download!

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike32217 wrote:

In the upper right corner of page there's a box "Available Downloads" click on the icon to download!

It took me a minute or two or searching to find it also.

 

BTW: Why is every page titled "Index" ?

Last Edited: Fri. Aug 14, 2020 - 12:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom

 

The site was written in ASP.Net MVC and by default all the routing pages are named Index, I just never changed them.

Short answer "Lazy"  :)

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike32217 wrote:

allano

 

In the upper right corner of page there's a box "Available Downloads" click on the icon to download!

 

Now that you pointed to the icon, it is obvious.  I hovered over a lot of items on the page to see if the cursor changed but not that icon.

 

Thanks,

Alan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to flash led every 1 seconds but when user press button , flashings should be off

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then you break it down:

 

state flash

state off

 

and button move between(press one way release the other).

 

then perhaps you can break flash down to

 

led off

led on

 

sec_counter change to odd 

sec_counter change to even

 

will then change the state

 

 

....

timer handling

...

...

 

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I guess you are saying the LED effectively has 3 states:

 

LED_OFF_PERMANENTLY

LED_OFF_WHILE_FLASHING

LED_ON_WHILE_FLASHING

 

If that's the case I'd maybe try and sketch out a state diagram that takes that into account and how it might transition between those states.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In your other thread, i pointed you to a tutorial. did you read it?

We seem to be spending an inordinate amount of time on a very fundamental subject........

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it show

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


No need for state machines or any code other than that needed to set-up a 1Hz timer output to a pin...

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

No need for state machines or any code other than that needed to set-up a 1Hz timer output to a pin...

Agreed - This example is too trivial to warrant or teach a state diagram. It in essence an On/Off switch controlling a LED flasher.

 

An often quoted FSM example is a pedestrian light controlled crossing. Without RADAR or Traffic loops etc. it's about the right level of complexity to demonstrate the concepts.

 

I'll see if I can locate a diagram...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
An often quoted FSM example
So often quoted that it had already been mentioned twice by this point in this interminable thread: https://www.avrfreaks.net/commen...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I did research for my article, virtually the only examples I could find where of the stop light.

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I did research for my article, virtually the only examples I could find where of the stop light.

 

Quadrature encoder knob is a good example

An elevator is one often used as a school example

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

When I did research for my article, virtually the only examples I could find where of the stop light.

 

Quadrature encoder knob is a good example

An elevator is one often used as a school example

 

 

That's always a fun one; not just from an FSM point of view, but also from an optimisation perspective. One place I used to work had 6 elevators/lifts serving around 20 floors. You would punch the number of the desired floor and it would tell you which elevator to take, presumably trying to group people together to avoid each one stopping at every floor.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

obdevel wrote:
You would punch the number of the desired floor and it would tell you which elevator to take

I recently worked on a "destination based" elevator system, it addition to that method, when you swiped your entry badge, it would look up what floor you worked on and direct you to the designated lift that would take you to that floor or if authorized to more then one, then present a menu of floors to choose from you were authorized to enter.

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Fri. Aug 14, 2020 - 06:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
An elevator is one often used as a school example

I remember the pre-historic lifts (aka elevators) at university. We raced to use the Goods Lift because it was fast and about 50 people could fit inside, but there was still competition for the nearby passenger lift. Both had an electro-mechanical control system, the algorithm was first-come-first-served without any memory. We engineers quickly discovered this and could easily "game the controller".

 

You had to listen carefully for the motor stopping, listen even more carefully for the waiting passenger to open the manual doors and after stepping aboard; closing them. At that instant; before the new passenger had pressed the button for his floor, you could press your call button. Whatever button the new passenger pressed, it didn't matter. You had called the lift. It became increasingly difficult for us to keep straight faces when we met the unfortunate passenger we had played the trick on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike32217 wrote:
Did you download the code? The scaffolding is there

I did Mike, looks well done, thanks!

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
I did Mike, looks well done, thanks!

 

Thanks for the kind words.

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to convert my state diagram into a state processing diagram.

 

 

 

How to make state task processing diagram

 

Ready means task is not active at yet

Running means task is active at time and running 

Blocked means task is deactivated

 

i don't understand transition that's why I have not completed

Last Edited: Sat. Aug 15, 2020 - 06:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Having two states named the same is not a good idea. There's got to be some good tutorials on fundamental like finite state machines - I'd suggest you try to find these and get the basics under control. At the moment you've got half an idea.

 

 

Again, you're confusing yourself. I have no idea what problem you are trying to solve - why is there task states mixed in?

 

 

There's various tools you can test your codes with. Python and javascript come to mind. But, I'd suggest getting the basics first.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

very simple---I have two variables, current_state and next_state

Here are some ideas, you can have a look, however no endless questions, you must think

The conditionals are formed, however you like: if  else, else if, elif, case , switch, whatever you like. 

This is just to give the overall idea..YOU provide the details and actual workings

 

biggie_state_machine:
if current_state = next_state
    (call) process_same state
else
    (call) start next_state
endif
==========================

process_same_state
    if current_state = monitor_for_fire
       do monitor doorway stuff (might set a new state)

    if current_state = sound_alarm
      next_state=sounding_alarm

    if current_state = sounding_alarm
         if  alarm reset = pressed
              turn off sound
              next_state = monitor_doorway  

    if current_state = fire_detected
              if bedroom_temperature < 100
                  shut off water
                  next_state = drain_pumping
              if bathroom_temp > 200
                 turn on exhaust
              else
                 turn off exhaust
 ===================================

start_next_state
  (call)  process current_state_exit   ...finish up any common tasks when leaving a state

     if next_state = sound_alarm    ...any of these will only be done ONCE, since a next state then becomes a current state
         turn on alarm bell       

     if next_state = fire_detected
        call fire dept
        trow a rock through the window
	turn on sprinklers

   current_state = next_state     ...important!!
===============================

process current_state_exit      ...any common things needing done
     if current_state = filling_washing_tub
         turn off water valve

     if current_state = begin_shutdown_event
         turn off power supply      

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Aug 15, 2020 - 04:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I have came across Wikipedia page https://en.m.wikipedia.org/wiki/....

 

There is a diagram given in page.

 

 

I understand given diagram theoretical, but when it comes to implementing it in a real practical, I do not understand it. 

 

I have spent a lot of time on this to understand. I have given my best to understand with real example. But it is my bad luck that I am unable to understand it even after my many of efforts

 

Can someone give me a real time example so that I can understand the diagram by comparing this diagram with real time example

 

Maybe if I have a real time example, then I can also try to create a diagram as given in page

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

This is the lifecycle of a process on a traditional computer under the control of an operating system, e.g. Windows, Linux, ...

 

I don't believe it's a helpful example for your learning, as it doesn't show the events that lead to the state changes. The states are controlled by the operating system's scheduler, which is trying to share finite resources (processor cycles, memory, ...) between multiple competing processes. It has little to do with embedded design and development.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This might be more applicable:

 

https://www.freertos.org/RTOS-ta...

 

You might want to work through the tutorials and other documents to get a better grasp on the basics.

 

This thread is a bit like this:

https://www.youtube.com/watch?v=...

 

 

Last Edited: Fri. Aug 21, 2020 - 10:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

obdevel wrote:

I don't believe it's a helpful example for your learning, as it doesn't show the events that lead to the state changes.

I agree with you, I have searched many  pages and links, There is more information about the state, they describe what happens in which state but doesn't gives real example. But I have not yet found any example in which explained  state with real  example. 

 

Real  example means an example that represents the objective of the project.

 

 

Last Edited: Fri. Aug 21, 2020 - 01:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

N.Winterbottom in #75 forgetfully wrote:
I'll see if I can locate a diagram...

How about this:

It's from this article:

https://www.drdobbs.com/architecture-and-design/uml-statecharts-at-1099/188101799

This looks like a really good write-up on hierarchical state machines. The author delivers a walk-though of his state machine design. It's lengthy but if you sit down and read it carefully; there's much to learn there.

 

The diagram is on the 2nd page:

https://www.drdobbs.com/architecture-and-design/uml-statecharts-at-1099/188101799?pgno=2

 

 

 

Last Edited: Fri. Aug 21, 2020 - 02:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The diagram above is a statechart.   That's not a place to start.

 

How about this problem: write a c function that looks at characters in a string to determine if the string is a legal integer or float.  The end of the string is denoted by end of string, a space, or non-character, non-digit.  This is how regular expressions are parsed.

 

start in state1: if character is minus, or a digit, go to state 2, otherwise return error

 

in state 2: if digit stay in state 2.   If end char, then go to state 11 and you are done: you have an integer.  If the character is '.' (period), proceed to state 3, other wise return an error.

 

in state3: if you read digits, stay in state 3; if 'e' goto state 4 (exponent notation), if end char, then goto state 12 and you are done: you have a float (w/o exponent), otherwise return an error.

 

in state4 (you've read 'e' in the floating point): if minus or digit go to state 5, else return error

 

state5: if digit stay in state 5, if end-char goto state 12, you are done, else signal error.

 

state 11: return "integer"

state 12: return "float"

 

EDIT: Part of the exercise is to find the errors in the above state machine.  :)

Last Edited: Fri. Aug 21, 2020 - 02:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MattRW wrote:

How about this problem: write a c function that looks at characters in a string...

 

A number of us have suggested possible projects which lend themselves to learning about FSMs. However the OP seems fixed on learning by reading rather than learning by doing, and in so doing keeps running up against the fact that 95% of stuff online is garbage.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MattRW wrote:

The diagram above is a statechart.   That's not a place to start.

Agreed - It's a step beyond that, but I don't believe that's a reason to dismiss it as a place to start - Especially when reading the linked article, it wasn't meant as a stand-alone diagram.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

 

A number of us have suggested possible projects which lend themselves to learning about FSMs.

Looks like you haven't read my last post 87. Everyone has given me advice on how to create a state diagram. Right now I have no problem in creating a state diagram, I have got the idea how to create this

 

My  question was that  I have an FSM #87 and  I do not understand how it is actually used in real projects. 

Last Edited: Fri. Aug 21, 2020 - 04:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Djsarkar wrote:

Looks like you haven't read my last post 87. Everyone has given me advice on how to create a state diagram. Right now I have no problem in creating a state diagram, I have got the idea how to create this

 

My  question was that  I have an FSM #87 and  I do not understand how it is actually used in real projects. 

I think obdevel gave a clear, full and accurate reply in the very next post #88.

 

The state diagram in post #87 is of no use to you, since you will never write an operating system.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't seem interested at all in any help offered, do you go through #86 or other examples?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@OP Good luck in your learning. I'm out.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

You don't seem interested at all in any help offered, do you go through #86 or other examples?

 

There is no doubt that you have given a great answer. I not saying now how to make a state diagram because I understand how to create

 

My question was how to understand the process state diagram with a real example. I have read all the states in the process state diagram and I understand it But I am not able to imaging process state diagram with any real project.

 My request  was to help me how you can understand process state diagram by comparing it with a real embedded project.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Methinks I see a bug:

A process can go from Blocked to Suspended, but not the other way around.

That could be deliberate.

FreeRTOS might require that the "owner" of a suspended process deal with blocking.

If so, that should be stated.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then use one of the examples given that represents a real-world device you're familiar with, e.g. traffic light, elevator, turnstile, rather than something abstract or invisible.

 

If you can imagine the device operating whilst reading the diagram, you're over 50% of the way there. Even better if it's a device that you can physically interact with, maybe as a car driver or pedestrian, or taking an elevator at your home or office.

 

OT ... I appreciate that some people are happy with abstract technical concepts, but I tend to want to relate things to a real-world example.

Pages

Topic locked