WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Odometry3d.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
13namespace wpi::math {
14
15/**
16 * Class for odometry. Robot code should not use this directly- Instead, use the
17 * particular type for your drivetrain (e.g., DifferentialDriveOdometry3d).
18 * Odometry allows you to track the robot's position on the field over a course
19 * of a match using readings from your wheel encoders.
20 *
21 * Teams can use odometry during the autonomous period for complex tasks like
22 * path following. Furthermore, odometry can be used for latency compensation
23 * when using computer-vision systems.
24 *
25 * @tparam WheelPositions Wheel positions type.
26 * @tparam WheelVelocities Wheel velocities type.
27 * @tparam WheelAccelerations Wheel accelerations type.
28 */
29template <typename WheelPositions, typename WheelVelocities,
30 typename WheelAccelerations>
32 public:
33 /**
34 * Constructs an Odometry3d object.
35 *
36 * @param kinematics The kinematics for your drivetrain.
37 * @param gyroAngle The angle reported by the gyroscope.
38 * @param wheelPositions The current distances measured by each wheel.
39 * @param initialPose The starting position of the robot on the field.
40 */
41 explicit Odometry3d(const Kinematics<WheelPositions, WheelVelocities,
42 WheelAccelerations>& kinematics,
43 const Rotation3d& gyroAngle,
44 const WheelPositions& wheelPositions,
45 const Pose3d& initialPose = Pose3d{})
46 : m_kinematics(kinematics),
47 m_pose(initialPose),
48 m_previousWheelPositions(wheelPositions) {
49 m_previousAngle = m_pose.Rotation();
50 // When applied extrinsically, m_gyroOffset cancels the
51 // current gyroAngle and then rotates to m_pose.Rotation()
52 m_gyroOffset = gyroAngle.Inverse().RotateBy(m_pose.Rotation());
53 }
54
55 /**
56 * Resets the robot's position on the field.
57 *
58 * The gyroscope angle does not need to be reset here on the user's robot
59 * code. The library automatically takes care of offsetting the gyro angle.
60 *
61 * @param gyroAngle The angle reported by the gyroscope.
62 * @param wheelPositions The current distances measured by each wheel.
63 * @param pose The position on the field that your robot is at.
64 */
65 void ResetPosition(const Rotation3d& gyroAngle,
66 const WheelPositions& wheelPositions, const Pose3d& pose) {
67 m_pose = pose;
68 m_previousAngle = pose.Rotation();
69 // When applied extrinsically, m_gyroOffset cancels the
70 // current gyroAngle and then rotates to m_pose.Rotation()
71 m_gyroOffset = gyroAngle.Inverse().RotateBy(m_pose.Rotation());
72 m_previousWheelPositions = wheelPositions;
73 }
74
75 /**
76 * Resets the pose.
77 *
78 * @param pose The pose to reset to.
79 */
80 void ResetPose(const Pose3d& pose) {
81 // Cancel the previous m_pose.Rotation() and then rotate to the new angle
82 m_gyroOffset = m_gyroOffset.RotateBy(m_pose.Rotation().Inverse())
83 .RotateBy(pose.Rotation());
84 m_pose = pose;
85 m_previousAngle = pose.Rotation();
86 }
87
88 /**
89 * Resets the translation of the pose.
90 *
91 * @param translation The translation to reset to.
92 */
93 void ResetTranslation(const Translation3d& translation) {
94 m_pose = Pose3d{translation, m_pose.Rotation()};
95 }
96
97 /**
98 * Resets the rotation of the pose.
99 *
100 * @param rotation The rotation to reset to.
101 */
102 void ResetRotation(const Rotation3d& rotation) {
103 // Cancel the previous m_pose.Rotation() and then rotate to the new angle
104 m_gyroOffset =
105 m_gyroOffset.RotateBy(m_pose.Rotation().Inverse()).RotateBy(rotation);
106 m_pose = Pose3d{m_pose.Translation(), rotation};
107 m_previousAngle = rotation;
108 }
109
110 /**
111 * Returns the position of the robot on the field.
112 * @return The pose of the robot.
113 */
114 const Pose3d& GetPose() const { return m_pose; }
115
116 /**
117 * Updates the robot's position on the field using forward kinematics and
118 * integration of the pose over time. This method takes in an angle parameter
119 * which is used instead of the angular rate that is calculated from forward
120 * kinematics, in addition to the current distance measurement at each wheel.
121 *
122 * @param gyroAngle The angle reported by the gyroscope.
123 * @param wheelPositions The current distances measured by each wheel.
124 *
125 * @return The new pose of the robot.
126 */
127 const Pose3d& Update(const Rotation3d& gyroAngle,
128 const WheelPositions& wheelPositions) {
129 auto angle = gyroAngle.RotateBy(m_gyroOffset);
130 auto angle_difference = angle.RelativeTo(m_previousAngle).ToVector();
131
132 auto twist2d =
133 m_kinematics.ToTwist2d(m_previousWheelPositions, wheelPositions);
134 Twist3d twist{twist2d.dx,
135 twist2d.dy,
136 0_m,
137 wpi::units::radian_t{angle_difference(0)},
138 wpi::units::radian_t{angle_difference(1)},
139 wpi::units::radian_t{angle_difference(2)}};
140
141 auto newPose = m_pose + twist.Exp();
142
143 m_previousWheelPositions = wheelPositions;
144 m_previousAngle = angle;
145 m_pose = {newPose.Translation(), angle};
146
147 return m_pose;
148 }
149
150 private:
152 m_kinematics;
153 Pose3d m_pose;
154
155 WheelPositions m_previousWheelPositions;
156
157 // Always equal to m_pose.Rotation()
158 Rotation3d m_previousAngle;
159
160 // Applying a rotation intrinsically to the measured gyro angle should cause
161 // the corrected angle to be rotated intrinsically in the same way, so the
162 // measured gyro angle must be applied intrinsically. This is equivalent to
163 // applying the offset extrinsically to the measured gyro angle.
164 Rotation3d m_gyroOffset;
165};
166
167} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel v...
Definition Kinematics.hpp:26
void ResetTranslation(const Translation3d &translation)
Resets the translation of the pose.
Definition Odometry3d.hpp:93
Odometry3d(const Kinematics< WheelPositions, WheelVelocities, WheelAccelerations > &kinematics, const Rotation3d &gyroAngle, const WheelPositions &wheelPositions, const Pose3d &initialPose=Pose3d{})
Constructs an Odometry3d object.
Definition Odometry3d.hpp:41
void ResetRotation(const Rotation3d &rotation)
Resets the rotation of the pose.
Definition Odometry3d.hpp:102
void ResetPose(const Pose3d &pose)
Resets the pose.
Definition Odometry3d.hpp:80
const Pose3d & GetPose() const
Returns the position of the robot on the field.
Definition Odometry3d.hpp:114
const Pose3d & Update(const Rotation3d &gyroAngle, const WheelPositions &wheelPositions)
Updates the robot's position on the field using forward kinematics and integration of the pose over t...
Definition Odometry3d.hpp:127
void ResetPosition(const Rotation3d &gyroAngle, const WheelPositions &wheelPositions, const Pose3d &pose)
Resets the robot's position on the field.
Definition Odometry3d.hpp:65
Represents a 3D pose containing translational and rotational elements.
Definition Pose3d.hpp:28
constexpr const Rotation3d & Rotation() const
Returns the underlying rotation.
Definition Pose3d.hpp:148
A rotation in a 3D coordinate frame, represented by a quaternion.
Definition Rotation3d.hpp:69
constexpr Eigen::Vector3d ToVector() const
Returns rotation vector representation of this rotation.
Definition Rotation3d.hpp:489
constexpr Rotation3d Inverse() const
Takes the inverse of the current rotation.
Definition Rotation3d.hpp:290
constexpr Rotation3d RelativeTo(const Rotation3d &other) const
Returns the current rotation relative to the given rotation.
Definition Rotation3d.hpp:349
constexpr Rotation3d RotateBy(const Rotation3d &other) const
Adds the new rotation to the current rotation.
Definition Rotation3d.hpp:333
Represents a translation in 3D space.
Definition Translation3d.hpp:31
Definition LinearSystem.hpp:20
A change in distance along a 3D arc since the last pose update.
Definition Twist3d.hpp:23
constexpr Transform3d Exp() const
Obtain a new Transform3d from a (constant curvature) velocity.
Definition Twist3d.hpp:106