Valor 6800 1.0
Loading...
Searching...
No Matches
AprilTagsSensor.h
1
2#pragma once
3
4#include <memory>
5#include <string>
6#include <unordered_set>
7
8#include <frc/apriltag/AprilTagFieldLayout.h>
9#include <frc/estimator/PoseEstimator.h>
10#include <frc/estimator/SwerveDrivePoseEstimator.h>
11#include <frc/geometry/Pose3d.h>
12#include <frc/geometry/Rotation3d.h>
13#include <networktables/NetworkTableEntry.h>
14#include <networktables/NetworkTableInstance.h>
15#include <units/angle.h>
16#include <units/angular_velocity.h>
17#include <units/length.h>
18#include <units/time.h>
19#include <units/velocity.h>
20
21#include "Eigen/Core"
22#include "frc/geometry/Twist3d.h"
23#include "valkyrie/sensors/VisionSensor.h"
24
25namespace valor {
26
35 public:
36 enum class IMUMode {
37 EXTERNAL_ONLY = 0,
38 EXTERNAL_SEED = 1,
39 INTERNAL_ONLY = 2,
40 INTERNAL_MT1_ASSIST = 3,
41 INTERNAL_EXTERNAL_ASSIST = 4,
42 };
43
44 enum class Solver {
45 MT1,
46 MT2,
47 };
48
49 struct Config {
50 // Field border margin for field border rejection
51 units::meter_t fieldBorderMargin = 0.5_m;
52
53 // Power-law std dev coefficients
54 double translationalStdDevCoefficient = 0.01;
55 double rotationalStdDevCoefficient = 0.03;
56 double perTagFactor = 1;
57
58 // Stale measurement rejection and MT1 ambiguity
59 units::second_t maxMeasurementAge = 2.0_s;
60 double ambiguityThreshold = 0.4;
61 };
62
70 AprilTagsSensor(frc::TimedRobot& robot, std::string name, frc::Pose3d _cameraPose, frc::AprilTagField field,
71 std::optional<Config> config = std::nullopt);
72
78 int getTagID();
79
85 void LoggablePeriodic() override;
86
97 void applyVisionMeasurement(frc::SwerveDrivePoseEstimator<4>* estimator);
98
104 frc::Pose3d getPoseFromAprilTag();
105
110 struct Orientation {
114 Eigen::Vector3<units::degree_t> theta;
115
119 Eigen::Vector3<units::degrees_per_second_t> omega;
120 };
121
129
135 frc::Pose3d getTargetToBotPose();
136
143
144 void setupSim(std::function<frc::Pose2d()> robotPoseGetter, std::unordered_set<int> tagIDs, units::degree_t horizontalFOV,
145 units::degree_t verticalFOV, units::meter_t maxDistance = units::meter_t{std::numeric_limits<double>::infinity()},
146 units::degree_t viewableAngle = 70_deg);
147
148 void setIMUMode(IMUMode);
149 void setSolver(Solver);
150 constexpr Solver getSolver() const { return solver; }
151 void setIMUDoubt(double doubt);
152
153 void setOrientation(Orientation orientation);
154
155 constexpr Config getConfig() const { return config; }
156 constexpr void setConfig(Config c) { config = c; }
157
158 protected:
159 struct SimState {
160 std::function<frc::Pose2d()> robotPoseGetter;
161 std::unordered_set<int> tagIDs;
162 units::degree_t horizontalFOV;
163 units::degree_t verticalFOV;
164 units::meter_t maxDistance;
165 units::degree_t viewableAngle;
166 };
167
168 void setGetter(std::function<frc::Pose3d()> getter);
169
170 std::unique_ptr<SimState> simState;
171
172 private:
173 void simulationLoop();
174
180 frc::Pose3d getGlobalPose() override;
181
182 Config config;
183
184 frc::Pose3d megaTag2Pose = frc::Pose3d();
185 units::meter_t distance{0_m};
186 Solver solver = Solver::MT1;
187 frc::AprilTagFieldLayout fieldLayout;
188};
189
190} // namespace valor
Sensor for detecting AprilTags and applying vision measurements.
Definition AprilTagsSensor.h:34
void LoggablePeriodic() override
Initialize the Sendable interface for telemetry.
void applyVisionMeasurement(frc::SwerveDrivePoseEstimator< 4 > *estimator)
Apply vision measurements to a pose estimator.
frc::Pose3d get_botpose_targetspace()
Get the robot's pose in the target's coordinate space.
frc::Pose3d getPoseFromAprilTag()
Get the pose of the detected AprilTag.
AprilTagsSensor(frc::TimedRobot &robot, std::string name, frc::Pose3d _cameraPose, frc::AprilTagField field, std::optional< Config > config=std::nullopt)
Construct a new AprilTagsSensor object.
frc::Pose3d getTargetToBotPose()
Get the pose of the target relative to the robot.
frc::Pose3d getMegaTagPose2(Orientation orient)
Get the pose of a MegaTag using orientation data.
int getTagID()
Get the ID of the detected AprilTag.
T ReadLog(std::string_view field, const T &defaultValue={})
Read a value from NetworkTables for the given field.
Definition Loggable.h:343
Base class for vision sensors, such as Limelight.
Definition VisionSensor.h:26
Definition AprilTagsSensor.h:49
Orientation data structure for AprilTags.
Definition AprilTagsSensor.h:110
Eigen::Vector3< units::degrees_per_second_t > omega
omega built in a way where it goes roll omega, pitch omega, yaw omega
Definition AprilTagsSensor.h:119
Eigen::Vector3< units::degree_t > theta
theta built in a way where it goes roll, pitch, yaw
Definition AprilTagsSensor.h:114
Definition AprilTagsSensor.h:159