TrajoptLib
Library for generating time-optimal trajectories for FRC robots.
Loading...
Searching...
No Matches
point_at_constraint.hpp
1// Copyright (c) TrajoptLib contributors
2
3#pragma once
4
5#include <cassert>
6#include <cmath>
7#include <utility>
8
9#include <sleipnir/autodiff/variable.hpp>
10#include <sleipnir/optimization/problem.hpp>
11
12#include "trajopt/geometry/pose2.hpp"
13#include "trajopt/geometry/translation2.hpp"
14#include "trajopt/util/symbol_exports.hpp"
15
16namespace trajopt {
17
21class TRAJOPT_DLLEXPORT PointAtConstraint {
22 public:
29 explicit PointAtConstraint(Translation2d field_point,
30 double heading_tolerance, bool flip = false)
31 : m_field_point{std::move(field_point)},
32 m_heading_tolerance{heading_tolerance},
33 m_flip{flip} {
34 assert(m_heading_tolerance >= 0.0);
35 }
36
45 void apply(
46 slp::Problem<double>& problem, const Pose2v<double>& pose,
47 [[maybe_unused]] const Translation2v<double>& linear_velocity,
48 [[maybe_unused]] const slp::Variable<double>& angular_velocity,
49 [[maybe_unused]] const Translation2v<double>& linear_acceleration,
50 [[maybe_unused]] const slp::Variable<double>& angular_acceleration) {
51 // dx,dy = desired heading
52 // ux,uy = unit vector of desired heading
53 // hx,hy = heading
54 // dot = dot product of ux,uy and hx,hy
55 //
56 // constrain dot to cos(1.0), which is colinear
57 // and cos(theta_tolerance)
58 auto dx = m_field_point.x() - pose.x();
59 auto dy = m_field_point.y() - pose.y();
60 auto dot = pose.rotation().cos() * dx + pose.rotation().sin() * dy;
61 if (!m_flip) {
62 // dot close to 1 * hypot (point toward)
63 problem.subject_to(dot >=
64 std::cos(m_heading_tolerance) * slp::hypot(dx, dy));
65 } else {
66 // dot close to -1 * hypot (point away)
67 problem.subject_to(dot <=
68 -std::cos(m_heading_tolerance) * slp::hypot(dx, dy));
69 }
70 }
71
72 private:
73 Translation2d m_field_point;
74 double m_heading_tolerance;
75 bool m_flip;
76};
77
78} // namespace trajopt
void apply(slp::Problem< double > &problem, const Pose2v< double > &pose, const Translation2v< double > &linear_velocity, const slp::Variable< double > &angular_velocity, const Translation2v< double > &linear_acceleration, const slp::Variable< double > &angular_acceleration)
Definition point_at_constraint.hpp:45
PointAtConstraint(Translation2d field_point, double heading_tolerance, bool flip=false)
Definition point_at_constraint.hpp:29
constexpr const Rotation2< T > & rotation() const
Definition pose2.hpp:66
constexpr const T & x() const
Definition pose2.hpp:56
constexpr const T & y() const
Definition pose2.hpp:61
constexpr const T & cos() const
Definition rotation2.hpp:122
constexpr const T & sin() const
Definition rotation2.hpp:127