0

Clearly, states of a machine will be abstracted into tasks, but how are transitions controlled?

The functionality I'm looking for is that only one of the state tasks is active at a time, while the rest block. The task that is running must block itself, and unblock whichever task is next in the state transition model.

The method I thought of is creating an index array of Binary semaphores for each task, and simply giving to the semaphore of whichever task is to be transitioned to.

Alternatively, I could handle all state machine functionality in one task, and regulate which functionality is executed by a switch statement?

Which is more efficient or better practice?

  • 1
    The state machine in separate tasks makes a little sense. – 0___________ Nov 16 '21 at 22:27
  • I agree to @0___________ .. it makes not much sense. But if you still want to do it, check the xTaskNotify() functions, to send events to tasks. – kesselhaus Nov 17 '21 at 08:38
  • "_Clearly, states of a machine will be abstracted into tasks_" - that is not clear at all, tasks are not states. Typically you would implement a state-machine _within_ a task and use tasks to run multiple such state-machines concurrently. That allows you to run simpler more cohesive state-machines rather then using the state-machine as a means of scheduling. It is also a means of implementing concurrency within a state-machine. Using tasks as discrete non-concurrent states serves no purpose. Events can be exchanged between such state-machines using RTOS IPC mechanisms such as event flags – Clifford Nov 19 '21 at 23:59

1 Answers1

0

Not sure SO is the right płace to ask this, as this is a really generał question. Anyway: imho the simplest way to get started is to utilise the exisiting tools, the state machine design pattern in this case. C is not the perfect language to implement it, but it can be done, see for example: https://stackoverflow.com/a/44955234/4885321 or https://www.adamtornhill.com/Patterns%20in%20C%202,%20STATE.pdf In the context of FreeRTOS the FSM is most likely going to end up as a single task.

alagner
  • 3,448
  • 1
  • 13
  • 25