Skip to content

Commit

Permalink
fix(components): clean up lifecycle nodes properly
Browse files Browse the repository at this point in the history
  • Loading branch information
domire8 committed Dec 9, 2024
1 parent dec18cd commit 84a336d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions source/modulo_components/modulo_components/lifecycle_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
from lifecycle_msgs.msg import State
from modulo_components.component_interface import ComponentInterface
from modulo_core.exceptions import AddSignalError
from rclpy.callback_groups import CallbackGroup
from rclpy.lifecycle import LifecycleNodeMixin, LifecycleState
from rclpy.lifecycle.node import TransitionCallbackReturn

MsgT = TypeVar('MsgT')
LIFECYCLE_NODE_MIXIN_KWARGS = ["enable_communication_interface", "callback_group"]


class LifecycleComponent(ComponentInterface, LifecycleNodeMixin):
class _LifecycleNodeMixin(LifecycleNodeMixin):
def __init__(self, enable_communication_interface: bool = True, callback_group: Optional[CallbackGroup] = None):
super().__init__(enable_communication_interface=enable_communication_interface, callback_group=callback_group)

def destroy(self):
self._state_machine = None
self._callbacks = {}


class LifecycleComponent(ComponentInterface, _LifecycleNodeMixin):
"""
Class to represent a LifecycleComponent in python, following the same logic pattern
as the C++ modulo_components::LifecycleComponent class.
Expand All @@ -25,9 +35,16 @@ def __init__(self, node_name: str, *args, **kwargs):
"""
lifecycle_node_kwargs = {key: value for key, value in kwargs.items() if key in LIFECYCLE_NODE_MIXIN_KWARGS}
ComponentInterface.__init__(self, node_name, *args, **kwargs)
LifecycleNodeMixin.__init__(self, *args, **lifecycle_node_kwargs)
_LifecycleNodeMixin.__init__(self, **lifecycle_node_kwargs)
self.__has_error = False

def destroy_node(self):
"""
Cleanly destroy the node by cleaning up the Mixin class.
"""
_LifecycleNodeMixin.destroy(self)
ComponentInterface.destroy_node(self)

def get_lifecycle_state(self) -> LifecycleState:
"""
Get the current state of the component.
Expand Down

0 comments on commit 84a336d

Please sign in to comment.