forked from AgibotTech/genie_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_collection_entrypoint.sh
More file actions
executable file
·384 lines (344 loc) · 14.3 KB
/
Copy pathdata_collection_entrypoint.sh
File metadata and controls
executable file
·384 lines (344 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
# Data Collection Entrypoint Script
# This script starts both data_collector_server.py and run_data_collection.py
# Usage: ./data_collection_entrypoint.sh [--headless] [--no-record] [--task TASK_PATH]
set -eo pipefail
# Default values
HEADLESS=false
RECORD=true
TASK="tasks/geniesim_2025/sort_fruit/g2/sort_the_fruit_into_the_box_apple_g2.json"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--headless)
HEADLESS=true
shift
;;
--no-record)
RECORD=false
shift
;;
--task)
TASK="$2"
shift 2
;;
--help|-h)
echo "Usage: ./data_collection_entrypoint.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --headless Run in headless mode (default: false)"
echo " --no-record Disable recording (default: record enabled)"
echo " --task TASK_PATH Task template path"
echo " --help, -h Show this help message"
exit 0
;;
*)
echo "Error: Unknown option '$1'"
echo "Use --help for usage information"
exit 1
;;
esac
done
# First, execute entry_point.sh setup (but skip the exec at the end)
echo "=========================================="
echo "Executing entry_point.sh setup..."
echo "=========================================="
ENTRY_POINT_SCRIPT="/geniesim/main/data_collection/scripts/entry_point.sh"
if [ -f "$ENTRY_POINT_SCRIPT" ]; then
# Source entry_point.sh but skip the exec at the end
# We'll execute it in a subshell and capture the environment setup
(
# Source the entry_point.sh script
# We need to prevent the exec from running, so we'll source it and stop before exec
set +e # Don't exit on error in this subshell
source "$ENTRY_POINT_SCRIPT" /bin/true 2>/dev/null || {
# If entry_point.sh tries to exec, it will fail with /bin/true
# But the environment setup should have been done
true
}
)
# Execute entry_point.sh logic manually (since exec would replace the process)
export ROS_DISTRO=jazzy
export ISAACSIM_HOME=/isaac-sim
export CUROBO_PATH=/tmp/curobo
export SIM_REPO_ROOT=/geniesim/main/data_collection
# user 1234 access
sudo setfacl -m u:1234:rwX /isaac-sim/.cache 2>/dev/null || true
sudo setfacl -m u:1234:rwX /isaac-sim/.nv/ComputeCache 2>/dev/null || true
sudo setfacl -m u:1234:rwX /isaac-sim/.nvidia-omniverse/logs 2>/dev/null || true
sudo setfacl -m u:1234:rwX /isaac-sim/.nvidia-omniverse/config 2>/dev/null || true
sudo setfacl -m u:1234:rwX /isaac-sim/.local/share/ov/data 2>/dev/null || true
sudo setfacl -m u:1234:rwX /isaac-sim/.local/share/ov/pkg 2>/dev/null || true
# bashrc configuration (entry_point.sh should have done this, but ensure it's done)
if ! grep -q "export SIM_REPO_ROOT=/geniesim/main/data_collection" ~/.bashrc 2>/dev/null; then
echo "export SIM_REPO_ROOT=/geniesim/main/data_collection" >>~/.bashrc
fi
if ! grep -q "export SIM_ASSETS=" ~/.bashrc 2>/dev/null; then
echo "export SIM_ASSETS=/geniesim_assets" >>~/.bashrc
fi
if ! grep -q "export ENABLE_SIM=" ~/.bashrc 2>/dev/null; then
echo "export ENABLE_SIM=1" >>~/.bashrc
fi
if ! grep -q "export ROS_DISTRO=" ~/.bashrc 2>/dev/null; then
echo "export ROS_DISTRO=${ROS_DISTRO}" >>~/.bashrc
fi
if ! grep -q "export ROS_VERSION=" ~/.bashrc 2>/dev/null; then
echo "export ROS_VERSION=2" >>~/.bashrc
fi
if ! grep -q "export ROS_PYTHON_VERSION=" ~/.bashrc 2>/dev/null; then
echo "export ROS_PYTHON_VERSION=3" >>~/.bashrc
fi
if ! grep -q "export ROS_LOCALHOST_ONLY=" ~/.bashrc 2>/dev/null; then
echo "export ROS_LOCALHOST_ONLY=1" >>~/.bashrc
fi
if ! grep -q "export RMW_IMPLEMENTATION=" ~/.bashrc 2>/dev/null; then
echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp" >>~/.bashrc
fi
if ! grep -q "LD_LIBRARY_PATH.*isaacsim.ros2.bridge" ~/.bashrc 2>/dev/null; then
echo "export LD_LIBRARY_PATH=\"\${LD_LIBRARY_PATH:+\$LD_LIBRARY_PATH:}${ISAACSIM_HOME}/exts/isaacsim.ros2.bridge/${ROS_DISTRO}/lib\"" >>~/.bashrc
fi
if ! grep -q "export ROS_CMD_DISTRO=" ~/.bashrc 2>/dev/null; then
echo "export ROS_CMD_DISTRO=jazzy" >>~/.bashrc
fi
if ! grep -q "source /isaac-sim/setup_ros_env.sh" ~/.bashrc 2>/dev/null; then
echo "source ${ISAACSIM_HOME}/setup_ros_env.sh" >>~/.bashrc
fi
if ! grep -q "alias omni_python=" ~/.bashrc 2>/dev/null; then
echo "alias omni_python='${ISAACSIM_HOME}/python.sh'" >>~/.bashrc
fi
if ! grep -q "alias isaacsim=" ~/.bashrc 2>/dev/null; then
echo "alias isaacsim='${ISAACSIM_HOME}/runapp.sh'" >>~/.bashrc
fi
if ! grep -q "alias geniesim=" ~/.bashrc 2>/dev/null; then
echo "alias geniesim='omni_python /geniesim/main/source/geniesim/app/app.py'" >>~/.bashrc
fi
if ! grep -q "alias source_ros_py311=" ~/.bashrc 2>/dev/null; then
echo "alias source_ros_py311='unset LD_LIBRARY_PATH && source /opt/ros/jazzy/setup.bash'" >>~/.bashrc
fi
# Copy curobo assets
sudo cp -r ${SIM_ASSETS}/robot/curobo_robot/assets/robot $ISAACSIM_HOME/kit/python/lib/python3.11/site-packages/curobo/content/assets/ 2>/dev/null || true
sudo cp -r ${SIM_REPO_ROOT}/config/curobo/configs $ISAACSIM_HOME/kit/python/lib/python3.11/site-packages/curobo/content/ 2>/dev/null || true
# NOTE: this is a workaround to fix the issue that the packaging is not installed in the core archive
/isaac-sim/python.sh -m pip install packaging -t /isaac-sim/exts/omni.isaac.core_archive/pip_prebundle/
# Editable-install geniesim_assets (bind-mounted at /geniesim_assets) so
# `import geniesim_assets` / system_utils.assets_path() resolve.
if [ -f /geniesim_assets/pyproject.toml ]; then
/isaac-sim/python.sh -m pip install -q -e /geniesim_assets 2>/dev/null || true
python3 -m pip install -q -e /geniesim_assets --break-system-packages 2>/dev/null || true
fi
echo "✓ entry_point.sh setup completed"
else
echo "Warning: entry_point.sh not found at $ENTRY_POINT_SCRIPT"
echo "Setting up basic environment..."
export ROS_DISTRO=jazzy
export ISAACSIM_HOME=/isaac-sim
export CUROBO_PATH=/tmp/curobo
export SIM_REPO_ROOT=/geniesim/main/data_collection
fi
echo ""
echo "Setting up environment variables directly..."
# Set all required environment variables directly (no need to source bashrc)
export ROS_DISTRO=jazzy
export ISAACSIM_HOME=/isaac-sim
export CUROBO_PATH=/tmp/curobo
export SIM_REPO_ROOT=/geniesim/main/data_collection
export SIM_ASSETS=/geniesim_assets
export ENABLE_SIM=1
export ROS_VERSION=2
export ROS_PYTHON_VERSION=3
export ROS_LOCALHOST_ONLY=1
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export ROS_CMD_DISTRO=jazzy
# Set up LD_LIBRARY_PATH
ROS_BRIDGE_LIB="${ISAACSIM_HOME}/exts/isaacsim.ros2.bridge/${ROS_DISTRO}/lib"
if [ -z "$LD_LIBRARY_PATH" ]; then
export LD_LIBRARY_PATH="${ROS_BRIDGE_LIB}"
else
# Only add if not already present
if [[ ":$LD_LIBRARY_PATH:" != *":${ROS_BRIDGE_LIB}:"* ]]; then
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${ROS_BRIDGE_LIB}"
fi
fi
# Source ROS environment if available
if [ -f "${ISAACSIM_HOME}/setup_ros_env.sh" ]; then
source "${ISAACSIM_HOME}/setup_ros_env.sh" 2>/dev/null || true
fi
# Put IsaacSim's bundled rclpy (built for its own Python) on the path so the
# server imports it instead of the system ROS one.
ROS_BRIDGE_RCLPY="${ISAACSIM_HOME}/exts/isaacsim.ros2.bridge/${ROS_DISTRO}/rclpy"
if [ -d "${ROS_BRIDGE_RCLPY}" ]; then
export PYTHONPATH="${ROS_BRIDGE_RCLPY}:${PYTHONPATH:-}"
export LD_LIBRARY_PATH="${ROS_BRIDGE_LIB}:${LD_LIBRARY_PATH}"
fi
# Verify critical environment variables
echo ""
echo "Environment variables:"
echo " SIM_ASSETS=$SIM_ASSETS"
echo " SIM_REPO_ROOT=$SIM_REPO_ROOT"
echo " ISAACSIM_HOME=$ISAACSIM_HOME"
echo " ROS_DISTRO=$ROS_DISTRO"
echo " LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
echo "✓ Environment variables set directly"
echo ""
# Set up paths
ISAACSIM_PYTHON="${ISAACSIM_HOME}/python.sh"
WORK_DIR="/geniesim/main/data_collection"
# Use LOG_DIR from environment if set, otherwise use default
if [ -n "$LOG_DIR" ]; then
# LOG_DIR is set from host (via docker run -e)
# It should be something like /geniesim/main/data_collection/logs/task_name
mkdir -p "$LOG_DIR"
# Ensure directory is writable by user 1234 (container user)
# Try sudo first, then fallback to regular chmod
sudo chown -R 1234:1234 "$LOG_DIR" 2>/dev/null || true
sudo chmod -R 777 "$LOG_DIR" 2>/dev/null || chmod -R 777 "$LOG_DIR" 2>/dev/null || true
# Verify we can write to the directory
touch "${LOG_DIR}/.test_write" 2>/dev/null && rm -f "${LOG_DIR}/.test_write" || {
echo "Warning: Cannot write to LOG_DIR: $LOG_DIR"
echo "Attempting to fix permissions..."
sudo chmod -R 777 "$LOG_DIR" 2>/dev/null || true
}
else
# Default log directory
LOG_DIR="${WORK_DIR}/logs"
mkdir -p "$LOG_DIR"
sudo chown -R 1234:1234 "$LOG_DIR" 2>/dev/null || true
sudo chmod -R 777 "$LOG_DIR" 2>/dev/null || chmod -R 777 "$LOG_DIR" 2>/dev/null || true
fi
# Change to work directory
cd "${WORK_DIR}"
echo "=========================================="
echo "Data Collection Entrypoint"
echo "=========================================="
echo "Configuration:"
echo " Headless mode: $HEADLESS"
echo " Recording: $RECORD"
echo " Task template: $TASK"
echo " Working directory: $WORK_DIR"
echo " Log directory: $LOG_DIR"
echo "=========================================="
echo ""
# Build command arguments
DATA_COLLECTOR_ARGS="--enable_physics --enable_curobo"
MAIN_ARGS=""
if [ "$HEADLESS" = true ]; then
DATA_COLLECTOR_ARGS="$DATA_COLLECTOR_ARGS --headless"
fi
if [ "$RECORD" = true ]; then
DATA_COLLECTOR_ARGS="$DATA_COLLECTOR_ARGS --publish_ros"
MAIN_ARGS="$MAIN_ARGS --use_recording"
fi
if [ -n "$TASK" ]; then
MAIN_ARGS="$MAIN_ARGS --task_template $TASK"
fi
# Function to cleanup on exit
cleanup() {
echo ""
echo "=========================================="
echo "Cleaning up..."
echo "=========================================="
# Kill processes by name
echo "Stopping data_collector_server..."
pkill -f "data_collector_server.py" 2>/dev/null || true
echo "Stopping run_data_collection.py..."
pkill -f "run_data_collection.py" 2>/dev/null || true
sleep 2
echo "Cleanup complete"
}
# Set trap for cleanup
trap cleanup EXIT INT TERM
# Start data_collector_server in background
echo "Starting data_collector_server..."
echo "Command: ${ISAACSIM_PYTHON} scripts/data_collector_server.py $DATA_COLLECTOR_ARGS"
echo "Logs: ${LOG_DIR}/data_collector_server.log"
echo "Environment: SIM_ASSETS=$SIM_ASSETS, SIM_REPO_ROOT=$SIM_REPO_ROOT"
# All environment variables are already exported above, Python will inherit them
${ISAACSIM_PYTHON} scripts/data_collector_server.py $DATA_COLLECTOR_ARGS > "${LOG_DIR}/data_collector_server.log" 2>&1 &
DATA_COLLECTOR_PID=$!
echo "✓ data_collector_server started (PID: $DATA_COLLECTOR_PID)"
# Wait for the server to initialize
if [ "$HEADLESS" = true ]; then
echo "Waiting for data_collector_server to initialize (headless mode, may take longer)..."
sleep 15
else
echo "Waiting for data_collector_server to initialize..."
sleep 10
fi
# Check if data_collector_server is still running
if ! kill -0 $DATA_COLLECTOR_PID 2>/dev/null; then
echo "Error: data_collector_server process died"
echo "Last 30 lines of log:"
tail -30 "${LOG_DIR}/data_collector_server.log" || true
exit 1
fi
echo "✓ data_collector_server is running"
# Start run_data_collection.py in background
echo ""
echo "Starting run_data_collection.py..."
echo "Command: ${ISAACSIM_PYTHON} scripts/run_data_collection.py $MAIN_ARGS"
echo "Logs: ${LOG_DIR}/run_data_collection.log"
echo "Environment: SIM_ASSETS=$SIM_ASSETS, SIM_REPO_ROOT=$SIM_REPO_ROOT"
# All environment variables are already exported above, Python will inherit them
${ISAACSIM_PYTHON} scripts/run_data_collection.py $MAIN_ARGS > "${LOG_DIR}/run_data_collection.log" 2>&1 &
MAIN_PID=$!
echo "✓ run_data_collection.py started (PID: $MAIN_PID)"
# Wait a bit and verify run_data_collection.py is running
sleep 5
if ! kill -0 $MAIN_PID 2>/dev/null; then
echo "Warning: run_data_collection.py process died"
echo "Last 30 lines of log:"
tail -30 "${LOG_DIR}/run_data_collection.log" || true
# Don't exit, let it try to continue
else
echo "✓ run_data_collection.py is running"
fi
echo ""
echo "=========================================="
echo "Both services are running!"
echo "=========================================="
echo ""
echo "Process IDs:"
echo " data_collector_server: $DATA_COLLECTOR_PID"
echo " run_data_collection.py: $MAIN_PID"
echo ""
echo "To view logs:"
echo " tail -f ${LOG_DIR}/data_collector_server.log"
echo " tail -f ${LOG_DIR}/run_data_collection.log"
echo ""
echo "Press Ctrl+C to stop all services"
echo ""
# Monitor processes
MONITOR_COUNT=0
while true; do
sleep 10
MONITOR_COUNT=$((MONITOR_COUNT + 1))
# Check if processes are still running
DATA_COLLECTOR_RUNNING=true
MAIN_RUNNING=true
if ! kill -0 $DATA_COLLECTOR_PID 2>/dev/null; then
DATA_COLLECTOR_RUNNING=false
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Error: data_collector_server.py process died"
echo "Last 30 lines of log:"
tail -30 "${LOG_DIR}/data_collector_server.log" || true
exit 1
fi
if ! kill -0 $MAIN_PID 2>/dev/null; then
MAIN_RUNNING=false
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Warning: run_data_collection.py process died"
echo "Last 30 lines of log:"
tail -30 "${LOG_DIR}/run_data_collection.log" || true
# Check if it's a normal exit or error
if grep -q "job done" "${LOG_DIR}/run_data_collection.log" 2>/dev/null; then
echo "Main process completed successfully"
exit 0
else
exit 1
fi
fi
# Print status every 6 checks (1 minute)
if [ $((MONITOR_COUNT % 6)) -eq 0 ]; then
if [ "$DATA_COLLECTOR_RUNNING" = true ] && [ "$MAIN_RUNNING" = true ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Status: Both services running normally"
fi
fi
done