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
28class Choreo {
29 public:
38 template <TrajectorySample SampleType>
39 static std::optional<Trajectory<SampleType>> LoadTrajectory(
40 std::string_view trajectoryName) {
41 if (trajectoryName.ends_with(TRAJECTORY_FILE_EXTENSION)) {
42 trajectoryName = trajectoryName.substr(
43 0, trajectoryName.size() - TRAJECTORY_FILE_EXTENSION.size());
44 }
45
46 std::string trajectoryFileName = fmt::format(
47 "{}/{}{}", CHOREO_DIR, trajectoryName, TRAJECTORY_FILE_EXTENSION);
48
49 auto fileBuffer = wpi::MemoryBuffer::GetFile(trajectoryFileName);
50 if (!fileBuffer) {
51 FRC_ReportError(frc::warn::Warning, "Could not find trajectory file: {}",
52 trajectoryName);
53 return {};
54 }
55
56 try {
58 std::string{fileBuffer.value()->GetCharBuffer().data(),
59 fileBuffer.value()->size()},
61 } catch (wpi::json::parse_error& ex) {
62 FRC_ReportError(frc::warn::Warning, "Could not parse trajectory file: {}",
64 FRC_ReportError(frc::warn::Warning, "{}", ex.what());
65 return {};
66 }
67 return {};
68 }
69
78 template <TrajectorySample SampleType>
79 static std::optional<Trajectory<SampleType>> LoadTrajectoryString(
80 std::string_view trajectoryJsonString, std::string_view trajectoryName) {
81 if constexpr (std::same_as<SampleType, SwerveSample>) {
82 HAL_Report(HALUsageReporting::kResourceType_ChoreoTrajectory, 1);
83 } else if constexpr (std::same_as<SampleType, DifferentialSample>) {
84 HAL_Report(HALUsageReporting::kResourceType_ChoreoTrajectory, 2);
85 }
86
87 wpi::json json = wpi::json::parse(trajectoryJsonString);
88 uint32_t version = json["version"];
89 if (version != kTrajSchemaVersion) {
90 throw fmt::format("{}.traj: Wrong version {}. Expected {}",
91 trajectoryName, version, kTrajSchemaVersion);
92 }
94 from_json(json, trajectory);
95 return trajectory;
96 }
97
100 template <TrajectorySample SampleType>
102 public:
114 static std::optional<Trajectory<SampleType>> LoadTrajectory(
115 std::string_view trajectoryName) {
116 if (cache.contains(std::string{trajectoryName})) {
117 return cache[std::string{trajectoryName}];
118 } else {
119 cache[std::string{trajectoryName}] =
121 return cache[std::string{trajectoryName}];
122 }
123 }
124
139 static std::optional<Trajectory<SampleType>> LoadTrajectory(
140 std::string_view trajectoryName, int splitIndex) {
141 std::string key = fmt::format("{}.:.{}", trajectoryName, splitIndex);
142
143 if (!cache.contains(key)) {
144 if (cache.contains(std::string{trajectoryName})) {
145 cache[key] =
146 cache[std::string{trajectoryName}].value().GetSplit(splitIndex);
147 } else {
149 cache[std::string{trajectoryName}] = possibleTrajectory;
150
151 if (possibleTrajectory.has_value()) {
152 cache[key] = possibleTrajectory.value().GetSplit(splitIndex);
153 }
154 }
155 }
156
157 return cache[key];
158 }
159
161 static void Clear() { cache.clear(); }
162
163 private:
164 static inline std::unordered_map<std::string,
165 std::optional<Trajectory<SampleType>>>
166 cache;
167 };
168
169 private:
170 static constexpr std::string_view TRAJECTORY_FILE_EXTENSION = ".traj";
171
172 static inline const std::string CHOREO_DIR =
173 frc::filesystem::GetDeployDirectory() + "/choreo";
174
175 Choreo();
176};
177
178} // namespace choreo
Definition Choreo.h:101
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName)
Definition Choreo.h:114
static void Clear()
Clears the trajectory cache.
Definition Choreo.h:161
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName, int splitIndex)
Definition Choreo.h:139
A class that handles loading choreo and caching choreo trajectories.
Definition Choreo.h:28
static std::optional< Trajectory< SampleType > > LoadTrajectory(std::string_view trajectoryName)
Definition Choreo.h:39
static std::optional< Trajectory< SampleType > > LoadTrajectoryString(std::string_view trajectoryJsonString, std::string_view trajectoryName)
Definition Choreo.h:79
Definition Trajectory.h:27