FSM Lecture 17: Exercise-003 Implementing PAUSE state

  • Post author:
  • Post category:Blog

 

Exercise-003 Implementing PAUSE state

 

 

In this article, let’s implement the PAUSE state.

Figure 1.Project requirements
Figure 1.Project requirements

 

The requirement is(see figure 1), When the countdown is paused, time can be modified. That means when the application is in the paused state, time can be modified. That means it respects the increment time and decrement time events.

 

Figure 2.PAUSE state processes increment and decrement time events
Figure 2. PAUSE state processes increment and decrement time events

 

See Figure 2, When we are in the PAUSE state and receive the increment time event, the transition from PAUSE state to TIME_SET state. And that trigger is INC_TIME.

The action is mobj->c_time += 60. That’s action along with the transition. 

And also, there is one more Transition. Transition from PAUSE state to TIME_SET state for the decrement time. The trigger is DEC_TIME. Guard is mobj->c_time >= 60. And action is  mobj->c_time -= 60. So, that’s how the PAUSE state processes increment time and decrement time events.

 

Figure 3. START_PAUSE Events
Figure 3. START_PAUSE Events

 

Next START_PAUSE event.

PAUSE is nothing but the countdown has paused. When the countdown has paused, the application is in PAUSE state.

And if it again receives the START_PAUSE event, then it should resume the countdown. That’s why it should go back to the COUNTDOWN state. So, the trigger is START_PAUSE. (Figure 3)

 

Figure 4. ABRT event
Figure 4. ABRT event

 

How about the ABORT event?

Everything must be Abort. It should go back to the IDLE mode. Let’s draw one more Transition from PAUSE state to IDLE state. Show in Figure 4 that is ABRT.

When the application is paused, it doesn’t process the TIME_TICK event. That’s why it ignores the TIME_TICK event.

 

The next state is COUNTDOWN.

When the time is counting down, you cannot modify the time. That’s why increment time and decrement time events are not processed while in this state. We can ignore them. 

 

Figure 5.START_PAUSE event for COUNTDOWN state
Figure 5. START_PAUSE event for COUNTDOWN state

 

Next is the START_PAUSE event.

Whenever it receives the START_PAUSE event, it must go to the PAUSE state. That’s why there must be one Transition from COUNTDOWN state to PAUSE state as shown in Figure 5. 

 

Figure 6 shows the ABORT for COUNTDOWN state.

Implementing PAUSE state
Figure 6. ABORT for COUNTDOWN state

 

Implementing PAUSE state
Figure 7. TIME_TICK for COUNTDOWN state

 

See Figure 7, the TIME_TICK event in the COUNTDOWN state.

Whenever the TIME_TICK event happens and when the sub-second field is 10 (that means 1000 milliseconds has elapsed), we should decrease the current time by 1 second. So, the COUNTDOWN happens every 1 second. Go to Internal Transition and add an Internal Transition TIME_TICK.

Guard is, when events parameter sub second =10 (e->ss==10). And Action is, we have to decrement –mobj->c_time, it is counting down. So, c_time is decremented. 

After that, you have to do ‘++’ to the elapsed_time.  And you have to display mobj->c_time. When ss=10, decrement the current time, increment the elapsed time, and show the current time. But you cannot decrement c_time when you have reached 0. So, during down counting, when you reach 0, the application should automatically go to the IDLE mode because the countdown is over.

 

Implementing PAUSE state
Figure 8. Choice pseudo state

 

Figure 8 shows the Choice Pseudo State. This is to make a choice. The choice Pseudo state will have 2 sections. One is incoming transition and outgoing transitions.

 

Implementing PAUSE state
Figure 9.Choice state

 

A choice state is a Pseudo state. It is represented in a state machine diagram, a UML state machine as a diamond-shaped symbol.

 

As you can see in Figure 9, it has got a single incoming transition and two or more outgoing transitions. We use this to evaluate which external transition should be taken based on the evaluation of an Extended state variable. 

 

Implementing PAUSE state
Figure 10. Choice Pseudostate specification

 

The Entry action is not required For the COUNTDOWN state. 

Implementing PAUSE state
Figure 11. Exit action for COUNTDOWN state

 

In this application, we are tracking the productive time(p_time). Whenever the COUNTDOWN is over, it has to update the p_time.

The Exit action will be mobj->p_time should be updated. +=mobj->e_time, so you should add the elapsed time to the productive time. mobj->e_time=0; You have to make that as 0. Because you are leaving that state. That’s why, elapsed_time must be reset to 0. That’s an exit action for the COUNTDOWN state.

 

FastBit Embedded Brain Academy Courses

click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.