-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcar_control.sh
executable file
·114 lines (102 loc) · 3.46 KB
/
car_control.sh
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
#!/bin/bash
# Port mapping check
PORT_MAPPING=""
if [ "$1" = "--port" ] && [ -n "$2" ] && [ -n "$3" ]; then
PORT_MAPPING="-p $2:$3"
shift 3 # Remove the first three arguments
fi
# 檢查系統架構與作業系統
ARCH=$(uname -m)
OS=$(uname -s)
# 初始化 GPU 相關變數
GPU_FLAGS=""
USE_GPU=false
# 檢查是否為 Linux 並且支援 NVIDIA GPU
if [ "$OS" = "Linux" ]; then
if [ -f "/etc/nv_tegra_release" ]; then
GPU_FLAGS="--runtime=nvidia"
USE_GPU=true
elif docker info --format '{{json .}}' | grep -q '"Runtimes".*nvidia'; then
GPU_FLAGS="--gpus all"
USE_GPU=true
fi
fi
# 測試 GPU 是否可用
if [ "$USE_GPU" = true ]; then
echo "Testing Docker run with GPU..."
docker run --rm $GPU_FLAGS ghcr.io/screamlab/pros_car_docker_image:latest /bin/bash -c "echo GPU test" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "GPU not supported or failed, disabling GPU flags."
GPU_FLAGS=""
USE_GPU=false
fi
fi
echo "Detected OS: $OS, Architecture: $ARCH"
echo "GPU Flags: $GPU_FLAGS"
# 設定適當的 Docker 參數
device_options=""
# 檢查設備並加入 --device 參數
if [ -e /dev/usb_front_wheel ]; then
device_options+=" --device=/dev/usb_front_wheel"
fi
if [ -e /dev/usb_rear_wheel ]; then
device_options+=" --device=/dev/usb_rear_wheel"
fi
if [ -e /dev/usb_robot_arm ]; then
device_options+=" --device=/dev/usb_robot_arm"
fi
# 根據不同架構選擇適當的 Docker 圖像
if [ "$ARCH" = "aarch64" ]; then
echo "Detected architecture: arm64"
docker run -it --rm \
--network compose_my_bridge_network \
$PORT_MAPPING \
$device_options \
--runtime=nvidia \
--env-file .env \
-v "$(pwd)/src:/workspaces/src" \
ghcr.io/screamlab/pros_car_docker_image:latest \
/bin/bash
elif [ "$ARCH" = "x86_64" ] || ([ "$ARCH" = "arm64" ] && [ "$OS" = "Darwin" ]); then
echo "Detected architecture: amd64 or macOS arm64"
if [ "$OS" = "Darwin" ]; then
echo "Running Docker on macOS (without GPU support)..."
docker run -it --rm \
--network compose_my_bridge_network \
$PORT_MAPPING \
$device_options \
--env-file .env \
-v "$(pwd)/src:/workspaces/src" \
-v "$(pwd)/screenshots:/workspaces/screenshots" \
ghcr.io/screamlab/pros_car_docker_image:latest \
/bin/bash
else
echo "Trying to run with GPU support..."
docker run -it --rm \
--network compose_my_bridge_network \
$PORT_MAPPING \
$GPU_FLAGS \
$device_options \
--env-file .env \
-v "$(pwd)/src:/workspaces/src" \
-v "$(pwd)/screenshots:/workspaces/screenshots" \
ghcr.io/screamlab/pros_car_docker_image:latest \
/bin/bash
# 如果 GPU 啟動失敗,回退到 CPU 模式
if [ $? -ne 0 ]; then
echo "GPU not supported or failed, falling back to CPU mode..."
docker run -it --rm \
--network compose_my_bridge_network \
$PORT_MAPPING \
--env-file .env \
$device_options \
-v "$(pwd)/src:/workspaces/src" \
-v "$(pwd)/screenshots:/workspaces/screenshots" \
ghcr.io/screamlab/pros_car_docker_image:latest \
/bin/bash
fi
fi
else
echo "Unsupported architecture: $ARCH"
exit 1
fi