ChoreoLib
Choreo support library.
Loading...
Searching...
No Matches
Choreo.h
1// Copyright (c) Choreo contributors
2
3#pragma once
4
5#include <concepts>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10
11#include <fmt/format.h>
12#include <frc/Errors.h>
13#include <frc/Filesystem.h>
14#include <frc2/command/Subsystem.h>
15#include <hal/FRCUsageReporting.h>
16#include <wpi/MemoryBuffer.h>
17#include <wpi/json.h>
18
19#include "choreo/trajectory/DifferentialSample.h"
20#include "choreo/trajectory/SwerveSample.h"
21#include "choreo/trajectory/Trajectory.h"
22#include "choreo/trajectory/TrajectorySample.h"
23#include "choreo/util/TrajSchemaVersion.h"
24
25namespace choreo {
26
30class Choreo {
31 public:
42 template <TrajectorySample SampleType>
43 static std::optional<Trajectory<SampleType>> LoadTrajectory(
44 std::string_view trajectoryName) {
45 if (trajectoryName.ends_with(TRAJECTORY_FILE_EXTENSION)) {
46 trajectoryName = trajectoryName.substr(
47 0, trajectoryName.size() - TRAJECTORY_FILE_EXTENSION.size());
48 }
49
50 std::string trajectoryFileName = fmt::format(
51 "{}/{}{}", CHOREO_DIR, trajectoryName, TRAJECTORY_FILE_EXTENSION);
52
53 auto fileBuffer = wpi::MemoryBuffer::GetFile(trajectoryFileName);
54 if (!fileBuffer) {
55 FRC_ReportError(frc::warn::Warning, "Could not find trajectory file: {}",
56 trajectoryName);
57 return {};
58 }
59
60 try {
62 std::string{fileBuffer.value()->GetCharBuffer().data(),
63 fileBuffer.value()->size()},
65 } catch (wpi::json::parse_error& ex) {
66 FRC_ReportError(frc::warn::Warning, "Could not parse trajectory file: {}",
68 FRC_ReportError(frc::warn::Warning, "{}", ex.what());
69 return {};
70 }
71 return {};
72 }
73
84 template <TrajectorySample SampleType>
85 static std::optional<Trajectory<SampleType>> LoadTrajectoryString(
86 std::string_view trajectoryJsonString, std::string_view trajectoryName) {
87 if constexpr (std::same_as<SampleType, SwerveSample>) {
88 HAL_Report(HALUsageReporting::kResourceType_ChoreoTrajectory, 1);
89 } else if constexpr (std::same_as<SampleType, DifferentialSample>) {
90 HAL_Report(HALUsageReporting::kResourceType_ChoreoTrajectory, 2);
91 }
92
93 wpi::json json = wpi::json::parse(trajectoryJsonString);
94 uint32_t version = json["version"];
95 if (version != kTrajSchemaVersion) {
96 throw fmt::format("{}.traj: Wrong version {}. Expected {}",
97 trajectoryName, version, kTrajSchemaVersion);
98 }
100 from_json(json, trajectory);
101 return trajectory;
102 }
103
108 template <TrajectorySample SampleType>
110 public:
124 static std::optional<Trajectory<SampleType>> LoadTrajectory(
125 std::string_view trajectoryName) {
126 if (cache.contains(std::string{trajectoryName})) {
127 return cache[std::string{trajectoryName}];
128 } else {
129 cache[std::string{trajectoryName}] =
131 return cache[std::string{trajectoryName}];
132 }
133 }
134
150 static std::optional<Trajectory<SampleType>> LoadTrajectory(
151 std::string_view trajectoryName, int splitIndex) {
152 std::string key = fmt::format("{}.:.{}", trajectoryName, splitIndex);
153
154 if (!cache.contains(key)) {
155 if (cache.contains(std::string{trajectoryName})) {
156 cache[key] =
157 cache[std::string{trajectoryName}].value().GetSplit(splitIndex);
158 } else {
160 cache[std::string{trajectoryName}] = possibleTrajectory;
161
162 if (possibleTrajectory.has_value()) {
163 cache[key] = possibleTrajectory.value().GetSplit(splitIndex);
164 }
165 }
166 }
167
168 return cache[key];
169 }
170
174 static void Clear() { cache.clear(); }
175
176 private:
177 static inline std::unordered_map<std::string,
178 std::optional<Trajectory<SampleType>>>
179 cache;
180 };
181
182 private:
183 static constexpr std::string_view TRAJECTORY_FILE_EXTENSION = ".traj";
184
185 static inline const std::string CHOREO_DIR =
186 frc::filesystem::GetDeployDirectory() + "/choreo";
187
188 Choreo();
189};
190
191} // namespace choreo
Definition Choreo.h:109
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName)
Definition Choreo.h:124
static void Clear()
Definition Choreo.h:174
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName, int splitIndex)
Definition Choreo.h:150
Definition Choreo.h:30
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName)
Definition Choreo.h:43
static std::optional< Trajectory< SampleType > > LoadTrajectoryString(std::string_view trajectoryJsonString, std::string_view trajectoryName)
Definition Choreo.h:85
Definition Trajectory.h:29