Being new to ignition, I’m unclear about the persistence of a variable that is defined in a python global/gateway or component script. In particular, I’m looking to implement a finite state machine that will change state based on operator input and changes to various tags. Where can I create an instance of my finite state machine so that it will be visible to all scripts in my project?
I want to define a finite state machine class that looks something like this:
[code]class FiniteStateMachine(object):
stateXition = [] # Two dimensional list of new states, row indexed by state, and column indexed by triggerEvent
stateXitionFunction = [] # Two dimensional list of functions, row indexed by state, and column indexed by triggerEvent
states = {}
triggers = {}
state = 0
def newState(self, triggerEvent):
trigger = self.triggers[triggerEvent]
newState = self.stateXition[self.state][trigger]
if newState != self.state:
# execute state transition function if it is registered
func = self.stateXitionFunction[self.state][trigger]
if func != 0:
func()
self.state = newState
def registerStateTransition(self, stateName, triggerEvent, newState, func):
trig = self.triggers[triggerEvent]
ste = self.states[stateName]
newSte = self.states[newState]
self.stateXition[ste][trig] = newSte
self.stateXitionFunction[ste][trig] = func
def tryFSM():
fsm1 = project.fsm.FiniteStateMachine()
fsm1.triggers = {“trig1”: 0, “trig2”: 1,“trig3”:2 }
fsm1.states = {“s1”: 0, “s2”: 1,“s3”:2 }
fsm1.stateXition = [
[0,0,2],
[0,2,0],
[1,1,2]
]
fsm1.stateXitionFunction = [ [0,0,0],
[0,0,0],
[0,0,0] ]
print fsm1.stateXition
print fsm1.stateXition[0][2]
fsm1.registerStateTransition("s1","trig3","s3",f1To3)
fsm1.newState("trig3")
print fsm1.state
fsm1.newState("trig1")
print fsm1.state
def f1To3():
print “trig3 causes transition from s1 to s3”[/code]
Where can I define fsm1 so that it persists and is accessible to all the gateway and client scripts.
By the way, when tryFSM is executed, the output is:
[quote][[0, 0, 2], [0, 2, 0], [1, 1, 2]]
2
trig3 causes transition from s1 to s3
2
1[/quote]