(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Turnstile FSM in C++

Description: Finite State Machine C++,SCXML and DOT representations

Keywords: decision_making

Tutorial Level: BEGINNER

Running the example

roslaunch decision_making_examples fsm_turnstile.launch

The Code

Following is the syntax for defining a simple state machine in C++.

   1 FSM(Turnstile)
   2 {
   3         FSM_STATES
   4         {
   5                 Locked,
   6                 Unlocked
   7         }
   8         FSM_START(Locked);
   9         FSM_BGN
  10         {
  11                 FSM_STATE(Locked)
  12                 {
  13                         FSM_TRANSITIONS
  14                         {
  15                                 FSM_ON_EVENT(COIN, FSM_NEXT(Unlocked));
  16                                 FSM_ON_EVENT(PUSH, FSM_NEXT(Locked));
  17                         }
  18                 }
  19                 FSM_STATE(Unlocked)
  20                 {
  21                         FSM_TRANSITIONS
  22                         {
  23                                 FSM_ON_EVENT(COIN, FSM_NEXT(Unlocked));
  24                                 FSM_ON_EVENT(PUSH, FSM_NEXT(Locked));
  25                         }
  26                 }
  27         }
  28         FSM_END
  29 }

When compiled the code above generates the following scxml from C++.

   1 <?xml version="1.0"?>
   2 <!-- from file: src/example1.cpp -->
   3 <scxml>
   4    <state name="Turnstile" initialstate="Locked" id="/Turnstile">
   5       <state name="Locked" id="/Turnstile/Locked">
   6          <transition event="COIN" target="/Turnstile/Unlocked"></transition>
   7          <transition event="PUSH" target="/Turnstile/Locked"></transition>
   8       </state>
   9       <state name="Unlocked" id="/Turnstile/Unlocked">
  10          <transition event="COIN" target="/Turnstile/Unlocked"></transition>
  11          <transition event="PUSH" target="/Turnstile/Locked"></transition>
  12       </state>
  13    </state>
  14 </scxml>

When compiled the code above generates the following dot diagram from the scxml.

Canonical FSM example

Wiki: decision_making_examples/Tutorials/Turnstile (last edited 2014-01-12 14:46:04 by IgorMakhtes)