CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/environment_install/install-ROS-ubuntu.sh
Views: 1798
1
#!/usr/bin/env bash
2
echo "---------- $0 start ----------"
3
set -e
4
# set -x
5
6
ROS_WS_ROOT=$HOME/ardupilot-ws
7
AP_GZ_ROOT=$HOME/ardupilot_gz_ws
8
9
red=`tput setaf 1`
10
green=`tput setaf 2`
11
reset=`tput sgr0`
12
13
sep="##############################################"
14
15
16
function heading() {
17
echo "$sep"
18
echo $*
19
echo "$sep"
20
}
21
22
ASSUME_YES=false
23
24
function maybe_prompt_user() {
25
if $ASSUME_YES; then
26
return 0
27
else
28
read -p "$1"
29
if [[ $REPLY =~ ^[Yy]$ ]]; then
30
return 0
31
else
32
return 1
33
fi
34
fi
35
}
36
37
function usage
38
{
39
echo "Usage: ./installROS.sh [[-p package] | [-h]]"
40
echo "Install ROS1"
41
echo "This script will select the ROS distribution according to the OS being used"
42
echo "Installs desktop-full as default base package; Use -p to override"
43
echo "-p | --package <packagename> ROS package to install"
44
echo " Multiple usage allowed"
45
echo " Must include one of the following:"
46
echo " ros-base"
47
echo " desktop"
48
echo " desktop-full"
49
echo "-h | --help This message"
50
}
51
52
function shouldInstallPackages
53
{
54
echo "${red}Your package list did not include a recommended base package${reset}"
55
echo "Please include one of the following:"
56
echo " ros-base"
57
echo " desktop"
58
echo " desktop-full"
59
echo ""
60
echo "ROS not installed"
61
}
62
63
function package_is_installed() {
64
dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed"
65
}
66
67
# Iterate through command line inputs
68
packages=()
69
while [ "$1" != "" ]; do
70
case $1 in
71
-p | --package ) shift
72
packages+=("$1")
73
;;
74
-h | --help ) usage
75
exit
76
;;
77
* ) usage
78
exit 1
79
esac
80
shift
81
done
82
83
# Install lsb-release as it is needed to check Ubuntu version
84
if ! package_is_installed "lsb-release"; then
85
heading "Installing lsb-release"
86
sudo apt install lsb-release -y
87
echo "Done!"
88
fi
89
90
# Checking Ubuntu release to adapt software version to install
91
RELEASE_CODENAME=$(lsb_release -c -s)
92
PYTHON_V="python3" # starting from ubuntu 20.04, python isn't symlink to default python interpreter
93
94
95
# echo $RELEASE_CODENAME
96
97
if [ ${RELEASE_CODENAME} == 'bionic' ] ; then
98
#Ubuntu 18.04 - Melodic
99
ROS_VERSION="melodic"
100
PYTHON_V="python2"
101
heading "${green}Detected Ubuntu 18.04, installing ROS Melodic${reset}"
102
elif [ ${RELEASE_CODENAME} == 'buster' ]; then
103
#RPi Buster - Melodic
104
ROS_VERSION="melodic"
105
PYTHON_V="python2"
106
heading "${green}Detected RPi Buster, installing ROS Melodic${reset}"
107
elif [ ${RELEASE_CODENAME} == 'focal' ]; then
108
#Ubuntu 20.04 - Noetic
109
ROS_VERSION="noetic"
110
PYTHON_V="python3"
111
heading "${green}Detected Ubuntu 20.04, installing ROS Noetic${reset}"
112
elif [ ${RELEASE_CODENAME} == 'jammy' ]; then
113
#Ubuntu 22.04 - unsupported only ROS2
114
heading "${red}Currently only ROS1 is supported. This Ubuntu release can only be used with ROS2.${reset}"
115
exit 1
116
else
117
# We assume an unsupported OS is being used.
118
heading "${red}Unsupported OS detected. Please refer to the ROS webpage to find how to install ROS1 on your system if at all possible.${reset}"
119
exit 1
120
fi
121
122
# Check to see if other packages were specified
123
# If not, set the default base package
124
if [ ${#packages[@]} -eq 0 ] ; then
125
packages+="desktop-full"
126
fi
127
echo "Packages to install: "${packages[@]}
128
# Check to see if we have a ROS base kinda thingie
129
hasBasePackage=false
130
for package in "${packages[@]}"; do
131
if [[ $package == "ros-base" ]]; then
132
delete=ros-base
133
packages=( "${packages[@]/$delete}" )
134
packages+=" ros-${ROS_VERSION}-ros-base"
135
hasBasePackage=true
136
break
137
elif [[ $package == "desktop" ]]; then
138
delete=desktop
139
packages=( "${packages[@]/$delete}" )
140
packages+=" ros-${ROS_VERSION}-desktop"
141
hasBasePackage=true
142
break
143
elif [[ $package == "desktop-full" ]]; then
144
delete=desktop-full
145
packages=( "${packages[@]/$delete}" )
146
packages+=" ros-${ROS_VERSION}-desktop-full"
147
hasBasePackage=true
148
break
149
fi
150
done
151
if [ $hasBasePackage == false ] ; then
152
shouldInstallPackages
153
exit 1
154
fi
155
156
heading "${green}Adding Repositories and source lists${reset}"
157
#Lets start instaling stuff
158
sudo apt install software-properties-common -y
159
sudo apt-add-repository universe
160
sudo apt-add-repository multiverse
161
sudo apt-add-repository restricted
162
163
# Setup sources.lst
164
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
165
# Setup keys
166
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
167
# 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.
168
# Installation
169
170
heading "${green}Updating apt${reset}"
171
sudo apt update
172
173
heading "${green}Installing ROS${reset}"
174
# Here we loop through any packages passed on the command line
175
# Install packages ...
176
for package in "${packages[@]}"; do
177
sudo apt install $package -y
178
done
179
180
# This is where you might start to modify the packages being installed, i.e.# sudo apt install ros-${ROS_VERSION}-{package_name}
181
# sudo apt install -y setpriv
182
# sudo apt install -y ros-${ROS_VERSION}-robot-upstart
183
# sudo apt install -y ros-${ROS_VERSION}-navigation
184
185
# Install MAVROS and the geographic libs
186
sudo apt install -y ros-${ROS_VERSION}-mavros
187
188
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
189
sudo bash ./install_geographiclib_datasets.sh
190
191
# install other needed packages
192
sudo apt install build-essential cmake -y
193
194
#
195
# To find available packages:
196
# apt-cache search ros-melodic
197
#
198
# Initialize rosdep
199
200
heading "${green}Installing rosdep${reset}"
201
202
sudo apt install ${PYTHON_V}-rosdep -y
203
# Certificates are messed up on earlier version Jetson for some reason
204
# Do not know if it is an issue with the Xavier, test by commenting out
205
# sudo c_rehash /etc/ssl/certs
206
# Initialize rosdep
207
208
heading "${green}Initializaing rosdep${reset}"
209
210
sudo rosdep init || true
211
# To find available packages, use:
212
rosdep update
213
# Use this to install dependencies of packages in a workspace
214
# rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
215
# Environment Setup - Don't add /opt/ros/${ROS_VERSION}/setup.bash if it's already in bashrc
216
if maybe_prompt_user "Do you want to add ROS_HOSTNAME and ROS_MASTER_URI to your .bashrc [N/y]?" ; then
217
heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"
218
grep -q -F "ROS_HOSTNAME=$HOSTNAME.local" ~/.bashrc || echo "ROS_HOSTNAME=$HOSTNAME.local" >> ~/.bashrc
219
grep -q -F "ROS_MASTER_URI=http://$HOSTNAME.local:11311" ~/.bashrc || echo "ROS_MASTER_URI=http://$HOSTNAME.local:11311" >> ~/.bashrc
220
grep -q -F "source /opt/ros/${ROS_VERSION}/setup.bash" ~/.bashrc || echo "source /opt/ros/${ROS_VERSION}/setup.bash" >> ~/.bashrc
221
source ~/.bashrc
222
fi
223
224
heading "${green}Installing rosinstall tools${reset}"
225
226
sudo apt install ${PYTHON_V}-rosinstall ${PYTHON_V}-rosinstall-generator ${PYTHON_V}-wstool ${PYTHON_V}-catkin-tools -y
227
228
heading "${green}Installing Ardupilot-ROS workspace${reset}"
229
230
if maybe_prompt_user "Add ardupilot-ws to your home folder [N/y]?" ; then
231
if [ ! -d $ROS_WS_ROOT ]; then
232
mkdir -p $ROS_WS_ROOT/src
233
pushd $ROS_WS_ROOT
234
source /opt/ros/${ROS_VERSION}/setup.bash
235
catkin init
236
pushd src
237
git clone https://github.com/ArduPilot/ardupilot_ros.git
238
popd
239
sudo apt update
240
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
241
catkin build
242
popd
243
else
244
heading "${red}ardupilot-ws already exists, skipping...${reset}"
245
fi
246
247
else
248
echo "Skipping adding ardupilot_ws to your home folder."
249
fi
250
251
252
if maybe_prompt_user "Add ardupilot_gazebo to your home folder [N/y]?" ; then
253
if [ ! -d $AP_GZ_ROOT ]; then
254
sudo apt install gz-garden rapidjson-dev
255
mkdir -p $AP_GZ_ROOT/src
256
pushd $AP_GZ_ROOT/src
257
git clone https://github.com/ArduPilot/ardupilot_gazebo
258
pushd ardupilot_gazebo
259
mkdir build && pushd build
260
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
261
make -j4
262
popd
263
popd
264
popd
265
echo "export GZ_SIM_SYSTEM_PLUGIN_PATH=${AP_GZ_ROOT}/src/ardupilot_gazebo/build:${GZ_SIM_SYSTEM_PLUGIN_PATH}" >> ~/.bashrc
266
echo "export GZ_SIM_RESOURCE_PATH=${AP_GZ_ROOT}/src/ardupilot_gazebo/models:${AP_GZ_ROOT}/src/ardupilot_gazebo/worlds:${GZ_SIM_RESOURCE_PATH}" >> ~/.bashrc
267
else
268
heading "${red}ardupilot_gazebo already exists, skipping...${reset}"
269
fi
270
271
else
272
echo "Skipping adding ardupilot_gazebo to your home folder."
273
fi
274
275
heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"
276
grep -q -F "source $ROS_WS_ROOT/devel/setup.bash" ~/.bashrc || echo "source $ROS_WS_ROOT/devel/setup.bash" >> ~/.bashrc
277
source ~/.bashrc
278
279
heading "${green}Installation complete! Please close this terminal and open a new one for changes to take effect!${reset}"
280
281