-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateMachine.py
More file actions
executable file
·171 lines (125 loc) · 6.08 KB
/
stateMachine.py
File metadata and controls
executable file
·171 lines (125 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
import time
import string
import os
import beamline_support
from beamline_support import *
#the following is a list of things the machine is interested in, and their default vals at startup. PVs channels are created for these
#and stored in stateChannelDict, indexed by these var names.
stateVars = {"collimator":0.0,"aperature":0.0,"lamp":0,"beamStop":0.0}
stateDefinitions = [{"stateName":"Maintenance","stateDef":{},"safeTransitions":[{"stateName":"SampleExchange","transitionProc":"transProcM2SE"}]}, \
{"stateName":"BeamLocation","stateDef":{"collimator":{"hi":0.0,"low":0.0},"aperature":{"hi":0.0,"low":0.0},"lamp":{"hi":0.0,"low":0.0},"beamStop":{"hi":0.0,"low":0.0}}, "safeTransitions":[{"stateName":"SampleExchange","transitionProc":"transProcBL2SE"},{"stateName":"Maintenance","transitionProc":"transProcBL2M"}]}, \
{"stateName":"DataCollection","stateDef":{"collimator":{"hi":0.0,"low":0.0},"aperature":{"hi":0.0,"low":0.0},"lamp":{"hi":0.0,"low":0.0},"beamStop":{"hi":10.0,"low":10.0}},"safeTransitions":[{"stateName":"SampleAlignment","transitionProc":"transProcDC2SA"},{"stateName":"Maintenance","transitionProc":"transProcDC2SM"}]}, \
{"stateName":"SampleAlignment","stateDef":{"collimator":{"hi":0.0,"low":0.0},"aperature":{"hi":20.0,"low":20.0},"lamp":{"hi":0.0,"low":0.0},"beamStop":{"hi":0.0,"low":0.0}},"safeTransitions":[{"stateName":"SampleExchange","transitionProc":"transProcSA2SE"},{"stateName":"DataCollection","transitionProc":"transProcSA2DC"},{"stateName":"Maintenance","transitionProc":"transProcSA2SM"}]}, \
{"stateName":"SampleExchange","stateDef":{"collimator":{"hi":0.0,"low":0.0},"aperature":{"hi":0.0,"low":0.0},"lamp":{"hi":0.0,"low":0.0},"beamStop":{"hi":11.0,"low":20.0}},"safeTransitions":[{"stateName":"SampleAlignment","transitionProc":"transProcSE2SA"},{"stateName":"BeamLocation","transitionProc":"transProcSE2BL"},{"stateName":"Maintenance","transitionProc":"transProcSE2M"}]}]
global var_channel_list
var_channel_list = {}
global beamline, beamlineStateChannel
beamlineStateChannel = None
beamline = "john"
global command_list,is_first
command_list = []
is_first = 1
def getCurrentState():
global beamlineStateChannel
for i in xrange(1,len(stateDefinitions)): #skipping maintenance for now
stateTest = 1
for key in stateDefinitions[i]["stateDef"].keys():
high = stateDefinitions[i]["stateDef"][key]["low"]
low = stateDefinitions[i]["stateDef"][key]["hi"]
if (stateVars[key] <= high and stateVars[key] >= low):
pass
else:
stateTest = 0
break
if (stateTest == 1):
beamline_support.pvPut(beamlineStateChannel,i)
return stateDefinitions[i] # if I made it here, then I passed all of the tests for a state
beamline_support.pvPut(beamlineStateChannel,0)
return stateDefinitions[0] # if I made it here, then none of the states passed all tests, so it must be maintenance
def gotoState(stateName):
print "trying to go to " + stateName
machineState = getCurrentState()
for i in xrange(len(machineState["safeTransitions"])):
if (machineState["safeTransitions"][i]["stateName"] == stateName):
print "OK to transition"
print machineState["safeTransitions"][i]['transitionProc']
exec machineState["safeTransitions"][i]['transitionProc']+"()"
return 1
return 0
def transProcM2SE():
print "transition Maintenance to Sample Exchange"
def transProcSE2SA():
print "transition Sample Exchange to Sample Alignment"
pvPut(var_channel_list["beamStop"],0)
pvPut(var_channel_list["aperature"],20)
def transProcSA2SE():
print "transition Sample Alignment to Sample Exchange"
pvPut(var_channel_list["beamStop"],11)
pvPut(var_channel_list["aperature"],0)
def transProcBL2SE():
print "transition Beam Location to Sample Exchange"
pvPut(var_channel_list["beamStop"],11)
def init_var_channels():
global var_channel_list,beamlineStateChannel
beamlineStateChannel = beamline_support.pvCreate(beamline + "_comm:beamlineState")
beamline_support.pvPut(beamlineStateChannel,0)
for varname in stateVars.keys():
var_channel_list[varname] = beamline_support.pvCreate(beamline + "_comm:" + varname)
# beamline_support.pvPut(var_channel_list[varname],stateVars[varname])
add_callback(var_channel_list[varname],var_list_item_changeCb,varname)
def var_list_item_changeCb(epics_args, user_args):
print "in callback " + str(epics_args['pv_value']) + " " + user_args[0]
if (ca.dbf_text(epics_args['type']) == "DBF_CHAR"):
stateVars[user_args[0]] = beamline_support.waveform_to_string(epics_args['pv_value'])
else:
stateVars[user_args[0]] = epics_args['pv_value']
print stateVars
def comm_cb(epics_args, user_args):
global command_list,is_first
# print waveform_to_string(epics_args['pv_value'])
if (is_first == 0):
command_list.append(waveform_to_string(epics_args['pv_value']))
else:
is_first = 0
def process_input(s):
input_tokens = string.split(s)
if (len(input_tokens)>0):
first_token = input_tokens[0]
if (first_token == "q"):
sys.exit()
else:
if (len(input_tokens)>0):
command_string = "%s(" % input_tokens[0]
for i in xrange(1,len(input_tokens)):
command_string = command_string + "\"" + input_tokens[i] + "\""
if (i != (len(input_tokens)-1)):
command_string = command_string + ","
command_string = command_string + ")"
try:
print command_string
# from my_macros import *
exec command_string
except NameError:
error_string = "Unknown command: " + command_string
print error_string
except SyntaxError:
print "Syntax error"
except KeyError:
print "Key error"
except TypeError:
print "Type error"
def main():
init_var_channels()
comm_pv = pvCreate(beamline + "_comm:stateCommand_s")
add_callback(comm_pv,comm_cb,0)
while 1:
if (len(command_list) > 0):
print "command list len " + str(len(command_list))
process_input(command_list.pop(0))
print "Command> "
time.sleep(.1)
getCurrentState()
main()
if __name__ == '__main__':
main()