PingPongRealWorldRL / scripts /h2_live_sim_orchestrator.sh
KurisiTOMA's picture
chore: publish H2 real-world RL system
d8290eb verified
Raw History Blame Contribute Delete
12.1 kB
#!/usr/bin/env bash
set -euo pipefail
SESSION=${1:?tmux session is required}
RUN_DIR=${2:?run directory is required}
H2_ROOT=${H2_ROOT:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)}
H2_PY=${H2_PY:-python3}
H2_SIM_DOMAIN=${H2_SIM_DOMAIN:-42}
H2_SIM_VIEWER=${H2_SIM_VIEWER:-0}
H2_SIM_DISPLAY=${H2_SIM_DISPLAY:-}
H2_SIM_XAUTHORITY=${H2_SIM_XAUTHORITY:-}
H2_MOCAP_DOMAIN=${H2_MOCAP_DOMAIN:-0}
H2_MOCAP_LOCALHOST_ONLY=${H2_MOCAP_LOCALHOST_ONLY:-0}
ADAPTER_ROOT=${H2_ADAPTER_ROOT:-$H2_ROOT/../mocap_ego_adapter}
source "$(dirname "${BASH_SOURCE[0]}")/h2_upstream_versions.sh"
h2_upstream_read_selection "$RUN_DIR"
PANE_SCRIPT="$H2_ROOT/scripts/h2_live_sim_tmux_pane.sh"
exec > >("$H2_PY" "$H2_ROOT/scripts/bounded_tee.py" \
--path "$RUN_DIR/control.log" --max-mib 8 --backups 3) 2>&1
pane_alive() {
[[ $(tmux display-message -p -t "$SESSION:0.$1" '#{pane_dead}' 2>/dev/null) == 0 ]]
}
cleanup_started_panes() {
set +e
local shm_name=""
local cleanup_failed=0
local graceful_requested=0
shm_name=$(cat "$RUN_DIR/sim_shm_name" 2>/dev/null)
if [[ -n $shm_name && -e "/dev/shm/$shm_name" ]]; then
"$H2_PY" "$H2_ROOT/scripts/h2_live_sim_control.py" r1 --run-dir "$RUN_DIR" >/dev/null 2>&1
sleep 0.2
if "$H2_PY" "$H2_ROOT/scripts/h2_live_sim_control.py" quit --run-dir "$RUN_DIR" >/dev/null 2>&1; then
graceful_requested=1
fi
fi
if [[ $graceful_requested -eq 1 ]]; then
for _ in $(seq 1 450); do
if ! pane_alive 3 \
&& [[ -e "$RUN_DIR/final_report.json" ]] \
&& [[ ! -e "/dev/shm/$shm_name" \
&& ! -e "/dev/shm/$shm_name.python_writer.lock" ]]; then
break
fi
sleep 0.1
done
fi
if pane_alive 3 && [[ $graceful_requested -eq 0 \
|| ! -e "$RUN_DIR/final_report.json" \
|| -e "/dev/shm/$shm_name" \
|| -e "/dev/shm/$shm_name.python_writer.lock" ]]; then
tmux send-keys -t "$SESSION:0.3" C-c 2>/dev/null
fi
for _ in $(seq 1 450); do
if ! pane_alive 3 \
&& [[ -z $shm_name || ( ! -e "/dev/shm/$shm_name" \
&& ! -e "/dev/shm/$shm_name.python_writer.lock" ) ]] \
&& [[ -z $shm_name || -e "$RUN_DIR/final_report.json" ]]; then
break
fi
sleep 0.1
done
for pane in 1 2 4 5; do
pane_alive "$pane" && tmux send-keys -t "$SESSION:0.$pane" C-c 2>/dev/null
done
for _ in $(seq 1 100); do
all_dead=1
for pane in 1 2 3 4 5; do
pane_alive "$pane" && all_dead=0
done
if [[ $all_dead -eq 1 ]] && ! h2_adapter_listeners | grep -q .; then
break
fi
sleep 0.1
done
for pane in 1 2 3 4 5; do
pane_alive "$pane" && tmux send-keys -t "$SESSION:0.$pane" C-c 2>/dev/null
done
for _ in $(seq 1 50); do
all_dead=1
for pane in 1 2 3 4 5; do
pane_alive "$pane" && all_dead=0
done
[[ $all_dead -eq 1 ]] && break
sleep 0.1
done
if pane_alive 1 || pane_alive 2 || pane_alive 3 || pane_alive 4 || pane_alive 5 \
|| h2_adapter_listeners | grep -q .; then
echo "[FAIL CLEANUP] a launcher-owned pane or $H2_ZMQ_ENDPOINT remains; inspect this tmux session" >&2
cleanup_failed=1
fi
if [[ -n $shm_name ]]; then
if [[ -e "/dev/shm/$shm_name" || -e "/dev/shm/$shm_name.python_writer.lock" ]]; then
echo "[FAIL CLEANUP] isolated simulation SHM or writer lock remains" >&2
cleanup_failed=1
fi
if [[ ! -e "$RUN_DIR/final_report.json" ]]; then
echo "[FAIL CLEANUP] live-sim started but no atomic final_report.json was written" >&2
cleanup_failed=1
elif ! "$H2_PY" - "$RUN_DIR/final_report.json" <<'PY'
import json, sys
report = json.load(open(sys.argv[1], encoding="utf-8"))
isolation = report.get("isolation", {})
required = (
report.get("clean_shutdown") is True,
isolation.get("simulation_shm_removed") is True,
isolation.get("writer_lock_removed") is True,
isolation.get("production_control_absent") is True,
)
raise SystemExit(0 if all(required) else 1)
PY
then
echo "[FAIL CLEANUP] final report does not prove clean isolated shutdown" >&2
cleanup_failed=1
fi
fi
if [[ -e /dev/shm/h2_pingpong_bridge || -e /dev/shm/g1_pingpong_bridge ]]; then
echo "[FAIL CLEANUP] a production robot SHM appeared during live-sim cleanup" >&2
cleanup_failed=1
fi
if [[ $cleanup_failed -ne 0 ]]; then
: >"$RUN_DIR/cleanup.failed"
else
rm -f "$RUN_DIR/cleanup.failed"
echo "[PASS CLEANUP] launcher panes, port, isolated SHM and report are clean"
fi
set -e
}
fail() {
local reason="$*"
echo "[STOP] $reason" >&2
cleanup_started_panes
tmux display-message -d 15000 "H2 LIVE SIM STOPPED: $reason" 2>/dev/null || true
exec bash
}
start_pane() {
local index=$1 role=$2
tmux respawn-pane -k -t "$SESSION:0.$index" \
"H2_ROOT='$H2_ROOT' H2_PY='$H2_PY' H2_SIM_DOMAIN='$H2_SIM_DOMAIN' H2_SIM_VIEWER='$H2_SIM_VIEWER' H2_SIM_DISPLAY='$H2_SIM_DISPLAY' H2_SIM_XAUTHORITY='$H2_SIM_XAUTHORITY' H2_MOCAP_DOMAIN='$H2_MOCAP_DOMAIN' H2_MOCAP_LOCALHOST_ONLY='$H2_MOCAP_LOCALHOST_ONLY' H2_ADAPTER_ROOT='$ADAPTER_ROOT' bash '$PANE_SCRIPT' '$role' '$RUN_DIR'"
}
source_sim_ros() {
set +u
# shellcheck disable=SC1091
source /opt/ros/humble/setup.bash || return 1
# shellcheck disable=SC1091
source "$H2_ROOT/install/setup.bash" || return 1
set -u
unset CYCLONEDDS_URI
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export ROS_DOMAIN_ID="$H2_SIM_DOMAIN"
export ROS_LOCALHOST_ONLY=1
export ROS2CLI_NO_DAEMON=1
[[ $RMW_IMPLEMENTATION == rmw_cyclonedds_cpp \
&& $ROS_DOMAIN_ID == 42 \
&& $ROS_LOCALHOST_ONLY == 1 \
&& -z ${CYCLONEDDS_URI:-} ]] || return 1
}
on_unexpected_error() {
local rc=$1 line=$2
trap - ERR
fail "unexpected orchestrator error at line $line (rc=$rc)"
}
check_zero_lowcmd_topics() {
local label=$1 topic info
for topic in /lowcmd /rt/lowcmd; do
info="$RUN_DIR/$(echo "$label$topic" | tr / _)_info.txt"
if ros2 topic info "$topic" --verbose --no-daemon --spin-time 2 >"$info" 2>&1; then
cat "$info"
grep -Eq '^Publisher count: +0$' "$info" || return 1
else
cat "$info"
grep -Fq "Unknown topic '$topic'" "$info" || return 1
fi
done
}
check_no_production_lowcmd() {
echo "[GATE 0] no FastDDS domain-0 LowCmd publisher"
(
set +u
# shellcheck disable=SC1091
source /opt/ros/humble/setup.bash || return 1
# shellcheck disable=SC1091
source "$H2_ROOT/install/setup.bash" || return 1
set -u
unset CYCLONEDDS_URI
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_DOMAIN_ID=0 ROS_LOCALHOST_ONLY=0 ROS2CLI_NO_DAEMON=1
[[ $RMW_IMPLEMENTATION == rmw_fastrtps_cpp && $ROS_DOMAIN_ID == 0 && $ROS_LOCALHOST_ONLY == 0 ]] \
|| return 1
check_zero_lowcmd_topics fastdds || return 1
) || return 1
}
trap 'on_unexpected_error $? $LINENO' ERR
echo "=== H2 LIVE MUJOCO / REAL MOCAP+PREDICTOR ==="
date
echo "session=$SESSION run_dir=$RUN_DIR planner_domain=$H2_SIM_DOMAIN mocap_domain=$H2_MOCAP_DOMAIN mocap_localhost_only=$H2_MOCAP_LOCALHOST_ONLY"
echo "There is no duration limit and no automatic L1. No Unitree bridge is started."
[[ $H2_SIM_DOMAIN == 42 ]] || fail "planner ROS domain must remain exactly 42"
[[ -f $PANE_SCRIPT ]] || fail "live-sim pane helper is missing"
[[ ! -e /dev/shm/h2_pingpong_bridge ]] || fail "production H2BR exists"
if pgrep -af '[h]2_lowcmd_shm_bridge|[r]un_real_shm.py|[g]1_lowcmd_shm_bridge|[g]1_shm_bridge.py|[r]eal_shm_runner.py|[p]lay_tt_hardware_g1_fb.py' >"$RUN_DIR/production_pids.txt"; then
cat "$RUN_DIR/production_pids.txt"
fail "production bridge/runner is active"
fi
if h2_adapter_listeners | grep -q .; then
h2_adapter_listeners || true
fail "$H2_ZMQ_ENDPOINT is occupied; inspect this H2 endpoint before launch"
fi
check_no_production_lowcmd || fail "a production LowCmd graph is active or unreadable"
echo "[GATE 1] immutable artifacts and selected code baseline"
cd "$H2_ROOT"
sha256sum -c ARTIFACTS.sha256 || fail "H2 artifact hash mismatch"
ADAPTER_IDENTITY_ROOT="$ADAPTER_ROOT"
ADAPTER_IDENTITY_NAME=mocap_ego_zmq_adapter_physics.py
ADAPTER_IDENTITY_SHA=b5cc1cd5f119ca7b3ef65e26620aae64d907425859479003063952a61d9cf325
PREDICTOR_IDENTITY_PATH="$H2_ROOT/predictor.py"
PREDICTOR_IDENTITY_SHA=d48468335155975a673f90c7fc68edb527d8567329c8fe64a55abd3ea3ca69bf
if [[ $H2_VELOCITY_VERSION != original ]]; then
ADAPTER_IDENTITY_ROOT="$H2_UPSTREAM_V1_ROOT/legacy/adapter"
fi
if [[ $H2_PREDICTOR_VERSION != original ]]; then
PREDICTOR_IDENTITY_PATH="$H2_UPSTREAM_V1_ROOT/legacy/predictor.py"
PREDICTOR_IDENTITY_SHA=1cb73f9970ecaa9add35d987c180d479d1b2dd8f7503d16d1f510d9ad3937b00
fi
if [[ $H2_VELOCITY_VERSION == physics-ls-v2 ]]; then
ADAPTER_IDENTITY_ROOT="$H2_UPSTREAM_V2_ROOT/references/adapter"
ADAPTER_IDENTITY_NAME=mocap_ego_zmq_adapter_physics_0822.py
ADAPTER_IDENTITY_SHA=ac452ef6bf9b492fe1bc3988158e13a3f67bd1f570d6fcb9f29a77065dfdf103
fi
if [[ $H2_PREDICTOR_VERSION == rk4-v2 ]]; then
PREDICTOR_IDENTITY_PATH="$H2_UPSTREAM_V2_ROOT/references/predict_node_0824.py"
PREDICTOR_IDENTITY_SHA=6bffb63625fb421dccf28cf85bdca4a287296710953546fc7820b2958b830049
fi
printf '%s %s\n' \
"$ADAPTER_IDENTITY_SHA" \
"$ADAPTER_IDENTITY_ROOT/scripts/$ADAPTER_IDENTITY_NAME" \
8a1903adf69f2afa690f94d21fffaeec82a03768161febf4680a5f4603599fea \
"$ADAPTER_IDENTITY_ROOT/mocap_ego_adapter/core.py" \
"$PREDICTOR_IDENTITY_SHA" \
"$PREDICTOR_IDENTITY_PATH" | sha256sum -c - \
|| fail "adapter/core/predictor identity mismatch"
source_sim_ros || fail "isolated ROS environment"
for topic in /lowcmd /rt/lowcmd; do
info="$RUN_DIR/$(echo "$topic" | tr / _)_before.txt"
if ros2 topic info "$topic" --verbose --no-daemon --spin-time 2 >"$info" 2>&1; then
cat "$info"
grep -Eq '^Publisher count: +0$' "$info" || fail "$topic has a publisher"
else
cat "$info"
grep -Fq "Unknown topic '$topic'" "$info" || fail "unexpected $topic graph error"
fi
done
echo "[GATE 2] start selected adapter transport"
start_pane 1 adapter || fail "could not start adapter pane"
for _ in $(seq 1 150); do
h2_adapter_listeners | grep -q . && break
sleep 0.1
done
h2_adapter_listeners | grep -q . || fail "adapter did not bind $H2_ZMQ_ENDPOINT"
echo "[SIM] all upstream versions: no P1/P2 or initial ball sample required."
echo "[GATE 3] start selected predictor and validate planner-v3"
h2_upstream_describe
start_pane 2 predictor || fail "could not start predictor pane"
echo "[SIM] MuJoCo ingress validates predictor idle/ball packets and ROS graph."
echo "[GATE 4] start isolated MuJoCo owner + production RealShmRunner"
: >"$RUN_DIR/live_entry_started"
start_pane 3 live_sim || fail "could not start live MuJoCo pane"
for _ in $(seq 1 900); do
[[ -e "$RUN_DIR/frozen.ready" ]] && break
grep -Fq '[LIVE SIM STOP]' "$RUN_DIR/live_sim.log" 2>/dev/null && fail "live sim stopped"
sleep 0.1
done
[[ -e "$RUN_DIR/frozen.ready" ]] || fail "FROZEN_WAIT was not reached within 90 seconds"
start_pane 4 monitor || fail "could not start monitor pane"
start_pane 5 telemetry || fail "could not start telemetry pane"
"$H2_PY" scripts/h2_live_sim_control.py check-frozen --run-dir "$RUN_DIR" \
|| fail "FROZEN_WAIT freshness gate"
echo
echo "[PASS] real ZMQ + selected predictor + production runner + MuJoCo is in FROZEN_WAIT."
echo "Runtime is unbounded. Type: h2sim status | h2sim l1 | h2sim r1 | h2sim quit"
echo "Interactive console: L=L1, R=R1, Q=quit after R1, Esc=close console only."
echo "Always use h2sim r1 before h2sim quit. Ctrl-b d detaches without stopping."
tmux display-message -d 20000 "H2 LIVE SIM FROZEN_WAIT - use h2sim l1/r1/status/quit"
export H2_LIVE_SIM_RUN_DIR="$RUN_DIR"
h2sim() {
"$H2_PY" "$H2_ROOT/scripts/h2_live_sim_control.py" "$1" \
--run-dir "$H2_LIVE_SIM_RUN_DIR"
}
export -f h2sim
h2sim-ui() {
"$H2_PY" "$H2_ROOT/scripts/h2_live_sim_console.py" \
--run-dir "$H2_LIVE_SIM_RUN_DIR" </dev/tty >/dev/tty 2>/dev/tty
}
export -f h2sim-ui
h2sim-ui || true
echo "Interactive console closed; run h2sim-ui to reopen it."
exec bash