-
Notifications
You must be signed in to change notification settings - Fork 49
/
machine.py
386 lines (298 loc) · 15 KB
/
machine.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import os
from contextlib import suppress
from transitions.extensions.states import Tags as MachineState
from transitions import Machine
from panoptes.utils import error
from panoptes.utils.utils import listify
from panoptes.utils.library import load_module
from panoptes.utils.serializers import from_yaml
class PanStateMachine(Machine):
""" A finite state machine for PANOPTES.
The state machine guides the overall action of the unit.
"""
def __init__(self, state_machine_table, **kwargs):
if isinstance(state_machine_table, str):
self.logger.info(f"Loading state table: {state_machine_table}")
state_machine_table = PanStateMachine.load_state_table(
state_table_name=state_machine_table)
assert 'states' in state_machine_table, self.logger.warning('states keyword required.')
assert 'transitions' in state_machine_table, self.logger.warning(
'transitions keyword required.')
self._state_table_name = state_machine_table.get('name', 'default')
self._states_location = state_machine_table.get('location', 'panoptes.pocs.state.states')
# Setup Transitions.
_transitions = [self._load_transition(transition)
for transition in state_machine_table['transitions']]
# States can require the horizon to be at a certain level.
self._horizon_lookup = dict()
# Setup States.
states = [
self._load_state(state, state_info=state_info)
for state, state_info
in state_machine_table.get('states', dict()).items()
]
self.logger.debug(f'Horizon limits: {self._horizon_lookup!r}')
# Create state machine.
super(PanStateMachine, self).__init__(
states=states,
transitions=_transitions,
initial=state_machine_table.get('initial'),
send_event=True,
before_state_change='before_state',
after_state_change='after_state',
auto_transitions=False,
name="POCS State Machine",
**kwargs
)
self._state_machine_table = state_machine_table
self.next_state = None
self.logger.debug("State machine created")
################################################################################################
# Properties
################################################################################################
@property
def next_state(self):
return self._next_state
@next_state.setter
def next_state(self, value):
""" Set the tracking rate """
self._next_state = value
################################################################################################
# Methods
################################################################################################
def run(self, exit_when_done=False, run_once=False, initial_next_state='ready'):
"""Runs the state machine loop.
This runs the state machine in a loop. Setting the machine property
`is_running` to False will stop the loop.
Args:
exit_when_done (bool, optional): If True, the loop will exit when `do_states`
has become False, otherwise will wait (default)
run_once (bool, optional): If the machine loop should only run one time, defaults
to False to loop continuously.
initial_next_state (str, optional): The first state the machine should move to from
the `sleeping` state, default `ready`.
"""
if not self.is_initialized:
self.logger.warning("POCS not initialized")
return False
run_once = run_once or self.run_once
self.next_state = initial_next_state
_transition_iteration = 0
max_transition_attempts = self.get_config('max_transition_attempts', default=5)
check_delay = self.get_config('wait_delay', default=120)
self.logger.debug(f'Starting run loop')
while self.keep_running:
# BEFORE TRANSITION TO STATE
self.logger.info(f'Run loop: {self.state!r} -> {self.next_state!r}')
# Before moving to next state, wait for required horizon if necessary
while True:
# If not safe, go to park
self.is_safe(park_if_not_safe=True, ignore=['is_dark'])
# The state may have changed since the start of the while loop
# e.g. if self.park is called from self.is_safe
# So we need to check if the new state is always safe
if self.get_state(self.next_state).is_always_safe:
break
# Check the horizon here because next state may have changed in loop
required_horizon = self._horizon_lookup.get(self.next_state, 'observe')
if self.is_dark(horizon=required_horizon):
break
self.logger.info(f"Waiting for {required_horizon=!r} for {self.next_state=!r}")
# Sleep before checking again
self.wait(delay=check_delay)
# TRANSITION TO STATE
self.logger.info(f'Going to {self.next_state!r}')
try:
# The state's `on_enter` logic will be performed here.
state_changed = self.goto_next_state()
except Exception as e:
self.logger.critical(f"Problem going from {self.state!r} to {self.next_state!r}, "
f"exiting loop [{e!r}]")
# TODO should we automatically park here?
self.stop_states()
break
# AFTER TRANSITION TO STATE (NOW INSIDE STATE)
# If we didn't successfully transition, wait a while then try again
if not state_changed:
self.logger.warning(f"Failed to move from {self.state!r} to {self.next_state!r}")
if self.is_safe() is False:
self.logger.warning(
"Conditions have become unsafe; setting next state to 'parking'")
self.next_state = 'parking'
elif _transition_iteration > max_transition_attempts:
self.logger.warning(
f"Stuck in current state for {max_transition_attempts=!r}, parking")
self.next_state = 'parking'
else:
_transition_iteration = _transition_iteration + 1
self.logger.warning(
f"Sleeping before trying again ({_transition_iteration}/"
f"{max_transition_attempts})")
self.wait(delay=7) # wait 7 seconds (no good reason)
else:
_transition_iteration = 0
# Note that `self.state` below has changed from above
# We started in the sleeping state, so if we are back here we have done a full loop.
if self.state == 'sleeping':
self._obs_run_retries -= 1
if run_once:
self.stop_states()
if exit_when_done:
self.logger.info(f'Leaving run loop {exit_when_done=!r}')
break
def goto_next_state(self):
"""Make a transition to the next state.
Each state is responsible for setting the `next_state` property based
off the logic that happens inside the state. This method will look up
the transition method to reach the next state and call that method.
If no transition method is defined for whatever is set as `next_state`
then the `park` method will be called.
Returns:
bool: If state was successfully changed.
"""
state_changed = False
# Get the next transition method based off `state` and `next_state`
transition_method_name = self._lookup_trigger()
transition_method = getattr(self, transition_method_name, self.park)
self.logger.debug(f'{transition_method_name}: {self.state} → {self.next_state}')
# Do transition.
state_changed = transition_method()
if state_changed:
self.logger.success(f'Successful transition to {self.state}')
self.db.insert_current('state', {"source": self.state, "dest": self.next_state})
return state_changed
def stop_states(self):
""" Stops the machine loop on the next iteration by setting do_states=False """
self.logger.success("Stopping POCS states")
self.do_states = False
##################################################################################################
# State Conditions
##################################################################################################
def check_safety(self, event_data=None):
""" Checks the safety flag of the system to determine if safe.
This will check the weather station as well as various other environmental
aspects of the system in order to determine if conditions are safe for operation.
Note:
This condition is called by the state machine during each transition
Args:
event_data(transitions.EventData): carries information about the event if
called from the state machine.
Returns:
bool: Latest safety flag
"""
self.logger.debug(f"Checking safety for {event_data.transition}")
if event_data is None:
return self.is_safe()
dest_state_name = event_data.transition.dest
dest_state = self.get_state(dest_state_name)
# See if the state requires a certain horizon limit.
required_horizon = self._horizon_lookup.get(dest_state_name, 'observe')
# It's always safe to be in some states
if dest_state.is_always_safe:
self.logger.debug(f"Always safe to move to {dest_state_name}")
is_safe = True
else:
is_safe = self.is_safe(horizon=required_horizon)
return is_safe
def mount_is_tracking(self, event_data):
""" Transitional check for mount.
This is used as a conditional check when transitioning between certain
states.
"""
return self.observatory.mount.is_tracking
def mount_is_initialized(self, event_data):
""" Transitional check for mount.
This is used as a conditional check when transitioning between certain
states.
"""
return self.observatory.mount.is_initialized
##################################################################################################
# Callback Methods
##################################################################################################
def before_state(self, event_data):
""" Called before each state.
Args:
event_data(transitions.EventData): Contains information about the event
"""
self.logger.debug(f"Changing state from {event_data.state.name} to {event_data.event.name}")
def after_state(self, event_data):
""" Called after each state.
Args:
event_data(transitions.EventData): Contains information about the event
"""
self.logger.debug(
f"After {event_data.event.name} transition. In {event_data.state.name} state")
##################################################################################################
# Class Methods
##################################################################################################
@classmethod
def load_state_table(cls, state_table_name='panoptes'):
""" Loads the state table
Args:
state_table_name(str): Name of state table. Corresponds to filename in
`$POCS/conf_files/state_table/` directory or to absolute path if
starts with "/". Default 'panoptes.yaml'.
Returns:
dict: Dictionary with `states` and `transitions` keys.
"""
if not state_table_name.startswith('/'):
state_table_file = os.path.join(
os.getenv('POCS', default='/panoptes-pocs'),
'conf_files',
'state_table',
f'{state_table_name}.yaml'
)
else:
state_table_file = state_table_name
try:
with open(state_table_file, 'r') as f:
state_table = from_yaml(f.read())
except Exception as err:
raise error.InvalidConfig(
f'Problem loading state table yaml file: {err!r} {state_table_file}')
return state_table
##################################################################################################
# Private Methods
##################################################################################################
def _lookup_trigger(self):
if self.state == 'parking' and self.next_state == 'parking':
return 'set_park'
else:
for state_info in self._state_machine_table['transitions']:
if self.state in state_info['source'] and state_info['dest'] == self.next_state:
return state_info['trigger']
# Return parking if we don't find anything
return 'parking'
def _update_status(self, event_data):
self.logger.debug(f'State change status: {self.status!r}')
def _load_state(self, state, state_info=None):
self.logger.debug(f"Loading state: {state}")
try:
state_location = self._states_location.replace("/", ".")
state_module = load_module(f"{state_location}.{self._state_table_name}.{state}")
# Get the `on_enter` method
self.logger.debug(f"Checking {state_module}")
on_enter_method = getattr(state_module, 'on_enter')
setattr(self, f'on_enter_{state}', on_enter_method)
self.logger.trace(f"Added `on_enter` method from {state_module} {on_enter_method}")
if state_info is None:
state_info = dict()
# Add horizon if state requires.
with suppress(KeyError):
self._horizon_lookup[state] = state_info['horizon']
del state_info['horizon']
self.logger.debug(f"Creating state={state!r} with state_info={state_info!r}")
state_machine = MachineState(name=state, **state_info)
# Add default callbacks.
state_machine.add_callback('enter', '_update_status')
state_machine.add_callback('enter', f'on_enter_{state}')
except Exception as e:
raise error.InvalidConfig(f"Can't load state modules: {state}\t{e!r}")
return state_machine
def _load_transition(self, transition):
# Add `check_safety` as the first transition for all states
conditions = listify(transition.get('conditions', []))
conditions.insert(0, 'check_safety')
transition['conditions'] = conditions
self.logger.trace(f"Returning transition: {transition}")
return transition