microduck-electric-slide-motion / scripts /export_literal_reference.py
Histochemichael's picture
Add literal-v2 18-count joint targets and physics validation
13ce173 verified
Raw History Blame Contribute Delete
4.12 kB
"""Export the literal Electric Slide count map and 50 Hz joint references."""
from __future__ import annotations
import argparse
import csv
import json
import math
from pathlib import Path
import sys
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from choreography import BEAT_SECONDS, CONTROL_HZ, SHOW_SECONDS, STAND_POSE
from literal_choreography import (
LITERAL_COUNTS,
command_vector,
count_phase,
joint_bias,
)
JOINT_ORDER = (
"left_hip_yaw",
"left_hip_roll",
"left_hip_pitch",
"left_knee",
"left_ankle",
"neck_pitch",
"head_pitch",
"head_yaw",
"head_roll",
"right_hip_yaw",
"right_hip_roll",
"right_hip_pitch",
"right_knee",
"right_ankle",
)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("output_dir", type=Path)
args = parser.parse_args()
output_dir = args.output_dir.resolve()
output_dir.mkdir(parents=True, exist_ok=True)
jsonl_path = output_dir / "literal_joint_targets_50hz.jsonl"
sample_count = int(math.ceil(SHOW_SECONDS * CONTROL_HZ))
with jsonl_path.open("w", encoding="utf-8") as stream:
for sample in range(sample_count):
time_seconds = sample / CONTROL_HZ
wall, count, phase = count_phase(time_seconds)
spec = LITERAL_COUNTS[count]
bias = joint_bias(time_seconds).astype(float)
nominal = np.asarray(STAND_POSE, dtype=float) + bias
row = {
"sample": sample,
"time_seconds": time_seconds,
"beat": time_seconds / BEAT_SECONDS,
"wall": wall + 1,
"count": count + 1,
"phase": phase,
"label": spec.label,
"swing_foot": spec.swing,
"placement": spec.placement,
"weight_transfer": spec.weight_transfer,
"command_13d": command_vector(time_seconds).astype(float).tolist(),
"additive_joint_bias_14d": bias.tolist(),
"nominal_joint_target_14d": nominal.tolist(),
}
stream.write(json.dumps(row, separators=(",", ":")) + "\n")
csv_path = output_dir / "literal_18_count_choreography.csv"
with csv_path.open("w", newline="", encoding="utf-8") as stream:
writer = csv.DictWriter(
stream,
fieldnames=("count", "label", "swing_foot", "placement", "weight_transfer"),
)
writer.writeheader()
for index, spec in enumerate(LITERAL_COUNTS, start=1):
writer.writerow(
{
"count": index,
"label": spec.label,
"swing_foot": spec.swing,
"placement": spec.placement,
"weight_transfer": spec.weight_transfer,
}
)
summary = {
"version": "literal-v2",
"control_hz": CONTROL_HZ,
"bpm": 60.0 / BEAT_SECONDS,
"walls": 4,
"counts_per_wall": len(LITERAL_COUNTS),
"duration_seconds": SHOW_SECONDS,
"sample_count": sample_count,
"joint_order": JOINT_ORDER,
"stand_pose_14d": STAND_POSE,
"command_order_13d": (
"vx",
"vy",
"yaw_rate",
"neck_pitch",
"head_pitch",
"head_yaw",
"head_roll",
"body_x",
"body_y",
"body_z",
"body_roll",
"body_pitch",
"body_yaw",
),
"important": (
"nominal_joint_target_14d is STAND_POSE plus the literal choreography bias. "
"At runtime the official balance-policy action is added before joint-limit clipping. "
"The free root is never written after reset."
),
}
(output_dir / "literal_reference_summary.json").write_text(
json.dumps(summary, indent=2),
encoding="utf-8",
)
print(jsonl_path)
print(csv_path)
if __name__ == "__main__":
main()