Author Topic: state machine for Tasks  (Read 2686 times)

0 Members and 1 Guest are viewing this topic.

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
state machine for Tasks
« on: February 25, 2022, 11:31:03 am »
Hello

I don't have any idea how state machine is designed for the the task of freertos operating system. I am taking a hypothetical situation, so i would like someone to help me to design freertos state machine for following  situation

Example : We have camera and we want to capture video at 40 frames per second. Processor takes less then 10us to finish this works. We have second task where we takes one reading of adc and stores it an a buffer. This also takes less then 10us to finish this works.

I want to make a state machine diagram where I can show the state ( ready to run, running, blocked, suspended ) of each tasks

Please advice me  how it will make ?


 
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: state machine for Tasks
« Reply #1 on: February 25, 2022, 12:15:41 pm »
« Last Edit: February 25, 2022, 01:02:35 pm by tggzzz »
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: thm_w, harerod

Offline agehall

  • Frequent Contributor
  • **
  • Posts: 383
  • Country: se
Re: state machine for Tasks
« Reply #2 on: February 25, 2022, 06:09:09 pm »
Why do you care so much about the somewhat internal nature of task states in your OS? For normal applications, you don’t need it. These states are managed by FreeRTOS and if you are developing an application, you should focus on the inter-task communication you need to trigger work in different tasks.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: state machine for Tasks
« Reply #3 on: February 25, 2022, 06:22:35 pm »
I suspect he is confusing the FSM states for his application with the thread states. They are, as we know, completely unrelated.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: state machine for Tasks
« Reply #4 on: February 25, 2022, 06:58:27 pm »
Expanding on tggzzz's comment, let's think about FSMs.  They are exactly like the child's game of "Simon Says".  You can't change state until Simon says.  In C, it is a simple switch/case statement
Code: [Select]
state = 1;  // initial state

switch(state) {  // state is a variable, we use the value to select a case
  case 1 : <do something in case/state 1>
               state = 2;
               break;
  case 2 : <wait for something to happen>
               if (it happened)
                 state = 3;
               else
                 state = 2;  // redundant, we're already in state 2 but I like to do it anyway.  It's required in FPGA code
               break;
  case 3 : <we did something (case 1), we got acknowledgement (case 2), now do something else>
               state = 4;
               break;
  default:     state = 1; // we're in serious trouble if this ever executes!
}
[/font]


As discussed in your previous threads, you need to know what transitions cause the FSM to leave each state for which inputs.  What happens if an unexpected input occurs?  You need to account for all possible inputs in every state.  You also need to define all possible outputs in each state.

It's pretty simple for FSMs with a dozen or so states.  It gets a little harder when there are over 100 states and a couple of dozen inputs.  Fortunately for FPGA designers, the languages give us the ability to define 'default' outputs that will be applied if a specific output signal isn't generated in a state.

You will see a lot of FSMs where the designer doesn't account for every input and every output in every state.  That may be because they know that certain events just can't physically happen.  That's ok too!

Google for Finite State Machine - there are hundreds of sites talking about them.  They are the fundamental building block of all sequential machines - like computers and machine controls.

« Last Edit: February 25, 2022, 07:05:05 pm by rstofer »
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: state machine for Tasks
« Reply #5 on: February 25, 2022, 07:58:17 pm »
And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: state machine for Tasks
« Reply #6 on: February 25, 2022, 08:52:46 pm »
And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.

Exactly right!  You could bend an RTOS system to make it work like an FSM but that would be the hard way to do things.

I still don't know what the OP is actually trying to accomplish but if it is machine control, a PLC is the place to start.  Yes, you still need to implement an FSM if the machine is sequential (most are) but the manner of doing so is a lot different than a C switch/case statement.

Here is some PLC code that looks a lot like C.  I have never used a PLC with this capability:

https://www.linkedin.com/pulse/efficient-plc-programming-using-finite-state-machine-concept-terzija

Here is a more typical PLC ladder diagram showing a small FSM

https://peer.asee.org/teaching-finite-state-machines-fsms-as-part-of-a-programmable-logic-control-plc-course.pdf

ETA:

The state machine in Figure 4 deserves a lot of thought.  Note how the beginning of some next state unlatches the current state.  Yet something in the current state kicks off the next state.  Look at the interaction of C101 and C102.  This is a typical pattern and is nearly a template.

« Last Edit: February 25, 2022, 09:00:28 pm by rstofer »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14484
  • Country: fr
Re: state machine for Tasks
« Reply #7 on: February 25, 2022, 08:53:42 pm »
And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.

Despite the OP claiming it was not homework (IIRC) in another thread, I strongly suspect it is. =)
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: state machine for Tasks
« Reply #8 on: February 25, 2022, 09:35:07 pm »
Regardless of technology, the project has to start with a written description of the sequential machine.  Every single detail (input/output,etc) needs to be noted.  If the system can't be described in words, a ladder diagram is right out the window.  In general, PLCs are programmed from ladder diagrams.

I usually use symbols stolen from flow charts of yesteryear.  To that end, https://www.lucidchart.com/pages/ might have an interesting approach.
 

Online xrunner

  • Super Contributor
  • ***
  • Posts: 7518
  • Country: us
  • hp>Agilent>Keysight>???
Re: state machine for Tasks
« Reply #9 on: February 25, 2022, 09:43:37 pm »
And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.

Despite the OP claiming it was not homework (IIRC) in another thread, I strongly suspect it is. =)

