fsm tutorial. fsm is a finite state machine editor that generate the structure in ‘c language’....

29
FSM Tutorial

Upload: chaim-morfitt

Post on 01-Apr-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial

·i·

·Disabled·

·Running·#entry: EnableLoadX();#during: UpdateTimerX();#else: ProcessFuncX();#exit: DisableLoadX();

·Cranking·

/InitFuncX();

[2]ValidKey()/UpdateActivationCounter()

[2]StartMotor()

!StartMotor()

·Sleep·

[3]SleepCondition()!SleepCondition()

[1]!ValidKey()

·FailSafe· ReducedFuncX();#exit: DisableLoadX();

[4]Error()

[2]

[1]FailSafeEnabled()

[1]DisableFuncX()

Page 2: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial• FSM is a finite state machine

editor that generate the structure in ‘C language’.

• If the functionality can be represented using state diagrams, this tool generate the structure automatically.

• The code added to the states and transitions is copied into the generated code.

• Permit to have a graphic documentation of the software synchronised with the code.

• Its easy to show, and understand, a functionality using a state diagram that a C code.

Page 3: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - State propertiesThe states have these fields:

• Index: used to sort the states in the in the final C-code.

• Name: name used into the C-code to refer this state.

• Entry: Code executed one time when this state gets active.

• During: Code executed always when this state is active.

• Else: Code executed, if all transitions are false, when this state is active.

• Exit: Code executed one time when this state leaves.

·Running·#entry: EnableLoadX();#during: UpdateTimerX();#else: ProcessFuncX();#exit: DisableLoadX();

Page 4: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - State propertiesThe states have these options:

• State confirmation: Activates a function that return TRUE (8bit) if this state is active:

FsmName_IsIn_StateName() (Used in hierarchical FSM’s)

• State jump: Activates a function that force that change the state active to this: FsmName_Goto_StateName(); (Designed for hierarchical FSM’s, please avoid other use)

Node mode:

• Initial: is the state active when the fist time.

• Final: mark this state like final.

• Standard: return this state to the default.

·Running·#entry: EnableLoadX();#during: UpdateTimerX();#else: ProcessFuncX();#exit: DisableLoadX();

Page 5: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Transition propertiesTransitions have these fields&options:

• Priority: is the priority used to analyse the transitions, if a more priority condition (lower number) is true the rest of conditions won’t be analysed.

• Transition condition: test condition that had to be true to modify the state active. If it’s empty it use ‘1’.

• Transition action: Code executed one when the condition is true.

• This Self-transition cause exit-entry: let us to decide if a self-transition had to cause these actions. (only valid for selfTrans.)

• Last transition: Ensure that even if we add new transitions this will be the last one.

Page 6: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these fields(1/2):

• Name: Is the same name of the FSM file that will be used for the C-modules (.c & .h) and also for the task function.

• Parameters: parameter list that will implement the task function. If it’s empty it use void.

• Headers: general comment to be included on the top of the module and on top of the task FSM function.

• Initialisation: Code area to put includes, prototypes and variable declarations.

Page 7: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these fields(2/2):

• PreProcess: Code executed always before call the state dispatcher. Can include local variable declarations.

• DefaultCase: Code executed if the dispatcher index don’t match wit a valid state. This can happens only if we manipulate this index. Only is used whit generation models based on “switch cases”.

• ProstProcess: Code executed always after call the state dispatcher.

• Extra Code: Code area to implement optionally C-functions.

Page 8: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these options(1/4):

• History: determine if this diagram had to be a historic behaviour or not. Applicable only to hierarchical FSM’s where a child diagram called from a state optionally can be initialised to its initial state when the state of the parent FSM changes.

• Restart Function: Activates a function that force that change the state active to the initial: FsmName_Init();

(This was used for hierarchical FSM’s but today we can use the History option, please avoid other use)

Page 9: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these options(2/4):

• Final node confirmation: State confirmation: Activate a function that return TRUE (8bit) if one final state is active:

FsmName_End() (By now this is the only use of final states)

• Debug Info: implements 3 interfaces; FSM_NAME(), FSM_STATE_NAME() & FSM_TRANS_NUMBER() that can be used for debugging. The compilation had to define FSM_DEBUG to enable these interfaces:#if defined(FSM_DEBUG)

#define FSM_STATE_NAME() debug_stateNames[ind_TaskX] #define FSM_TRANS_NUMBER() debug_transNumber #define FSM_NAME() "TaskX"#else #define FSM_STATE_NAME() #define FSM_TRANS_NUMBER() #define FSM_NAME()#endif

Page 10: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these options(3/4):

• Ansi-C comments: The generator convert all lines that have // ~~~ comment to /* ~~~ */.

