Recipe to Demonstrate a State Machine
This recipe creates a StateSpace, loads in a set of tables, and instantiates a state machine. The state machine is designed to control a system with one switch and 2 lights.
Each time the StateMachine switch input sees an "on", its lights outputs will go to a different "on" state (they count in binary). Anytime the switch is turned off, both lights will go off. Since there is not an actual switch for stimulating the StateMachine, "on"s and "off"s are simulated by an EventGenerator device, stimulated by an oscillator. The EventGenerator generates 5 "on"s followed by an "off", one event each time it sees an "on" value from the oscillator.
The Script is as follows:
# Loads some state machine tables into state space and builds a state machine.
# Hooks state machine input to a state generator powered by an oscillator.
# create a state space for holding tables and state machines
# load state machine tables into state space from a file
run_script("Scripts/Device/Virtual/StateSpace/loadTablesFile", file_name:"Scripts/examples/state_space/state_space_tables.tbl", state_space_id:"state_space1")
# Create a state machine in state space from the "switch_2lights_count" table.
# The table comes from the tables file loaded above.
# The table has a switch and 2 light terminals. Turning the switch on changes the
# state of the 2 light outputs. Turning the switch off turns
# off both lights.
# When creating a state machine from a table, the state machine name and the table inputs
# and outputs need to be specified (see table definition to see the table's inputs and
# The state machine "name" can be any name, but must be unique within a state space.
# The table inputs and outputs, "switch", "light1", and "light2", are used to define
# the terminal names the newly created state machine will have.
run_script("Scripts/Device/Virtual/StateSpace/StateMachine", state_space_id:"state_space1", table_name:"switch_2lights_count", switch:"my_switch", light1:"my_light1", light2:"my_light2")
# build an oscillator
run_script("Scripts/Device/Virtual/Logic/Active/Oscillator" ,id:"osc", period:5)
# build an EventGenerator (generates 5 ons and 1 off, then repeats)
run_script("Scripts/Device/Virtual/EventGenerator", id:"event_generator", triggers:"on", values:"on,on,on,on,on,off")
# wire everything together
wire("osc:out", "event_generator:in") # osc causes EventGenerator to change state
wire("event_generator:out", "state_space1:my_switch") # EventGenerator stimulates ss