mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add JSON support for Trajectories (#2025)
This commit is contained in:
committed by
Peter Johnson
parent
2b6811eddb
commit
ed30d5d40e
@@ -7,8 +7,20 @@
|
||||
|
||||
#include "frc/trajectory/Trajectory.h"
|
||||
|
||||
#include <wpi/json.h>
|
||||
|
||||
using namespace frc;
|
||||
|
||||
bool Trajectory::State::operator==(const Trajectory::State& other) const {
|
||||
return t == other.t && velocity == other.velocity &&
|
||||
acceleration == other.acceleration && pose == other.pose &&
|
||||
curvature == other.curvature;
|
||||
}
|
||||
|
||||
bool Trajectory::State::operator!=(const Trajectory::State& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
Trajectory::State Trajectory::State::Interpolate(State endValue,
|
||||
double i) const {
|
||||
// Find the new [t] value.
|
||||
@@ -94,3 +106,21 @@ Trajectory::State Trajectory::Sample(units::second_t t) const {
|
||||
return prevSample.Interpolate(sample,
|
||||
(t - prevSample.t) / (sample.t - prevSample.t));
|
||||
}
|
||||
|
||||
void frc::to_json(wpi::json& json, const Trajectory::State& state) {
|
||||
json = wpi::json{{"time", state.t.to<double>()},
|
||||
{"velocity", state.velocity.to<double>()},
|
||||
{"acceleration", state.acceleration.to<double>()},
|
||||
{"pose", state.pose},
|
||||
{"curvature", state.curvature.to<double>()}};
|
||||
}
|
||||
|
||||
void frc::from_json(const wpi::json& json, Trajectory::State& state) {
|
||||
state.pose = json.at("pose").get<Pose2d>();
|
||||
state.t = units::second_t{json.at("time").get<double>()};
|
||||
state.velocity =
|
||||
units::meters_per_second_t{json.at("velocity").get<double>()};
|
||||
state.acceleration =
|
||||
units::meters_per_second_squared_t{json.at("acceleration").get<double>()};
|
||||
state.curvature = frc::curvature_t{json.at("curvature").get<double>()};
|
||||
}
|
||||
|
||||
58
wpilibc/src/main/native/cpp/trajectory/TrajectoryUtil.cpp
Normal file
58
wpilibc/src/main/native/cpp/trajectory/TrajectoryUtil.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "frc/trajectory/TrajectoryUtil.h"
|
||||
|
||||
#include <system_error>
|
||||
|
||||
#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,
|
||||
const wpi::Twine& path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::SmallString<128> buf;
|
||||
wpi::raw_fd_ostream output{path.toStringRef(buf), error_code};
|
||||
if (error_code) {
|
||||
throw std::runtime_error(("Cannot open file: " + path).str());
|
||||
}
|
||||
|
||||
wpi::json json = trajectory.States();
|
||||
output << json;
|
||||
output.flush();
|
||||
}
|
||||
|
||||
Trajectory TrajectoryUtil::FromPathweaverJson(const wpi::Twine& path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::SmallString<128> buf;
|
||||
wpi::raw_fd_istream input{path.toStringRef(buf), error_code};
|
||||
if (error_code) {
|
||||
throw std::runtime_error(("Cannot open file: " + path).str());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Trajectory TrajectoryUtil::DeserializeTrajectory(
|
||||
const wpi::StringRef json_str) {
|
||||
wpi::json json = wpi::json::parse(json_str);
|
||||
return Trajectory{json.get<std::vector<Trajectory::State>>()};
|
||||
}
|
||||
Reference in New Issue
Block a user