Repository navigation
Expand file tree
/
Copy pathsitlctl
More file actions
executable file
·577 lines (515 loc) · 15.1 KB
/
Copy pathsitlctl
File metadata and controls
executable file
·577 lines (515 loc) · 15.1 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/env bash
# ArduPilot SITL operator control for two independent Docker instances.
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log_info() {
printf "%b[INFO]%b %s\n" "$GREEN" "$NC" "$1"
}
log_warn() {
printf "%b[WARN]%b %s\n" "$YELLOW" "$NC" "$1"
}
log_error() {
printf "%b[ERROR]%b %s\n" "$RED" "$NC" "$1" >&2
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "$SCRIPT_DIR/docker-compose.yml" ]; then
DOCKER_COMPOSE_FILE="$SCRIPT_DIR/docker-compose.yml"
elif [ -f "$HOME/sitlctl/docker-compose.yml" ]; then
DOCKER_COMPOSE_FILE="$HOME/sitlctl/docker-compose.yml"
else
log_error "Cannot find docker-compose.yml."
log_error "Expected $SCRIPT_DIR/docker-compose.yml or $HOME/sitlctl/docker-compose.yml."
exit 1
fi
SITL_IMAGE="${SITL_IMAGE:-vanfleetdev/sitl-ardupilot:4.6.3}"
SITL_LOG_ROOT="${SITL_LOG_ROOT:-$SCRIPT_DIR/logs}"
SITL_LOCATIONS_FILE="$SCRIPT_DIR/locations.txt"
MAX_VEHICLES=20
START_LOCK_OWNED=false
COMPOSE_COMMAND=()
show_help() {
cat <<'EOF'
ArduPilot SITL control
Usage:
sitlctl start <1|2> <vehicle> [options]
sitlctl stop <1|2|all>
sitlctl status [1|2|all]
sitlctl logs <1|2>
sitlctl shell <1|2>
sitlctl --help
Vehicles:
plane ArduPlane
quadplane ArduPlane, quadplane frame
copter ArduCopter, quad frame
copter-hexa ArduCopter, hexa frame
copter-octa ArduCopter, octa frame
copter-tri ArduCopter, tri frame
rover Rover
rover-skid Rover, skid-steering frame
Start options:
--swarm <n> Start 1-20 vehicles of the same type
--frame <name> Override the default frame
--wipe Wipe parameters before startup
--location <n> Use a named location from locations.txt
--speedup <n> Positive integer simulation speedup (default: 1)
--offset-line <heading,distance>
Spread a swarm in a line (default: 90,10)
Instances:
1 ardupilot-sitl-1, TCP 5760..5950, SYSID 1..20
2 ardupilot-sitl-2, TCP 5960..6150, SYSID 21..40
Examples:
sitlctl start 1 copter
sitlctl start 2 rover --location CMAC
sitlctl status all
sitlctl stop 1
sitlctl stop all
Two single-vehicle MAVProxy links:
mavproxy.py --master=tcp:localhost:5760 --master=tcp:localhost:5960
EOF
}
check_docker() {
if ! command -v docker >/dev/null 2>&1; then
log_error "Docker not found."
exit 1
fi
if ! docker info >/dev/null 2>&1; then
local docker_context
docker_context=$(docker context show 2>/dev/null || printf "unknown")
log_error "Docker is not reachable using context '$docker_context'."
if command -v colima >/dev/null 2>&1 && colima status >/dev/null 2>&1; then
log_info "Colima is running; select it with: docker context use colima"
else
log_info "Start Docker or Colima, then retry."
fi
exit 1
fi
if docker compose version >/dev/null 2>&1; then
COMPOSE_COMMAND=(docker compose)
elif command -v docker-compose >/dev/null 2>&1; then
COMPOSE_COMMAND=(docker-compose)
else
log_error "Docker Compose is not installed."
exit 1
fi
}
configure_instance() {
case "$1" in
1)
SITL_INSTANCE=1
CONTAINER_NAME="ardupilot-sitl-1"
COMPOSE_PROJECT="sitlctl-1"
PORT_BASE=5760
INSTANCE_OFFSET=0
SYSID_BASE=1
;;
2)
SITL_INSTANCE=2
CONTAINER_NAME="ardupilot-sitl-2"
COMPOSE_PROJECT="sitlctl-2"
PORT_BASE=5960
INSTANCE_OFFSET=20
SYSID_BASE=21
;;
*)
log_error "Instance must be 1 or 2."
exit 1
;;
esac
START_LOCK_FILE="${TMPDIR:-/tmp}/sitlctl-${UID:-$(id -u)}-${SITL_INSTANCE}.lock"
SITL_LOG_DIR="$SITL_LOG_ROOT/$SITL_INSTANCE"
}
export_instance_environment() {
export SITL_INSTANCE
export SITL_CONTAINER_NAME="$CONTAINER_NAME"
export SITL_LOG_DIR
export SITL_PORT_BASE="$PORT_BASE"
export SITL_INSTANCE_OFFSET="$INSTANCE_OFFSET"
export SITL_SYSID_BASE="$SYSID_BASE"
local index=0
while [ "$index" -lt "$MAX_VEHICLES" ]; do
export "SITL_PORT_${index}=$((PORT_BASE + index * 10))"
index=$((index + 1))
done
}
container_exists() {
docker ps -a --filter "name=^/${CONTAINER_NAME}$" --format "{{.Names}}" 2>/dev/null | grep -qx "$CONTAINER_NAME"
}
container_is_running() {
docker ps --filter "name=^/${CONTAINER_NAME}$" --format "{{.Names}}" 2>/dev/null | grep -qx "$CONTAINER_NAME"
}
release_start_lock() {
if [ "$START_LOCK_OWNED" != true ]; then
return 0
fi
local lock_pid=""
if [ -f "$START_LOCK_FILE" ]; then
read -r lock_pid < "$START_LOCK_FILE" || true
fi
if [ "$lock_pid" = "$$" ]; then
rm -f "$START_LOCK_FILE" 2>/dev/null || true
fi
START_LOCK_OWNED=false
}
acquire_start_lock() {
if (set -o noclobber; printf '%s\n' "$$" > "$START_LOCK_FILE") 2>/dev/null; then
START_LOCK_OWNED=true
trap release_start_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
return 0
fi
local lock_pid=""
if [ -f "$START_LOCK_FILE" ]; then
read -r lock_pid < "$START_LOCK_FILE" || true
fi
if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
log_error "Instance $SITL_INSTANCE is already starting (PID $lock_pid)."
else
log_error "Stale start lock: $START_LOCK_FILE"
log_info "Remove it only after confirming no start is active."
fi
exit 1
}
select_vehicle() {
case "$1" in
plane)
SITL_VEHICLE="ArduPlane"
SITL_FRAME="plane"
;;
quadplane)
SITL_VEHICLE="ArduPlane"
SITL_FRAME="quadplane"
;;
copter|quad)
SITL_VEHICLE="ArduCopter"
SITL_FRAME="quad"
;;
copter-hexa|hexa)
SITL_VEHICLE="ArduCopter"
SITL_FRAME="hexa"
;;
copter-octa|octa)
SITL_VEHICLE="ArduCopter"
SITL_FRAME="octa"
;;
copter-tri|tri)
SITL_VEHICLE="ArduCopter"
SITL_FRAME="tri"
;;
rover)
SITL_VEHICLE="Rover"
SITL_FRAME="rover"
;;
rover-skid)
SITL_VEHICLE="Rover"
SITL_FRAME="rover-skid"
;;
*)
log_error "Unknown vehicle: $1"
exit 1
;;
esac
}
require_option_value() {
if [ "$#" -lt 2 ] || [ -z "$2" ] || [[ "$2" = --* ]]; then
log_error "$1 requires a value."
exit 1
fi
}
parse_start_options() {
SWARM_COUNT=""
OFFSET_LINE="90,10"
SITL_LOCATION="MAVPROXY_USGS1M"
SITL_WIPE=false
SITL_SPEEDUP=1
while [ "$#" -gt 0 ]; do
case "$1" in
--swarm)
require_option_value "$@"
SWARM_COUNT="$2"
if ! [[ "$SWARM_COUNT" =~ ^[0-9]+$ ]] || [ "$SWARM_COUNT" -lt 1 ] || [ "$SWARM_COUNT" -gt "$MAX_VEHICLES" ]; then
log_error "--swarm must be an integer from 1 to $MAX_VEHICLES."
exit 1
fi
shift 2
;;
--offset-line)
require_option_value "$@"
OFFSET_LINE="$2"
shift 2
;;
--frame)
require_option_value "$@"
SITL_FRAME="$2"
shift 2
;;
--wipe)
SITL_WIPE=true
shift
;;
--location|-L)
require_option_value "$@"
SITL_LOCATION="$2"
shift 2
;;
--speedup)
require_option_value "$@"
SITL_SPEEDUP="$2"
if ! [[ "$SITL_SPEEDUP" =~ ^[1-9][0-9]*$ ]]; then
log_error "--speedup must be a positive integer."
exit 1
fi
shift 2
;;
*)
log_error "Unknown option: $1"
exit 1
;;
esac
done
}
validate_start_configuration() {
if [ ! -f "$SITL_LOCATIONS_FILE" ]; then
log_error "Locations file not found: $SITL_LOCATIONS_FILE"
exit 1
fi
if ! awk -F= -v name="$SITL_LOCATION" '$1 == name { found = 1 } END { exit !found }' "$SITL_LOCATIONS_FILE"; then
log_error "Unknown location: $SITL_LOCATION"
log_info "Choose a name defined in $SITL_LOCATIONS_FILE"
exit 1
fi
if [ "$SITL_VEHICLE" != "ArduCopter" ]; then
return 0
fi
case "$SITL_FRAME" in
heli|heli-gas|heli-dual|heli-blade360)
log_error "Frame $SITL_FRAME is unavailable: the pinned image lacks arducopter-heli."
exit 1
;;
esac
}
print_endpoints() {
local count="$1"
local index=0
while [ "$index" -lt "$count" ]; do
local port=$((PORT_BASE + index * 10))
local sysid=$((SYSID_BASE + index))
printf " SYSID %-3s tcp:localhost:%s\n" "$sysid" "$port"
index=$((index + 1))
done
}
start_instance() {
local instance="$1"
local vehicle_name="$2"
shift 2
configure_instance "$instance"
select_vehicle "$vehicle_name"
parse_start_options "$@"
validate_start_configuration
check_docker
acquire_start_lock
if container_is_running; then
log_error "Instance $SITL_INSTANCE is already running."
log_info "Inspect it with: sitlctl status $SITL_INSTANCE"
exit 1
fi
if container_exists; then
docker rm -f "$CONTAINER_NAME" >/dev/null
fi
if ! docker image inspect "$SITL_IMAGE" >/dev/null 2>&1; then
log_error "Required image is not cached locally: $SITL_IMAGE"
log_info "Pull it explicitly with: docker pull $SITL_IMAGE"
exit 1
fi
mkdir -p "$SITL_LOG_DIR"
export_instance_environment
export SITL_IMAGE SITL_VEHICLE SITL_FRAME SITL_LOCATION SITL_WIPE SITL_SPEEDUP OFFSET_LINE
if [ -n "$SWARM_COUNT" ]; then
export SWARM_MODE=true SWARM_COUNT
vehicle_count="$SWARM_COUNT"
else
export SWARM_MODE=false
unset SWARM_COUNT
vehicle_count=1
fi
log_info "Starting instance $SITL_INSTANCE: $SITL_VEHICLE ($SITL_FRAME)"
(
cd "$SCRIPT_DIR"
COMPOSE_PROGRESS=plain "${COMPOSE_COMMAND[@]}" -p "$COMPOSE_PROJECT" -f "$DOCKER_COMPOSE_FILE" up -d
)
sleep "${SITLCTL_START_WAIT_SECONDS:-2}"
if ! container_is_running; then
log_error "Instance $SITL_INSTANCE did not stay running."
log_info "Inspect it with: sitlctl logs $SITL_INSTANCE"
exit 1
fi
log_info "Instance $SITL_INSTANCE is running in $CONTAINER_NAME."
printf "MAVLink endpoints:\n"
print_endpoints "$vehicle_count"
}
stop_instance() {
configure_instance "$1"
export_instance_environment
if ! container_exists; then
log_warn "Instance $SITL_INSTANCE is not present."
return 0
fi
log_info "Stopping instance $SITL_INSTANCE..."
(
cd "$SCRIPT_DIR"
"${COMPOSE_COMMAND[@]}" -p "$COMPOSE_PROJECT" -f "$DOCKER_COMPOSE_FILE" down
)
if container_exists; then
log_error "Instance $SITL_INSTANCE still exists after Compose shutdown."
exit 1
fi
log_info "Instance $SITL_INSTANCE stopped."
}
container_env_value() {
local key="$1"
docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$CONTAINER_NAME" 2>/dev/null \
| grep -m1 "^${key}=" \
| cut -d= -f2-
}
show_instance_status() {
configure_instance "$1"
if ! container_exists; then
printf "Instance %s: stopped\n" "$SITL_INSTANCE"
printf " Container: %s\n" "$CONTAINER_NAME"
printf " Primary: tcp:localhost:%s\n" "$PORT_BASE"
return 0
fi
local state
state=$(docker inspect --format '{{.State.Status}}' "$CONTAINER_NAME")
printf "Instance %s: %s\n" "$SITL_INSTANCE" "$state"
printf " Container: %s\n" "$CONTAINER_NAME"
printf " Vehicle: %s (%s)\n" "$(container_env_value SITL_VEHICLE)" "$(container_env_value SITL_FRAME)"
printf " Location: %s\n" "$(container_env_value SITL_LOCATION)"
local swarm_mode
local count
swarm_mode=$(container_env_value SWARM_MODE)
count=$(container_env_value SWARM_COUNT)
if [ "$swarm_mode" = true ] && [[ "$count" =~ ^[0-9]+$ ]]; then
printf " Mode: swarm (%s)\n" "$count"
else
count=1
printf " Mode: single\n"
fi
printf " Endpoints:\n"
print_endpoints "$count"
}
show_status() {
local target="${1:-all}"
check_docker
case "$target" in
1|2)
show_instance_status "$target"
;;
all)
show_instance_status 1
show_instance_status 2
;;
*)
log_error "Status target must be 1, 2, or all."
exit 1
;;
esac
}
stop_target() {
local target="$1"
check_docker
case "$target" in
1|2)
stop_instance "$target"
;;
all)
stop_instance 1
stop_instance 2
;;
*)
log_error "Stop target must be 1, 2, or all."
exit 1
;;
esac
}
show_logs() {
configure_instance "$1"
check_docker
if ! container_exists; then
log_error "Instance $SITL_INSTANCE is not present."
exit 1
fi
docker logs -f "$CONTAINER_NAME"
}
open_shell() {
configure_instance "$1"
check_docker
if ! container_is_running; then
log_error "Instance $SITL_INSTANCE is not running."
exit 1
fi
docker exec -it "$CONTAINER_NAME" bash
}
require_instance_argument() {
local command_name="$1"
shift
if [ "$#" -ne 1 ]; then
log_error "$command_name requires exactly one instance: 1 or 2."
exit 1
fi
}
main() {
if [ "$#" -eq 0 ]; then
show_help
return 0
fi
case "$1" in
start)
if [ "$#" -lt 3 ]; then
log_error "Usage: sitlctl start <1|2> <vehicle> [options]"
exit 1
fi
shift
start_instance "$@"
;;
stop)
shift
if [ "$#" -ne 1 ]; then
log_error "Usage: sitlctl stop <1|2|all>"
exit 1
fi
stop_target "$1"
;;
status)
shift
if [ "$#" -gt 1 ]; then
log_error "Usage: sitlctl status [1|2|all]"
exit 1
fi
show_status "${1:-all}"
;;
logs)
shift
require_instance_argument logs "$@"
show_logs "$1"
;;
shell)
shift
require_instance_argument shell "$@"
open_shell "$1"
;;
--help|-h|help)
show_help
;;
*)
log_error "Unknown command: $1"
printf "\n" >&2
show_help >&2
exit 1
;;
esac
}
main "$@"