2019-12-11 06:11:03 -04:00
|
|
|
#!/bin/bash
|
2019-12-18 17:33:28 -04:00
|
|
|
# run multiple instances of the 'px4' binary, with the gazebo SITL simulation
|
2020-07-20 21:36:15 -03:00
|
|
|
# It assumes px4 is already built, with 'make px4_sitl_default gazebo'
|
2019-12-11 06:11:03 -04:00
|
|
|
|
|
|
|
# The simulator is expected to send to TCP port 4560+i for i in [0, N-1]
|
2019-12-11 06:55:38 -04:00
|
|
|
# For example gazebo can be run like this:
|
2019-12-13 07:57:03 -04:00
|
|
|
#./Tools/gazebo_sitl_multiple_run.sh -n 10 -m iris
|
2019-12-11 06:11:03 -04:00
|
|
|
|
2019-12-13 07:57:03 -04:00
|
|
|
function cleanup() {
|
|
|
|
pkill -x px4
|
|
|
|
pkill gzclient
|
2020-08-08 08:19:19 -03:00
|
|
|
pkill gzserver
|
2019-12-13 07:57:03 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 03:23:16 -03:00
|
|
|
function spawn_model() {
|
|
|
|
MODEL=$1
|
|
|
|
N=$2 #Instance Number
|
|
|
|
|
2020-11-16 23:20:45 -04:00
|
|
|
SUPPORTED_MODELS=("iris" "iris_rtps" "plane" "standard_vtol" "rover" "r1_rover" "typhoon_h480")
|
2020-12-07 01:17:15 -04:00
|
|
|
if [[ " ${SUPPORTED_MODELS[*]} " != *"$MODEL"* ]];
|
2020-06-24 03:23:16 -03:00
|
|
|
then
|
2020-08-08 08:19:19 -03:00
|
|
|
echo "ERROR: Currently only vehicle model $MODEL is not supported!"
|
|
|
|
echo " Supported Models: [${SUPPORTED_MODELS[@]}]"
|
|
|
|
trap "cleanup" SIGINT SIGTERM EXIT
|
2020-06-24 03:23:16 -03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
working_dir="$build_path/instance_$n"
|
|
|
|
[ ! -d "$working_dir" ] && mkdir -p "$working_dir"
|
|
|
|
|
|
|
|
pushd "$working_dir" &>/dev/null
|
2020-09-29 10:45:57 -03:00
|
|
|
echo "starting instance $N in $(pwd)"
|
|
|
|
../bin/px4 -i $N -d "$build_path/etc" -w sitl_${MODEL}_${N} -s etc/init.d-posix/rcS >out.log 2>err.log &
|
2021-01-08 14:29:47 -04:00
|
|
|
python3 ${src_path}/Tools/sitl_gazebo/scripts/jinja_gen.py ${src_path}/Tools/sitl_gazebo/models/${MODEL}/${MODEL}.sdf.jinja ${src_path}/Tools/sitl_gazebo --mavlink_tcp_port $((4560+${N})) --mavlink_udp_port $((14560+${N})) --mavlink_id $((1+${N})) --gst_udp_port $((5600+${N})) --video_uri $((5600+${N})) --mavlink_cam_udp_port $((14530+${N})) --output-file /tmp/${MODEL}_${N}.sdf
|
2020-06-24 03:23:16 -03:00
|
|
|
|
|
|
|
echo "Spawning ${MODEL}_${N}"
|
|
|
|
|
|
|
|
gz model --spawn-file=/tmp/${MODEL}_${N}.sdf --model-name=${MODEL}_${N} -x 0.0 -y $((3*${N})) -z 0.0
|
|
|
|
|
|
|
|
popd &>/dev/null
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-13 07:57:03 -04:00
|
|
|
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
|
|
|
|
then
|
2020-06-24 03:23:16 -03:00
|
|
|
echo "Usage: $0 [-n <num_vehicles>] [-m <vehicle_model>] [-w <world>] [-s <script>]"
|
|
|
|
echo "-s flag is used to script spawning vehicles e.g. $0 -s iris:3,plane:2"
|
2019-12-13 07:57:03 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-08-08 08:19:19 -03:00
|
|
|
while getopts n:m:w:s:t: option
|
2019-12-13 07:57:03 -04:00
|
|
|
do
|
|
|
|
case "${option}"
|
|
|
|
in
|
|
|
|
n) NUM_VEHICLES=${OPTARG};;
|
|
|
|
m) VEHICLE_MODEL=${OPTARG};;
|
2020-04-27 06:57:47 -03:00
|
|
|
w) WORLD=${OPTARG};;
|
2020-06-24 03:23:16 -03:00
|
|
|
s) SCRIPT=${OPTARG};;
|
2020-08-08 08:19:19 -03:00
|
|
|
t) TARGET=${OPTARG};;
|
2019-12-13 07:57:03 -04:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
num_vehicles=${NUM_VEHICLES:=3}
|
2020-04-27 06:57:47 -03:00
|
|
|
world=${WORLD:=empty}
|
2020-08-08 08:19:19 -03:00
|
|
|
target=${TARGET:=px4_sitl_default}
|
|
|
|
export PX4_SIM_MODEL=${VEHICLE_MODEL:=iris}
|
2020-02-19 04:46:38 -04:00
|
|
|
|
2020-08-08 08:19:19 -03:00
|
|
|
echo ${SCRIPT}
|
2019-12-11 06:11:03 -04:00
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
src_path="$SCRIPT_DIR/.."
|
|
|
|
|
2020-08-08 08:19:19 -03:00
|
|
|
build_path=${src_path}/build/${target}
|
2019-12-11 06:11:03 -04:00
|
|
|
mavlink_udp_port=14560
|
|
|
|
mavlink_tcp_port=4560
|
|
|
|
|
|
|
|
echo "killing running instances"
|
|
|
|
pkill -x px4 || true
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
2020-08-08 08:19:19 -03:00
|
|
|
source ${src_path}/Tools/setup_gazebo.bash ${src_path} ${src_path}/build/${target}
|
2019-12-12 05:08:55 -04:00
|
|
|
|
2019-12-11 06:11:03 -04:00
|
|
|
echo "Starting gazebo"
|
|
|
|
gzserver ${src_path}/Tools/sitl_gazebo/worlds/${world}.world --verbose &
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
n=0
|
2020-06-24 03:23:16 -03:00
|
|
|
if [ -z ${SCRIPT} ]; then
|
|
|
|
if [ $num_vehicles -gt 255 ]
|
|
|
|
then
|
|
|
|
echo "Tried spawning $num_vehicles vehicles. The maximum number of supported vehicles is 255"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
while [ $n -lt $num_vehicles ]; do
|
|
|
|
spawn_model ${PX4_SIM_MODEL} $n
|
|
|
|
n=$(($n + 1))
|
|
|
|
done
|
|
|
|
else
|
|
|
|
IFS=,
|
|
|
|
for target in ${SCRIPT}; do
|
|
|
|
target="$(echo "$target" | tr -d ' ')" #Remove spaces
|
|
|
|
target_vehicle="${target%:*}"
|
|
|
|
target_number="${target#*:}"
|
|
|
|
|
|
|
|
if [ $n -gt 255 ]
|
|
|
|
then
|
|
|
|
echo "Tried spawning $n vehicles. The maximum number of supported vehicles is 255"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
m=0
|
|
|
|
while [ $m -lt ${target_number} ]; do
|
|
|
|
spawn_model ${target_vehicle} $n
|
|
|
|
m=$(($m + 1))
|
|
|
|
n=$(($n + 1))
|
|
|
|
done
|
|
|
|
done
|
2019-12-11 06:11:03 -04:00
|
|
|
|
2020-06-24 03:23:16 -03:00
|
|
|
fi
|
2019-12-13 07:57:03 -04:00
|
|
|
trap "cleanup" SIGINT SIGTERM EXIT
|
|
|
|
|
2019-12-11 06:11:03 -04:00
|
|
|
echo "Starting gazebo client"
|
|
|
|
gzclient
|