Repository navigation
Expand file tree
/
Copy pathsequence_library.py
More file actions
129 lines (116 loc) · 4.89 KB
/
Copy pathsequence_library.py
File metadata and controls
129 lines (116 loc) · 4.89 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
"""
Pre-built sequence library for common robot routines.
Sequences are stored as JSON files in the sequences/ directory.
They can be loaded by name via the API.
"""
import json
import logging
from pathlib import Path
from typing import Optional
logger = logging.getLogger(__name__)
SEQUENCES_DIR = Path(__file__).parent / "sequences"
# Built-in sequences that ship with the project
# NOTE: Only uses documented G1 actions (state transitions + velocity movement).
# Gesture actions (wave, bow, etc.) require DDS API and are not available here.
BUILTIN_SEQUENCES: dict[str, dict] = {
"demo": {
"name": "demo",
"steps": [
{"action": "action", "params": {"name": "stand_up"}, "duration": 2.0},
{"action": "action", "params": {"name": "start_control"}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.3}, "duration": 2.0},
{"action": "move", "params": {"vyaw": 0.3}, "duration": 1.5},
{"action": "move", "params": {"vx": 0.3}, "duration": 2.0},
{"action": "action", "params": {"name": "toggle_walk"}, "duration": 2.0},
],
"loop": False,
},
"walk_square": {
"name": "walk_square",
"steps": [
{"action": "action", "params": {"name": "stand_up"}, "duration": 2.0},
{"action": "action", "params": {"name": "start_control"}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.4}, "duration": 3.0},
{"action": "move", "params": {"vyaw": 0.39}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.4}, "duration": 3.0},
{"action": "move", "params": {"vyaw": 0.39}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.4}, "duration": 3.0},
{"action": "move", "params": {"vyaw": 0.39}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.4}, "duration": 3.0},
{"action": "wait", "params": {}, "duration": 1.0},
],
"loop": False,
},
"patrol": {
"name": "patrol",
"steps": [
{"action": "action", "params": {"name": "stand_up"}, "duration": 2.0},
{"action": "action", "params": {"name": "start_control"}, "duration": 2.0},
{"action": "move", "params": {"vx": 0.3}, "duration": 5.0},
{"action": "move", "params": {"vyaw": 0.39}, "duration": 4.0},
{"action": "move", "params": {"vx": 0.3}, "duration": 5.0},
{"action": "move", "params": {"vyaw": 0.39}, "duration": 4.0},
],
"loop": True,
},
"getup_routine": {
"name": "getup_routine",
"steps": [
{"action": "action", "params": {"name": "damping"}, "duration": 1.0},
{"action": "wait", "params": {}, "duration": 1.0},
{"action": "action", "params": {"name": "stand_up"}, "duration": 3.0},
{"action": "action", "params": {"name": "start_control"}, "duration": 2.0},
],
"loop": False,
},
}
class SequenceLibrary:
"""Load, save, and list named sequences."""
def __init__(self, sequences_dir: Optional[Path] = None):
self._dir = sequences_dir or SEQUENCES_DIR
self._dir.mkdir(parents=True, exist_ok=True)
def list(self) -> dict[str, dict]:
"""List all available sequences (built-in + saved)."""
result = dict(BUILTIN_SEQUENCES)
# Load any user-saved sequences from disk
for f in self._dir.glob("*.json"):
try:
data = json.loads(f.read_text())
name = data.get("name", f.stem)
result[name] = data
except Exception as e:
logger.warning("Failed to load sequence %s: %s", f, e)
return result
def get(self, name: str) -> Optional[dict]:
"""Get a sequence by name."""
# Check built-in first
if name in BUILTIN_SEQUENCES:
return BUILTIN_SEQUENCES[name]
# Check saved files
path = self._dir / f"{name}.json"
if path.exists():
try:
return json.loads(path.read_text())
except Exception as e:
logger.warning("Failed to load sequence %s: %s", path, e)
return None
def save(self, name: str, data: dict) -> bool:
"""Save a sequence to disk."""
data["name"] = name
path = self._dir / f"{name}.json"
try:
path.write_text(json.dumps(data, indent=2))
logger.info("Saved sequence: %s", path)
return True
except OSError as e:
logger.error("Failed to save sequence %s: %s", path, e)
return False
def delete(self, name: str) -> bool:
"""Delete a saved sequence (cannot delete built-in)."""
if name in BUILTIN_SEQUENCES:
return False
path = self._dir / f"{name}.json"
if path.exists():
path.unlink()
return True
return False