From cc5aceafab2769fead0194a149d90585967df424 Mon Sep 17 00:00:00 2001 From: shueja-personal Date: Fri, 11 Oct 2024 21:29:17 -0700 Subject: [PATCH 1/2] Filter event markers with null timestamps or empty names --- choreolib/py/choreo/__init__.py | 25 ++++++++----------- choreolib/py/choreo/trajectory/__init__.py | 12 +++++++++ choreolib/src/main/java/choreo/Choreo.java | 11 +++++++- .../java/choreo/trajectory/EventMarker.java | 7 ++++-- .../cpp/choreo/trajectory/EventMarker.cpp | 13 +++++++--- .../cpp/choreo/trajectory/Trajectory.cpp | 16 ++++++++++-- 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/choreolib/py/choreo/__init__.py b/choreolib/py/choreo/__init__.py index 96420bd35..e9cd24e46 100644 --- a/choreolib/py/choreo/__init__.py +++ b/choreolib/py/choreo/__init__.py @@ -1,5 +1,6 @@ import json +from choreo.trajectory import load_event_marker from choreo.trajectory import EventMarker from choreo.trajectory import DifferentialSample from choreo.trajectory import DifferentialTrajectory @@ -39,14 +40,12 @@ def load_differential_trajectory_string( for sample in data["trajectory"]["samples"] ] splits = [int(split) for split in data["trajectory"]["splits"]] - events = [ - EventMarker( - float(event["from"]["offset"]["val"]) - + float(event["from"]["targetTimestamp"]), - event["name"], + events = list( + filter( + lambda marker: marker is not None, + [load_event_marker(event) for event in data["events"]], ) - for event in data["events"] - ] + ) return DifferentialTrajectory(data["name"], samples, splits, events) @@ -94,14 +93,12 @@ def load_swerve_trajectory_string(trajectory_json_string: str) -> SwerveTrajecto for sample in data["trajectory"]["samples"] ] splits = [int(split) for split in data["trajectory"]["splits"]] - events = [ - EventMarker( - float(event["from"]["offset"]["val"]) - + float(event["from"]["targetTimestamp"]), - event["name"], + events = list( + filter( + lambda marker: marker is not None, + [load_event_marker(event) for event in data["events"]], ) - for event in data["events"] - ] + ) return SwerveTrajectory(data["name"], samples, splits, events) diff --git a/choreolib/py/choreo/trajectory/__init__.py b/choreolib/py/choreo/trajectory/__init__.py index 9b7f2fba1..1a11a0ab6 100644 --- a/choreolib/py/choreo/trajectory/__init__.py +++ b/choreolib/py/choreo/trajectory/__init__.py @@ -49,6 +49,18 @@ def __eq__(self, other: EventMarker) -> bool: return self.timestamp == other.timestamp and self.event == other.event +def load_event_marker(event) -> EventMarker | None: + try: + offset = float(event["from"]["offset"]["val"]) + target_timestamp = float(event["from"]["targetTimestamp"]) + name = event["name"] + if target_timestamp + offset < 0 or len(name) == 0: + return None + return EventMarker(target_timestamp + offset, name) + except TypeError as e: + return None + + class DifferentialSample: def __init__( self, diff --git a/choreolib/src/main/java/choreo/Choreo.java b/choreolib/src/main/java/choreo/Choreo.java index c06ddf0f9..b09bfed81 100644 --- a/choreolib/src/main/java/choreo/Choreo.java +++ b/choreolib/src/main/java/choreo/Choreo.java @@ -29,6 +29,8 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -162,7 +164,14 @@ static Trajectory> loadTrajectoryString( throw new RuntimeException( name + ".traj: Wrong version: " + version + ". Expected " + SPEC_VERSION); } - EventMarker[] events = GSON.fromJson(wholeTrajectory.get("events"), EventMarker[].class); + // Filter out markers with negative timestamps or empty names + List unfilteredEvents = + new ArrayList( + Arrays.asList(GSON.fromJson(wholeTrajectory.get("events"), EventMarker[].class))); + unfilteredEvents.removeIf(marker -> marker.timestamp < 0 || marker.event.length() == 0); + EventMarker[] events = new EventMarker[unfilteredEvents.size()]; + unfilteredEvents.toArray(events); + JsonObject trajectoryObj = wholeTrajectory.getAsJsonObject("trajectory"); Integer[] splits = GSON.fromJson(trajectoryObj.get("splits"), Integer[].class); if (projectFile.type.equals("Swerve")) { diff --git a/choreolib/src/main/java/choreo/trajectory/EventMarker.java b/choreolib/src/main/java/choreo/trajectory/EventMarker.java index 66e384cfd..c2204aab9 100644 --- a/choreolib/src/main/java/choreo/trajectory/EventMarker.java +++ b/choreolib/src/main/java/choreo/trajectory/EventMarker.java @@ -49,8 +49,11 @@ public EventMarker deserialize( var event = json.getAsJsonObject().get("name").getAsString(); return new EventMarker(targetTimestamp + offset, event); - } catch (IllegalStateException e) { - return new EventMarker(0, ""); + } catch (IllegalStateException + | UnsupportedOperationException + | NullPointerException + | NumberFormatException e) { + return new EventMarker(-1, ""); } } } diff --git a/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp b/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp index df37bd1f4..16fa52e2b 100644 --- a/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp +++ b/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp @@ -14,8 +14,13 @@ void choreo::to_json(wpi::json& json, const EventMarker& event) { } void choreo::from_json(const wpi::json& json, EventMarker& event) { - event.timestamp = - units::second_t{json.at("from").at("offset").at("val").get() + - json.at("from").at("targetTimestamp").get()}; - event.event = json.at("name").get(); + auto targetTimestamp = json.at("from").at("targetTimestamp"); + if (!targetTimestamp.is_number()) { + event.timestamp = units::second_t{-1}; + event.event = ""; + } else { + event.timestamp = units::second_t{ + json.at("from").at("offset").at("val").get() + targetTimestamp.get()}; + event.event = json.at("name").get(); + } } diff --git a/choreolib/src/main/native/cpp/choreo/trajectory/Trajectory.cpp b/choreolib/src/main/native/cpp/choreo/trajectory/Trajectory.cpp index c6689b4ef..11fb68f91 100644 --- a/choreolib/src/main/native/cpp/choreo/trajectory/Trajectory.cpp +++ b/choreolib/src/main/native/cpp/choreo/trajectory/Trajectory.cpp @@ -23,7 +23,13 @@ void choreo::from_json(const wpi::json& json, json.at("trajectory").at("samples").get>(); trajectory.splits = json.at("trajectory").at("splits").get>(); - trajectory.events = json.at("events").get>(); + auto events = json.at("events").get>(); + trajectory.events.clear(); + for (EventMarker event : events) { + if (event.timestamp >= units::second_t{0} || event.event.size() == 0) { + trajectory.events.push_back(event); + } + } } void choreo::to_json(wpi::json& json, @@ -41,5 +47,11 @@ void choreo::from_json(const wpi::json& json, .get>(); trajectory.splits = json.at("trajectory").at("splits").get>(); - trajectory.events = json.at("events").get>(); + auto events = json.at("events").get>(); + trajectory.events.clear(); + for (EventMarker event : events) { + if (event.timestamp >= units::second_t{0} || event.event.size() == 0) { + trajectory.events.push_back(event); + } + } } From 1d0154cca2a109bd8c2c1235e134bdaf8c625e3e Mon Sep 17 00:00:00 2001 From: shueja-personal Date: Fri, 11 Oct 2024 22:07:01 -0700 Subject: [PATCH 2/2] Format --- choreolib/py/choreo/trajectory/__init__.py | 2 +- .../src/main/native/cpp/choreo/trajectory/EventMarker.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/choreolib/py/choreo/trajectory/__init__.py b/choreolib/py/choreo/trajectory/__init__.py index 1a11a0ab6..fd98dcf43 100644 --- a/choreolib/py/choreo/trajectory/__init__.py +++ b/choreolib/py/choreo/trajectory/__init__.py @@ -57,7 +57,7 @@ def load_event_marker(event) -> EventMarker | None: if target_timestamp + offset < 0 or len(name) == 0: return None return EventMarker(target_timestamp + offset, name) - except TypeError as e: + except TypeError: return None diff --git a/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp b/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp index 16fa52e2b..c85ac5a1e 100644 --- a/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp +++ b/choreolib/src/main/native/cpp/choreo/trajectory/EventMarker.cpp @@ -19,8 +19,9 @@ void choreo::from_json(const wpi::json& json, EventMarker& event) { event.timestamp = units::second_t{-1}; event.event = ""; } else { - event.timestamp = units::second_t{ - json.at("from").at("offset").at("val").get() + targetTimestamp.get()}; + event.timestamp = + units::second_t{json.at("from").at("offset").at("val").get() + + targetTimestamp.get()}; event.event = json.at("name").get(); } }