2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
|
|
|
#include "frc/trajectory/TrajectoryUtil.h"
|
|
|
|
|
|
|
|
|
|
#include <system_error>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2019-11-02 13:35:03 -05:00
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/json.h>
|
|
|
|
|
#include <wpi/raw_istream.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
void TrajectoryUtil::ToPathweaverJson(const Trajectory& trajectory,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view path) {
|
2019-11-02 13:35:03 -05:00
|
|
|
std::error_code error_code;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
wpi::raw_fd_ostream output{path, error_code};
|
2019-11-02 13:35:03 -05:00
|
|
|
if (error_code) {
|
2021-06-06 16:13:58 -07:00
|
|
|
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
2019-11-02 13:35:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::json json = trajectory.States();
|
|
|
|
|
output << json;
|
|
|
|
|
output.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) {
|
2019-11-02 13:35:03 -05:00
|
|
|
std::error_code error_code;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
wpi::raw_fd_istream input{path, error_code};
|
2019-11-02 13:35:03 -05:00
|
|
|
if (error_code) {
|
2021-06-06 16:13:58 -07:00
|
|
|
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
2019-11-02 13:35:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::json json;
|
|
|
|
|
input >> json;
|
|
|
|
|
|
|
|
|
|
return Trajectory{json.get<std::vector<Trajectory::State>>()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string TrajectoryUtil::SerializeTrajectory(const Trajectory& trajectory) {
|
|
|
|
|
wpi::json json = trajectory.States();
|
|
|
|
|
return json.dump();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
Trajectory TrajectoryUtil::DeserializeTrajectory(std::string_view jsonStr) {
|
|
|
|
|
wpi::json json = wpi::json::parse(jsonStr);
|
2019-11-02 13:35:03 -05:00
|
|
|
return Trajectory{json.get<std::vector<Trajectory::State>>()};
|
|
|
|
|
}
|