Light Chasing#

Each state can use actions to set output channels and transitions to react to input events. A state without a state timer transition waits indefinitely, so the state machine only advances when the expected input occurs.

In this example, each state lights the LED of one port (actions PWM1 to PWM3 at maximum intensity) and waits for that same port to go high before advancing. After the light has been chased across ports 1 to 3 twice, the state machine exits.

../../_images/light_chasing__light.svg ../../_images/light_chasing__dark.svg
 1from bpod_core.fsm import StateMachine
 2
 3fsm = StateMachine()
 4
 5fsm.add_state(
 6    name='Port1Active1',
 7    transitions={'Port1_High': 'Port2Active1'},
 8    actions={'PWM1': 255},
 9)
10fsm.add_state(
11    name='Port2Active1',
12    transitions={'Port2_High': 'Port3Active1'},
13    actions={'PWM2': 255},
14)
15fsm.add_state(
16    name='Port3Active1',
17    transitions={'Port3_High': 'Port1Active2'},
18    actions={'PWM3': 255},
19)
20fsm.add_state(
21    name='Port1Active2',
22    transitions={'Port1_High': 'Port2Active2'},
23    actions={'PWM1': 255},
24)
25fsm.add_state(
26    name='Port2Active2',
27    transitions={'Port2_High': 'Port3Active2'},
28    actions={'PWM2': 255},
29)
30fsm.add_state(
31    name='Port3Active2',
32    transitions={'Port3_High': '>exit'},
33    actions={'PWM3': 255},
34)
 1{
 2  "states": {
 3    "Port1Active1": {
 4      "transitions": {
 5        "Port1_High": "Port2Active1"
 6      },
 7      "actions": {
 8        "PWM1": 255
 9      }
10    },
11    "Port2Active1": {
12      "transitions": {
13        "Port2_High": "Port3Active1"
14      },
15      "actions": {
16        "PWM2": 255
17      }
18    },
19    "Port3Active1": {
20      "transitions": {
21        "Port3_High": "Port1Active2"
22      },
23      "actions": {
24        "PWM3": 255
25      }
26    },
27    "Port1Active2": {
28      "transitions": {
29        "Port1_High": "Port2Active2"
30      },
31      "actions": {
32        "PWM1": 255
33      }
34    },
35    "Port2Active2": {
36      "transitions": {
37        "Port2_High": "Port3Active2"
38      },
39      "actions": {
40        "PWM2": 255
41      }
42    },
43    "Port3Active2": {
44      "transitions": {
45        "Port3_High": ">exit"
46      },
47      "actions": {
48        "PWM3": 255
49      }
50    }
51  }
52}
 1states:
 2  Port1Active1:
 3    transitions:
 4      Port1_High: Port2Active1
 5    actions:
 6      PWM1: 255
 7  Port2Active1:
 8    transitions:
 9      Port2_High: Port3Active1
10    actions:
11      PWM2: 255
12  Port3Active1:
13    transitions:
14      Port3_High: Port1Active2
15    actions:
16      PWM3: 255
17  Port1Active2:
18    transitions:
19      Port1_High: Port2Active2
20    actions:
21      PWM1: 255
22  Port2Active2:
23    transitions:
24      Port2_High: Port3Active2
25    actions:
26      PWM2: 255
27  Port3Active2:
28    transitions:
29      Port3_High: '>exit'
30    actions:
31      PWM3: 255