-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.m
295 lines (262 loc) · 9.5 KB
/
StateMachine.m
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
classdef StateMachine < matlab.DiscreteEventSystem
properties
FUEL_Press;
LOX_Press;
FUEL_Vent;
LOX_Vent;
MAIN;
FUEL_Purge;
LOX_Purge;
IGNITE;
LOXLVL;
WATER_Flow;
ip;
sequence;
timers;
n;
KeyList;
app;
end
methods
% Default constructor
function obj = StateMachine(app)
obj.FUEL_Press = false;
obj.LOX_Press = false;
obj.FUEL_Vent = true;
obj.LOX_Vent = true;
obj.MAIN = false;
obj.FUEL_Purge = false;
obj.LOX_Purge = false;
obj.IGNITE = false;
obj.LOXLVL = 0;
obj.WATER_Flow = false;
obj.n = 1;
obj.KeyList = {
'FUEL_Press',
'LOX_Press',
'FUEL_Vent',
'LOX_Vent',
'MAIN',
'FUEL_Purge',
'LOX_Purge',
'IGNITE',
'WATER_Flow'
};
obj.app = app;
% debug parse
% parseSequence(obj, "sequence.json")
% tic
% start(obj.timers(1))
end
function out = stateToMessage(obj)
A = [ obj.FUEL_Press;
obj.LOX_Press;
obj.FUEL_Vent;
obj.LOX_Vent;
obj.MAIN;
obj.FUEL_Purge;
obj.LOX_Purge;
obj.IGNITE;
obj.WATER_Flow;
];
out = jsonencode(containers.Map(obj.KeyList,A));
% disp(out)
end
function responseParsed = poll(obj)
% Request a valve status update from the stand
url = strcat('http://',obj.ip,':3003/serial/valve/update');
options = weboptions;
options.RequestMethod = 'get';
options.Timeout = 1;
try
response = webread(url,options);
responseParsed = parseResponse(response);
% disp(response)
catch ME
% disp(ME)
if (strcmp(ME.identifier,'MATLAB:webservices:Timeout'))
disp('=== TIMEOUT ON VALVE POLL ===')
end
end
end
% Deprecated
function responseParsed = pollLOX(obj)
% Request a LOXLVL value update from the stand
% Enable auxiliary data collection
% url = strcat('http://',obj.ip,'3004')
% disp('polling LOX...')
url = strcat('http://',obj.ip,':3004/serial/auxdata/update');
options = weboptions;
options.RequestMethod = 'get';
options.Timeout = 1;
try
response = webread(url,options);
% disp('WEBREAD RESPONSE:')
% disp(response)
responseParsed = response.LOXLVL;
% disp('EXTRACTED VALUE:')
% disp(responseParsed)
catch ME
disp(ME)
if (strcmp(ME.identifier,'MATLAB:webservices:Timeout'))
disp('=== TIMEOUT ON LOXLVL POLL ===')
end
end
end
function responseParsed = post(obj)
% Post a valve state to the stand
url = strcat('http://',obj.ip,':3003/serial/valve/update');
options = weboptions;
options.Timeout = 1;
disp(stateToMessage(obj))
try
response = webwrite(url, stateToMessage(obj));
responseParsed = parseResponse(response);
catch ME
% disp(ME)
if (strcmp(ME.identifier,'MATLAB:webservices:ConnectionRefused'))
disp('=== POST REFUSED ===')
elseif (strcmp(ME.identifier,'MATLAB:webservices:Timeout'))
disp('=== POST TIMEOUT ===')
end
end
end
function parseSequence(obj, list)
file = fopen(list, 'r');
% jsonObj = char(fread(file));
jsonObj = fread(file,'*char');
obj.sequence = jsondecode(jsonObj');
% disp(obj.sequence)
fclose(file);
% create timer array
obj.n = 1;
% parse the durations and names
sequenceDurations = [];
sequenceNames = {};
struct_names = fieldnames(obj.sequence);
% disp(struct_names)
% disp(length(struct_names))
for i = 1:length(struct_names)
sequenceDurations(i) = getfield(obj.sequence, struct_names{i}).Duration;
sequenceNames{i} = getfield(obj.sequence, struct_names{i}).Name;
end
% disp(sequenceDurations)
% disp(sequenceNames)
% generate timers
obj.timers = timer.empty(length(sequenceDurations),0);
for i = 1:length(sequenceDurations)
% disp(i)
% A = sprintf('obj.timerReadPost({%d})', i)
obj.timers(i) = timer( ...
'Tag','StandInstruction', ...
'Name', sequenceNames{i}, ...
'ExecutionMode', 'singleShot', ...
'StartDelay', sequenceDurations(i));
obj.timers(i).StartFcn = @(~,~)obj.timerReadPost;
obj.timers(i).TimerFcn = @(~,~)obj.timerNext;
end
end
% executes event
function timerReadPost(obj)
% disp(num2str(i))
% Mark the previous node as expired
try
obj.app.colorizeTreeNode(obj.n-1,'expired')
catch ME
% Do nothing
end
% Mark the current node as running
obj.app.colorizeTreeNode(obj.n,'running')
% Mark the next node as waiting
try
obj.app.colorizeTreeNode(obj.n+1,'waiting')
catch ME
% Do nothing
end
tic
struct_names = fieldnames(obj.sequence);
state = getfield(obj.sequence, struct_names{obj.n}).State;
obj.FUEL_Press = getfield(state, obj.KeyList{1});
obj.LOX_Press = getfield(state, obj.KeyList{2});
obj.FUEL_Vent = getfield(state, obj.KeyList{3});
obj.LOX_Vent = getfield(state, obj.KeyList{4});
obj.MAIN = getfield(state, obj.KeyList{5});
obj.FUEL_Purge = getfield(state, obj.KeyList{6});
obj.LOX_Purge = getfield(state, obj.KeyList{7}); % weird fix
obj.IGNITE = getfield(state, obj.KeyList{8});
obj.WATER_Flow = getfield(state, obj.KeyList{9});
obj.post();
end
function timerNext(obj)
toc;
obj.n = obj.n+1;
% start(obj.timers(obj.n));
% MException.last
try
start(obj.timers(obj.n));
catch ME
if strcmp( ME.identifier, "MATLAB:badsubscript")
% disp(ME.identifier);
stop(obj.timers);
delete(obj.timers);
disp("Done");
else
disp("Different Error");
rethrow( ME );
end
end
% start(obj.timers(obj.n));
end
function stopTimer(obj)
stop(obj.timers);
disp("Timer Stopped. Next timer:");
disp(obj.n);
end
function abortTimer(obj)
disp("ABORTING . . .")
TimerTempArray = timerfind('Tag','StandInstruction');
% disp('Before delete:')
% disp('timer temp array:')
% disp(TimerTempArray)
% disp('timer findall array:')
% disp(timerfindall)
% disp('timer obj array:')
% disp(obj.timers)
if (~isempty(TimerTempArray))
stop(TimerTempArray);
disp("TIMERS ABORTED");
% disp(obj.n);
for j = 1:length(obj.timers)
obj.app.colorizeTreeNode(j,'aborted');
end
delete(TimerTempArray)
disp("TIMERS DELETED")
else
disp("NO TIMERS IN MEMORY")
end
% disp('After delete:')
% disp('timer temp array:')
% disp(TimerTempArray)
% disp('timer findall array:')
% disp(timerfindall)
% disp('timer obj array:')
% disp(obj.timers)
end
function responseParsed = safe(obj)
disp("SAFING SYSTEM . . .")
% hardcode safe state (same as constructor)
obj.FUEL_Press = false;
obj.LOX_Press = false;
obj.FUEL_Vent = false;
obj.LOX_Vent = false;
obj.MAIN = false;
obj.FUEL_Purge = false;
obj.LOX_Purge = false;
obj.IGNITE = false;
obj.WATER_Flow = false;
% stop(obj.timers);
% delete(obj.timers);
responseParsed = obj.post();
end
end
end