Skip to content

Commit

Permalink
Wrappers for common ros tools to start them in a robot namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Oehler authored and JoLichtenfeld committed Jan 23, 2025
1 parent 860b54c commit 5948dbd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.8)
project(hector_multi_robot_tools)

# Find necessary packages
find_package(ament_cmake REQUIRED)

if (WIN32)
message(FATAL_ERROR "Windows is currently not supported.")
else()
ament_environment_hooks(env_hooks/50.scripts.bash.in)
endif()

# Install directories
install(DIRECTORY
launch
scripts
env_hooks
DESTINATION share/${PROJECT_NAME}
)

ament_package()

3 changes: 3 additions & 0 deletions env_hooks/50.scripts.bash.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

source @CMAKE_INSTALL_PREFIX@/share/@PROJECT_NAME@/scripts/multi_robot_tool_launch.bash
19 changes: 19 additions & 0 deletions launch/tool_namespace.launch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
launch:
- arg:
name: pkg
- arg:
name: exec
- arg:
name: namespace
default: ""

- node:
pkg: "$(var pkg)"
exec: "$(var exec)"
# name: "name"
namespace: "$(var namespace)"
remap:
- from: "/tf"
to: "tf"
- from: "/tf_static"
to: "tf_static"
15 changes: 15 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>hector_multi_robot_tools</name>
<version>0.0.0</version>
<description>Tools for ROS2 to make working in a multi-robot setup easier.</description>
<maintainer email="[email protected]">Martin Oeler</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
97 changes: 97 additions & 0 deletions scripts/multi_robot_tool_launch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# multi_robot_tool_launch.bash - Defines wrappers for common ros tools to start them in a robot namespace

# Function to get valid robot names by searching for robot_description topic
get_robot_names() {
ros2 topic list | grep robot_description | awk -F'/' '{print $2}'
}

# Function to display help message
show_help() {
local func_name="$1"
echo "Usage: $func_name [--global] [<robot_name>]"
echo ""
echo "Options:"
echo " --global Start in the global namespace, ignoring available robot names"
echo " <robot_name> Start with the specified robot namespace"
echo " -h, --help Show this help message"
}

# Run ROS2 tool with appropriate parameters
run_ros_tool() {
local pkg_name="$1"
local exec_name="$2"
local robot_name="$3"
local cmd="ros2 launch hector_multi_robot_tools tool_namespace.launch.yaml pkg:=${pkg_name} exec:=${exec_name}"

if [ -n "$robot_name" ]; then
cmd+=" namespace:=/${robot_name}"
echo "Starting in namespace: /${robot_name}"
else
echo "Starting in namespace: /"
fi

eval "$cmd"
}

ros_tool() {
local func_name="$1"
local pkg_name="$2"
local exec_name="$3"
shift
shift
shift

# Process command line options
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
show_help "$func_name"
return
fi

if [ "$1" == "--global" ]; then
run_ros_tool "${pkg_name}" "${exec_name}" # Force global namespace
return
fi

if [ -n "$1" ]; then
# If a robot name is provided as an argument, use it directly
run_ros_tool "${pkg_name}" "${exec_name}" "$1"
else
# No robot name provided, check available robots
local robot_names=($(get_robot_names))
local robot_count=${#robot_names[@]}

if [ "$robot_count" -eq 1 ]; then
# If exactly one robot is available, use it automatically
run_ros_tool "${pkg_name}" "${exec_name}" "${robot_names[0]}"
else
# If multiple robots exist, run in global namespace
run_ros_tool "${pkg_name}" "${exec_name}"
fi
fi
}

# Bash completion function for robot names and options
_ros_tool_completion() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local options="--global --help -h"

# Provide completion options only for first argument
if [ ${#COMP_WORDS[@]} -eq 2 ]; then
local robot_names=$(get_robot_names)
COMPREPLY=($(compgen -W "${robot_names} ${options}" -- "${cur}"))
fi
}

# Wrappers

# rqt function wrapper
mrqt() {
ros_tool "$FUNCNAME" rqt_gui rqt_gui "$@"
}
complete -F _ros_tool_completion mrqt

# rviz function wrapper
mrviz() {
ros_tool "$FUNCNAME" rviz2 rviz2 "$@"
}
complete -F _ros_tool_completion mrviz

0 comments on commit 5948dbd

Please sign in to comment.