Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userdata constants defined outside state code #90

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
130s marked this conversation as resolved.
Show resolved Hide resolved
<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