WPILibC++ 2024.3.2
SwerveDrivePoseEstimator.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 <cmath>
8
9#include <wpi/SymbolExports.h>
10#include <wpi/array.h>
11
13#include "frc/geometry/Pose2d.h"
18#include "units/time.h"
19
20namespace frc {
21
22/**
23 * This class wraps Swerve Drive Odometry to fuse latency-compensated
24 * vision measurements with swerve drive encoder distance measurements. It is
25 * intended to be a drop-in for SwerveDriveOdometry.
26 *
27 * Update() should be called every robot loop.
28 *
29 * AddVisionMeasurement() can be called as infrequently as you want; if you
30 * never call it, then this class will behave as regular encoder
31 * odometry.
32 */
33template <size_t NumModules>
35 : public PoseEstimator<SwerveDriveWheelSpeeds<NumModules>,
36 SwerveDriveWheelPositions<NumModules>> {
37 public:
38 /**
39 * Constructs a SwerveDrivePoseEstimator with default standard deviations
40 * for the model and vision measurements.
41 *
42 * The default standard deviations of the model states are
43 * 0.1 meters for x, 0.1 meters for y, and 0.1 radians for heading.
44 * The default standard deviations of the vision measurements are
45 * 0.9 meters for x, 0.9 meters for y, and 0.9 radians for heading.
46 *
47 * @param kinematics A correctly-configured kinematics object for your
48 * drivetrain.
49 * @param gyroAngle The current gyro angle.
50 * @param modulePositions The current distance and rotation measurements of
51 * the swerve modules.
52 * @param initialPose The starting pose estimate.
53 */
56 const Rotation2d& gyroAngle,
58 const Pose2d& initialPose)
59 : SwerveDrivePoseEstimator{kinematics, gyroAngle,
60 modulePositions, initialPose,
61 {0.1, 0.1, 0.1}, {0.9, 0.9, 0.9}} {}
62
63 /**
64 * Constructs a SwerveDrivePoseEstimator.
65 *
66 * @param kinematics A correctly-configured kinematics object for your
67 * drivetrain.
68 * @param gyroAngle The current gyro angle.
69 * @param modulePositions The current distance and rotation measurements of
70 * the swerve modules.
71 * @param initialPose The starting pose estimate.
72 * @param stateStdDevs Standard deviations of the pose estimate (x position in
73 * meters, y position in meters, and heading in radians). Increase these
74 * numbers to trust your state estimate less.
75 * @param visionMeasurementStdDevs Standard deviations of the vision pose
76 * measurement (x position in meters, y position in meters, and heading in
77 * radians). Increase these numbers to trust the vision pose measurement
78 * less.
79 */
82 const Rotation2d& gyroAngle,
84 const Pose2d& initialPose, const wpi::array<double, 3>& stateStdDevs,
85 const wpi::array<double, 3>& visionMeasurementStdDevs)
87 SwerveDriveWheelPositions<NumModules>>(
88 kinematics, m_odometryImpl, stateStdDevs, visionMeasurementStdDevs),
89 m_odometryImpl{kinematics, gyroAngle, modulePositions, initialPose} {}
90
91 /**
92 * Resets the robot's position on the field.
93 *
94 * The gyroscope angle does not need to be reset in the user's robot code.
95 * The library automatically takes care of offsetting the gyro angle.
96 *
97 * @param gyroAngle The angle reported by the gyroscope.
98 * @param modulePositions The current distance and rotation measurements of
99 * the swerve modules.
100 * @param pose The position on the field that your robot is at.
101 */
103 const Rotation2d& gyroAngle,
105 const Pose2d& pose) {
108 SwerveDriveWheelPositions<NumModules>>::ResetPosition(gyroAngle,
109 {modulePositions},
110 pose);
111 }
112
113 /**
114 * Updates the pose estimator with wheel encoder and gyro information. This
115 * should be called every loop.
116 *
117 * @param gyroAngle The current gyro angle.
118 * @param modulePositions The current distance and rotation measurements of
119 * the swerve modules.
120 * @return The estimated robot pose in meters.
121 */
123 const Rotation2d& gyroAngle,
124 const wpi::array<SwerveModulePosition, NumModules>& modulePositions) {
125 return PoseEstimator<
127 SwerveDriveWheelPositions<NumModules>>::Update(gyroAngle,
128 {modulePositions});
129 }
130
131 /**
132 * Updates the pose estimator with wheel encoder and gyro information. This
133 * should be called every loop.
134 *
135 * @param currentTime Time at which this method was called, in seconds.
136 * @param gyroAngle The current gyro angle.
137 * @param modulePositions The current distance traveled and rotations of
138 * the swerve modules.
139 * @return The estimated robot pose in meters.
140 */
142 units::second_t currentTime, const Rotation2d& gyroAngle,
143 const wpi::array<SwerveModulePosition, NumModules>& modulePositions) {
146 UpdateWithTime(currentTime, gyroAngle, {modulePositions});
147 }
148
149 private:
150 SwerveDriveOdometry<NumModules> m_odometryImpl;
151};
152
153extern template class EXPORT_TEMPLATE_DECLARE(WPILIB_DLLEXPORT)
154 SwerveDrivePoseEstimator<4>;
155
156} // namespace frc
#define EXPORT_TEMPLATE_DECLARE(export)
Definition: SymbolExports.h:94
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
This class wraps odometry to fuse latency-compensated vision measurements with encoder measurements.
Definition: PoseEstimator.h:38
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:23
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual module ...
Definition: SwerveDriveKinematics.h:56
Class for swerve drive odometry.
Definition: SwerveDriveOdometry.h:36
This class wraps Swerve Drive Odometry to fuse latency-compensated vision measurements with swerve dr...
Definition: SwerveDrivePoseEstimator.h:36
Pose2d Update(const Rotation2d &gyroAngle, const wpi::array< SwerveModulePosition, NumModules > &modulePositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition: SwerveDrivePoseEstimator.h:122
void ResetPosition(const Rotation2d &gyroAngle, const wpi::array< SwerveModulePosition, NumModules > &modulePositions, const Pose2d &pose)
Resets the robot's position on the field.
Definition: SwerveDrivePoseEstimator.h:102
SwerveDrivePoseEstimator(SwerveDriveKinematics< NumModules > &kinematics, const Rotation2d &gyroAngle, const wpi::array< SwerveModulePosition, NumModules > &modulePositions, const Pose2d &initialPose, const wpi::array< double, 3 > &stateStdDevs, const wpi::array< double, 3 > &visionMeasurementStdDevs)
Constructs a SwerveDrivePoseEstimator.
Definition: SwerveDrivePoseEstimator.h:80
Pose2d UpdateWithTime(units::second_t currentTime, const Rotation2d &gyroAngle, const wpi::array< SwerveModulePosition, NumModules > &modulePositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition: SwerveDrivePoseEstimator.h:141
SwerveDrivePoseEstimator(SwerveDriveKinematics< NumModules > &kinematics, const Rotation2d &gyroAngle, const wpi::array< SwerveModulePosition, NumModules > &modulePositions, const Pose2d &initialPose)
Constructs a SwerveDrivePoseEstimator with default standard deviations for the model and vision measu...
Definition: SwerveDrivePoseEstimator.h:54
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
Definition: AprilTagPoseEstimator.h:15
Represents the wheel positions for a swerve drive drivetrain.
Definition: SwerveDriveWheelPositions.h:18