mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Add JSON support for Trajectories (#2025)
This commit is contained in:
committed by
Peter Johnson
parent
2b6811eddb
commit
ed30d5d40e
@@ -13,6 +13,10 @@
|
||||
|
||||
#include "frc/geometry/Pose2d.h"
|
||||
|
||||
namespace wpi {
|
||||
class json;
|
||||
} // namespace wpi
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
@@ -47,6 +51,22 @@ class Trajectory {
|
||||
// The curvature at that point of the trajectory.
|
||||
curvature_t curvature{0.0};
|
||||
|
||||
/**
|
||||
* Checks equality between this State and another object.
|
||||
*
|
||||
* @param other The other object.
|
||||
* @return Whether the two objects are equal.
|
||||
*/
|
||||
bool operator==(const State& other) const;
|
||||
|
||||
/**
|
||||
* Checks inequality between this State and another object.
|
||||
*
|
||||
* @param other The other object.
|
||||
* @return Whether the two objects are not equal.
|
||||
*/
|
||||
bool operator!=(const State& other) const;
|
||||
|
||||
/**
|
||||
* Interpolates between two States.
|
||||
*
|
||||
@@ -103,4 +123,9 @@ class Trajectory {
|
||||
return startValue + (endValue - startValue) * t;
|
||||
}
|
||||
};
|
||||
|
||||
void to_json(wpi::json& json, const Trajectory::State& state);
|
||||
|
||||
void from_json(const wpi::json& json, Trajectory::State& state);
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <wpi/StringRef.h>
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "frc/trajectory/Trajectory.h"
|
||||
|
||||
namespace frc {
|
||||
class TrajectoryUtil {
|
||||
public:
|
||||
TrajectoryUtil() = delete;
|
||||
|
||||
/**
|
||||
* Exports a Trajectory to a PathWeaver-style JSON file.
|
||||
*
|
||||
* @param trajectory the trajectory to export
|
||||
* @param path the path of the file to export to
|
||||
*
|
||||
* @return The interpolated state.
|
||||
*/
|
||||
static void ToPathweaverJson(const Trajectory& trajectory,
|
||||
const wpi::Twine& path);
|
||||
/**
|
||||
* Imports a Trajectory from a PathWeaver-style JSON file.
|
||||
*
|
||||
* @param path The path of the json file to import from.
|
||||
*
|
||||
* @return The trajectory represented by the file.
|
||||
*/
|
||||
static Trajectory FromPathweaverJson(const wpi::Twine& path);
|
||||
|
||||
/**
|
||||
* Deserializes a Trajectory from PathWeaver-style JSON.
|
||||
|
||||
* @param json the string containing the serialized JSON
|
||||
|
||||
* @return the trajectory represented by the JSON
|
||||
*/
|
||||
static std::string SerializeTrajectory(const Trajectory& trajectory);
|
||||
|
||||
/**
|
||||
* Serializes a Trajectory to PathWeaver-style JSON.
|
||||
|
||||
* @param trajectory the trajectory to export
|
||||
|
||||
* @return the string containing the serialized JSON
|
||||
*/
|
||||
static Trajectory DeserializeTrajectory(wpi::StringRef json_str);
|
||||
};
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user