Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Add acceleration to constrainable variables #211

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -30,11 +31,15 @@ class TRAJOPT_DLLEXPORT AngularVelocityEqualityConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem,
[[maybe_unused]] const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
const sleipnir::Variable& angularVelocity) {
const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
problem.SubjectTo(angularVelocity == m_angularVelocity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cassert>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -35,11 +36,15 @@ class TRAJOPT_DLLEXPORT AngularVelocityMaxMagnitudeConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem,
[[maybe_unused]] const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
const sleipnir::Variable& angularVelocity) {
const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
if (m_maxMagnitude == 0.0) {
problem.SubjectTo(angularVelocity == 0.0);
} else {
Expand Down
10 changes: 10 additions & 0 deletions include/trajopt/constraint/Constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <utility>
#include <variant>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/constraint/AngularVelocityEqualityConstraint.hpp"
Expand All @@ -28,8 +29,17 @@ namespace trajopt {
template <typename T>
concept ConstraintType = requires(T t) {
{
// Parameters:
//
// sleipnir::OptimizationProblem& problem
// const Pose2v& pose
// const Translation2v& linearVelocity
// const sleipnir::Variable& angularVelocity
// const Translation2v& linearAcceleration
// const sleipnir::Variable& angularAcceleration
t.Apply(std::declval<sleipnir::OptimizationProblem>(),
std::declval<Pose2v>(), std::declval<Translation2v>(),
std::declval<sleipnir::Variable>(), std::declval<Translation2v>(),
std::declval<sleipnir::Variable>())
}; // NOLINT(readability/braces)
};
Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/LinePointConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <utility>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/constraint/detail/LinePointDistance.hpp"
Expand Down Expand Up @@ -48,10 +49,14 @@ class TRAJOPT_DLLEXPORT LinePointConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
auto lineStart =
pose.Translation() + m_robotLineStart.RotateBy(pose.Rotation());
auto lineEnd =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -29,11 +30,15 @@ class TRAJOPT_DLLEXPORT LinearVelocityDirectionConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply([[maybe_unused]] sleipnir::OptimizationProblem& problem,
[[maybe_unused]] const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
// <v_x, v_y> and <u_x, u_y> must be parallel
//
// (v ⋅ u)/‖v‖ = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cassert>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -35,11 +36,15 @@ class TRAJOPT_DLLEXPORT LinearVelocityMaxMagnitudeConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem,
[[maybe_unused]] const Pose2v& pose,
const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
if (m_maxMagnitude == 0.0) {
problem.SubjectTo(linearVelocity.X() == 0.0);
problem.SubjectTo(linearVelocity.Y() == 0.0);
Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/PointAtConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <utility>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -40,10 +41,14 @@ class TRAJOPT_DLLEXPORT PointAtConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
// dx,dy = desired heading
// ux,uy = unit vector of desired heading
// hx,hy = heading
Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/PointLineConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <utility>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/constraint/detail/LinePointDistance.hpp"
Expand Down Expand Up @@ -48,10 +49,14 @@ class TRAJOPT_DLLEXPORT PointLineConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
auto point = pose.Translation() + m_robotPoint.RotateBy(pose.Rotation());
auto dist =
detail::LinePointDistance(m_fieldLineStart, m_fieldLineEnd, point);
Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/PointPointConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <utility>

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -44,10 +45,14 @@ class TRAJOPT_DLLEXPORT PointPointConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
auto bumperCorner =
pose.Translation() + m_robotPoint.RotateBy(pose.Rotation());
auto dx = m_fieldPoint.X() - bumperCorner.X();
Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/PoseEqualityConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -32,10 +33,14 @@ class TRAJOPT_DLLEXPORT PoseEqualityConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
problem.SubjectTo(pose == m_pose);
}

Expand Down
7 changes: 6 additions & 1 deletion include/trajopt/constraint/TranslationEqualityConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <sleipnir/autodiff/Variable.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "trajopt/geometry/Pose2.hpp"
Expand Down Expand Up @@ -30,10 +31,14 @@ class TRAJOPT_DLLEXPORT TranslationEqualityConstraint {
* @param pose The robot's pose.
* @param linearVelocity The robot's linear velocity.
* @param angularVelocity The robot's angular velocity.
* @param linearAcceleration The robot's linear acceleration.
* @param angularAcceleration The robot's angular acceleration.
*/
void Apply(sleipnir::OptimizationProblem& problem, const Pose2v& pose,
[[maybe_unused]] const Translation2v& linearVelocity,
[[maybe_unused]] const sleipnir::Variable& angularVelocity) {
[[maybe_unused]] const sleipnir::Variable& angularVelocity,
[[maybe_unused]] const Translation2v& linearAcceleration,
[[maybe_unused]] const sleipnir::Variable& angularAcceleration) {
problem.SubjectTo(pose.Translation() == m_translation);
}

Expand Down
10 changes: 8 additions & 2 deletions src/SwerveTrajectoryGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,13 @@ SwerveTrajectoryGenerator::SwerveTrajectoryGenerator(
x.at(index), y.at(index), {thetacos.at(index), thetasin.at(index)}};
Translation2v linearVelocity{vx.at(index), vy.at(index)};
auto angularVelocity = omega.at(index);
Translation2v linearAcceleration{ax.at(index), ay.at(index)};
auto angularAcceleration = alpha.at(index);

std::visit(
[&](auto&& arg) {
arg.Apply(problem, pose, linearVelocity, angularVelocity);
arg.Apply(problem, pose, linearVelocity, angularVelocity,
linearAcceleration, angularAcceleration);
},
constraint);
}
Expand All @@ -252,10 +255,13 @@ SwerveTrajectoryGenerator::SwerveTrajectoryGenerator(
x.at(index), y.at(index), {thetacos.at(index), thetasin.at(index)}};
Translation2v linearVelocity{vx.at(index), vy.at(index)};
auto angularVelocity = omega.at(index);
Translation2v linearAcceleration{ax.at(index), ay.at(index)};
auto angularAcceleration = alpha.at(index);

std::visit(
[&](auto&& arg) {
arg.Apply(problem, pose, linearVelocity, angularVelocity);
arg.Apply(problem, pose, linearVelocity, angularVelocity,
linearAcceleration, angularAcceleration);
},
constraint);
}
Expand Down
Loading