Python object persistence

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:
		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 = [
fsm1.stateXitionFunction = [ [0,0,0],
[0,0,0] ]

print fsm1.stateXition
print fsm1.stateXition[0][2]


print fsm1.state
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]]
trig3 causes transition from s1 to s3

Unless you persisted the state to a database, file, or gateway memory tag, and read that state upon instantiating the state machine there is no place where you could reliably do this.

There is no visibility across scopes. None of your clients share any memory with each other and they don’t share any memory with anything running in the gateway.

I think you may be trying to do too much in scripting here.

The SFC module would probably be a better choice for implementing a state machine.

FWIW, python objects instantiated at the top level in script modules are persistent in their respective scope until a designer saves/publishes the containing project, or any designer saves a global change, like a shared script. But as Kevin noted, there’s an instance in the gateway, an instance in each designer, and an instance in each client. Message scripts and tag change scripts could be used to mimic synchronization, but I can’t think of any way to guarantee it.

Actually, I don’t really need the finite state machine to be accessible from the client scripts.
I’ve implemented a PLC simulator as a gateway script that runs on a timer every 300 milliseconds.
It would work for me, if the data persists between runs of this script. Reviewing data when I had some errors in my code suggests that at least some of the script variables retain their values between runs, but I don’t know if this is behavior I can count on.

I’m pretty impressed, the gateway script reads about 1000 tags and writes back about 100 of them on each cycle.
Run time of the script is typically less than 6 ms.

If I can tolerate reinitializing the state matrices on each cycle, all that really needs to be saved to a tag is the state.

I’ll take a look at the SFC module.

Thank you kindly Kevin.

Just saw your post Mr Turmel. Your saying I can do what I want to do, so long as I keep it within the gateway scope, right?

[quote=“JimBranKelly”]Just saw your post Mr Turmel. Your saying I can do what I want to do, so long as I keep it within the gateway scope, right?[/quote]Basically, yes. If you are trying to do PLC-ish things in the gateway, you might also be interested in my Ethernet/IP module. :smiley: