FSM Lecture 61: Exercise-007 Drawing an HSM

 

Exercise-007 Drawing an HSM

 

 

In the exercise 007ClockAlarm, let’s create a new folder and call it ‘qm.’ Now, we have to keep the new model file, which we will create using the ‘qm’ tool. Let’s launch the qm tool. 

  1. Go to File and click on New model.
  2. Select Framework is ‘qpn’ → Model template is None → Name for your project or model; let’s call this ClockAlarm. And provide the path of the ‘qm’ folder. I will open that in the File Explorer to copy this path. Just open that location and select ‘qm,’ and click OK.  
Figure 1. Creating a New model file
Figure 1. Creating a New model file

 

You must see ‘qpn’ as shown in Figure 2.

Figure 2. qpn framework
Figure 2. qpn framework

 

After that, before creating a state machine model, you must create a package. Right-click over the project ClockAlarm and select Add Package, as shown in Figure 3. 

Figure 3. Creating a package
Figure 3. Creating a package

 

‘Package’ is nothing but it’s a group of different elements, and by means of a package, you can provide a namespace. The project may have different packages. The project may contain any number of packages. And if you have any plan to reuse certain variable names, attribute names, or function names in different packages, you can assign a different namespace.

You can get more information about packages in the Working with Packages – QM  user manual, shown in Figure 4.

Figure 4. User manual of package
Figure 4. User manual of package

 

It is used to “group elements, and to provide a namespace for the grouped elements.” A package may contain other packages. Inside the package, you can create classes, create free attributes, and create free operations, directories, etc.

 

Figure 5. Property editor of package
Figure 5. Property editor of package

 

It also provides a namespace option here. You can assign a name for a namespace if you plan to use more than one package.  

For the stereotype option, select ‘components’ here. 

You can give a name for the package; I’ll provide HSMs ( Figure 5). That’s how you create a package.

 

Under the package, let’s create a class. 

  1. Let’s click on the package HSMs and select Add class.
  2. This is our main application structure name; let’s give Clock_Alarm. And select the superclass. The superclass is ‘QHsm.’  So, choose qpn::QHsm. Our Clock_Alarm is derived from QHSM. Then save.(Figure 6) 
Figure 6. Creating a Class
Figure 6. Creating a Class

 

Now we can create an attribute, as shown in Figure 7. 

Figure 7. Creating an Attribute
Figure 7. Creating an Attribute
  1. Click on that Class name and select Add attribute.
  2. The First attribute is the current time; the type is uint32_t; visibility is private; this is a non-static attribute. 
  3. After that, let’s create one more attribute. Name as temp_time; type is uint32_t; visibility is private. Like that, you create a couple of attributes.

 

Now also add a directory and add files to keep the generated code. 

  1. Go to your package HSMs. Right-click and select Add directory.
  2. Let’s give the path for the directory ‘../src’. We are going to keep all our source files in ‘src.’
Figure 8. Adding a Directory
Figure 8. Adding a Directory

 

Next, let’s add a file. 

  1. Right-click on the ‘../src’ directory and select Add File.
  2. Name the source file as ClockAlarm_SM.cpp; that’s a ‘cpp’ file. And again, add another file, ClockAlarm_SM.h, as shown in Figure 9. Currently, I’ll keep them as internal files. 
Figure 9. Adding a files to src directory
Figure 9. Adding a files to src directory

 

And now, let’s provide the include guards for the header file, as shown in Figure 10.

Drawing an HSM(hierarchical state machine)
Figure 10. Include guards

 

Now, let’s add the state machine. State machine we add to the Class. Right-click on the ClockAlarm, and select Add State Machine. 

Drawing an HSM(hierarchical state machine)
Figure 11. Creating a state machine

 

Double click on SM, you get the canvas to draw your state machine, shown in Figure 12.

Drawing an HSM(hierarchical state machine)
Figure 12. State machine canvas

 

You can draw a state machine like this shown in Figure 13.

Drawing an HSM(hierarchical state machine)
Figure 13. Whole state machine

 

You can get all the required state machine components in canvas, like states, transitions, choice segment, initial transition, etc(Figure 14). Now click on the state; you need to drag anything, click it on canvas. That’s it.

Drawing an HSM(hierarchical state machine)
Figure 14. State machine Components

 

Drawing an HSM(hierarchical state machine)
Figure 15. Drawing a state machine

 

Click on the state, and you can change the name. I’ll call it Ticking, Clock, Settings, Clock_Setting, and Alarm_Setting.

Let’s draw some transitions. First of all, the initial state. When the application starts, the initial state will be ticking. It is already ready-made, the initial transition is available. Just click on this initial transition, and you have to draw. 

Now, let’s draw a transition from Ticking state to Clock_Setting state. When the signal ‘SET’ arrives, it should transit to Clock_setting. Just take the transition component and draw. By default, it names it TRIG1; you can select that component and change the name. We’ll use capital letters for all the signals’ SET’. And when ‘OK’ arrives, when the state is Ticking, it transits to the Alarm_setting state. Take the transition component and draw. Name it as ‘OK.’

And you give one superstate ‘Setting,’ then let’s draw ‘ABRT’ from settings state to Ticking. And after that, the transition from the setting state to ticking. Take the transition component, and draw. Name it as OK, as shown in Figure 15.

 

Drawing an HSM(hierarchical state machine)
Figure 16. Alarm_notify state

 

We need one more state for Alarm notification; I will draw that, as shown in Figure 16. And I’ll call this Alarm_Notify, and it happens with the event Alarm. 

When it is in the Alarm_Notify state, it will show the notification. It will say to the user, “Hey, Alarm has happened.” So, the user says OK. And when the user presses OK here, it should come back to the Clock state, but it should go to the history state.

Let’s define the history for Clock. We need deep history. History for the clock can be achieved by using a history state on states any of the boundary line. I take the transition to history component, and I put that on the boundary. You have to put that on the boundary. 

Transition to history is a history of Clock. If the history is not available, then this is a default transition. This signifies the default transition. And we’ll take one transition from Alarm_Notify to history. I’ll call this OK. Save it. Complete up to here.

 

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.