Skip to main content

Working in the Statechart Editor

In the statechart editor, you graphically implement a statechart.

Implementing a statechart

You can insert states which call methods or actions. When you insert transitions, you can define conditions for a state transition. When inserted, the condition is TRUE by default, as represented as _uml_icon_add_transition_condition.png. After that, you can change this value to call an action or method.

You can implement control structures by inserting pseudo states. To improve the overview, you can group states in composite states for which you implement a common behavior with completion transitions. You can use exception transitions to program the behavior in the event of an error.

CODESYS is started.

  1. Click File → New Project.

  2. Specify the name and location, and select the Standard Project template.

    The Standard Project dialog opens.

  3. For PLC_PRG, select the UML Statechart (SC) implementation language.

  4. In the device tree, click the program PLC_PRG.

    The editor consists of a window for the declaration and a window for the graphical implementation. In the ToolBox view, elements are listed which you can use to create a statechart.

    _uml_img_sc_simple_new_project.png
  5. Drag a Start state to the implementation.

    _uml_icon_start_state.png
  6. Click the start state.

    The _uml_icon_transition_completion_outgoing.png symbol for a completion transition is displayed.

  7. Click the _uml_icon_transition_completion_outgoing.png transition.

    A mouse pointer is displayed with a line as the transition and a pointer as the element.

  8. Click below the start state.

    A state is inserted.

  9. Double-click the default name.

    A line editor opens.

  10. Specify the name PowerOff.

    _uml_img_sc_simple_first_result.png
  11. Click the state, and then click the transition. Then click below the state.

    A transition is inserted with an additional state.

  12. Specify the name Idle.

  13. Insert a transition to still another state named DoIt.

  14. Hover over the state DoIt.

    Icons are displayed in the state to added actions or methods.

    _uml_img_simple_plc_chart_doit_focused.png
  15. Click the _uml_icon_sc_action_do.png symbol.

    The state is extended by a DO action.

  16. Hover over the DO symbol.

    The _uml_icon_sc_action_add.png symbol is displayed on the right.

  17. Click the icon.

    The New action object dialog opens. There you can configure the initial settings for a POU that is subordinate to the program PLC_PRG. The object can be either a method or an action.

  18. Select an action and click Add to exit the dialog.

    In the device tree, the new and empty action PLC_PRG_DoIt_DO is inserted below PLC_PRG. In the state DoIt, the DO action is listed by name. The action is opened in the editor.

  19. Declare control variables and local variables in the declaration part of PLC_PRG.

    PROGRAM PLC_PRG
    VAR
        iCounter: INT;
        bReady: BOOL;
        bDoIt: BOOL;
        bPowerOn : BOOL;
        bPowerOff : BOOL;
    END_VAR
  20. In the action PLC_PRG_DoIt_DO, implement code for flow and control.

    iCounter := iCounter + 1;
    IF iCounter > 100 THEN
        iCounter := 0;
        bReady := TRUE;
        RETURN;
    END_IF
  21. Hover over the DoIt state and click the _uml_icon_sc_action_exit.png symbol. Then click New in the section and confirm the dialog.

    For the state Doit, the EXIT action PLC_PRG_DoIt_EXIT was added.

  22. Implement the code there.

    bReady := FALSE;
    bDoIt := FALSE;
  23. Add a transition from the state DoIt to the state Idle.

  24. For the transition, click the _uml_icon_add_transition_condition.png symbol.

    The line editor opens. If you have selected the SmartCoding feature, then a list box with all declared variables is displayed.

  25. Select the Boolean variable bReady.

    The variable is used as a control variable to switch to the state Idle.

  26. Add a transition from the state Idle to the state PowerOff.

  27. For the transition, click the _uml_icon_add_transition_condition.png symbol.

    The line editor opens. If you have selected the SmartCoding feature, then a list box with all declared variables is displayed.

  28. Specify the control variable bPowerOff.

  29. In the device tree, select the program PLC_PRG and added an object Action named PowerDown.

  30. Implement the action.

    bPowerOn := FALSE;
  31. In the editor, select PLC_PRG.

  32. In the transition from the Idle state to the PowerOff state, click the _uml_icon_sc_transition_action.png symbol to call an action.

    A list box with actions is displayed.

  33. Select the action PowerDown.

    _uml_img_simple_plc_chart_powerdown.png
  34. Implement the condition from the state PowerOff to the state Idle.

    bPowerOn

  35. In the device tree, select the program PLC_PRG and added an object Action named PowerUp.

  36. Implement the code of the action.

    bPowerOff := FALSE;
  37. In the editor, select PLC_PRG.

  38. In the transition from the Idle state to the PowerOff state, click the _uml_icon_sc_transition_action.png symbol.

    A list box with actions is displayed.

  39. Select the PowerUp action and set the variable between Idle and DoIt to bDoIt.

    The statechart is implemented. All transitions have a condition. The actions which are called are implemented.

    _uml_img_sc_simple.png
  40. Click Build → Generate Code to compile the application.

    The compilation has completed without errors.

Editing element properties

  1. In the statechart editor, select an element.

    All properties of the element are listed in the Properties view.

  2. Edit the properties in the Value column.

Example 2. Example

You can edit the DoIt state in the Properties → Property view.

  1. Select the state Doit in the statechart editor.

    _uml_img_simple_plc_chart_doit_selected.png

    In the Properties view, the option In-cycle (1) is cleared.

  2. Select the option.

    The state is now in-cycle, not clocked by bus cycles.

    _uml_img_sc_simple_doit_incycle.png


Multiselection

When Selection is selected (default setting) in the ToolBox view, you can drag a box over multiple elements in the editor. Then all the captured elements are selected. You can also press the Ctrl key and click each element.

Create Image

You can create and use a graphic in PNG format from your statechart object.

  1. In the device tree, select a statechart object.

  2. In its context menu, click Create Image.

    The Create Image dialog opens.

  3. Select the desired options.

  4. Click OK to confirm the dialog.

    The image file is saved.

  5. Switch to your desktop.

  6. Open the link.

    A default editor opens and shows the image.

Example 3. Example
_uml_img_sc_simple_bmp.png