am-freescan/scripts/frame_scan/render.py

49 lines
2.4 KiB
Python
Raw Permalink Normal View History

"""Render the pure planner using approved local presets and captured base TCP pose."""
import argparse
import json
import math
from pathlib import Path
def finite(value):
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
def render(presets, pose):
stations = {"LM302", "LM304", "LM306", "LM308"}
if not isinstance(presets, dict) or set(presets) != stations:
raise ValueError("Presets must contain exactly LM302, LM304, LM306 and LM308")
for station, points in presets.items():
if not isinstance(points, list) or len(points) != 2:
raise ValueError(station + " requires two preset endpoints")
if not all(isinstance(point, dict) and finite(point.get("xMm")) for point in points):
raise ValueError(station + " requires finite xMm endpoints")
if pose.get("referenceFrame") != "base" or pose.get("endFrame") != "tcp":
raise ValueError("The captured pose must use base/TCP frames")
radians, degrees = pose["radians"], pose["degrees"]
for axis in ("rx", "ry", "rz"):
if not finite(radians.get(axis)) or not finite(degrees.get(axis + "Deg")):
raise ValueError("Pose orientation must be finite")
if not math.isclose(math.degrees(radians[axis]), degrees[axis + "Deg"], abs_tol=1e-9):
raise ValueError("Pose radians and degrees disagree")
orientation = {**degrees, **radians}
template = Path(__file__).with_name("planner.template.js").read_text(encoding="utf-8")
return template.replace("PRESETS_PLACEHOLDER", json.dumps(presets, ensure_ascii=False)).replace("ORIENTATION_PLACEHOLDER", json.dumps(orientation, ensure_ascii=False))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--presets", type=Path, required=True)
parser.add_argument("--pose", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
if args.output.resolve() in {args.presets.resolve(), args.pose.resolve(), Path(__file__).with_name("planner.template.js").resolve(), Path(__file__).resolve()}:
parser.error("Output must not overwrite an input or implementation file")
presets = json.loads(args.presets.read_text(encoding="utf-8-sig"))
pose = json.loads(args.pose.read_text(encoding="utf-8-sig"))
args.output.write_text(render(presets, pose), encoding="utf-8")
if __name__ == "__main__":
main()