Skip to content

Commit

Permalink
Add userdata constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jk-ethz committed May 20, 2022
1 parent 31cc557 commit f974e58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion smach/package.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package>
<name>smach</name>
<version>2.5.0</version>
<version>2.5.99</version>
<description>
SMACH is a task-level architecture for rapidly creating complex robot
behavior. At its core, SMACH is a ROS-independent Python library to build
Expand Down
18 changes: 14 additions & 4 deletions smach/src/smach/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, outcomes, input_keys=[], output_keys=[]):
self._states = {}
self._transitions = {}
self._remappings = {}
self._constants = {}

# Construction vars
self._last_added_label = None
Expand All @@ -73,7 +74,7 @@ def __init__(self, outcomes, input_keys=[], output_keys=[]):

### Construction methods
@staticmethod
def add(label, state, transitions=None, remapping=None):
def add(label, state, transitions=None, remapping=None, constants=None):
"""Add a state to the opened state machine.
@type label: string
Expand All @@ -84,8 +85,11 @@ def add(label, state, transitions=None, remapping=None):
@param transitions: A dictionary mapping state outcomes to other state
labels or container outcomes.
@param remapping: A dictrionary mapping local userdata keys to userdata
@param remapping: A dictionary mapping local userdata keys to userdata
keys in the container.
@param constants: A dictionary mapping userdata keys in the container
to constant values.
"""
# Get currently opened container
self = StateMachine._currently_opened_container()
Expand All @@ -102,6 +106,9 @@ def add(label, state, transitions=None, remapping=None):
if remapping is None:
remapping = {}

if constants is None:
constants = {}

# Add group transitions to this new state, if they exist
"""
if 'transitions' in smach.Container._context_kwargs:
Expand Down Expand Up @@ -133,6 +140,7 @@ def add(label, state, transitions=None, remapping=None):
self._states[label] = state
self._transitions[label] = transitions
self._remappings[label] = remapping
self._constants[label] = constants
smach.logdebug("TRANSITIONS FOR %s: %s" % (label, str(self._transitions[label])))

# Add transition to this state if connected outcome is defined
Expand All @@ -146,7 +154,7 @@ def add(label, state, transitions=None, remapping=None):
return state

@staticmethod
def add_auto(label, state, connector_outcomes, transitions=None, remapping=None):
def add_auto(label, state, connector_outcomes, transitions=None, remapping=None, constants=None):
"""Add a state to the state machine such that it automatically
transitions to the next added state.
Expand All @@ -172,7 +180,7 @@ def add_auto(label, state, connector_outcomes, transitions=None, remapping=None)
self = StateMachine._currently_opened_container()

# First add this state
add_ret = smach.StateMachine.add(label, state, transitions, remapping)
add_ret = smach.StateMachine.add(label, state, transitions, remapping, constants)

# Make sure the connector outcomes are valid for this state
registered_outcomes = state.get_registered_outcomes()
Expand Down Expand Up @@ -239,6 +247,8 @@ def _update_once(self):
# Execute the state
try:
self._state_transitioning_lock.release()
for k, v in self._constants[self._current_label].items():
self.userdata[k] = v
outcome = self._current_state.execute(
smach.Remapper(
self.userdata,
Expand Down

0 comments on commit f974e58

Please sign in to comment.