Maybe we just need to be totally open about helping with homework and have a new forum board for that.  :-DD
I told my friends I could teach them to be funny, but they all just laughed at me.
 

Offline floobydust

  • Super Contributor
  • ***
  • Posts: 7004
  • Country: ca
Re: state machine for Tasks
« Reply #10 on: February 25, 2022, 09:52:55 pm »
If you don't know the difference between a state machine, task scheduler or operating system... just give up now.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: state machine for Tasks
« Reply #11 on: February 25, 2022, 11:31:39 pm »
If you don't know the difference between a state machine, task scheduler or operating system... just give up now.

...or do the research to find out the differences.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
Re: state machine for Tasks
« Reply #12 on: February 26, 2022, 03:19:32 am »
You seem to keep asking equivalent questions. People have spent a lot of their time giving you pointers, but there is little indication you have read and understood them.
I know it's not easy to design state machine for real application (RTOS). i know I won't be learn it fast. I am not expert person. I am trying my best efforts 

And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.
If you don't know the difference between a state machine, task scheduler or operating system... just give up now.
I already know that real time operating system is not neccessary for real time system. it can be designed by state machine.   

Why do you care so much about the somewhat internal nature of task states in your OS?

It seems to me that in order  to schedule tasks in any operating system, we need to know the state of tasks   

If you don't know the difference between a state machine, task scheduler or operating system... just give up now.
I know the difference between state machine, task scheduler or operating system.


 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: state machine for Tasks
« Reply #13 on: February 26, 2022, 07:59:28 am »
Why do you care so much about the somewhat internal nature of task states in your OS?

It seems to me that in order  to schedule tasks in any operating system, we need to know the state of tasks   

No. You don't schedule tasks, the OS does that.

If several tasks are runnable then the OS will pick one to run. Or more, in a multicore processor. Whether a task runs to completion or can be pre-empted is a characteristic of the operating system.

In a non-premptive system, a task becomes runnable when it has input to be processed and continues until all its processing has been done or it explicitly yields or an interrupt is serviced.

Quote
If you don't know the difference between a state machine, task scheduler or operating system... just give up now.
I know the difference between state machine, task scheduler or operating system.

And yet you continue to use the wrong abstraction to solve your problem.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: state machine for Tasks
« Reply #14 on: February 26, 2022, 02:58:07 pm »
Go to FreeRTOS.org and download the package.  In the .zip are 26,521 files and most are related to examples for various configurations of hardware.

The Win32 demo builds with a recent installation of Microsoft Visual Studio and it runs but I didn't play with it.  It implements some kind of heap test or it implements a 'blinky' depending on a #define which I didn't chase.  By default it implements the heap test.  The important part is that it builds.

At the FreeRTOS.org go to Support -> Books and Manuals to find a couple of large manuals.  The link to the .zip file goes to SourceForge.

There is even a project for the ATmega328P (used in Arduino).  It requires the Atmel Studio IDE.  I have no idea how to set this up.  It may not actually run on an Arduino, it may require a different board.  It's a limited demo because the chip has so little RAM.

At the site, there are a lot of documented projects including one for the LPC1768 which I like to use:

https://www.freertos.org/LPC1768_RedSuite.html

Or this manual:

https://www.nxp.com/wcm_documents/techzones/microcontrollers-techzone/LPCLibrary-Books/Books-pdf/using.freertos.lpc17xx.summary.pdf

There is a lot of documentation on the web and plenty of books.

Using the free compiler/toolchain at mbed.org, there is an incantation of FreeRTOS targeting the LPC1768.  I haven't used it but it probably works.  mbed.org is now owned by ARM
« Last Edit: February 26, 2022, 03:08:56 pm by rstofer »
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8178
  • Country: fi
Re: state machine for Tasks
« Reply #15 on: February 26, 2022, 03:14:33 pm »
- How do I move the cylinder up and down and when do I inject fuel?
- What are you actually doing?
- Trying to go shopping with my car.
- Oh, then you need to press the gas pedal and turn the steering wheel...
- Oh I know that, but I also need to make the cylinder go up like really fast I guess, but I don't know how fast exactly, can you help? Do I do it with bare hands?
- Oh, just get a driver's license?
- I got one, but I still don't know how much fuel I need to inject, and when?
- ...
 
The following users thanked this post: agehall

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14484
  • Country: fr
Re: state machine for Tasks
« Reply #16 on: February 26, 2022, 05:49:09 pm »
- How do I move the cylinder up and down and when do I inject fuel?
- What are you actually doing?
- Trying to go shopping with my car.
- Oh, then you need to press the gas pedal and turn the steering wheel...
- Oh I know that, but I also need to make the cylinder go up like really fast I guess, but I don't know how fast exactly, can you help? Do I do it with bare hands?
- Oh, just get a driver's license?
- I got one, but I still don't know how much fuel I need to inject, and when?
- ...

Fun and kind of spot on. =)
 
The following users thanked this post: Siwastaja

Offline agehall

  • Frequent Contributor
  • **
  • Posts: 383
  • Country: se
Re: state machine for Tasks
« Reply #17 on: February 26, 2022, 08:53:35 pm »
I know it's not easy to design state machine for real application (RTOS). i know I won't be learn it fast. I am not expert person. I am trying my best efforts 

Actually, no, it isn’t that hard. But you are asking all the wrong questions and refuse to listen when we tell you so. Just explain what your homework question is and we can help you understand it but the questions you are putting forth are not really relevant.

And if this truly isn’t homework - go do some reading on basic embedded programming (or any kind of programming!) to learn the basics of how to write applications.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf