Skip to content

Commit

Permalink
Wait for interface to be up before launching node (#7)
Browse files Browse the repository at this point in the history
* Wait for interface to be UP before starting node
  • Loading branch information
roni-kreinin authored Jan 31, 2025
1 parent b92fdfb commit 5f5a751
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
22 changes: 19 additions & 3 deletions launch/receiver.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
from launch.actions import (
DeclareLaunchArgument,
EmitEvent,
ExecuteProcess,
RegisterEventHandler)
from launch.conditions import IfCondition
from launch.event_handlers import OnProcessStart
from launch.event_handlers import OnProcessExit, OnProcessStart
from launch.events import matches_action
from launch.substitutions import LaunchConfiguration
from launch.substitutions import FindExecutable, LaunchConfiguration
from launch_ros.actions import LifecycleNode
from launch_ros.event_handlers import OnStateTransition
from launch_ros.events.lifecycle import ChangeState
Expand Down Expand Up @@ -102,6 +103,20 @@ def generate_launch_description():
remappings=[('from_can_bus', from_can_bus_topic)],
output='screen')

# Wait for interface to be up
wait_for_can_interface_proc = ExecuteProcess(
cmd=[['until ', FindExecutable(name='ip'), ' link show ', interface, ' | ',
FindExecutable(name='grep'), ' \"state UP\"', '; do sleep 1; done']],
shell=True
)

launch_node = RegisterEventHandler(
event_handler=OnProcessExit(
target_action=wait_for_can_interface_proc,
on_exit=node
)
)

configure_event = RegisterEventHandler(
event_handler=OnProcessStart(
target_action=node,
Expand Down Expand Up @@ -144,7 +159,8 @@ def generate_launch_description():
ld.add_action(arg_auto_configure)
ld.add_action(arg_auto_activate)
ld.add_action(arg_from_can_bus_topic)
ld.add_action(node)
ld.add_action(wait_for_can_interface_proc)
ld.add_action(launch_node)
ld.add_action(configure_event)
ld.add_action(activate_event)
return ld
22 changes: 19 additions & 3 deletions launch/sender.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
from launch.actions import (
DeclareLaunchArgument,
EmitEvent,
ExecuteProcess,
RegisterEventHandler)
from launch.conditions import IfCondition
from launch.event_handlers import OnProcessStart
from launch.event_handlers import OnProcessExit, OnProcessStart
from launch.events import matches_action
from launch.substitutions import LaunchConfiguration
from launch.substitutions import FindExecutable, LaunchConfiguration
from launch_ros.actions import LifecycleNode
from launch_ros.event_handlers import OnStateTransition
from launch_ros.events.lifecycle import ChangeState
Expand Down Expand Up @@ -90,6 +91,20 @@ def generate_launch_description():
remappings=[('to_can_bus', to_can_bus_topic)],
output='screen')

# Wait for interface to be up
wait_for_can_interface_proc = ExecuteProcess(
cmd=[['until ', FindExecutable(name='ip'), ' link show ', interface, ' | ',
FindExecutable(name='grep'), ' \"state UP\"', '; do sleep 1; done']],
shell=True
)

launch_node = RegisterEventHandler(
event_handler=OnProcessExit(
target_action=wait_for_can_interface_proc,
on_exit=node
)
)

configure_event = RegisterEventHandler(
event_handler=OnProcessStart(
target_action=node,
Expand Down Expand Up @@ -130,7 +145,8 @@ def generate_launch_description():
ld.add_action(arg_auto_configure)
ld.add_action(arg_auto_activate)
ld.add_action(arg_to_can_bus_topic)
ld.add_action(node)
ld.add_action(wait_for_can_interface_proc)
ld.add_action(launch_node)
ld.add_action(configure_event)
ld.add_action(activate_event)
return ld
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<depend>rclcpp</depend>
<depend>can_msgs</depend>

<exec_depend>iproute2</exec_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

Expand Down

0 comments on commit 5f5a751

Please sign in to comment.