WPILibC++ 2024.3.2
EllipticalRegionConstraint.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <concepts>
8#include <limits>
9
13#include "units/length.h"
14
15namespace frc {
16/**
17 * Enforces a particular constraint only within an elliptical region.
18 */
19template <std::derived_from<TrajectoryConstraint> Constraint>
21 public:
22 /**
23 * Constructs a new EllipticalRegionConstraint.
24 *
25 * @param center The center of the ellipse in which to enforce the constraint.
26 * @param xWidth The width of the ellipse in which to enforce the constraint.
27 * @param yWidth The height of the ellipse in which to enforce the constraint.
28 * @param rotation The rotation to apply to all radii around the origin.
29 * @param constraint The constraint to enforce when the robot is within the
30 * region.
31 */
32 EllipticalRegionConstraint(const Translation2d& center, units::meter_t xWidth,
33 units::meter_t yWidth, const Rotation2d& rotation,
34 const Constraint& constraint)
35 : m_center(center),
36 m_radii(xWidth / 2.0, yWidth / 2.0),
37 m_constraint(constraint) {
38 m_radii = m_radii.RotateBy(rotation);
39 }
40
41 units::meters_per_second_t MaxVelocity(
42 const Pose2d& pose, units::curvature_t curvature,
43 units::meters_per_second_t velocity) const override {
44 if (IsPoseInRegion(pose)) {
45 return m_constraint.MaxVelocity(pose, curvature, velocity);
46 } else {
47 return units::meters_per_second_t{
48 std::numeric_limits<double>::infinity()};
49 }
50 }
51
53 units::meters_per_second_t speed) const override {
54 if (IsPoseInRegion(pose)) {
55 return m_constraint.MinMaxAcceleration(pose, curvature, speed);
56 } else {
57 return {};
58 }
59 }
60
61 /**
62 * Returns whether the specified robot pose is within the region that the
63 * constraint is enforced in.
64 *
65 * @param pose The robot pose.
66 * @return Whether the robot pose is within the constraint region.
67 */
68 bool IsPoseInRegion(const Pose2d& pose) const {
69 // The region bounded by the ellipse is given by the equation:
70 //
71 // (x−h)²/Rx² + (y−k)²/Ry² ≤ 1
72 //
73 // Multiply by Rx²Ry² for efficiency reasons:
74 //
75 // (x−h)²Ry² + (y−k)²Rx² ≤ Rx²Ry²
76 //
77 // If the inequality is satisfied, then it is inside the ellipse; otherwise
78 // it is outside the ellipse.
79 return units::math::pow<2>(pose.X() - m_center.X()) *
80 units::math::pow<2>(m_radii.Y()) +
81 units::math::pow<2>(pose.Y() - m_center.Y()) *
82 units::math::pow<2>(m_radii.X()) <=
83 units::math::pow<2>(m_radii.X()) * units::math::pow<2>(m_radii.Y());
84 }
85
86 private:
87 Translation2d m_center;
88 Translation2d m_radii;
89 Constraint m_constraint;
90};
91} // namespace frc
Enforces a particular constraint only within an elliptical region.
Definition: EllipticalRegionConstraint.h:20
bool IsPoseInRegion(const Pose2d &pose) const
Returns whether the specified robot pose is within the region that the constraint is enforced in.
Definition: EllipticalRegionConstraint.h:68
MinMax MinMaxAcceleration(const Pose2d &pose, units::curvature_t curvature, units::meters_per_second_t speed) const override
Returns the minimum and maximum allowable acceleration for the trajectory given pose,...
Definition: EllipticalRegionConstraint.h:52
EllipticalRegionConstraint(const Translation2d &center, units::meter_t xWidth, units::meter_t yWidth, const Rotation2d &rotation, const Constraint &constraint)
Constructs a new EllipticalRegionConstraint.
Definition: EllipticalRegionConstraint.h:32
units::meters_per_second_t MaxVelocity(const Pose2d &pose, units::curvature_t curvature, units::meters_per_second_t velocity) const override
Returns the max velocity given the current pose and curvature.
Definition: EllipticalRegionConstraint.h:41
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition: Pose2d.h:96
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition: Pose2d.h:89
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:23
An interface for defining user-defined velocity and acceleration constraints while generating traject...
Definition: TrajectoryConstraint.h:22
Represents a translation in 2D space.
Definition: Translation2d.h:27
constexpr units::meter_t X() const
Returns the X component of the translation.
Definition: Translation2d.h:76
constexpr Translation2d RotateBy(const Rotation2d &other) const
Applies a rotation to the translation in 2D space.
Definition: Translation2d.inc:27
constexpr units::meter_t Y() const
Returns the Y component of the translation.
Definition: Translation2d.h:83
Definition: AprilTagPoseEstimator.h:15
Represents a minimum and maximum acceleration.
Definition: TrajectoryConstraint.h:37