WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DifferentialDrivePoseEstimator.hpp
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
12#include "wpi/units/time.hpp"
14#include "wpi/util/array.hpp"
15
16namespace wpi::math {
17/**
18 * This class wraps Differential Drive Odometry to fuse latency-compensated
19 * vision measurements with differential drive encoder measurements. It will
20 * correct for noisy vision measurements and encoder drift. It is intended to be
21 * an easy drop-in for DifferentialDriveOdometry. In fact, if you never call
22 * AddVisionMeasurement(), and only call Update(), this will behave exactly the
23 * same as DifferentialDriveOdometry.
24 *
25 * Update() should be called every robot loop (if your robot loops are faster or
26 * slower than the default of 20 ms, then you should change the nominal delta
27 * time via the constructor).
28 *
29 * AddVisionMeasurement() can be called as infrequently as you want; if you
30 * never call it, then this class will behave like regular encoder odometry.
31 */
33 : public PoseEstimator<DifferentialDriveWheelPositions,
34 DifferentialDriveWheelVelocities,
35 DifferentialDriveWheelAccelerations> {
36 public:
37 /**
38 * Constructs a DifferentialDrivePoseEstimator with default standard
39 * deviations for the model and vision measurements.
40 *
41 * The default standard deviations of the model states are
42 * 0.02 meters for x, 0.02 meters for y, and 0.01 radians for heading.
43 * The default standard deviations of the vision measurements are
44 * 0.1 meters for x, 0.1 meters for y, and 0.1 radians for heading.
45 *
46 * @param kinematics A correctly-configured kinematics object for your
47 * drivetrain.
48 * @param gyroAngle The gyro angle of the robot.
49 * @param leftDistance The distance traveled by the left encoder.
50 * @param rightDistance The distance traveled by the right encoder.
51 * @param initialPose The estimated initial pose.
52 */
54 const Rotation2d& gyroAngle,
55 wpi::units::meter_t leftDistance,
56 wpi::units::meter_t rightDistance,
57 const Pose2d& initialPose);
58
59 /**
60 * Constructs a DifferentialDrivePoseEstimator.
61 *
62 * @param kinematics A correctly-configured kinematics object for your
63 * drivetrain.
64 * @param gyroAngle The gyro angle of the robot.
65 * @param leftDistance The distance traveled by the left encoder.
66 * @param rightDistance The distance traveled by the right encoder.
67 * @param initialPose The estimated initial pose.
68 * @param stateStdDevs Standard deviations of the pose estimate (x position in
69 * meters, y position in meters, and heading in radians). Increase these
70 * numbers to trust your state estimate less.
71 * @param visionMeasurementStdDevs Standard deviations of the vision pose
72 * measurement (x position in meters, y position in meters, and heading in
73 * radians). Increase these numbers to trust the vision pose measurement
74 * less.
75 */
77 DifferentialDriveKinematics& kinematics, const Rotation2d& gyroAngle,
78 wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance,
79 const Pose2d& initialPose,
80 const wpi::util::array<double, 3>& stateStdDevs,
81 const wpi::util::array<double, 3>& visionMeasurementStdDevs);
82
83 /**
84 * Resets the robot's position on the field.
85 *
86 * @param gyroAngle The current gyro angle.
87 * @param leftDistance The distance traveled by the left encoder.
88 * @param rightDistance The distance traveled by the right encoder.
89 * @param pose The estimated pose of the robot on the field.
90 */
91 void ResetPosition(const Rotation2d& gyroAngle,
92 wpi::units::meter_t leftDistance,
93 wpi::units::meter_t rightDistance, const Pose2d& pose) {
94 PoseEstimator::ResetPosition(gyroAngle, {leftDistance, rightDistance},
95 pose);
96 }
97
98 /**
99 * Updates the pose estimator with wheel encoder and gyro information. This
100 * should be called every loop.
101 *
102 * @param gyroAngle The current gyro angle.
103 * @param leftDistance The distance traveled by the left encoder.
104 * @param rightDistance The distance traveled by the right encoder.
105 *
106 * @return The estimated pose of the robot.
107 */
108 Pose2d Update(const Rotation2d& gyroAngle, wpi::units::meter_t leftDistance,
109 wpi::units::meter_t rightDistance) {
110 return PoseEstimator::Update(gyroAngle, {leftDistance, rightDistance});
111 }
112
113 /**
114 * Updates the pose estimator with wheel encoder and gyro information. This
115 * should be called every loop.
116 *
117 * @param currentTime The time at which this method was called.
118 * @param gyroAngle The current gyro angle.
119 * @param leftDistance The distance traveled by the left encoder.
120 * @param rightDistance The distance traveled by the right encoder.
121 *
122 * @return The estimated pose of the robot.
123 */
124 Pose2d UpdateWithTime(wpi::units::second_t currentTime,
125 const Rotation2d& gyroAngle,
126 wpi::units::meter_t leftDistance,
127 wpi::units::meter_t rightDistance) {
128 return PoseEstimator::UpdateWithTime(currentTime, gyroAngle,
129 {leftDistance, rightDistance});
130 }
131
132 private:
133 DifferentialDriveOdometry m_odometryImpl;
134};
135
136} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Helper class that converts a chassis velocity (dx and dtheta components) to left and right wheel velo...
Definition DifferentialDriveKinematics.hpp:33
Class for differential drive odometry.
Definition DifferentialDriveOdometry.hpp:31
DifferentialDrivePoseEstimator(DifferentialDriveKinematics &kinematics, const Rotation2d &gyroAngle, wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance, const Pose2d &initialPose)
Constructs a DifferentialDrivePoseEstimator with default standard deviations for the model and vision...
void ResetPosition(const Rotation2d &gyroAngle, wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance, const Pose2d &pose)
Resets the robot's position on the field.
Definition DifferentialDrivePoseEstimator.hpp:91
Pose2d UpdateWithTime(wpi::units::second_t currentTime, const Rotation2d &gyroAngle, wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance)
Updates the pose estimator with wheel encoder and gyro information.
Definition DifferentialDrivePoseEstimator.hpp:124
DifferentialDrivePoseEstimator(DifferentialDriveKinematics &kinematics, const Rotation2d &gyroAngle, wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance, const Pose2d &initialPose, const wpi::util::array< double, 3 > &stateStdDevs, const wpi::util::array< double, 3 > &visionMeasurementStdDevs)
Constructs a DifferentialDrivePoseEstimator.
Pose2d Update(const Rotation2d &gyroAngle, wpi::units::meter_t leftDistance, wpi::units::meter_t rightDistance)
Updates the pose estimator with wheel encoder and gyro information.
Definition DifferentialDrivePoseEstimator.hpp:108
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
Pose2d UpdateWithTime(wpi::units::second_t currentTime, const Rotation2d &gyroAngle, const WheelPositions &wheelPositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition PoseEstimator.hpp:409
void ResetPosition(const Rotation2d &gyroAngle, const WheelPositions &wheelPositions, const Pose2d &pose)
Resets the robot's position on the field.
Definition PoseEstimator.hpp:121
Pose2d Update(const Rotation2d &gyroAngle, const WheelPositions &wheelPositions)
Updates the pose estimator with wheel encoder and gyro information.
Definition PoseEstimator.hpp:393
PoseEstimator(Kinematics< DifferentialDriveWheelPositions, DifferentialDriveWheelVelocities, DifferentialDriveWheelAccelerations > &kinematics, Odometry< DifferentialDriveWheelPositions, DifferentialDriveWheelVelocities, DifferentialDriveWheelAccelerations > &odometry, const wpi::util::array< double, 3 > &stateStdDevs, const wpi::util::array< double, 3 > &visionMeasurementStdDevs)
Definition PoseEstimator.hpp:66
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition Rotation2d.hpp:26
This class is a wrapper around std::array that does compile time size checking.
Definition array.hpp:26
Definition LinearSystem.hpp:20