• Print Text Boxes: The text boxes (comments) are also printed like C- comments in the bottom of the ‘.c file’. (This feature must be enhanced in future versions)

• Use bitfields: Applicable only to hierarchical FSM’s compiled with the format hierarchical switch case. This format join all fsm’s (parent & child-diagrams) in a single file, then all entry flags variables can be packed in a bit structure.

Page 11: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - General propertiesThe FSM has these options(4/4):

• Implement interfaces like macros: Applicable only to hierarchical FSM’s compiled with the format hierarchical switch case. This format join all fsm’s (parent & child-diagrams) in a single file. Here all interfaces, that originally were developed to improve the hierarchical navigability, can be declared like macros. This encapsulate more the code and optimise the execution.

• Color buttons: Let us to modify the state colors. Note you can create a template with your favourite colors.

Page 12: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Buttons:

• New, Open, Save & Generate C-Code (using the predefined method).

• Print & Print Preview.

• Cut, Copy, Paste: (Copy & paste work only with remark boxes).

• Find Text: Find and optionally replace text.

• Undo last action & Redo last undo.

• Reduce Font, Change Font, Enlarge Font.

• Lock & Unlock diagram,

• InsertMode: Change the editor between insert mode and select mode.

• AutoSize: Set if the state size can be adjusted automatically or manually.

• Properties edit: Opens the properties menu (equivalent to do a double click in the diagram)

• Insert Remark: Inserts a new remark box.

• Open Parent diagram & Open child diagram (hierarchical navigation)

Page 13: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - File(1/2):

• New: opens a new diagram.

• New Child opens a new diagram into a subdirectory.(this directory is named like the parent).

• Open opens a existent diagram (only fsm format).

• Close closes the diagram.

• Save saves the diagram.

• Save As… saves the diagram with a new name.

• Save All saves all open diagrams.

• Save As enhanced metafile… saves the diagram like a metafile.

• Save As HTML File… saves the diagram like HTML.

• Generate List… saves the diagram info in a list.

Page 14: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - File(2/2):

• Generate C-code:- Pointer Array: Generates the code using a function pointer array dispatcher.

- Direct Pointers: Generates the code using a function pointer dispatcher.(Probably this pointer methods won’t be updated in future versions due that have more

disadvantages that the switch case methods)

- Switch Case: Generates the code using a switch case dispatcher.

- Hierarchical Switch Case: Generates the code of a diagram and all child diagrams into a single module, using switch case dispatchers.

(We tend to use this format because is more polyvalent)

• Printer Setup, Print Preview & Print.

• List of recent files.

• Exit

Page 15: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - Edit(1/2):

• FSM Properties: Opens the properties menu (equivalent to do a double click in the diagram)

• Insert Remark: Inserts a new remark box.

• Font- Change: Menu to edit font parameters

- Fixed Pitch: Let us to select fixed or non fixed pitch fonts.

• Find: Find and optionally replace text.

• Undo: Undo last action.

• Redo: Redo last undo.

• Cut, Copy & Paste: (Copy & paste work only with remark boxes).

• Delete.

• Export as Enhanced Metafile: Puts the selected items into clipboard.

Page 16: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - Edit(2/2):

• Select All: Select all items (states, transitions & Remark boxes)

• Select Mode: Change the editor between insert mode and select mode.

• Autosize Nodes: Set if the state size can be adjusted automatically.

• Locked: Lock & Unlock diagram.

Page 17: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - View:

• Toolbar: Hide or show the toolbar.

• Status Bar : Hide or show the status bar.

• Hide Comments ‘//’ : Hide all comment lines that star with //.

• Grid- Show grid

- Snap to grid

- Grid Size (60, 120, 180, 240, 300, 360).

• Zoom: 150%, 100% (&Normal), 75%, 50%, 25%, Fit in Window

• Zoom Window

Page 18: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Menus & buttons

Menu - Window:

• Window

• Cascade

• Tile Horizontally

• Tile Vertically

• Reorganise

Menu - ? :

• Help

• About

Page 19: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Basic navigation

Adding a state:

• In insert mode press the left mouse key move the mouse one cm and release.

Edit a state properties:

• Double click the left mouse key over the state.

• (Sometimes it needs first to select the state with a single click).

Move states:

• Select, Drag & drop.

Page 20: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Basic navigation

Adding a transition:

• Select the source state with a click (left mouse key).

• Drag the central connection point (small black square) and drop over the destination state.

Edit a transition properties:

• Double click the left mouse key over the transition text.

• (Sometimes it needs first to select the transition with a single click).

Move transition text:

• Select, Drag & drop.

Page 21: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Basic navigation

Modifying transition lines:

• Its possible to convert in curve lines with the mouse.

• A double click over a line convert it in a straight line.

Change the source state for a transition:

• Select the new source state with a click.

