TrajoptLib
Library for generating time-optimal trajectories for FRC robots.
Loading...
Searching...
No Matches
lane_constraint.hpp
1// Copyright (c) TrajoptLib contributors
2
3#pragma once
4
5#include <cmath>
6#include <optional>
7
8#include <sleipnir/autodiff/variable.hpp>
9#include <sleipnir/optimization/problem.hpp>
10
11#include "trajopt/constraint/point_line_region_constraint.hpp"
12#include "trajopt/geometry/pose2.hpp"
13#include "trajopt/geometry/rotation2.hpp"
14#include "trajopt/geometry/translation2.hpp"
15#include "trajopt/util/symbol_exports.hpp"
16
17namespace trajopt {
18
22class TRAJOPT_DLLEXPORT LaneConstraint {
23 public:
30 LaneConstraint(Translation2d center_line_start, Translation2d center_line_end,
31 double tolerance)
32 : m_top_line{[&] {
33 if (tolerance != 0.0) {
34 double dx = center_line_end.x() - center_line_start.x();
35 double dy = center_line_end.y() - center_line_start.y();
36 double dist = std::hypot(dx, dy);
37 auto offset = Translation2d{0.0, tolerance}.rotate_by(
38 Rotation2d{dx / dist, dy / dist});
39
40 return PointLineRegionConstraint{{0.0, 0.0},
41 center_line_start + offset,
42 center_line_end + offset,
43 Side::BELOW};
44 } else {
46 {0.0, 0.0}, center_line_start, center_line_end, Side::ON};
47 }
48 }()},
49 m_bottom_line{[&]() -> std::optional<PointLineRegionConstraint> {
50 if (tolerance != 0.0) {
51 double dx = center_line_end.x() - center_line_start.x();
52 double dy = center_line_end.y() - center_line_start.y();
53 double dist = std::hypot(dx, dy);
54 auto offset = Translation2d{0.0, tolerance}.rotate_by(
55 Rotation2d{dx / dist, dy / dist});
56
57 return PointLineRegionConstraint{{0.0, 0.0},
58 center_line_start - offset,
59 center_line_end - offset,
60 Side::ABOVE};
61 } else {
62 return std::nullopt;
63 }
64 }()} {}
65
74 void apply(slp::Problem<double>& problem, const Pose2v<double>& pose,
75 const Translation2v<double>& linear_velocity,
76 const slp::Variable<double>& angular_velocity,
77 const Translation2v<double>& linear_acceleration,
78 const slp::Variable<double>& angular_acceleration) {
79 m_top_line.apply(problem, pose, linear_velocity, angular_velocity,
80 linear_acceleration, angular_acceleration);
81 if (m_bottom_line.has_value()) {
82 m_bottom_line.value().apply(problem, pose, linear_velocity,
83 angular_velocity, linear_acceleration,
84 angular_acceleration);
85 }
86 }
87
88 private:
90 std::optional<PointLineRegionConstraint> m_bottom_line;
91};
92
93} // 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 lane_constraint.hpp:74
LaneConstraint(Translation2d center_line_start, Translation2d center_line_end, double tolerance)
Definition lane_constraint.hpp:30
Definition point_line_region_constraint.hpp:32
constexpr const T & y() const
Definition translation2.hpp:60
constexpr auto rotate_by(const Rotation2< U > &rotation) const
Definition translation2.hpp:117
constexpr const T & x() const
Definition translation2.hpp:55