Skip to content

Commit d717eb5

Browse files
committed
Add our own launch files that allow us to change namespaces
1 parent a23603f commit d717eb5

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ target_include_directories(socketcan_interface PUBLIC
2727
# which is appropriate when building the dll but not consuming it.
2828
target_compile_definitions(socketcan_interface PRIVATE "CLEARPATH_ROS2_SOCKETCAN_INTERFACE_BUILDING_LIBRARY")
2929

30+
install(DIRECTORY launch
31+
DESTINATION share/${PROJECT_NAME}
32+
)
33+
3034
install(
3135
DIRECTORY include/
3236
DESTINATION include/${PROJECT_NAME}

launch/receiver.launch.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Software License Agreement (BSD)
2+
#
3+
# @author Luis Camero <[email protected]>
4+
# @copyright (c) 2024, Clearpath Robotics, Inc., All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# * Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# * Neither the name of Clearpath Robotics nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
from launch import LaunchDescription
29+
from launch.actions import (
30+
DeclareLaunchArgument,
31+
EmitEvent,
32+
RegisterEventHandler)
33+
from launch.conditions import IfCondition
34+
from launch.event_handlers import OnProcessStart
35+
from launch.events import matches_action
36+
from launch.substitutions import LaunchConfiguration
37+
from launch_ros.actions import LifecycleNode
38+
from launch_ros.event_handlers import OnStateTransition
39+
from launch_ros.events.lifecycle import ChangeState
40+
from lifecycle_msgs.msg import Transition
41+
42+
43+
44+
def generate_launch_description():
45+
namespace = LaunchConfiguration('namespace')
46+
interface = LaunchConfiguration('interface')
47+
enable_can_fd = LaunchConfiguration('enable_can_fd')
48+
interval_sec = LaunchConfiguration('interval_sec')
49+
use_bus_time = LaunchConfiguration('use_bus_time')
50+
filters = LaunchConfiguration('filters')
51+
auto_configure = LaunchConfiguration('auto_configure')
52+
auto_activate = LaunchConfiguration('auto_activate')
53+
from_can_bus_topic = LaunchConfiguration('from_can_bus_topic')
54+
55+
arg_namespace = DeclareLaunchArgument(
56+
'namespace',
57+
default_value='')
58+
59+
arg_interface = DeclareLaunchArgument(
60+
'interface',
61+
default_value='can0')
62+
63+
arg_enable_can_fd = DeclareLaunchArgument(
64+
'enable_can_fd',
65+
default_value='false')
66+
67+
arg_interval_sec = DeclareLaunchArgument(
68+
'interval_sec',
69+
default_value='0.01')
70+
71+
arg_use_bus_time = DeclareLaunchArgument(
72+
'use_bus_time',
73+
default_value='false')
74+
75+
arg_filters = DeclareLaunchArgument(
76+
'filters',
77+
default_value='0:0')
78+
79+
arg_auto_configure = DeclareLaunchArgument(
80+
'auto_configure',
81+
default_value='true')
82+
83+
arg_auto_activate = DeclareLaunchArgument(
84+
'auto_activate',
85+
default_value='true')
86+
87+
arg_from_can_bus_topic = DeclareLaunchArgument(
88+
'from_can_bus_topic',
89+
default_value='rx')
90+
91+
node = LifecycleNode(
92+
package='ros2_socketcan',
93+
executable='socket_can_receiver_node_exe',
94+
name='socket_can_receiver',
95+
namespace=namespace,
96+
parameters=[{
97+
'interface': interface,
98+
'enable_can_fd': enable_can_fd,
99+
'interval_sec': interval_sec,
100+
'filters': filters,
101+
'use_bus_time': use_bus_time,
102+
}],
103+
remappings=[('from_can_bus', from_can_bus_topic)],
104+
output='screen')
105+
106+
configure_event = RegisterEventHandler(
107+
event_handler=OnProcessStart(
108+
target_action=node,
109+
on_start=[
110+
EmitEvent(
111+
event=ChangeState(
112+
lifecycle_node_matcher=matches_action(node),
113+
transition_id=Transition.TRANSITION_CONFIGURE,
114+
),
115+
),
116+
],
117+
),
118+
condition=IfCondition(auto_configure),
119+
)
120+
121+
activate_event = RegisterEventHandler(
122+
event_handler=OnStateTransition(
123+
target_lifecycle_node=node,
124+
start_state='configuring',
125+
goal_state='inactive',
126+
entities=[
127+
EmitEvent(
128+
event=ChangeState(
129+
lifecycle_node_matcher=matches_action(node),
130+
transition_id=Transition.TRANSITION_ACTIVATE,
131+
),
132+
),
133+
],
134+
),
135+
condition=IfCondition(auto_activate),
136+
)
137+
138+
ld = LaunchDescription()
139+
ld.add_action(arg_namespace)
140+
ld.add_action(arg_interface)
141+
ld.add_action(arg_enable_can_fd)
142+
ld.add_action(arg_interval_sec)
143+
ld.add_action(arg_use_bus_time)
144+
ld.add_action(arg_filters)
145+
ld.add_action(arg_auto_configure)
146+
ld.add_action(arg_auto_activate)
147+
ld.add_action(arg_from_can_bus_topic)
148+
ld.add_action(node)
149+
ld.add_action(configure_event)
150+
ld.add_action(activate_event)
151+
return ld

launch/sender.launch.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Software License Agreement (BSD)
2+
#
3+
# @author Luis Camero <[email protected]>
4+
# @copyright (c) 2024, Clearpath Robotics, Inc., All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# * Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# * Neither the name of Clearpath Robotics nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
from launch import LaunchDescription
29+
from launch.actions import (
30+
DeclareLaunchArgument,
31+
EmitEvent,
32+
RegisterEventHandler)
33+
from launch.conditions import IfCondition
34+
from launch.event_handlers import OnProcessStart
35+
from launch.events import matches_action
36+
from launch.substitutions import LaunchConfiguration
37+
from launch_ros.actions import LifecycleNode
38+
from launch_ros.event_handlers import OnStateTransition
39+
from launch_ros.events.lifecycle import ChangeState
40+
from lifecycle_msgs.msg import Transition
41+
42+
43+
44+
def generate_launch_description():
45+
namespace = LaunchConfiguration('namespace')
46+
interface = LaunchConfiguration('interface')
47+
enable_can_fd = LaunchConfiguration('enable_can_fd')
48+
timeout_sec = LaunchConfiguration('timeout_sec')
49+
auto_configure = LaunchConfiguration('auto_configure')
50+
auto_activate = LaunchConfiguration('auto_activate')
51+
to_can_bus_topic = LaunchConfiguration('to_can_bus_topic')
52+
53+
arg_namespace = DeclareLaunchArgument(
54+
'namespace',
55+
default_value='')
56+
57+
arg_interface = DeclareLaunchArgument(
58+
'interface',
59+
default_value='can0')
60+
61+
arg_enable_can_fd = DeclareLaunchArgument(
62+
'enable_can_fd',
63+
default_value='false')
64+
65+
arg_timeout_sec = DeclareLaunchArgument(
66+
'timeout_sec',
67+
default_value='0.01')
68+
69+
arg_auto_configure = DeclareLaunchArgument(
70+
'auto_configure',
71+
default_value='true')
72+
73+
arg_auto_activate = DeclareLaunchArgument(
74+
'auto_activate',
75+
default_value='true')
76+
77+
arg_to_can_bus_topic = DeclareLaunchArgument(
78+
'to_can_bus_topic',
79+
default_value='tx')
80+
81+
node = LifecycleNode(
82+
package='ros2_socketcan',
83+
executable='socket_can_sender_node_exe',
84+
name='socket_can_sender',
85+
namespace=namespace,
86+
parameters=[{
87+
'interface': interface,
88+
'enable_can_fd': enable_can_fd,
89+
'timeout_sec': timeout_sec,
90+
}],
91+
remappings=[('to_can_bus', to_can_bus_topic)],
92+
output='screen')
93+
94+
configure_event = RegisterEventHandler(
95+
event_handler=OnProcessStart(
96+
target_action=node,
97+
on_start=[
98+
EmitEvent(
99+
event=ChangeState(
100+
lifecycle_node_matcher=matches_action(node),
101+
transition_id=Transition.TRANSITION_CONFIGURE,
102+
),
103+
),
104+
],
105+
),
106+
condition=IfCondition(auto_configure),
107+
)
108+
109+
activate_event = RegisterEventHandler(
110+
event_handler=OnStateTransition(
111+
target_lifecycle_node=node,
112+
start_state='configuring',
113+
goal_state='inactive',
114+
entities=[
115+
EmitEvent(
116+
event=ChangeState(
117+
lifecycle_node_matcher=matches_action(node),
118+
transition_id=Transition.TRANSITION_ACTIVATE,
119+
),
120+
),
121+
],
122+
),
123+
condition=IfCondition(auto_activate),
124+
)
125+
126+
ld = LaunchDescription()
127+
ld.add_action(arg_namespace)
128+
ld.add_action(arg_interface)
129+
ld.add_action(arg_enable_can_fd)
130+
ld.add_action(arg_timeout_sec)
131+
ld.add_action(arg_auto_configure)
132+
ld.add_action(arg_auto_activate)
133+
ld.add_action(arg_to_can_bus_topic)
134+
ld.add_action(node)
135+
ld.add_action(configure_event)
136+
ld.add_action(activate_event)
137+
return ld

0 commit comments

Comments
 (0)