Skip to content

Commit

Permalink
Setup that works for ROS 2 Humble and ROS 2 Jazzy (#2)
Browse files Browse the repository at this point in the history
Setup that works for ROS 2 Humble and ROS 2 Jazzy
  • Loading branch information
DominikN authored Jul 17, 2024
1 parent bf575a1 commit 4397609
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 102 deletions.
199 changes: 109 additions & 90 deletions local-ros/configure_hook_ros.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,135 +8,154 @@
# Define a function to log and echo messages
source $SNAP/usr/bin/utils.sh

# Function to check the type of the provided XML file
check_xml_profile_type() {
local xml_file="$1"
source_ros

if [[ ! -f "$xml_file" ]]; then
log_and_echo "File '$xml_file' does not exist."
return 1
fi
# Create the ${SNAP_COMMON}/ros.env file and export variables (for bash session running ROS2)
ROS_ENV_FILE="${SNAP_COMMON}/ros.env"

local root_element
local namespace
# Create the ${SNAP_COMMON}/ros.env file and export variables (for bash session running ROS2)
ROS_SNAP_ARGS="${SNAP_COMMON}/ros_snap_args"

# Extract the root element
root_element=$(yq '. | keys | .[1]' "$xml_file")
rm -rf "${ROS_ENV_FILE}.tmp"
rm -rf "${ROS_SNAP_ARGS}.tmp"

# Extract the namespace based on the root element
if [[ "$root_element" == "CycloneDDS" ]]; then
namespace=$(yq .CycloneDDS."+@xmlns" "$xml_file")
elif [[ "$root_element" == "profiles" ]]; then
namespace=$(yq .profiles."+@xmlns" "$xml_file")
else
namespace="unknown"
fi
if [[ $ROS_DISTRO == "humble" ]]; then
VALID_ROS_KEYS=("localhost-only" "domain-id" "transport" "namespace")

# Remove quotes from the extracted values
root_element=${root_element//\"/}
namespace=${namespace//\"/}
# Call the validation function
validate_keys "ros" VALID_ROS_KEYS[@]
elif [[ $ROS_DISTRO == "jazzy" ]]; then
VALID_ROS_KEYS=("localhost-only" "domain-id" "transport" "namespace" "automatic-discovery-range" "static-peers")

if [[ "$root_element" == "profiles" ]] && [[ "$namespace" == "http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles" ]]; then
echo "rmw_fastrtps_cpp"
elif [[ "$root_element" == "CycloneDDS" ]] && [[ "$namespace" == "https://cdds.io/config" ]]; then
echo "rmw_cyclonedds_cpp"
else
exit 1
fi
}
# Call the validation function
validate_keys "ros" VALID_ROS_KEYS[@]

VALID_ROS_KEYS=("localhost-only" "domain-id" "transport" "namespace")
# validate the ROS_AUTOMATIC_DISCOVERY_RANGE
ROS_AUTOMATIC_DISCOVERY_RANGE="$(snapctl get ros.automatic-discovery-range)"

# Call the validation function
validate_keys "ros" VALID_ROS_KEYS[@]
VALID_ROS_AUTOMATIC_DISCOVERY_RANGE_OPTIONS=("subnet" "localhost" "off" "system_default")
validate_option --allow-unset "ros.automatic-discovery-range" VALID_ROS_AUTOMATIC_DISCOVERY_RANGE_OPTIONS[@]

ROS_LOCALHOST_ONLY="$(snapctl get ros.localhost-only)"
ROS_DOMAIN_ID="$(snapctl get ros.domain-id)"
ROS_AUTOMATIC_DISCOVERY_RANGE="$(snapctl get ros.automatic-discovery-range)"

if [ -n "$ROS_AUTOMATIC_DISCOVERY_RANGE" ]; then
echo "export ROS_AUTOMATIC_DISCOVERY_RANGE=$(echo "$ROS_AUTOMATIC_DISCOVERY_RANGE" | tr '[:lower:]' '[:upper:]')" >> "${ROS_ENV_FILE}.tmp"
echo "ros.automatic-discovery-range=${ROS_AUTOMATIC_DISCOVERY_RANGE}" >> ${ROS_SNAP_ARGS}.tmp
else
echo "unset ROS_AUTOMATIC_DISCOVERY_RANGE" >> "${ROS_ENV_FILE}.tmp"
echo "ros.automatic-discovery-range=''" >> ${ROS_SNAP_ARGS}.tmp
snapctl set ros.automatic-discovery-range=''
fi

# validate the ROS_STATIC_PEERS
validate_peers_list --allow-unset "ros.static-peers"

ROS_STATIC_PEERS="$(snapctl get ros.static-peers)"

if [ -n "$ROS_STATIC_PEERS" ]; then
echo "export ROS_STATIC_PEERS='${ROS_STATIC_PEERS}'" >> "${ROS_ENV_FILE}.tmp"
echo "ros.static-peers='${ROS_STATIC_PEERS}'" >> ${ROS_SNAP_ARGS}.tmp
else
echo "unset ROS_STATIC_PEERS" >> "${ROS_ENV_FILE}.tmp"
echo "ros.static-peers=''" >> ${ROS_SNAP_ARGS}.tmp
snapctl set ros.static-peers=''
fi

else
log_and_echo "ROS 2 \"$ROS_DISTRO\" is not supported by this snap."
fi

# Make sure ROS_LOCALHOST_ONLY is valid
VALID_ROS_LOCALHOST_ONLY_OPTIONS=(1 0)
validate_option "ros.localhost-only" VALID_ROS_LOCALHOST_ONLY_OPTIONS[@]
validate_option --allow-unset "ros.localhost-only" VALID_ROS_LOCALHOST_ONLY_OPTIONS[@]

ROS_LOCALHOST_ONLY="$(snapctl get ros.localhost-only)"

if [ -n "$ROS_LOCALHOST_ONLY" ]; then
echo "export ROS_LOCALHOST_ONLY=${ROS_LOCALHOST_ONLY}" >> "${ROS_ENV_FILE}.tmp"
echo "ros.localhost-only=${ROS_LOCALHOST_ONLY}" >> ${ROS_SNAP_ARGS}.tmp
else
echo "unset ROS_LOCALHOST_ONLY" >> "${ROS_ENV_FILE}.tmp"
echo "ros.localhost-only=''" >> ${ROS_SNAP_ARGS}.tmp
snapctl set ros.localhost-only=''
fi

# Make sure ROS_DOMAIN_ID is valid
# Make sure WEBUI_PORT is valid
SUPPORTED_RANGE=(0 232)
# Validate a specific port, for example, webui.port
validate_number "ros.domain-id" SUPPORTED_RANGE[@]

# Get the ros.transport setting using snapctl
OPT="ros.transport"
TRANSPORT_SETTING="$(snapctl get ${OPT})"
ROS_DOMAIN_ID="$(snapctl get ros.domain-id)"

# Check if TRANSPORT_SETTING is "builtin"
if [ "$TRANSPORT_SETTING" == "builtin" ]; then
# Change the value to "rmw_fastrtps_cpp"
TRANSPORT_SETTING="rmw_fastrtps_cpp"
fi
echo "export ROS_DOMAIN_ID=${ROS_DOMAIN_ID}" >> "${ROS_ENV_FILE}.tmp"
echo "ros.domain-id=${ROS_DOMAIN_ID}" >> ${ROS_SNAP_ARGS}.tmp

# Only exit with status 1 if conditions are not met
if [ "$TRANSPORT_SETTING" != "rmw_fastrtps_cpp" ] && [ "$TRANSPORT_SETTING" != "rmw_cyclonedds_cpp" ] && [ ! -f "${SNAP_COMMON}/${TRANSPORT_SETTING}.xml" ]; then
log_and_echo "'${SNAP_COMMON}/${TRANSPORT_SETTING}.xml' does not exist."
exit 1
fi
# Validate the ROS_NAMESPACE

if [ "$TRANSPORT_SETTING" = "rmw_fastrtps_cpp" ] || [ "$TRANSPORT_SETTING" = "shm" ]; then
if ! snapctl is-connected shm-plug; then
log_and_echo "to use 'rmw_fastrtps_cpp' and 'shm' tranport shm-plug need to be connected, please run:"
log_and_echo "sudo snap connect ${SNAP_NAME}:shm-plug ${SNAP_NAME}:shm-slot"
exit 1
fi
fi
validate_regex --allow-unset "ros.namespace" "^[0-9a-z_-]{1,20}$"

# Make sure ros-humble-ros-base is connected
ROS_PLUG="ros-humble-ros-base"
ROS_NAMESPACE=$(snapctl get ros.namespace)

if ! snapctl is-connected ${ROS_PLUG}; then
log_and_echo "Plug '${ROS_PLUG}' isn't connected. Please run:"
log_and_echo "snap connect ${SNAP_NAME}:${ROS_PLUG} ${ROS_PLUG}:${ROS_PLUG}"
exit 1
if [ -n "$ROS_NAMESPACE" ]; then
echo "export ROS_NAMESPACE=${ROS_NAMESPACE}" >> "${ROS_ENV_FILE}.tmp"
echo "ros.namespace=${ROS_NAMESPACE}" >> ${ROS_SNAP_ARGS}.tmp
else
echo "unset ROS_NAMESPACE" >> "${ROS_ENV_FILE}.tmp"
echo "ros.namespace=''" >> ${ROS_SNAP_ARGS}.tmp
snapctl set ros.namespace=''
fi

# Create the ${SNAP_COMMON}/ros.env file and export variables (for bash session running ROS2)
ROS_ENV_FILE="${SNAP_COMMON}/ros.env"

# Create the ${SNAP_COMMON}/ros.env file and export variables (for bash session running ROS2)
ROS_SNAP_ARGS="${SNAP_COMMON}/ros_snap_args"
# Validate the TRANSPORT_SETTING
ADDITIONAL_DDS_OPTIONS=("rmw_fastrtps_cpp" "rmw_cyclonedds_cpp")

echo "export ROS_DOMAIN_ID=${ROS_DOMAIN_ID}" > "${ROS_ENV_FILE}"
echo "export ROS_LOCALHOST_ONLY=${ROS_LOCALHOST_ONLY}" >> "${ROS_ENV_FILE}"
validate_config_param "ros.transport" "dds-config-VALUE.xml" ADDITIONAL_DDS_OPTIONS[@]

NAMESPACE=$(snapctl get ros.namespace)
TRANSPORT_SETTING="$(snapctl get ros.transport)"

# Check if the namespace is set and not empty
if [ -n "$NAMESPACE" ]; then
echo "ros.domain-id=${ROS_DOMAIN_ID} ros.localhost-only=${ROS_LOCALHOST_ONLY} ros.transport=${TRANSPORT_SETTING} ros.namespace=${NAMESPACE}" > "${ROS_SNAP_ARGS}"
echo "export ROS_NAMESPACE=${NAMESPACE}" >> "${ROS_ENV_FILE}"
else
echo "ros.domain-id=${ROS_DOMAIN_ID} ros.localhost-only=${ROS_LOCALHOST_ONLY} ros.transport=${TRANSPORT_SETTING} ros.namespace!" > "${ROS_SNAP_ARGS}"
echo "unset ROS_NAMESPACE" >> "${ROS_ENV_FILE}"
if [ "$TRANSPORT_SETTING" = "rmw_fastrtps_cpp" ] || [ "$TRANSPORT_SETTING" = "shm" ]; then
if ! snapctl is-connected shm-plug; then
log_and_echo "to use 'rmw_fastrtps_cpp' and 'shm' tranport shm-plug need to be connected, please run:"
log_and_echo "sudo snap connect ${SNAP_NAME}:shm-plug ${SNAP_NAME}:shm-slot"
exit 1
fi
fi

# Check the ros.transport setting and export the appropriate environment variable
if [ "$TRANSPORT_SETTING" != "rmw_fastrtps_cpp" ] && [ "$TRANSPORT_SETTING" != "rmw_cyclonedds_cpp" ]; then
profile_type=$(check_xml_profile_type "${SNAP_COMMON}/${TRANSPORT_SETTING}.xml")
profile_type=$(check_xml_profile_type "${SNAP_COMMON}/dds-config-${TRANSPORT_SETTING}.xml")
if [[ "$profile_type" == "rmw_fastrtps_cpp" ]]; then
echo "unset CYCLONEDDS_URI" >> "${ROS_ENV_FILE}"
echo "export RMW_IMPLEMENTATION=${profile_type}" >> "${ROS_ENV_FILE}"
echo "export FASTRTPS_DEFAULT_PROFILES_FILE=${SNAP_COMMON}/${TRANSPORT_SETTING}.xml" >> "${ROS_ENV_FILE}"
echo "unset CYCLONEDDS_URI" >> "${ROS_ENV_FILE}.tmp"
echo "export RMW_IMPLEMENTATION=${profile_type}" >> "${ROS_ENV_FILE}.tmp"
echo "export FASTRTPS_DEFAULT_PROFILES_FILE=${SNAP_COMMON}/dds-config-${TRANSPORT_SETTING}.xml" >> "${ROS_ENV_FILE}.tmp"
elif [[ "$profile_type" == "rmw_cyclonedds_cpp" ]]; then
echo "unset FASTRTPS_DEFAULT_PROFILES_FILE" >> "${ROS_ENV_FILE}"
echo "export RMW_IMPLEMENTATION=${profile_type}" >> "${ROS_ENV_FILE}"
echo "export CYCLONEDDS_URI=file://${SNAP_COMMON}/${TRANSPORT_SETTING}.xml" >> "${ROS_ENV_FILE}"
echo "unset FASTRTPS_DEFAULT_PROFILES_FILE" >> "${ROS_ENV_FILE}.tmp"
echo "export RMW_IMPLEMENTATION=${profile_type}" >> "${ROS_ENV_FILE}.tmp"
echo "export CYCLONEDDS_URI=file://${SNAP_COMMON}/dds-config-${TRANSPORT_SETTING}.xml" >> "${ROS_ENV_FILE}.tmp"
else
log_and_echo "'${TRANSPORT_SETTING}' is not a supported value for '${OPT}'. Possible values are: rmw_fastrtps_cpp, rmw_cyclonedds_cpp, or a valid profile XML file."
log_and_echo "'${TRANSPORT_SETTING}' error: The transport setting is not valid."
exit 1
fi
elif [ "$TRANSPORT_SETTING" == "rmw_fastrtps_cpp" ] || [ "$TRANSPORT_SETTING" == "rmw_cyclonedds_cpp" ]; then
echo "unset CYCLONEDDS_URI" >> "${ROS_ENV_FILE}"
echo "unset FASTRTPS_DEFAULT_PROFILES_FILE" >> "${ROS_ENV_FILE}"
echo "export RMW_IMPLEMENTATION=${TRANSPORT_SETTING}" >> "${ROS_ENV_FILE}"
echo "unset CYCLONEDDS_URI" >> "${ROS_ENV_FILE}.tmp"
echo "unset FASTRTPS_DEFAULT_PROFILES_FILE" >> "${ROS_ENV_FILE}.tmp"
echo "export RMW_IMPLEMENTATION=${TRANSPORT_SETTING}" >> "${ROS_ENV_FILE}.tmp"
fi

echo "ros.transport=${TRANSPORT_SETTING}" >> ${ROS_SNAP_ARGS}.tmp


# Make sure ros-humble-ros-base is connected
ROS_PLUG="ros-${ROS_DISTRO}-ros-base"

if ! snapctl is-connected ${ROS_PLUG}; then
log_and_echo "Plug '${ROS_PLUG}' isn't connected. Please run:"
log_and_echo "snap connect ${SNAP_NAME}:${ROS_PLUG} ${ROS_PLUG}:${ROS_PLUG}"
exit 1
fi

mv "${ROS_ENV_FILE}.tmp" "${ROS_ENV_FILE}"
# Combine all lines into a single line and write to the final file
tr '\n' ' ' < "${ROS_SNAP_ARGS}.tmp" | sed 's/ $/\n/' > "${ROS_SNAP_ARGS}"

# Define the path for the manage_ros_env.sh script
MANAGE_SCRIPT="${SNAP_COMMON}/manage_ros_env.sh"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 9 additions & 11 deletions local-ros/install_hook_ros.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
# Define a function to log messages
source $SNAP/usr/bin/utils.sh

snapctl set ros.transport="udp"
snapctl set ros.localhost-only=0
snapctl set ros.domain-id=0
snapctl set ros.namespace! # unset
source_ros

if ! snapctl is-connected ros-humble-ros-base; then
log "Plug 'ros-humble-ros-base' isn't connected, please run:"
log "sudo snap connect ${SNAP_NAME}:ros-humble-ros-base ros-humble-ros-base:ros-humble-ros-base"
if [[ $ROS_DISTRO == "humble" ]]; then
snapctl set ros.localhost-only=''
elif [[ $ROS_DISTRO == "jazzy" ]]; then
snapctl set ros.automatic-discovery-range="" # unset
snapctl set ros.static-peers='' # unset
fi

if ! snapctl is-connected shm-plug; then
log "Plug 'shm-plug' isn't connected, please run:"
log "sudo snap connect ${SNAP_NAME}:shm-plug ${SNAP_NAME}:shm-slot"
fi
snapctl set ros.transport="udp"
snapctl set ros.domain-id=0
snapctl set ros.namespace="" # unset

# copy DDS config files to shared folder
cp -r $SNAP/usr/share/husarion-snap-common/config/*.xml ${SNAP_COMMON}/
Loading

0 comments on commit 4397609

Please sign in to comment.