SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
TurnToPose.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/controller/HolonomicDriveController.h>
4#include <frc/controller/PIDController.h>
5#include <frc/controller/ProfiledPIDController.h>
6#include <frc/geometry/Pose2d.h>
7#include <frc/geometry/Rotation2d.h>
8#include <frc/kinematics/ChassisSpeeds.h>
9#include <frc/kinematics/SwerveDriveKinematics.h>
10#include <frc/smartdashboard/Field2d.h>
11
12#include <functional>
13#include <memory>
14
16
17namespace subzero {
18
23class TurnToPose : public ITurnToTarget {
24public:
26 frc::TrapezoidProfile<units::radians>::Constraints rotationConstraints;
27 double turnP;
28 double turnI;
29 double turnD;
37 frc::Pose2d poseTolerance;
38 };
39
48 std::function<frc::Pose2d()> poseGetter,
49 std::function<frc::Field2d *()> fieldGetter);
50
56 void Update() override;
57
63 void SetTargetPose(frc::Pose2d pose);
64
71 void SetTargetAngleRelative(units::degree_t angle);
72
79 void SetTargetAngleAbsolute(units::degree_t angle);
80
86 frc::ChassisSpeeds GetSpeedCorrection() override;
87
94 static units::degree_t GetAngleFromOtherPose(const frc::Pose2d &,
95 const frc::Pose2d &);
96
105 frc::ChassisSpeeds BlendWithInput(const frc::ChassisSpeeds &other,
106 double correctionFactor) override;
107
114 inline bool AtGoal() override { return m_driveController->AtReference(); }
115
116 inline std::optional<frc::Pose2d> GetTargetPose() const {
117 return m_targetPose;
118 }
119
120 inline std::optional<units::degree_t> GetTargetAngle() const {
121 return m_targetAngle;
122 }
123
124 inline units::degree_t GetTargetHeading() const { return m_targetHeading; }
125
136 static double NormalizeScalar(double x, double from_min, double from_max,
137 double to_min, double to_max) {
138 return (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min;
139 }
140
141private:
142 TurnToPoseConfig m_config;
143 std::function<frc::Pose2d()> m_poseGetter;
144 std::function<frc::Field2d *()> m_fieldGetter;
145 std::unique_ptr<frc::HolonomicDriveController> m_driveController;
146 frc::Pose2d m_startPose;
147 std::optional<frc::Pose2d> m_targetPose;
148 std::optional<units::degree_t> m_targetAngle;
149 units::degree_t m_targetHeading;
150 frc::ChassisSpeeds m_speeds;
151};
152} // namespace subzero
Interface for classes that move towards a target while maintaining driver input.
Allows the robot/an axis to face an arbitrary Pose2d.
Definition TurnToPose.h:23
bool AtGoal() override
Check if the robot is at the goal angle.
Definition TurnToPose.h:114
void SetTargetAngleRelative(units::degree_t angle)
Set the angle the robot/axis should be facing relative to its current rotation.
void SetTargetAngleAbsolute(units::degree_t angle)
Set the angle the robot/axis should be facing relative to the field's coordinate system.
std::optional< units::degree_t > GetTargetAngle() const
Definition TurnToPose.h:120
static units::degree_t GetAngleFromOtherPose(const frc::Pose2d &, const frc::Pose2d &)
Helper to calculate the relative angle between two poses.
std::optional< frc::Pose2d > GetTargetPose() const
Definition TurnToPose.h:116
void SetTargetPose(frc::Pose2d pose)
Set the pose the robot/axis should be facing.
frc::ChassisSpeeds BlendWithInput(const frc::ChassisSpeeds &other, double correctionFactor) override
Combines two ChassisSpeeds into a single result via a linear regression and coefficient.
units::degree_t GetTargetHeading() const
Definition TurnToPose.h:124
void Update() override
Call this every loop to get the latest angle relative to the robot's position.
TurnToPose(TurnToPoseConfig config, std::function< frc::Pose2d()> poseGetter, std::function< frc::Field2d *()> fieldGetter)
Construct a new TurnToPose instance.
frc::ChassisSpeeds GetSpeedCorrection() override
Get the Speed Correction object.
static double NormalizeScalar(double x, double from_min, double from_max, double to_min, double to_max)
Helper to normalize a value to a new range.
Definition TurnToPose.h:136
frc::TrapezoidProfile< units::radians >::Constraints rotationConstraints
Definition TurnToPose.h:26
frc::Pose2d poseTolerance
A Pose2d within this range will be considered "at-goal".
Definition TurnToPose.h:37