mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
SCRIPT namespace replacements
This commit is contained in:
committed by
Peter Johnson
parent
ae6c043632
commit
9aca8e0fd6
@@ -6,22 +6,22 @@
|
||||
|
||||
#include "wpi/util/json.hpp"
|
||||
|
||||
using namespace frc;
|
||||
using namespace wpi::math;
|
||||
|
||||
void frc::to_json(wpi::json& json, const Trajectory::State& state) {
|
||||
json = wpi::json{{"time", state.t.value()},
|
||||
void wpi::math::to_json(wpi::util::json& json, const Trajectory::State& state) {
|
||||
json = wpi::util::json{{"time", state.t.value()},
|
||||
{"velocity", state.velocity.value()},
|
||||
{"acceleration", state.acceleration.value()},
|
||||
{"pose", state.pose},
|
||||
{"curvature", state.curvature.value()}};
|
||||
}
|
||||
|
||||
void frc::from_json(const wpi::json& json, Trajectory::State& state) {
|
||||
void wpi::math::from_json(const wpi::util::json& json, Trajectory::State& state) {
|
||||
state.pose = json.at("pose").get<Pose2d>();
|
||||
state.t = units::second_t{json.at("time").get<double>()};
|
||||
state.t = wpi::units::second_t{json.at("time").get<double>()};
|
||||
state.velocity =
|
||||
units::meters_per_second_t{json.at("velocity").get<double>()};
|
||||
wpi::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 = units::curvature_t{json.at("curvature").get<double>()};
|
||||
wpi::units::meters_per_second_squared_t{json.at("acceleration").get<double>()};
|
||||
state.curvature = wpi::units::curvature_t{json.at("curvature").get<double>()};
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include "wpi/math/trajectory/TrajectoryParameterizer.hpp"
|
||||
#include "wpi/util/print.hpp"
|
||||
|
||||
using namespace frc;
|
||||
using namespace wpi::math;
|
||||
|
||||
const Trajectory TrajectoryGenerator::kDoNothingTrajectory(
|
||||
std::vector<Trajectory::State>{Trajectory::State()});
|
||||
@@ -22,7 +22,7 @@ void TrajectoryGenerator::ReportError(const char* error) {
|
||||
if (s_errorFunc) {
|
||||
s_errorFunc(error);
|
||||
} else {
|
||||
wpi::print(stderr, "TrajectoryGenerator error: {}\n", error);
|
||||
wpi::util::print(stderr, "TrajectoryGenerator error: {}\n", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ Trajectory TrajectoryGenerator::GenerateTrajectory(
|
||||
end.y[1] *= -1;
|
||||
}
|
||||
|
||||
std::vector<frc::SplineParameterizer::PoseWithCurvature> points;
|
||||
std::vector<wpi::math::SplineParameterizer::PoseWithCurvature> points;
|
||||
try {
|
||||
points =
|
||||
SplinePointsFromSplines(SplineHelper::CubicSplinesFromControlVectors(
|
||||
@@ -86,7 +86,7 @@ Trajectory TrajectoryGenerator::GenerateTrajectory(
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<frc::SplineParameterizer::PoseWithCurvature> points;
|
||||
std::vector<wpi::math::SplineParameterizer::PoseWithCurvature> points;
|
||||
try {
|
||||
points = SplinePointsFromSplines(
|
||||
SplineHelper::QuinticSplinesFromControlVectors(controlVectors));
|
||||
|
||||
@@ -34,15 +34,15 @@
|
||||
|
||||
#include "wpi/units/math.hpp"
|
||||
|
||||
using namespace frc;
|
||||
using namespace wpi::math;
|
||||
|
||||
Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
const std::vector<PoseWithCurvature>& points,
|
||||
const std::vector<std::unique_ptr<TrajectoryConstraint>>& constraints,
|
||||
units::meters_per_second_t startVelocity,
|
||||
units::meters_per_second_t endVelocity,
|
||||
units::meters_per_second_t maxVelocity,
|
||||
units::meters_per_second_squared_t maxAcceleration, bool reversed) {
|
||||
wpi::units::meters_per_second_t startVelocity,
|
||||
wpi::units::meters_per_second_t endVelocity,
|
||||
wpi::units::meters_per_second_t maxVelocity,
|
||||
wpi::units::meters_per_second_squared_t maxAcceleration, bool reversed) {
|
||||
std::vector<ConstrainedState> constrainedStates(points.size());
|
||||
|
||||
ConstrainedState predecessor{points.front(), 0_m, startVelocity,
|
||||
@@ -56,7 +56,7 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
constrainedState.pose = points[i];
|
||||
|
||||
// Begin constraining based on predecessor
|
||||
units::meter_t ds = constrainedState.pose.first.Translation().Distance(
|
||||
wpi::units::meter_t ds = constrainedState.pose.first.Translation().Distance(
|
||||
predecessor.pose.first.Translation());
|
||||
constrainedState.distance = ds + predecessor.distance;
|
||||
|
||||
@@ -66,9 +66,9 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// Enforce global max velocity and max reachable velocity by global
|
||||
// acceleration limit. v_f = √(v_i² + 2ad).
|
||||
|
||||
constrainedState.maxVelocity = units::math::min(
|
||||
constrainedState.maxVelocity = wpi::units::math::min(
|
||||
maxVelocity,
|
||||
units::math::sqrt(predecessor.maxVelocity * predecessor.maxVelocity +
|
||||
wpi::units::math::sqrt(predecessor.maxVelocity * predecessor.maxVelocity +
|
||||
predecessor.maxAcceleration * ds * 2.0));
|
||||
|
||||
constrainedState.minAcceleration = -maxAcceleration;
|
||||
@@ -77,7 +77,7 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// At this point, the constrained state is fully constructed apart from
|
||||
// all the custom-defined user constraints.
|
||||
for (const auto& constraint : constraints) {
|
||||
constrainedState.maxVelocity = units::math::min(
|
||||
constrainedState.maxVelocity = wpi::units::math::min(
|
||||
constrainedState.maxVelocity,
|
||||
constraint->MaxVelocity(constrainedState.pose.first,
|
||||
constrainedState.pose.second,
|
||||
@@ -94,7 +94,7 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// If the actual acceleration for this state is higher than the max
|
||||
// acceleration that we applied, then we need to reduce the max
|
||||
// acceleration of the predecessor and try again.
|
||||
units::meters_per_second_squared_t actualAcceleration =
|
||||
wpi::units::meters_per_second_squared_t actualAcceleration =
|
||||
(constrainedState.maxVelocity * constrainedState.maxVelocity -
|
||||
predecessor.maxVelocity * predecessor.maxVelocity) /
|
||||
(ds * 2.0);
|
||||
@@ -123,14 +123,14 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// Backward pass
|
||||
for (int i = points.size() - 1; i >= 0; i--) {
|
||||
auto& constrainedState = constrainedStates[i];
|
||||
units::meter_t ds =
|
||||
wpi::units::meter_t ds =
|
||||
constrainedState.distance - successor.distance; // negative
|
||||
|
||||
while (true) {
|
||||
// Enforce max velocity limit (reverse)
|
||||
// v_f = √(v_i² + 2ad), where v_i = successor.
|
||||
units::meters_per_second_t newMaxVelocity =
|
||||
units::math::sqrt(successor.maxVelocity * successor.maxVelocity +
|
||||
wpi::units::meters_per_second_t newMaxVelocity =
|
||||
wpi::units::math::sqrt(successor.maxVelocity * successor.maxVelocity +
|
||||
successor.minAcceleration * ds * 2.0);
|
||||
|
||||
// No more limits to impose! This state can be finalized.
|
||||
@@ -150,7 +150,7 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// If the actual acceleration for this state is lower than the min
|
||||
// acceleration, then we need to lower the min acceleration of the
|
||||
// successor and try again.
|
||||
units::meters_per_second_squared_t actualAcceleration =
|
||||
wpi::units::meters_per_second_squared_t actualAcceleration =
|
||||
(constrainedState.maxVelocity * constrainedState.maxVelocity -
|
||||
successor.maxVelocity * successor.maxVelocity) /
|
||||
(ds * 2.0);
|
||||
@@ -168,30 +168,30 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
|
||||
// trajectory states.
|
||||
|
||||
std::vector<Trajectory::State> states(points.size());
|
||||
units::second_t t = 0_s;
|
||||
units::meter_t s = 0_m;
|
||||
units::meters_per_second_t v = 0_mps;
|
||||
wpi::units::second_t t = 0_s;
|
||||
wpi::units::meter_t s = 0_m;
|
||||
wpi::units::meters_per_second_t v = 0_mps;
|
||||
|
||||
for (unsigned int i = 0; i < constrainedStates.size(); i++) {
|
||||
auto state = constrainedStates[i];
|
||||
|
||||
// Calculate the change in position between the current state and the
|
||||
// previous state.
|
||||
units::meter_t ds = state.distance - s;
|
||||
wpi::units::meter_t ds = state.distance - s;
|
||||
|
||||
// Calculate the acceleration between the current state and the previous
|
||||
// state.
|
||||
units::meters_per_second_squared_t accel =
|
||||
wpi::units::meters_per_second_squared_t accel =
|
||||
(state.maxVelocity * state.maxVelocity - v * v) / (ds * 2);
|
||||
|
||||
// Calculate dt.
|
||||
units::second_t dt = 0_s;
|
||||
wpi::units::second_t dt = 0_s;
|
||||
if (i > 0) {
|
||||
states.at(i - 1).acceleration = reversed ? -accel : accel;
|
||||
if (units::math::abs(accel) > 1E-6_mps_sq) {
|
||||
if (wpi::units::math::abs(accel) > 1E-6_mps_sq) {
|
||||
// v_f = v_0 + at
|
||||
dt = (state.maxVelocity - v) / accel;
|
||||
} else if (units::math::abs(v) > 1E-6_mps) {
|
||||
} else if (wpi::units::math::abs(v) > 1E-6_mps) {
|
||||
// delta_x = vt
|
||||
dt = ds / v;
|
||||
} else {
|
||||
@@ -230,11 +230,11 @@ void TrajectoryParameterizer::EnforceAccelerationLimits(
|
||||
"back one-by-one.");
|
||||
}
|
||||
|
||||
state->minAcceleration = units::math::max(
|
||||
state->minAcceleration = wpi::units::math::max(
|
||||
state->minAcceleration,
|
||||
reverse ? -minMaxAccel.maxAcceleration : minMaxAccel.minAcceleration);
|
||||
|
||||
state->maxAcceleration = units::math::min(
|
||||
state->maxAcceleration = wpi::units::math::min(
|
||||
state->maxAcceleration,
|
||||
reverse ? -minMaxAccel.minAcceleration : minMaxAccel.maxAcceleration);
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
#include "wpi/util/protobuf/ProtobufCallbacks.hpp"
|
||||
#include "wpimath/protobuf/trajectory.npb.h"
|
||||
|
||||
std::optional<frc::Trajectory> wpi::Protobuf<frc::Trajectory>::Unpack(
|
||||
std::optional<wpi::math::Trajectory> wpi::util::Protobuf<wpi::math::Trajectory>::Unpack(
|
||||
InputStream& stream) {
|
||||
wpi::StdVectorUnpackCallback<frc::Trajectory::State, SIZE_MAX> states;
|
||||
wpi::util::StdVectorUnpackCallback<wpi::math::Trajectory::State, SIZE_MAX> states;
|
||||
wpi_proto_ProtobufTrajectory msg{
|
||||
.states = states.Callback(),
|
||||
};
|
||||
@@ -19,12 +19,12 @@ std::optional<frc::Trajectory> wpi::Protobuf<frc::Trajectory>::Unpack(
|
||||
return {};
|
||||
}
|
||||
|
||||
return frc::Trajectory{states.Vec()};
|
||||
return wpi::math::Trajectory{states.Vec()};
|
||||
}
|
||||
|
||||
bool wpi::Protobuf<frc::Trajectory>::Pack(OutputStream& stream,
|
||||
const frc::Trajectory& value) {
|
||||
wpi::PackCallback<frc::Trajectory::State> states{value.States()};
|
||||
bool wpi::util::Protobuf<wpi::math::Trajectory>::Pack(OutputStream& stream,
|
||||
const wpi::math::Trajectory& value) {
|
||||
wpi::util::PackCallback<wpi::math::Trajectory::State> states{value.States()};
|
||||
wpi_proto_ProtobufTrajectory msg{
|
||||
.states = states.Callback(),
|
||||
};
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
#include "wpi/util/protobuf/ProtobufCallbacks.hpp"
|
||||
#include "wpimath/protobuf/trajectory.npb.h"
|
||||
|
||||
std::optional<frc::Trajectory::State>
|
||||
wpi::Protobuf<frc::Trajectory::State>::Unpack(InputStream& stream) {
|
||||
wpi::UnpackCallback<frc::Pose2d> pose;
|
||||
std::optional<wpi::math::Trajectory::State>
|
||||
wpi::util::Protobuf<wpi::math::Trajectory::State>::Unpack(InputStream& stream) {
|
||||
wpi::util::UnpackCallback<wpi::math::Pose2d> pose;
|
||||
wpi_proto_ProtobufTrajectoryState msg;
|
||||
msg.pose = pose.Callback();
|
||||
|
||||
@@ -25,18 +25,18 @@ wpi::Protobuf<frc::Trajectory::State>::Unpack(InputStream& stream) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return frc::Trajectory::State{
|
||||
units::second_t{msg.time},
|
||||
units::meters_per_second_t{msg.velocity},
|
||||
units::meters_per_second_squared_t{msg.acceleration},
|
||||
return wpi::math::Trajectory::State{
|
||||
wpi::units::second_t{msg.time},
|
||||
wpi::units::meters_per_second_t{msg.velocity},
|
||||
wpi::units::meters_per_second_squared_t{msg.acceleration},
|
||||
std::move(ipose[0]),
|
||||
units::curvature_t{msg.curvature},
|
||||
wpi::units::curvature_t{msg.curvature},
|
||||
};
|
||||
}
|
||||
|
||||
bool wpi::Protobuf<frc::Trajectory::State>::Pack(
|
||||
OutputStream& stream, const frc::Trajectory::State& value) {
|
||||
wpi::PackCallback pose{&value.pose};
|
||||
bool wpi::util::Protobuf<wpi::math::Trajectory::State>::Pack(
|
||||
OutputStream& stream, const wpi::math::Trajectory::State& value) {
|
||||
wpi::util::PackCallback pose{&value.pose};
|
||||
wpi_proto_ProtobufTrajectoryState msg{
|
||||
.time = value.t.value(),
|
||||
.velocity = value.velocity.value(),
|
||||
|
||||
Reference in New Issue
Block a user