sitl_run.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env bash
  2. set -e
  3. if [ "$#" -lt 6 ]; then
  4. echo usage: sitl_run.sh sitl_bin debugger model world src_path build_path
  5. exit 1
  6. fi
  7. if [[ -n "$DONT_RUN" ]]; then
  8. echo "Not running simulation (DONT_RUN is set)."
  9. exit 0
  10. fi
  11. sitl_bin="$1"
  12. debugger="$2"
  13. model="$3"
  14. world="$4"
  15. src_path="$5"
  16. build_path="$6"
  17. echo SITL ARGS
  18. echo sitl_bin: $sitl_bin
  19. echo debugger: $debugger
  20. echo model: $model
  21. echo world: $world
  22. echo src_path: $src_path
  23. echo build_path: $build_path
  24. rootfs="$build_path/rootfs" # this is the working directory
  25. mkdir -p "$rootfs"
  26. # To disable user input
  27. if [[ -n "$NO_PXH" ]]; then
  28. no_pxh=-d
  29. else
  30. no_pxh=""
  31. fi
  32. # To disable user input
  33. if [[ -n "$VERBOSE_SIM" ]]; then
  34. verbose="--verbose"
  35. else
  36. verbose=""
  37. fi
  38. # Disable follow mode
  39. if [[ "$PX4_NO_FOLLOW_MODE" != "1" ]]; then
  40. follow_mode="--gui-client-plugin libgazebo_user_camera_plugin.so"
  41. else
  42. follow_mode=""
  43. fi
  44. # To use gazebo_ros ROS2 plugins
  45. if [[ -n "$ROS_VERSION" ]] && [ "$ROS_VERSION" == "2" ]; then
  46. ros_args="-s libgazebo_ros_init.so -s libgazebo_ros_factory.so"
  47. else
  48. ros_args="-s libgazebo_ros_api_plugin.so"
  49. fi
  50. if [ "$model" == "" ] || [ "$model" == "none" ]; then
  51. echo "empty model, setting iris as default"
  52. model="iris"
  53. fi
  54. # kill process names that might stil
  55. # be running from last time
  56. pkill -x gazebo || true
  57. export PX4_SIM_MODEL=gazebo-classic_${model}
  58. export PX4_SIM_WORLD=${world}
  59. SIM_PID=0
  60. if [ -x "$(command -v gazebo)" ]; then
  61. # Get the model name
  62. model_name="${model}"
  63. # Set the plugin path so Gazebo finds our model and sim
  64. source "$src_path/Tools/simulation/gazebo-classic/setup_gazebo.bash" "${src_path}" "${build_path}"
  65. export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${src_path}
  66. export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic
  67. echo "ll:$ROS_PACKAGE_PATH"
  68. if [ -z $PX4_SITL_WORLD ]; then
  69. #Spawn predefined world
  70. if [ "$world" == "none" ]; then
  71. if [ -f ${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/${model}.world ]; then
  72. echo "empty world, default world ${model}.world for model found"
  73. world_path="${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/${model}.world"
  74. else
  75. echo "empty world, setting empty.world as default"
  76. world_path="${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/test.world"
  77. fi
  78. else
  79. #Spawn empty world if world with model name doesn't exist
  80. world_path="${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/${world}.world"
  81. fi
  82. else
  83. if [ -f ${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/${PX4_SITL_WORLD}.world ]; then
  84. # Spawn world by name if exists in the worlds directory from environment variable
  85. world_path="${src_path}/Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/${PX4_SITL_WORLD}.world"
  86. else
  87. # Spawn world from environment variable with absolute path
  88. world_path="$PX4_SITL_WORLD"
  89. fi
  90. fi
  91. gzserver $verbose $world_path $ros_args &
  92. SIM_PID=$!
  93. # Check all paths in ${GAZEBO_MODEL_PATH} for specified model
  94. IFS_bak=$IFS
  95. IFS=":"
  96. for possible_model_path in ${GAZEBO_MODEL_PATH}; do
  97. if [ -z $possible_model_path ]; then
  98. continue
  99. fi
  100. # trim \r from path
  101. possible_model_path=$(echo $possible_model_path | tr -d '\r')
  102. if test -f "${possible_model_path}/${model}/${model}.sdf" ; then
  103. modelpath=$possible_model_path
  104. break
  105. fi
  106. done
  107. IFS=$IFS_bak
  108. if [ -z $modelpath ]; then
  109. echo "Model ${model} not found in model path: ${GAZEBO_MODEL_PATH}"
  110. exit 1
  111. else
  112. echo "Using: ${modelpath}/${model}/${model}.sdf"
  113. fi
  114. while gz model --verbose --spawn-file="${modelpath}/${model}/${model_name}.sdf" --model-name=${model} -x 300 -y -400 -z 0 -R 0 -P 0 -Y 1.57 2>&1 | grep -q "An instance of Gazebo is not running."; do
  115. echo "gzserver not ready yet, trying again!"
  116. sleep 1
  117. done
  118. if [[ -n "$HEADLESS" ]]; then
  119. echo "not running gazebo gui"
  120. else
  121. # gzserver needs to be running to avoid a race. Since the launch
  122. # is putting it into the background we need to avoid it by backing off
  123. sleep 3
  124. nice -n 20 gzclient --verbose $follow_mode &
  125. GUI_PID=$!
  126. fi
  127. else
  128. echo "You need to have gazebo simulator installed!"
  129. exit 1
  130. fi
  131. pushd "$rootfs" >/dev/null
  132. # Do not exit on failure now from here on because we want the complete cleanup
  133. set +e
  134. sitl_command="\"$sitl_bin\" $no_pxh \"$build_path\"/etc"
  135. echo SITL COMMAND: $sitl_command
  136. if [ "$debugger" == "lldb" ]; then
  137. eval lldb -- $sitl_command
  138. elif [ "$debugger" == "gdb" ]; then
  139. eval gdb --args $sitl_command
  140. elif [ "$debugger" == "valgrind" ]; then
  141. eval valgrind --track-origins=yes --leak-check=full -v $sitl_command
  142. elif [ "$debugger" == "callgrind" ]; then
  143. eval valgrind --tool=callgrind -v $sitl_command
  144. else
  145. eval $sitl_command
  146. fi
  147. popd >/dev/null
  148. kill -9 $SIM_PID
  149. if [[ ! -n "$HEADLESS" ]]; then
  150. kill -9 $GUI_PID
  151. fi