Skip to content

Commit

Permalink
got choreo trajectory following working
Browse files Browse the repository at this point in the history
  • Loading branch information
r4stered committed Aug 30, 2024
1 parent e8855b1 commit d7b9781
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/main/cpp/choreo/lib/ChoreoTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <units/math.h>
#include <wpi/json.h>
#include <iostream>

using namespace choreolib;

Expand Down Expand Up @@ -107,5 +108,5 @@ void choreolib::to_json(wpi::json& json, const ChoreoTrajectory& traj) {
}

void choreolib::from_json(const wpi::json& json, ChoreoTrajectory& traj) {
traj.SetSamples(json.at("samples").get<std::vector<ChoreoTrajectoryState>>());
traj.SetSamples(json.at("traj").at("samples").at(0).get<std::vector<ChoreoTrajectoryState>>());
}
24 changes: 12 additions & 12 deletions src/main/cpp/choreo/lib/ChoreoTrajectoryState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,33 @@ void choreolib::to_json(wpi::json& json,
std::transform(trajState.moduleForcesY.begin(), trajState.moduleForcesY.end(),
fy.begin(), [](units::newton_t x) { return x.value(); });

json = wpi::json{{"timestamp", trajState.timestamp.value()},
json = wpi::json{{"t", trajState.timestamp.value()},
{"x", trajState.x.value()},
{"y", trajState.y.value()},
{"heading", trajState.heading.value()},
{"velocityX", trajState.velocityX.value()},
{"velocityY", trajState.velocityY.value()},
{"angularVelocity", trajState.angularVelocity.value()},
{"moduleForcesX", fx},
{"moduleForcesY", fy}};
{"vx", trajState.velocityX.value()},
{"vy", trajState.velocityY.value()},
{"omega", trajState.angularVelocity.value()},
{"fx", fx},
{"fy", fy}};
}

void choreolib::from_json(const wpi::json& json,
ChoreoTrajectoryState& trajState) {
trajState.timestamp = units::second_t{json.at("timestamp").get<double>()};
trajState.timestamp = units::second_t{json.at("t").get<double>()};
trajState.x = units::meter_t{json.at("x").get<double>()};
trajState.y = units::meter_t{json.at("y").get<double>()};
trajState.heading = units::radian_t{json.at("heading").get<double>()};
trajState.velocityX =
units::meters_per_second_t{json.at("velocityX").get<double>()};
units::meters_per_second_t{json.at("vx").get<double>()};
trajState.velocityY =
units::meters_per_second_t{json.at("velocityY").get<double>()};
units::meters_per_second_t{json.at("vy").get<double>()};
trajState.angularVelocity =
units::radians_per_second_t{json.at("angularVelocity").get<double>()};
units::radians_per_second_t{json.at("omega").get<double>()};

// these probably get optimized out anyways, but wanted to reduce accesses
const auto& fx = json.at("moduleForcesX");
const auto& fy = json.at("moduleForcesY");
const auto& fx = json.at("fx");
const auto& fy = json.at("fy");
for (int i = 0; i < 4; ++i) {
trajState.moduleForcesX[i] = units::newton_t{fx.at(i).get<double>()};
trajState.moduleForcesY[i] = units::newton_t{fy.at(i).get<double>()};
Expand Down
6 changes: 4 additions & 2 deletions src/main/cpp/subsystems/SwerveSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ void SwerveSubsystem::LoadChoreoTrajectories() {
for (const auto &entry : std::filesystem::directory_iterator(
frc::filesystem::GetDeployDirectory() + "/choreo/")) {
std::string fileName = entry.path().stem().string();
fmt::print("Loaded choreo trajectory: {}\n", fileName);
//pathMap[fileName] = choreolib::Choreo::GetTrajectory(fileName);
if(fileName != "choreo") {
pathMap[fileName] = choreolib::Choreo::GetTrajectory(fileName);
fmt::print("Loaded choreo trajectory: {}\n", fileName);
}
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/main/deploy/choreo/NewPath.traj

This file was deleted.

Loading

0 comments on commit d7b9781

Please sign in to comment.