|
| 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 |
0 commit comments