• Drag the central connection point and drop over the transition.

Change the destination for a transition:

• Select the transition with a click.

• Drag the central connection point (if it’s empty sometimes is difficult) and drop over the destination state.

Page 22: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Command Line options

• This command are focused in the integration of FSM into a make file.• These commands override the default options.

path\FSM.exe file.fsm [/?][/C[S|P|A]][/H[L]][/W][/O<path>][/E<path>][/D+|-][/AC+|-]

/C generates default C-code /CS generates "switch case" code

/CP generates "direct pointer" code /CA generates "pointer array" code

/CH generates "hierachical switch case" code

/LS generates list

/H generates HTML /HL generates HTML with links in the same path

/W generates WMF

/O output path

/E error file output path

/D+ with debug info /D- without debug info

/AC+ ANSI comments: "/* */” /AC- not ANSI comments: "// "

/PT+ print the text boxes into the code /PT- don't print the text boxes into the code

/BF- don't use bitfields /BF+ use bitfields to save RAM

/? Help

Return: A file ("FSM_ERR.LOG"), in /Epath, with the error list, if FSM couldn't generate code.

Page 23: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Basic concepts about Hierarchical implementations :

• Are useful to divide a problem into small sub-problems.– Example of a three level hierarchy:

• The first level decides if the functionality is “disabled”, “running” or “sleep”.

• The second level implement the “running” main lines: “On”, “Off”, “waiting inputs”, etc.

• The third level does the basic functions. E.g: if “On” handle a intermittent output, a child-FSM of “On” can control the flashing of the output timings.

• Are built nesting one or more child-FSM into other parent-FSM. The child-FSM is a function called into a “during” or “else” action of a parent-FSM.

• A child-FSM file should be found into a subdirectory called like their parent. This enables the editor automatic hierarchical capabilities. – TaskX.fsm

– TaskX\ProcessFuncX.fsm

– now “ProcessFuncX.fsm” will be a child of “TaskX.fsm” if ProcessFuncX() is called from a ”during” or “else” action of TaskX().

Page 24: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

History concept:

• A child-FSM can have a historical behaviour if only is started to its initial state on a program reset.

• A child-FSM can have a standard behaviour if is initialized (the state index points to the initial state again) when the parent-FSM state changes.

• A <history> remark appears into the initial state to distinguish the historical FSMs.

Page 25: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Hierarchical transition examples:

• The implementation of a transition is easy if the source and destination are in the same level. This means in the same diagram.

– This hierarchy:

– Is Implemented in two FSMs called Parent_FSM() & Child_FSM():note: the child is standard (not historical) then on every SwitchOff()+SwitchOn() the child will start in LoadOn state again.

Page 26: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Hierarchical transition examples:

• If we have a transition from a parent level state to a child level state. We should use the interface function called ”Child_FSM_GoTo_LoadOff();” to implement the same behaviour.– This hierarchy:

– Is Implemented in two FSMs called Parent_FSM() & Child_FSM():

Page 27: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Hierarchical transition examples:

• If we have a transition from a child level state to a parent level state. We should use the interface function called ”Child_FSM_IsIn_LoadOff()” to implement the same behaviour.– This hierarchy:

– Is Implemented in two FSMs called Parent_FSM() & Child_FSM():

Page 28: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Execution sequence on every call to a two level diagram if the child-FSM is called into a “else action”:• Parent-state “entry action” (if it wasn’t the active state on last call).• Parent-state “during action”. • Parent-state “transition conditions” analysis in sequence.

– If a transition condition is valid:• Parent-state “exit action”. • Parent-state “transition action” (of the accepted transition). • END

• Parent-state “else action” (here is the call to the child-Fsm).– Child-state “entry action” (if it wasn’t the active child-state on last call).– Child-state “during action”. – Child-state “transition conditions” analysis in sequence.

• If a transition condition is valid:– Child-state “exit action”. – Child-state “transition action” (of the accepted transition). – END

Page 29: FSM Tutorial. FSM is a finite state machine editor that generate the structure in ‘C language’. If the functionality can be represented using state diagrams,

FSM Tutorial - Hierarchical files

Execution sequence on every call to a two level diagram if the child-FSM is called into a “during action”:• Parent-state “entry action” (if it wasn’t the active state on last call).• Parent-state “during action” (here is the call to the child-Fsm).

– Child-state “entry action” (if it wasn’t the active child-state on last call).– Child-state “during action”. – Child-state “transition conditions” analysis in sequence.

• If a transition condition is valid:– Child-state “exit action”. – Child-state “transition action” (of the accepted transition). – END

• Parent-state “transition conditions” analysis in sequence.– If a transition condition is valid:

• Parent-state “exit action”. • Parent-state “transition action” (of the accepted transition). • END

• Parent-state “else action”.