Skip to content

Commit 68bc206

Browse files
Merge pull request #1 from floesche/optimize
Add examples
2 parents 53a0018 + df79ee0 commit 68bc206

File tree

12 files changed

+900
-318
lines changed

12 files changed

+900
-318
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SCM syntax highlighting & preventing 3-way merges
2+
pixi.lock merge=binary linguist-language=YAML linguist-generated=true -diff

examples/all_on.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import time
2+
3+
from arena_interface import ArenaInterface
4+
5+
ai = ArenaInterface(debug=True)
6+
ai.set_ethernet_mode(ip_address="10.103.40.45")
7+
8+
start_time = time.time()
9+
ai.all_on()
10+
end_time = time.time()
11+
duration = end_time - start_time
12+
13+
time.sleep(5)
14+
ai.all_off()
15+
16+
print(f"Duration of all_on() call: {duration:.6f} seconds")

examples/mode2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Play 5 patterns for 5 seconds each using play_pattern.
2+
3+
Pattern 1 runs at 300 fps; the rest at random rates between 10 and 300 fps.
4+
The 4 additional patterns are chosen randomly from IDs 2–700.
5+
"""
6+
7+
import os
8+
import random
9+
10+
from arena_interface import ArenaInterface
11+
12+
PATTERN_IDS = [1] + random.sample(range(2, 10), 4)
13+
RUNTIME_DURATION = 50 # 50 × 100 ms = 5 s
14+
15+
ip = os.environ.get("ARENA_ETH_IP", "10.103.40.45")
16+
ai = ArenaInterface(debug=True)
17+
ai.set_ethernet_mode(ip_address=ip)
18+
19+
for pat_id in PATTERN_IDS:
20+
fps = 300 if pat_id == PATTERN_IDS[0] else random.randint(10, 300)
21+
print(f"\n--- Playing pattern {pat_id} at {fps} fps for 5 s ---")
22+
ai.play_pattern(
23+
pattern_id=pat_id,
24+
frame_rate=fps,
25+
runtime_duration=RUNTIME_DURATION,
26+
)
27+
print(f" Pattern {pat_id} done.")

examples/mode3.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Show 5 patterns for 5 seconds each using show_pattern_frame.
2+
3+
For each pattern, send as many show_pattern_frame commands as possible
4+
at a target rate of 300 Hz with random frame indices between 1 and 15.
5+
"""
6+
7+
import os
8+
import random
9+
import time
10+
11+
from arena_interface import ArenaInterface
12+
13+
PATTERN_IDS = [1] + random.sample(range(2, 10), 4)
14+
DURATION_S = 5.0
15+
TARGET_RATE_HZ = 300
16+
FRAME_INDEX_MIN = 1
17+
FRAME_INDEX_MAX = 15
18+
19+
ip = os.environ.get("ARENA_ETH_IP", "10.103.40.45")
20+
ai = ArenaInterface(debug=True)
21+
ai.set_ethernet_mode(ip_address=ip)
22+
23+
for pat_id in PATTERN_IDS:
24+
print(f"\n--- Showing pattern {pat_id} for {DURATION_S} s at {TARGET_RATE_HZ} Hz ---")
25+
interval = 1.0 / TARGET_RATE_HZ
26+
count = 0
27+
t_start = time.perf_counter()
28+
deadline = t_start + DURATION_S
29+
30+
while True:
31+
t_now = time.perf_counter()
32+
if t_now >= deadline:
33+
break
34+
frame_idx = random.randint(FRAME_INDEX_MIN, FRAME_INDEX_MAX)
35+
ai.show_pattern_frame(pattern_id=pat_id, frame_index=frame_idx)
36+
count += 1
37+
# spin-wait until next slot
38+
next_time = t_start + count * interval
39+
while time.perf_counter() < next_time:
40+
pass
41+
42+
elapsed = time.perf_counter() - t_start
43+
actual_hz = count / elapsed if elapsed > 0 else 0
44+
print(f" Pattern {pat_id}: {count} frames in {elapsed:.2f} s ({actual_hz:.1f} Hz)")

