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

avoid lifecycle node transition exception #1319

Open
wants to merge 2 commits into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 23 additions & 5 deletions rclpy/rclpy/lifecycle/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
:param callback_group: Callback group that will be used by all the lifecycle
node services.
"""
self._logger = self.get_logger()
self._callbacks: Dict[int, Callable[[LifecycleState], TransitionCallbackReturn]] = {}
# register all state machine transition callbacks
self.__register_callback(
Expand Down Expand Up @@ -323,18 +324,35 @@ def __change_state(self, transition_id: int) -> TransitionCallbackReturn:
self.__check_is_initialized()
initial_state = self._state_machine.current_state
initial_state = LifecycleState(state_id=initial_state[0], label=initial_state[1])
self._state_machine.trigger_transition_by_id(transition_id, True)

try:
self._state_machine.trigger_transition_by_id(transition_id, True)
except _rclpy.RCLError:
self._logger.error(
'Unable to start transition {0} from current state {1}'
.format(transition_id, self._state_machine.current_state))
return TransitionCallbackReturn.ERROR
cb_return_code = self.__execute_callback(
self._state_machine.current_state[0], initial_state)
self._state_machine.trigger_transition_by_label(cb_return_code.to_label(), True)

try:
self._state_machine.trigger_transition_by_label(cb_return_code.to_label(), True)
except _rclpy.RCLError:
self._logger.error(
'Failed to complete transition {0}, current state is {1}'
.format(transition_id, self._state_machine.current_state))
return TransitionCallbackReturn.ERROR
if cb_return_code == TransitionCallbackReturn.ERROR:
# Now we're in the errorprocessing state, trigger the on_error callback
# and transition again based on the return code.
error_cb_ret_code = self.__execute_callback(
self._state_machine.current_state[0], initial_state)
self._state_machine.trigger_transition_by_label(error_cb_ret_code.to_label(), True)
try:
self._state_machine.trigger_transition_by_label(error_cb_ret_code.to_label(), True)
except _rclpy.RCLError:
self._logger.error(
'Failed to complete transition to {0} during error processing state, '
'current state is {1}'
.format(error_cb_ret_code.to_label(), self._state_machine.current_state))
return TransitionCallbackReturn.ERROR
return cb_return_code

def __check_is_initialized(self):
Expand Down
7 changes: 0 additions & 7 deletions rclpy/test/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import rclpy
from rclpy.executors import SingleThreadedExecutor
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy
from rclpy.lifecycle import LifecycleNode
from rclpy.lifecycle import TransitionCallbackReturn
from rclpy.node import Node
Expand Down Expand Up @@ -69,13 +68,7 @@ def test_lifecycle_state_transitions():
assert node.trigger_deactivate() == TransitionCallbackReturn.SUCCESS
assert node.trigger_cleanup() == TransitionCallbackReturn.SUCCESS
# some that are not possible from the current state
with pytest.raises(_rclpy.RCLError):
node.trigger_activate()
with pytest.raises(_rclpy.RCLError):
node.trigger_deactivate()
assert node.trigger_shutdown() == TransitionCallbackReturn.SUCCESS
with pytest.raises(_rclpy.RCLError):
node.trigger_shutdown()
node.destroy_node()
# Again but trigger shutdown from 'inactive' instead of 'unconfigured'
node = LifecycleNode(
Expand Down