Conditions#

Regular events fire only when a channel changes state. A condition, in contrast, is evaluated when a state is entered: if the configured channel (or global timer) is already in the specified state, the corresponding Condition{N} event fires immediately. This allows states to be skipped based on the current state of a channel rather than waiting for a change to occur.

In this example, Condition2 is met while Port2 is high. If that is the case when the state Port2Light is entered, the Condition2 event fires and the state machine advances to Port3Light without waiting for the one-second state timer to expire.

../../_images/conditions__light.svg ../../_images/conditions__dark.svg
 1from bpod_core.fsm import StateMachine
 2
 3fsm = StateMachine()
 4
 5fsm.set_condition(
 6    index=2,
 7    channel='Port2',
 8    value=True,  # condition is true when Port2 is high
 9)
10
11fsm.add_state(
12    name='Port1Light',
13    timer=1,
14    transitions={'Tup': 'Port2Light'},
15    actions={'PWM1': 255},
16)
17fsm.add_state(
18    name='Port2Light',
19    timer=1,
20    transitions={'Tup': 'Port3Light', 'Condition2': 'Port3Light'},
21    actions={'PWM2': 255},
22)
23fsm.add_state(
24    name='Port3Light',
25    timer=1,
26    transitions={'Tup': '>exit'},
27    actions={'PWM3': 255},
28)
 1{
 2  "states": {
 3    "Port1Light": {
 4      "timer": 1.0,
 5      "transitions": {
 6        "Tup": "Port2Light"
 7      },
 8      "actions": {
 9        "PWM1": 255
10      }
11    },
12    "Port2Light": {
13      "timer": 1.0,
14      "transitions": {
15        "Tup": "Port3Light",
16        "Condition2": "Port3Light"
17      },
18      "actions": {
19        "PWM2": 255
20      }
21    },
22    "Port3Light": {
23      "timer": 1.0,
24      "transitions": {
25        "Tup": ">exit"
26      },
27      "actions": {
28        "PWM3": 255
29      }
30    }
31  },
32  "conditions": {
33    "2": {
34      "channel": "Port2",
35      "value": true
36    }
37  }
38}
 1states:
 2  Port1Light:
 3    timer: 1.0
 4    transitions:
 5      Tup: Port2Light
 6    actions:
 7      PWM1: 255
 8  Port2Light:
 9    timer: 1.0
10    transitions:
11      Tup: Port3Light
12      Condition2: Port3Light
13    actions:
14      PWM2: 255
15  Port3Light:
16    timer: 1.0
17    transitions:
18      Tup: '>exit'
19    actions:
20      PWM3: 255
21conditions:
22  2:
23    channel: Port2
24    value: true