Files
a7-handeye-calibration/run.sh
T

71 lines
3.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
kit_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
source "$kit_dir/scripts/environment.sh"
cd -- "$kit_dir"
export TZ=Asia/Shanghai
action="${1:---help}"
if [[ $# -gt 0 ]]; then shift; fi
arm=left
if [[ $# -gt 0 ]]; then
if [[ $# != 2 || "$1" != --arm || ( "$2" != left && "$2" != right ) ]]; then
echo '用法:bash run.sh capture|record|solve|validate|status [--arm left|right]' >&2; exit 2
fi
arm="$2"
fi
case "$action" in
capture|record|solve|validate|status) ;;
*) echo '用法:bash run.sh capture|record|solve|validate|status [--arm left|right](默认left)'
echo 'capture 自动新建时间戳目录,其余命令自动使用最近一次成功采集目录。'
echo '仅采集与计算,不自动使能或运动,不自动部署。'; exit 0 ;;
esac
exec 9>"$kit_dir/.session.lock"
flock -n 9 || { echo '另一个采集/求解任务正在运行,请先结束它。' >&2; exit 2; }
if [[ "$action" == capture ]]; then
export ROS_AUTOMATIC_DISCOVERY_RANGE="${ROS_AUTOMATIC_DISCOVERY_RANGE:-LOCALHOST}"
set +u
source "$HANDEYE_ROS_SETUP"
set -u
"$HANDEYE_ROS_PYTHON" scripts/ensure_camera.py
capture_dir="$("$HANDEYE_ROS_PYTHON" scripts/capture_board.py)"
"$HANDEYE_ROS_PYTHON" - "$capture_dir" <<'PY'
import sys,json
from pathlib import Path
p=Path(sys.argv[1]);m=json.loads((p/'camera.json').read_text());k=m['K'];(p/'intrinsics.txt').write_text(f'{k[0][0]} {k[1][1]} {k[0][2]} {k[1][2]}\n')
PY
mkdir -p "$capture_dir/logs"
"$HANDEYE_ROS_PYTHON" scripts/handeye_solve.py --capture "$capture_dir" --square .027 --cols 8 --rows 5 --arm "$arm" 2>&1 | tee "$capture_dir/logs/capture_$(date +%Y%m%d_%H%M%S_%N).log"
"$HANDEYE_ROS_PYTHON" - "$capture_dir" <<'PY'
import sys,json
from pathlib import Path
sys.path.insert(0,'scripts')
from storage import save_json
p=Path(sys.argv[1]);cam=json.loads((p/'camera.json').read_text())
if cam['board_reproj_px']>.5:raise SystemExit('重投影误差>0.5px,本次数据保留,但不切换当前标定目录。')
save_json(Path('current_session.json'),{'capture_dir':str(p.resolve().relative_to(Path.cwd())),'timezone':'Asia/Shanghai'})
PY
echo "当前标定目录:$capture_dir"
echo '下一步:bash run.sh record(不需要路径)'
exit 0
fi
capture_dir="$("$HANDEYE_ROS_PYTHON" - <<'PY'
from pathlib import Path
import json
p=Path('current_session.json')
if not p.exists():raise SystemExit('还没有新标定目录,请先运行 bash run.sh capture')
c=Path(json.loads(p.read_text())['capture_dir']).resolve()
if not c.is_relative_to(Path('data').resolve()) or not (c/'camera.json').is_file():raise SystemExit('当前标定目录无效,请重新capture')
print(c)
PY
)"
echo "当前标定目录:$capture_dir"
if [[ "$action" == status ]]; then exit 0; fi
echo "标定臂:$arm(只读订阅 /$arm/arm_state)"
case "$action" in
record) script=record_touch.py ;;
solve) script=solve_base_board.py ;;
validate) script=validate_touch.py ;;
esac
mkdir -p "$capture_dir/logs"
"$HANDEYE_PYTHON" -u "scripts/$script" --capture "$capture_dir" --arm "$arm" 2>&1 | tee "$capture_dir/logs/${action}_$(date +%Y%m%d_%H%M%S_%N).log"