Path: blob/master/Tools/environment_install/install-ROS-ubuntu.sh
9447 views
#!/usr/bin/env bash1echo "---------- $0 start ----------"2set -e3# set -x45ROS_WS_ROOT=$HOME/ardupilot-ws6AP_GZ_ROOT=$HOME/ardupilot_gz_ws78red=`tput setaf 1`9green=`tput setaf 2`10reset=`tput sgr0`1112sep="##############################################"131415function heading() {16echo "$sep"17echo $*18echo "$sep"19}2021ASSUME_YES=false2223function maybe_prompt_user() {24if $ASSUME_YES; then25return 026else27read -p "$1"28if [[ $REPLY =~ ^[Yy]$ ]]; then29return 030else31return 132fi33fi34}3536function usage37{38echo "Usage: ./installROS.sh [[-p package] | [-h]]"39echo "Install ROS1"40echo "This script will select the ROS distribution according to the OS being used"41echo "Installs desktop-full as default base package; Use -p to override"42echo "-p | --package <packagename> ROS package to install"43echo " Multiple usage allowed"44echo " Must include one of the following:"45echo " ros-base"46echo " desktop"47echo " desktop-full"48echo "-h | --help This message"49}5051function shouldInstallPackages52{53echo "${red}Your package list did not include a recommended base package${reset}"54echo "Please include one of the following:"55echo " ros-base"56echo " desktop"57echo " desktop-full"58echo ""59echo "ROS not installed"60}6162function package_is_installed() {63dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed"64}6566# Iterate through command line inputs67packages=()68while [ "$1" != "" ]; do69case $1 in70-p | --package ) shift71packages+=("$1")72;;73-h | --help ) usage74exit75;;76* ) usage77exit 178esac79shift80done8182# Install lsb-release as it is needed to check Ubuntu version83if ! package_is_installed "lsb-release"; then84heading "Installing lsb-release"85sudo apt install lsb-release -y86echo "Done!"87fi8889# Checking Ubuntu release to adapt software version to install90RELEASE_CODENAME=$(lsb_release -c -s)91PYTHON_V="python3" # starting from ubuntu 20.04, python isn't symlink to default python interpreter929394# echo $RELEASE_CODENAME9596if [ ${RELEASE_CODENAME} == 'focal' ]; then97#Ubuntu 20.04 - Noetic98ROS_VERSION="noetic"99PYTHON_V="python3"100heading "${green}Detected Ubuntu 20.04, installing ROS Noetic${reset}"101elif [ ${RELEASE_CODENAME} == 'jammy' ]; then102#Ubuntu 22.04 - unsupported only ROS2103heading "${red}Currently only ROS1 is supported. This Ubuntu release can only be used with ROS2.${reset}"104exit 1105else106# We assume an unsupported OS is being used.107heading "${red}Unsupported OS detected. Please refer to the ROS webpage to find how to install ROS1 on your system if at all possible.${reset}"108exit 1109fi110111# Check to see if other packages were specified112# If not, set the default base package113if [ ${#packages[@]} -eq 0 ] ; then114packages+="desktop-full"115fi116echo "Packages to install: "${packages[@]}117# Check to see if we have a ROS base kinda thingie118hasBasePackage=false119for package in "${packages[@]}"; do120if [[ $package == "ros-base" ]]; then121delete=ros-base122packages=( "${packages[@]/$delete}" )123packages+=" ros-${ROS_VERSION}-ros-base"124hasBasePackage=true125break126elif [[ $package == "desktop" ]]; then127delete=desktop128packages=( "${packages[@]/$delete}" )129packages+=" ros-${ROS_VERSION}-desktop"130hasBasePackage=true131break132elif [[ $package == "desktop-full" ]]; then133delete=desktop-full134packages=( "${packages[@]/$delete}" )135packages+=" ros-${ROS_VERSION}-desktop-full"136hasBasePackage=true137break138fi139done140if [ $hasBasePackage == false ] ; then141shouldInstallPackages142exit 1143fi144145heading "${green}Adding Repositories and source lists${reset}"146#Lets start instaling stuff147sudo apt install software-properties-common -y148sudo apt-add-repository universe149sudo apt-add-repository multiverse150sudo apt-add-repository restricted151152# Setup sources.lst153sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'154# Setup keys155sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654156# If you experience issues connecting to the keyserver, you can try substituting hkp://pgp.mit.edu:80 or hkp://keyserver.ubuntu.com:80 in the previous command.157# Installation158159heading "${green}Updating apt${reset}"160sudo apt update161162heading "${green}Installing ROS${reset}"163# Here we loop through any packages passed on the command line164# Install packages ...165for package in "${packages[@]}"; do166sudo apt install $package -y167done168169# This is where you might start to modify the packages being installed, i.e.# sudo apt install ros-${ROS_VERSION}-{package_name}170# sudo apt install -y setpriv171# sudo apt install -y ros-${ROS_VERSION}-robot-upstart172# sudo apt install -y ros-${ROS_VERSION}-navigation173174# Install MAVROS and the geographic libs175sudo apt install -y ros-${ROS_VERSION}-mavros176177wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh178sudo bash ./install_geographiclib_datasets.sh179180# install other needed packages181sudo apt install build-essential cmake -y182183#184# To find available packages:185# apt-cache search ros-melodic186#187# Initialize rosdep188189heading "${green}Installing rosdep${reset}"190191sudo apt install ${PYTHON_V}-rosdep -y192# Certificates are messed up on earlier version Jetson for some reason193# Do not know if it is an issue with the Xavier, test by commenting out194# sudo c_rehash /etc/ssl/certs195# Initialize rosdep196197heading "${green}Initializaing rosdep${reset}"198199sudo rosdep init || true200# To find available packages, use:201rosdep update202# Use this to install dependencies of packages in a workspace203# rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y204# Environment Setup - Don't add /opt/ros/${ROS_VERSION}/setup.bash if it's already in bashrc205if maybe_prompt_user "Do you want to add ROS_HOSTNAME and ROS_MASTER_URI to your .bashrc [N/y]?" ; then206heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"207grep -q -F "ROS_HOSTNAME=$HOSTNAME.local" ~/.bashrc || echo "ROS_HOSTNAME=$HOSTNAME.local" >> ~/.bashrc208grep -q -F "ROS_MASTER_URI=http://$HOSTNAME.local:11311" ~/.bashrc || echo "ROS_MASTER_URI=http://$HOSTNAME.local:11311" >> ~/.bashrc209grep -q -F "source /opt/ros/${ROS_VERSION}/setup.bash" ~/.bashrc || echo "source /opt/ros/${ROS_VERSION}/setup.bash" >> ~/.bashrc210source ~/.bashrc211fi212213heading "${green}Installing rosinstall tools${reset}"214215sudo apt install ${PYTHON_V}-rosinstall ${PYTHON_V}-rosinstall-generator ${PYTHON_V}-wstool ${PYTHON_V}-catkin-tools -y216217heading "${green}Installing Ardupilot-ROS workspace${reset}"218219if maybe_prompt_user "Add ardupilot-ws to your home folder [N/y]?" ; then220if [ ! -d $ROS_WS_ROOT ]; then221mkdir -p $ROS_WS_ROOT/src222pushd $ROS_WS_ROOT223source /opt/ros/${ROS_VERSION}/setup.bash224catkin init225pushd src226git clone https://github.com/ArduPilot/ardupilot_ros.git227popd228sudo apt update229rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y230catkin build231popd232else233heading "${red}ardupilot-ws already exists, skipping...${reset}"234fi235236else237echo "Skipping adding ardupilot_ws to your home folder."238fi239240241if maybe_prompt_user "Add ardupilot_gazebo to your home folder [N/y]?" ; then242if [ ! -d $AP_GZ_ROOT ]; then243sudo apt install gz-garden rapidjson-dev244mkdir -p $AP_GZ_ROOT/src245pushd $AP_GZ_ROOT/src246git clone https://github.com/ArduPilot/ardupilot_gazebo247pushd ardupilot_gazebo248mkdir build && pushd build249cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo250make -j4251popd252popd253popd254echo "export GZ_SIM_SYSTEM_PLUGIN_PATH=${AP_GZ_ROOT}/src/ardupilot_gazebo/build:${GZ_SIM_SYSTEM_PLUGIN_PATH}" >> ~/.bashrc255echo "export GZ_SIM_RESOURCE_PATH=${AP_GZ_ROOT}/src/ardupilot_gazebo/models:${AP_GZ_ROOT}/src/ardupilot_gazebo/worlds:${GZ_SIM_RESOURCE_PATH}" >> ~/.bashrc256else257heading "${red}ardupilot_gazebo already exists, skipping...${reset}"258fi259260else261echo "Skipping adding ardupilot_gazebo to your home folder."262fi263264heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"265grep -q -F "source $ROS_WS_ROOT/devel/setup.bash" ~/.bashrc || echo "source $ROS_WS_ROOT/devel/setup.bash" >> ~/.bashrc266source ~/.bashrc267268heading "${green}Installation complete! Please close this terminal and open a new one for changes to take effect!${reset}"269270271