Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/environment_install/install-ROS-ubuntu.sh
9447 views
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} == 'focal' ]; then
98
#Ubuntu 20.04 - Noetic
99
ROS_VERSION="noetic"
100
PYTHON_V="python3"
101
heading "${green}Detected Ubuntu 20.04, installing ROS Noetic${reset}"
102
elif [ ${RELEASE_CODENAME} == 'jammy' ]; then
103
#Ubuntu 22.04 - unsupported only ROS2
104
heading "${red}Currently only ROS1 is supported. This Ubuntu release can only be used with ROS2.${reset}"
105
exit 1
106
else
107
# We assume an unsupported OS is being used.
108
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}"
109
exit 1
110
fi
111
112
# Check to see if other packages were specified
113
# If not, set the default base package
114
if [ ${#packages[@]} -eq 0 ] ; then
115
packages+="desktop-full"
116
fi
117
echo "Packages to install: "${packages[@]}
118
# Check to see if we have a ROS base kinda thingie
119
hasBasePackage=false
120
for package in "${packages[@]}"; do
121
if [[ $package == "ros-base" ]]; then
122
delete=ros-base
123
packages=( "${packages[@]/$delete}" )
124
packages+=" ros-${ROS_VERSION}-ros-base"
125
hasBasePackage=true
126
break
127
elif [[ $package == "desktop" ]]; then
128
delete=desktop
129
packages=( "${packages[@]/$delete}" )
130
packages+=" ros-${ROS_VERSION}-desktop"
131
hasBasePackage=true
132
break
133
elif [[ $package == "desktop-full" ]]; then
134
delete=desktop-full
135
packages=( "${packages[@]/$delete}" )
136
packages+=" ros-${ROS_VERSION}-desktop-full"
137
hasBasePackage=true
138
break
139
fi
140
done
141
if [ $hasBasePackage == false ] ; then
142
shouldInstallPackages
143
exit 1
144
fi
145
146
heading "${green}Adding Repositories and source lists${reset}"
147
#Lets start instaling stuff
148
sudo apt install software-properties-common -y
149
sudo apt-add-repository universe
150
sudo apt-add-repository multiverse
151
sudo apt-add-repository restricted
152
153
# Setup sources.lst
154
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
155
# Setup keys
156
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
157
# 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.
158
# Installation
159
160
heading "${green}Updating apt${reset}"
161
sudo apt update
162
163
heading "${green}Installing ROS${reset}"
164
# Here we loop through any packages passed on the command line
165
# Install packages ...
166
for package in "${packages[@]}"; do
167
sudo apt install $package -y
168
done
169
170
# This is where you might start to modify the packages being installed, i.e.# sudo apt install ros-${ROS_VERSION}-{package_name}
171
# sudo apt install -y setpriv
172
# sudo apt install -y ros-${ROS_VERSION}-robot-upstart
173
# sudo apt install -y ros-${ROS_VERSION}-navigation
174
175
# Install MAVROS and the geographic libs
176
sudo apt install -y ros-${ROS_VERSION}-mavros
177
178
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
179
sudo bash ./install_geographiclib_datasets.sh
180
181
# install other needed packages
182
sudo apt install build-essential cmake -y
183
184
#
185
# To find available packages:
186
# apt-cache search ros-melodic
187
#
188
# Initialize rosdep
189
190
heading "${green}Installing rosdep${reset}"
191
192
sudo apt install ${PYTHON_V}-rosdep -y
193
# Certificates are messed up on earlier version Jetson for some reason
194
# Do not know if it is an issue with the Xavier, test by commenting out
195
# sudo c_rehash /etc/ssl/certs
196
# Initialize rosdep
197
198
heading "${green}Initializaing rosdep${reset}"
199
200
sudo rosdep init || true
201
# To find available packages, use:
202
rosdep update
203
# Use this to install dependencies of packages in a workspace
204
# rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
205
# Environment Setup - Don't add /opt/ros/${ROS_VERSION}/setup.bash if it's already in bashrc
206
if maybe_prompt_user "Do you want to add ROS_HOSTNAME and ROS_MASTER_URI to your .bashrc [N/y]?" ; then
207
heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"
208
grep -q -F "ROS_HOSTNAME=$HOSTNAME.local" ~/.bashrc || echo "ROS_HOSTNAME=$HOSTNAME.local" >> ~/.bashrc
209
grep -q -F "ROS_MASTER_URI=http://$HOSTNAME.local:11311" ~/.bashrc || echo "ROS_MASTER_URI=http://$HOSTNAME.local:11311" >> ~/.bashrc
210
grep -q -F "source /opt/ros/${ROS_VERSION}/setup.bash" ~/.bashrc || echo "source /opt/ros/${ROS_VERSION}/setup.bash" >> ~/.bashrc
211
source ~/.bashrc
212
fi
213
214
heading "${green}Installing rosinstall tools${reset}"
215
216
sudo apt install ${PYTHON_V}-rosinstall ${PYTHON_V}-rosinstall-generator ${PYTHON_V}-wstool ${PYTHON_V}-catkin-tools -y
217
218
heading "${green}Installing Ardupilot-ROS workspace${reset}"
219
220
if maybe_prompt_user "Add ardupilot-ws to your home folder [N/y]?" ; then
221
if [ ! -d $ROS_WS_ROOT ]; then
222
mkdir -p $ROS_WS_ROOT/src
223
pushd $ROS_WS_ROOT
224
source /opt/ros/${ROS_VERSION}/setup.bash
225
catkin init
226
pushd src
227
git clone https://github.com/ArduPilot/ardupilot_ros.git
228
popd
229
sudo apt update
230
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
231
catkin build
232
popd
233
else
234
heading "${red}ardupilot-ws already exists, skipping...${reset}"
235
fi
236
237
else
238
echo "Skipping adding ardupilot_ws to your home folder."
239
fi
240
241
242
if maybe_prompt_user "Add ardupilot_gazebo to your home folder [N/y]?" ; then
243
if [ ! -d $AP_GZ_ROOT ]; then
244
sudo apt install gz-garden rapidjson-dev
245
mkdir -p $AP_GZ_ROOT/src
246
pushd $AP_GZ_ROOT/src
247
git clone https://github.com/ArduPilot/ardupilot_gazebo
248
pushd ardupilot_gazebo
249
mkdir build && pushd build
250
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
251
make -j4
252
popd
253
popd
254
popd
255
echo "export GZ_SIM_SYSTEM_PLUGIN_PATH=${AP_GZ_ROOT}/src/ardupilot_gazebo/build:${GZ_SIM_SYSTEM_PLUGIN_PATH}" >> ~/.bashrc
256
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
257
else
258
heading "${red}ardupilot_gazebo already exists, skipping...${reset}"
259
fi
260
261
else
262
echo "Skipping adding ardupilot_gazebo to your home folder."
263
fi
264
265
heading "${green}Adding setup.bash, ROS_MASTER_URI and ROS_HOSTNAME to .bashrc ${reset}"
266
grep -q -F "source $ROS_WS_ROOT/devel/setup.bash" ~/.bashrc || echo "source $ROS_WS_ROOT/devel/setup.bash" >> ~/.bashrc
267
source ~/.bashrc
268
269
heading "${green}Installation complete! Please close this terminal and open a new one for changes to take effect!${reset}"
270
271