Skip to content

Commit 457c80b

Browse files
committed
- Changed how user functions for interacting with framework and state machine are made availible in task file. Previously they were monkey-patched into the user task definition file when the State_machine was instantiated. This was not ideal as: 1) It could cause a patched-in function to overwrite user function or variables in the event of a name collision, causing hard to diagnose errors. 2) It prevented the user from importing these functions using a named import if they wish to, e.g. to ensure task code passes linting. 3) It made the framework code harder to understand. This commit moves all pyControl functions that are used in task files to the pyControl.utility module, so they can be imported into the task definition file using standard python imports.
1 parent a4ad7ab commit 457c80b

File tree

3 files changed

+56
-71
lines changed

3 files changed

+56
-71
lines changed

pyControl/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import pyControl.framework as fw
22
import pyControl.hardware as hw
33
import pyControl.state_machine as sm
4-
from pyControl.utility import *
4+
import pyControl.utility as ut

pyControl/state_machine.py

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,6 @@ def __init__(self, smd):
2121
else:
2222
self.event_dispatch_dict[state] = None
2323

24-
# Attach user methods to discription object namespace, this allows the user
25-
# to write e.g. goto_state(state) in the task description to call
26-
# State_machine.goto_state.
27-
smd.goto_state = self.goto_state
28-
smd.goto = self.goto_state # For backwards compatibility.
29-
smd.timed_goto_state = self.timed_goto_state
30-
smd.set_timer = self.set_timer
31-
smd.disarm_timer = self.disarm_timer
32-
smd.reset_timer = self.reset_timer
33-
smd.pause_timer = self.pause_timer
34-
smd.unpause_timer = self.unpause_timer
35-
smd.print = self.print
36-
smd.stop_framework = self.stop_framework
37-
smd.publish_event = self.publish_event
38-
smd.get_current_time = self.get_current_time
39-
smd.timer_remaining = self.timer_remaining
40-
41-
# Methods called by user
42-
4324
def goto_state(self, next_state):
4425
# Transition to next state, calling exit action of old state
4526
# and entry action of next state.
@@ -56,57 +37,6 @@ def goto_state(self, next_state):
5637
self._process_event('entry')
5738
self.state_transition_in_progress = False
5839

59-
def timed_goto_state(self, next_state, interval):
60-
# Transition to next_state after interval milliseconds. timed_goto_state()
61-
# is cancelled if goto_state() occurs before interval elapses.
62-
fw.timer.set(interval, fw.state_typ, fw.states[next_state])
63-
64-
def set_timer(self, event, interval, output_event=False):
65-
# Set a timer to return specified event after interval milliseconds.
66-
event_type = fw.event_typ if output_event else fw.timer_typ
67-
fw.timer.set(interval, event_type, fw.events[event])
68-
69-
def disarm_timer(self, event):
70-
# Disable all timers due to return specified event.
71-
fw.timer.disarm(fw.events[event])
72-
73-
def reset_timer(self, event, interval, output_event=False):
74-
# Disarm all timers due to return specified event and set new timer
75-
# to return specified event after interval milliseconds.
76-
fw.timer.disarm(fw.events[event])
77-
event_type = fw.event_typ if output_event else fw.timer_typ
78-
fw.timer.set(interval, event_type, fw.events[event])
79-
80-
def pause_timer(self,event):
81-
# Pause all timers due to return specified event.
82-
fw.timer.pause(fw.events[event])
83-
84-
def unpause_timer(self,event):
85-
# Unpause all timers due to return specified event.
86-
fw.timer.unpause(fw.events[event])
87-
88-
def timer_remaining(self,event):
89-
# Return time until timer for specified event elapses, returns 0 if no timer set for event.
90-
return fw.timer.remaining(fw.events[event])
91-
92-
def print(self, print_string):
93-
# Used to output data print_string with timestamp. print_string is stored and only
94-
# printed to serial line once higher priority tasks have all been processed.
95-
if fw.data_output:
96-
fw.data_output_queue.put((fw.current_time, fw.print_typ, str(print_string)))
97-
98-
def publish_event(self, event):
99-
# Put event with specified name in the event queue.
100-
fw.event_queue.put((fw.current_time, fw.event_typ, fw.events[event]))
101-
102-
def stop_framework(self):
103-
fw.running = False
104-
105-
def get_current_time(self):
106-
return fw.current_time
107-
108-
# Methods called by pyControl framework.
109-
11040
def _process_event(self, event):
11141
# Process event given event name by calling appropriate state event handler function.
11242
if self.event_dispatch_dict['all_states']: # If machine has all_states event handler function.

pyControl/utility.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
import pyb
22
import math
3+
from . import framework as fw
4+
5+
# State machine functions -----------------------------------------------------
6+
7+
def goto_state(next_state):
8+
fw.state_machine.goto_state(next_state)
9+
10+
def timed_goto_state(next_state, interval):
11+
# Transition to next_state after interval milliseconds. timed_goto_state()
12+
# is cancelled if goto_state() occurs before interval elapses.
13+
fw.timer.set(interval, fw.state_typ, fw.states[next_state])
14+
15+
def set_timer(event, interval, output_event=False):
16+
# Set a timer to return specified event after interval milliseconds.
17+
event_type = fw.event_typ if output_event else fw.timer_typ
18+
fw.timer.set(interval, event_type, fw.events[event])
19+
20+
def disarm_timer(event):
21+
# Disable all timers due to return specified event.
22+
fw.timer.disarm(fw.events[event])
23+
24+
def reset_timer(event, interval, output_event=False):
25+
# Disarm all timers due to return specified event and set new timer
26+
# to return specified event after interval milliseconds.
27+
fw.timer.disarm(fw.events[event])
28+
event_type = fw.event_typ if output_event else fw.timer_typ
29+
fw.timer.set(interval, event_type, fw.events[event])
30+
31+
def pause_timer(event):
32+
# Pause all timers due to return specified event.
33+
fw.timer.pause(fw.events[event])
34+
35+
def unpause_timer(event):
36+
# Unpause all timers due to return specified event.
37+
fw.timer.unpause(fw.events[event])
38+
39+
def timer_remaining(event):
40+
# Return time until timer for specified event elapses, returns 0 if no timer set for event.
41+
return fw.timer.remaining(fw.events[event])
42+
43+
def print(print_string):
44+
# Used to output data print_string with timestamp. print_string is stored and only
45+
# printed to serial line once higher priority tasks have all been processed.
46+
if fw.data_output:
47+
fw.data_output_queue.put((fw.current_time, fw.print_typ, str(print_string)))
48+
49+
def publish_event(event):
50+
# Put event with specified name in the event queue.
51+
fw.event_queue.put((fw.current_time, fw.event_typ, fw.events[event]))
52+
53+
def stop_framework():
54+
fw.running = False
55+
56+
def get_current_time():
57+
return fw.current_time
358

459
# Random functions and classes -----------------------------------------------
560

0 commit comments

Comments
 (0)