Skip to content

Commit 4a3ab34

Browse files
committed
gz: wait for world before spawning model, delay 1s after model spawn to allow time
1 parent 9ac03f0 commit 4a3ab34

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

ROMFS/px4fmu_common/init.d-posix/px4-rc.simulator

+29-28
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ elif [ "$PX4_SIMULATOR" = "gz" ] || [ "$(param show -q SIM_GZ_EN)" = "1" ]; then
9595
echo "INFO [init] Starting gz gui"
9696
${gz_command} ${gz_sub_command} -g > /dev/null 2>&1 &
9797
fi
98+
99+
# Wait for Gazebo world to be ready before proceeding
100+
check_scene_info() {
101+
SERVICE_INFO=$(${gz_command} service -i --service "/world/${PX4_GZ_WORLD}/scene/info" 2>&1)
102+
if echo "$SERVICE_INFO" | grep -q "Service providers"; then
103+
return 0
104+
else
105+
return 1
106+
fi
107+
}
108+
109+
ATTEMPTS=30
110+
while [ $ATTEMPTS -gt 0 ]; do
111+
if check_scene_info; then
112+
echo "INFO [init] Gazebo world is ready"
113+
break
114+
fi
115+
ATTEMPTS=$((ATTEMPTS-1))
116+
if [ $ATTEMPTS -eq 0 ]; then
117+
echo "ERROR [init] Timed out waiting for Gazebo world"
118+
exit 1
119+
fi
120+
echo "INFO [init] Waiting for Gazebo world..."
121+
sleep 1
122+
done
123+
98124
else
99125
# Gazebo is already running
100126
echo "INFO [init] gazebo already running world: ${gz_world}"
@@ -131,51 +157,26 @@ elif [ "$PX4_SIMULATOR" = "gz" ] || [ "$(param show -q SIM_GZ_EN)" = "1" ]; then
131157
--reptype gz.msgs.Boolean --timeout 5000 \
132158
--req "sdf_filename: \"${PX4_GZ_MODELS}/${MODEL_NAME}/model.sdf\", name: \"${MODEL_NAME_INSTANCE}\", allow_renaming: false${POSE_ARG}" > /dev/null 2>&1
133159

160+
# Wait for model to spawn
161+
sleep 1
162+
134163
# Start gz_bridge
135164
if ! gz_bridge start -w "${PX4_GZ_WORLD}" -n "${MODEL_NAME_INSTANCE}"; then
136165
echo "ERROR [init] gz_bridge failed to start and spawn model"
137166
exit 1
138167
fi
139168

140169
# Set physics parameters for faster-than-realtime simulation if needed
141-
check_scene_info() {
142-
SERVICE_INFO=$(${gz_command} service -i --service "/world/${PX4_GZ_WORLD}/scene/info" 2>&1)
143-
if echo "$SERVICE_INFO" | grep -q "Service providers"; then
144-
return 0
145-
else
146-
return 1
147-
fi
148-
}
149-
150-
ATTEMPTS=30
151-
while [ $ATTEMPTS -gt 0 ]; do
152-
if check_scene_info; then
153-
echo "INFO [init] Gazebo world is ready"
154-
break
155-
fi
156-
ATTEMPTS=$((ATTEMPTS-1))
157-
if [ $ATTEMPTS -eq 0 ]; then
158-
echo "ERROR [init] Timed out waiting for Gazebo world"
159-
exit 1
160-
fi
161-
echo "INFO [init] Waiting for Gazebo world..."
162-
sleep 1
163-
done
164-
165170
if [ -n "${PX4_SIM_SPEED_FACTOR}" ]; then
166171
echo "INFO [init] Setting simulation speed factor: ${PX4_SIM_SPEED_FACTOR}"
167172
${gz_command} service -s "/world/${PX4_GZ_WORLD}/set_physics" --reqtype gz.msgs.Physics \
168173
--reptype gz.msgs.Boolean --timeout 5000 \
169174
--req "real_time_factor: ${PX4_SIM_SPEED_FACTOR}" > /dev/null 2>&1
170175
fi
171176

172-
173177
# Set up camera to follow the model if requested
174178
if [ -n "${PX4_GZ_FOLLOW}" ]; then
175179

176-
# Wait for model to spawn
177-
sleep 1
178-
179180
echo "INFO [init] Setting camera to follow ${MODEL_NAME_INSTANCE}"
180181

181182
# Set camera to follow the model

0 commit comments

Comments
 (0)