WPILibC++ 2024.3.2
Odometry.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 <wpi/SymbolExports.h>
8
12
13namespace frc {
14/**
15 * Class for odometry. Robot code should not use this directly- Instead, use the
16 * particular type for your drivetrain (e.g., DifferentialDriveOdometry).
17 * Odometry allows you to track the robot's position on the field over a course
18 * of a match using readings from your wheel encoders.
19 *
20 * Teams can use odometry during the autonomous period for complex tasks like
21 * path following. Furthermore, odometry can be used for latency compensation
22 * when using computer-vision systems.
23 *
24 * @tparam WheelSpeeds Wheel speeds type.
25 * @tparam WheelPositions Wheel positions type.
26 */
27template <typename WheelSpeeds, WheelPositions WheelPositions>
29 public:
30 /**
31 * Constructs an Odometry object.
32 *
33 * @param kinematics The kinematics for your drivetrain.
34 * @param gyroAngle The angle reported by the gyroscope.
35 * @param wheelPositions The current distances measured by each wheel.
36 * @param initialPose The starting position of the robot on the field.
37 */
38 explicit Odometry(const Kinematics<WheelSpeeds, WheelPositions>& kinematics,
39 const Rotation2d& gyroAngle,
40 const WheelPositions& wheelPositions,
41 const Pose2d& initialPose = Pose2d{});
42
43 /**
44 * Resets the robot's position on the field.
45 *
46 * The gyroscope angle does not need to be reset here on the user's robot
47 * code. The library automatically takes care of offsetting the gyro angle.
48 *
49 * @param gyroAngle The angle reported by the gyroscope.
50 * @param wheelPositions The current distances measured by each wheel.
51 * @param pose The position on the field that your robot is at.
52 */
53 void ResetPosition(const Rotation2d& gyroAngle,
54 const WheelPositions& wheelPositions, const Pose2d& pose) {
55 m_pose = pose;
56 m_previousAngle = pose.Rotation();
57 m_gyroOffset = m_pose.Rotation() - gyroAngle;
58 m_previousWheelPositions = wheelPositions;
59 }
60
61 /**
62 * Returns the position of the robot on the field.
63 * @return The pose of the robot.
64 */
65 const Pose2d& GetPose() const { return m_pose; }
66
67 /**
68 * Updates the robot's position on the field using forward kinematics and
69 * integration of the pose over time. This method takes in an angle parameter
70 * which is used instead of the angular rate that is calculated from forward
71 * kinematics, in addition to the current distance measurement at each wheel.
72 *
73 * @param gyroAngle The angle reported by the gyroscope.
74 * @param wheelPositions The current distances measured by each wheel.
75 *
76 * @return The new pose of the robot.
77 */
78 const Pose2d& Update(const Rotation2d& gyroAngle,
79 const WheelPositions& wheelPositions);
80
81 private:
83 Pose2d m_pose;
84
85 WheelPositions m_previousWheelPositions;
86 Rotation2d m_previousAngle;
87 Rotation2d m_gyroOffset;
88};
89} // namespace frc
90
91#include "Odometry.inc"
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel s...
Definition: Kinematics.h:23
Class for odometry.
Definition: Odometry.h:28
void ResetPosition(const Rotation2d &gyroAngle, const WheelPositions &wheelPositions, const Pose2d &pose)
Resets the robot's position on the field.
Definition: Odometry.h:53
const Pose2d & GetPose() const
Returns the position of the robot on the field.
Definition: Odometry.h:65
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
constexpr const Rotation2d & Rotation() const
Returns the underlying rotation.
Definition: Pose2d.h:103
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:23
Definition: WheelPositions.h:11
Definition: AprilTagPoseEstimator.h:15