examples/play_pat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
3+
from arena_interface import ArenaInterface
4+
5+
ai = ArenaInterface(debug=True)
6+
ai.set_ethernet_mode(ip_address="10.103.40.45")
7+
8+
# Measure time for play_pattern call
9+
start_time = time.time()
10+
ai.play_pattern(pattern_id=532, frame_rate=20, runtime_duration=10)
11+
end_time = time.time()
12+
13+
duration = end_time - start_time
14+
print(f"play_pattern() duration: {duration:.6f} seconds")

examples/stream_all_patterns.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Stream each pattern file in patterns/ for 5 seconds."""
2+
3+
import os
4+
from pathlib import Path
5+
6+
from arena_interface import ArenaInterface
7+
8+
PATTERNS_DIR = Path(__file__).resolve().parent.parent / "patterns"
9+
FRAME_RATE = 0
10+
RUNTIME_DURATION = 50 # 50 × 100 ms = 5 s
11+
12+
ip = os.environ.get("ARENA_ETH_IP", "10.103.40.45")
13+
ai = ArenaInterface(debug=True)
14+
ai.set_ethernet_mode(ip_address=ip)
15+
16+
for pat_file in sorted(PATTERNS_DIR.glob("*.pat")):
17+
print(f"\n--- Streaming {pat_file.name} for 5 s at {FRAME_RATE} Hz ---")
18+
result = ai.stream_frames(
19+
pattern_path=str(pat_file),
20+
frame_rate=FRAME_RATE,
21+
runtime_duration=RUNTIME_DURATION,
22+
analog_out_waveform="constant",
23+
analog_update_rate=1.0,
24+
analog_frequency=0.0,
25+
)
26+
print(f" frames: {result['frames']}")
27+
print(f" elapsed: {result['elapsed_s']:.2f} s")
28+
print(f" rate: {result['rate_hz']:.1f} Hz")
29+
print(f" tx: {result['tx_mbps']:.2f} Mb/s")

pixi.lock

Lines changed: 75 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ platforms = ["linux-64", "osx-64", "osx-arm64", "win-64"]
8484

8585
[tool.pixi.dependencies]
8686
git = "*"
87+
pyserial = ">=3.5,<4"
88+
click = ">=8.3.1,<9"
89+
schedule = ">=1.2.2,<2"
8790

8891
[tool.pixi.pypi-dependencies]
8992
"arena-interface" = { path = ".", editable = true }
@@ -110,6 +113,8 @@ qtools-install = "python tools/quantum_leaps_tools.py qtools-install"
110113
qspy = "python tools/quantum_leaps_tools.py qspy"
111114
bench = "arena-interface bench"
112115
bench-full = "arena-interface bench --stream-path patterns/pat0004.pat"
116+
bench-max-rate = "arena-interface bench --stream-path patterns/pat0004.pat --stream-max-rate"
117+
bench-max-rate-smoke = "arena-interface bench --cmd-iters 250 --spf-seconds 2 --stream-path patterns/pat0004.pat --stream-seconds 2 --stream-max-rate --stream-max-rate-seconds 2"
113118
bench-smoke = "arena-interface bench --cmd-iters 250 --spf-seconds 2 --stream-path patterns/pat0004.pat --stream-seconds 2"
114119
bench-persistent = "arena-interface bench --cmd-connect-mode persistent"
115120
bench-new-connection = "arena-interface bench --cmd-connect-mode new_connection"

src/arena_interface/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
__url__,
1010
__version__,
1111
)
12-
from .arena_interface import ArenaInterface
12+
from .arena_interface import ArenaInterface, CommandTimeouts
1313

1414
__all__ = [
1515
"ArenaInterface",
16+
"CommandTimeouts",
1617
"__author__",
1718
"__copyright__",
1819
"__description__",

0 commit comments

Comments
 (0)