state machine for Tasks  - Page 1
Forum section: electronics / general

author: BlogRahul
author_rank: Regular Contributor
timestamp: February 25, 2022, 11:31:03 am
content: HelloI 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 situationExample : 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 tasksPlease advice me how it will make ?

author: tggzzz
author_rank: Super Contributor
timestamp: February 25, 2022, 12:15:41 pm
content: 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. At this point I suggest you employ a consultant, and learn from how they solve the problem.https://www.eevblog.com/forum/microcontrollers/how-to-split-system-task-into-operating-system-task/msg4013455/#msg4013455https://www.eevblog.com/forum/microcontrollers/process-state/msg4009153/#msg4009153https://www.eevblog.com/forum/microcontrollers/audio-spectrum-monitor/msg3955895/#msg3955895https://www.eevblog.com/forum/microcontrollers/program-structure-for-cooperative-scheduling/msg3935659/#msg3935659

author: agehall
author_rank: Frequent Contributor
timestamp: February 25, 2022, 06:09:09 pm
content: 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.

author: tggzzz
author_rank: Super Contributor
timestamp: February 25, 2022, 06:22:35 pm
content: I suspect he is confusing the FSM states for his application with the thread states. They are, asweknow, completely unrelated.

author: rstofer
author_rank: Super Contributor
timestamp: February 25, 2022, 06:58:27 pm
content: 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 statementCode:state = 1; // initial stateswitch(state) { // state is a variable, we use the value to select a casecase 1 : <do something in case/state 1>state = 2;break;case 2 : <wait for something to happen>if (it happened)state = 3;elsestate = 2; // redundant, we're already in state 2 but I like to do it anyway. It's required in FPGA codebreak;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!}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.

author: tggzzz
author_rank: Super Contributor
timestamp: February 25, 2022, 07:58:17 pm
content: And the OP should note that that code all runs inside one thread - which means you don't need an RTOS for that alone.

author: rstofer
author_rank: Super Contributor
timestamp: February 25, 2022, 08:52:46 pm
content: Quote from: tggzzz on February 25, 2022, 07:58:17 pmExactly 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-terzijaHere is a more typical PLC ladder diagram showing a small FSMhttps://peer.asee.org/teaching-finite-state-machines-fsms-as-part-of-a-programmable-logic-control-plc-course.pdfETA: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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: February 25, 2022, 08:53:42 pm
content: Quote from: tggzzz on February 25, 2022, 07:58:17 pmDespite the OP claiming it was not homework (IIRC) in another thread, I strongly suspect it is. =)

author: rstofer
author_rank: Super Contributor
timestamp: February 25, 2022, 09:35:07 pm
content: 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.

author: xrunner
author_rank: Super Contributor
timestamp: February 25, 2022, 09:43:37 pm
content: Quote from: SiliconWizard on February 25, 2022, 08:53:42 pmMaybe we just need to be totally open about helping with homework and have a new forum board for that.

author: floobydust
author_rank: Super Contributor
timestamp: February 25, 2022, 09:52:55 pm
content: If you don't know the difference between a state machine, task scheduler or operating system... just give up now.

author: tggzzz
author_rank: Super Contributor
timestamp: February 25, 2022, 11:31:39 pm
content: Quote from: floobydust on February 25, 2022, 09:52:55 pm...or do the research to find out the differences.

author: BlogRahul
author_rank: Regular Contributor
timestamp: February 26, 2022, 03:19:32 am
content: Quote from: tggzzz on February 25, 2022, 12:15:41 pmI 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 effortsQuote from: tggzzz on February 25, 2022, 07:58:17 pmQuote from: floobydust on February 25, 2022, 09:52:55 pmI already know that real time operating system is not neccessary for real time system. it can be designed by state machine.Quote from: agehall on February 25, 2022, 06:09:09 pmIt seems to me that in order to schedule tasks in any operating system, we need to know the state of tasksQuote from: floobydust on February 25, 2022, 09:52:55 pmI know the difference between state machine, task scheduler or operating system.

author: tggzzz
author_rank: Super Contributor
timestamp: February 26, 2022, 07:59:28 am
content: Quote from: BlogRahul on February 26, 2022, 03:19:32 amNo. You don't schedule tasks, the OS does that.If several tasks are runnablethen 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.QuoteAnd yet you continue to use the wrong abstraction to solve your problem.

author: rstofer
author_rank: Super Contributor
timestamp: February 26, 2022, 02:58:07 pm
content: 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.htmlOr this manual:https://www.nxp.com/wcm_documents/techzones/microcontrollers-techzone/LPCLibrary-Books/Books-pdf/using.freertos.lpc17xx.summary.pdfThere 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

author: Siwastaja
author_rank: Super Contributor
timestamp: February 26, 2022, 03:14:33 pm
content: - 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?- ...

author: SiliconWizard
author_rank: Super Contributor
timestamp: February 26, 2022, 05:49:09 pm
content: Quote from: Siwastaja on February 26, 2022, 03:14:33 pmFun and kind of spot on. =)

author: agehall
author_rank: Frequent Contributor
timestamp: February 26, 2022, 08:53:35 pm
content: Quote from: BlogRahul on February 26, 2022, 03:19:32 amActually, 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.