Skip to content

Commit

Permalink
[hal,wpilib,wpimath] Add Usage Reporting for Choreo and PathWeaver (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sciencewhiz authored Dec 1, 2024
1 parent 9807d60 commit 892e062
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hal/src/generate/ResourceType.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ kResourceType_RevSparkFlexPWM = 115
kResourceType_BangBangController = 116
kResourceType_DataLogManager = 117
kResourceType_LoggingFramework = 118
kResourceType_ChoreoTrajectory = 119
kResourceType_ChoreoTrigger = 120
kResourceType_PathWeaverTrajectory = 121
6 changes: 6 additions & 0 deletions hal/src/generated/main/java/edu/wpi/first/hal/FRCNetComm.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hal/src/generated/main/native/include/hal/FRCUsageReporting.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hal/src/generated/main/native/include/hal/UsageReporting.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions wpilibc/src/main/native/cppcs/RobotBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class WPILibMathShared : public wpi::math::MathShared {
case wpi::math::MathUsageId::kController_BangBangController:
HAL_Report(HALUsageReporting::kResourceType_BangBangController, count);
break;
case wpi::math::MathUsageId::kTrajectory_PathWeaver:
HAL_Report(HALUsageReporting::kResourceType_PathWeaverTrajectory,
count);
break;
}
}

Expand Down
2 changes: 2 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public void reportUsage(MathUsageId id, int count) {
tResourceType.kResourceType_ProfiledPIDController, count);
case kController_BangBangController -> HAL.report(
tResourceType.kResourceType_BangBangController, count);
case kTrajectory_PathWeaver -> HAL.report(
tResourceType.kResourceType_PathWeaverTrajectory, count);
default -> {
// NOP
}
Expand Down
3 changes: 3 additions & 0 deletions wpimath/src/main/java/edu/wpi/first/math/MathUsageId.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public enum MathUsageId {

/** BangBangController. */
kController_BangBangController,

/** PathWeaver Trajectory. */
kTrajectory_PathWeaver,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.math.trajectory;

import edu.wpi.first.math.MathSharedStore;
import edu.wpi.first.math.MathUsageId;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.jni.TrajectoryUtilJNI;
Expand Down Expand Up @@ -68,6 +70,8 @@ private static double[] getElementsFromTrajectory(Trajectory trajectory) {
return elements;
}

private static int pathWeaverTrajectoryInstances;

/**
* Imports a Trajectory from a JSON file exported from PathWeaver.
*
Expand All @@ -76,6 +80,8 @@ private static double[] getElementsFromTrajectory(Trajectory trajectory) {
* @throws IOException if reading from the file fails.
*/
public static Trajectory fromPathweaverJson(Path path) throws IOException {
MathSharedStore.reportUsage(
MathUsageId.kTrajectory_PathWeaver, ++pathWeaverTrajectoryInstances);
return createTrajectoryFromElements(TrajectoryUtilJNI.fromPathweaverJson(path.toString()));
}

Expand Down
4 changes: 4 additions & 0 deletions wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) {

wpi::json json = wpi::json::parse(fileBuffer.value()->GetCharBuffer());

wpi::math::MathSharedStore::ReportUsage(
wpi::math::MathUsageId::kTrajectory_PathWeaver,
++pathWeaverTrajectoryInstances);

return Trajectory{json.get<std::vector<Trajectory::State>>()};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ class WPILIB_DLLEXPORT TrajectoryUtil {
* @return the trajectory represented by the JSON
*/
static Trajectory DeserializeTrajectory(std::string_view jsonStr);

private:
// Usage reporting for PathWeaver Trajectory instances
inline static int pathWeaverTrajectoryInstances = 0;
};
} // namespace frc
1 change: 1 addition & 0 deletions wpimath/src/main/native/include/wpimath/MathShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum class MathUsageId {
kController_PIDController2,
kController_ProfiledPIDController,
kController_BangBangController,
kTrajectory_PathWeaver,
};

class WPILIB_DLLEXPORT MathShared {
Expand Down

0 comments on commit 892e062

Please sign in to comment.