40 Rotation2d m_previousAngle;
41 Rotation2d m_gyroOffset;
53 m_kinematics{std::move(kinematics)},
54 m_pose{std::move(initialPose)},
55 m_previousWheelPositions{std::move(modulePositions)},
56 m_previousAngle{m_pose.Rotation()},
57 m_gyroOffset{(-gyroAngle).RotateBy(m_pose.Rotation())}
73 m_previousAngle = m_pose.Rotation();
74 m_gyroOffset = (-gyroAngle).RotateBy(m_pose.Rotation());
75 m_previousWheelPositions = std::move(wheelPositions);
85 m_gyroOffset = m_gyroOffset.RotateBy(-m_pose.Rotation()).RotateBy(pose.Rotation());
87 m_previousAngle = pose.Rotation();
97 m_pose = Pose2d{translation, m_pose.Rotation()};
107 m_gyroOffset = m_gyroOffset.RotateBy(-m_pose.Rotation()).RotateBy(rotation);
108 m_pose = Pose2d{m_pose.Translation(), rotation};
109 m_previousAngle = rotation;
117 Pose2d
const &
GetPose()
const {
return m_pose; }
150 struct VisionUpdate {
165 Pose2d Compensate(
const Pose2d& pose)
const
167 auto const delta = pose - odometryPose;
168 return visionPose + delta;
172 static constexpr wpi::units::second_t kBufferDuration = 1.5_s;
175 std::array<double, 3> m_q{};
176 std::array<double, 3> m_r{};
178 struct VisionMatrices;
179 std::unique_ptr<VisionMatrices> m_matrices;
182 TimeInterpolatableBuffer<Pose2d> m_odometryPoseBuffer{kBufferDuration};
190 std::map<wpi::units::second_t, VisionUpdate> m_visionUpdates;
192 Pose2d m_poseEstimate;
197 void CleanUpVisionUpdates();
202 void UpdateVisionMatrices();
224 {0.1, 0.1, 0.1}, {0.9, 0.9, 0.9}}
246 std::array<double, 3>
const &stateStdDevs, std::array<double, 3>
const &visionMeasurementStdDevs);
260 for (
size_t i = 0; i < m_q.size(); ++i) {
261 m_q[i] = stateStdDevs[i] * stateStdDevs[i];
263 UpdateVisionMatrices();
278 for (
size_t i = 0; i < m_r.size(); ++i) {
279 m_r[i] = visionMeasurementStdDevs[i] * visionMeasurementStdDevs[i];
281 UpdateVisionMatrices();
296 m_odometry.ResetPosition(gyroAngle, std::move(wheelPositions), pose);
297 m_odometryPoseBuffer.Clear();
298 m_visionUpdates.clear();
299 m_poseEstimate = m_odometry.GetPose();
309 m_odometry.ResetPose(pose);
310 m_odometryPoseBuffer.Clear();
311 m_visionUpdates.clear();
312 m_poseEstimate = m_odometry.GetPose();
322 m_odometry.ResetTranslation(translation);
324 std::optional<std::pair<wpi::units::second_t, VisionUpdate>> latestVisionUpdate =
325 m_visionUpdates.empty() ? std::nullopt : std::optional{*m_visionUpdates.crbegin()};
326 m_odometryPoseBuffer.Clear();
327 m_visionUpdates.clear();
329 if (latestVisionUpdate) {
330 VisionUpdate
const visionUpdate{
331 Pose2d{translation, latestVisionUpdate->second.visionPose.Rotation()},
332 Pose2d{translation, latestVisionUpdate->second.odometryPose.Rotation()}
334 m_visionUpdates[latestVisionUpdate->first] = visionUpdate;
335 m_poseEstimate = visionUpdate.Compensate(m_odometry.GetPose());
337 m_poseEstimate = m_odometry.GetPose();
348 m_odometry.ResetRotation(rotation);
350 std::optional<std::pair<wpi::units::second_t, VisionUpdate>> latestVisionUpdate =
351 m_visionUpdates.empty() ? std::nullopt : std::optional{*m_visionUpdates.crbegin()};
352 m_odometryPoseBuffer.Clear();
353 m_visionUpdates.clear();
355 if (latestVisionUpdate) {
356 VisionUpdate
const visionUpdate{
357 Pose2d{latestVisionUpdate->second.visionPose.Translation(), rotation},
358 Pose2d{latestVisionUpdate->second.odometryPose.Translation(), rotation}
360 m_visionUpdates[latestVisionUpdate->first] = visionUpdate;
361 m_poseEstimate = visionUpdate.Compensate(m_odometry.GetPose());
363 m_poseEstimate = m_odometry.GetPose();
374 return m_poseEstimate;
384 std::optional<Pose2d>
SampleAt(wpi::units::second_t timestamp)
const;
437 std::array<double, 3>
const &visionMeasurementStdDevs)
469 auto odometryEstimate = m_odometry.Update(gyroAngle, wheelPositions);
471 m_odometryPoseBuffer.AddSample(currentTime, odometryEstimate);
473 if (m_visionUpdates.empty()) {
474 m_poseEstimate = std::move(odometryEstimate);
476 auto const visionUpdate = m_visionUpdates.rbegin()->second;
477 m_poseEstimate = visionUpdate.Compensate(odometryEstimate);
Class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states ...
Definition SwerveDriveKinematics.hpp:41
Class for swerve drive odometry.
Definition SwerveDrivePoseEstimator.hpp:29
void ResetPosition(Rotation2d const &gyroAngle, WheelPositions wheelPositions, Pose2d const &pose)
Resets the robot's position on the field.
Definition SwerveDrivePoseEstimator.hpp:70
std::vector< SwerveModulePosition > WheelPositions
Definition SwerveDrivePoseEstimator.hpp:31
Pose2d const & GetPose() const
Returns the position of the robot on the field.
Definition SwerveDrivePoseEstimator.hpp:117
void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the pose.
Definition SwerveDrivePoseEstimator.hpp:105
Pose2d const & Update(Rotation2d const &gyroAngle, WheelPositions wheelPositions)
Updates the robot's position on the field using forward kinematics and integration of the pose over t...
std::vector< SwerveModuleVelocity > WheelVelocities
Definition SwerveDrivePoseEstimator.hpp:32
std::vector< SwerveModuleAcceleration > WheelAccelerations
Definition SwerveDrivePoseEstimator.hpp:33
void ResetTranslation(Translation2d const &translation)
Resets the translation of the pose.
Definition SwerveDrivePoseEstimator.hpp:95
void ResetPose(Pose2d const &pose)
Resets the pose.
Definition SwerveDrivePoseEstimator.hpp:83
SwerveDriveOdometry(SwerveDriveKinematics kinematics, Rotation2d const &gyroAngle, WheelPositions modulePositions, Pose2d initialPose=Pose2d{})
Constructs a SwerveDriveOdometry object.
Definition SwerveDrivePoseEstimator.hpp:52
~SwerveDrivePoseEstimator()
void ResetPosition(Rotation2d const &gyroAngle, WheelPositions wheelPositions, Pose2d const &pose)
Resets the robot's position on the field.
Definition SwerveDrivePoseEstimator.hpp:294
Pose2d UpdateWithTime(wpi::units::second_t currentTime, Rotation2d const &gyroAngle, WheelPositions const &wheelPositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition SwerveDrivePoseEstimator.hpp:467
SwerveDrivePoseEstimator(SwerveDriveKinematics const &kinematics, Rotation2d const &gyroAngle, WheelPositions modulePositions, Pose2d initialPose, std::array< double, 3 > const &stateStdDevs, std::array< double, 3 > const &visionMeasurementStdDevs)
Constructs a SwerveDrivePoseEstimator.
Pose2d GetEstimatedPosition() const
Gets the estimated robot pose.
Definition SwerveDrivePoseEstimator.hpp:372
SwerveDriveOdometry::WheelPositions WheelPositions
Definition SwerveDrivePoseEstimator.hpp:145
void ResetTranslation(Translation2d const &translation)
Resets the robot's translation.
Definition SwerveDrivePoseEstimator.hpp:320
std::optional< Pose2d > SampleAt(wpi::units::second_t timestamp) const
Return the pose at a given timestamp, if the buffer is not empty.
void ResetPose(Pose2d const &pose)
Resets the robot's pose.
Definition SwerveDrivePoseEstimator.hpp:307
SwerveDrivePoseEstimator(SwerveDriveKinematics const &kinematics, Rotation2d const &gyroAngle, WheelPositions modulePositions, Pose2d initialPose)
Constructs a SwerveDrivePoseEstimator with default standard deviations for the model and vision measu...
Definition SwerveDrivePoseEstimator.hpp:221
void AddVisionMeasurement(Pose2d const &visionRobotPose, wpi::units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
void SetStateStdDevs(std::array< double, 3 > const &stateStdDevs)
Sets the pose estimator's trust in robot odometry.
Definition SwerveDrivePoseEstimator.hpp:258
SwerveDriveOdometry::WheelAccelerations WheelAccelerations
Definition SwerveDrivePoseEstimator.hpp:147
SwerveDriveOdometry::WheelVelocities WheelVelocities
Definition SwerveDrivePoseEstimator.hpp:146
void ResetRotation(Rotation2d const &rotation)
Resets the robot's rotation.
Definition SwerveDrivePoseEstimator.hpp:346
void AddVisionMeasurement(Pose2d const &visionRobotPose, wpi::units::second_t timestamp, std::array< double, 3 > const &visionMeasurementStdDevs)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivePoseEstimator.hpp:436
void SetVisionMeasurementStdDevs(std::array< double, 3 > const &visionMeasurementStdDevs)
Sets the pose estimator's trust in vision measurements.
Definition SwerveDrivePoseEstimator.hpp:276
Pose2d Update(Rotation2d const &gyroAngle, WheelPositions const &wheelPositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition SwerveDrivePoseEstimator.hpp:452
Definition SwerveDrivetrainImpl.hpp:21
Definition SwerveModule.hpp:28
wpi::units::second_t GetCurrentTime()
Get the current timestamp.
Definition Utils.hpp:27
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14