001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package org.wpilib.math.kinematics; 006 007import org.wpilib.math.geometry.Pose2d; 008import org.wpilib.math.geometry.Pose3d; 009import org.wpilib.math.geometry.Rotation2d; 010import org.wpilib.math.geometry.Rotation3d; 011import org.wpilib.math.geometry.Translation2d; 012import org.wpilib.math.geometry.Translation3d; 013import org.wpilib.math.geometry.Twist3d; 014 015/** 016 * Class for odometry. Robot code should not use this directly- Instead, use the particular type for 017 * your drivetrain (e.g., {@link DifferentialDriveOdometry3d}). Odometry allows you to track the 018 * robot's position on the field over the course of a match using readings from encoders and a 019 * gyroscope. 020 * 021 * <p>This class is meant to be an easy replacement for {@link Odometry}, only requiring the 022 * addition of appropriate conversions between 2D and 3D versions of geometry classes. (See {@link 023 * Pose3d#Pose3d(Pose2d)}, {@link Rotation3d#Rotation3d(Rotation2d)}, {@link 024 * Translation3d#Translation3d(Translation2d)}, and {@link Pose3d#toPose2d()}.) 025 * 026 * <p>Teams can use odometry during the autonomous period for complex tasks like path following. 027 * Furthermore, odometry can be used for latency compensation when using computer-vision systems. 028 * 029 * @param <T> Wheel positions type. 030 */ 031public class Odometry3d<T> { 032 private final Kinematics<T, ?, ?> m_kinematics; 033 private Pose3d m_pose; 034 035 // Applying a rotation intrinsically to the measured gyro angle should cause the corrected angle 036 // to be rotated intrinsically in the same way, so the measured gyro angle must be applied 037 // intrinsically. This is equivalent to applying the offset extrinsically to the measured gyro 038 // angle. 039 private Rotation3d m_gyroOffset; 040 041 // Always equal to m_poseMeters.getRotation() 042 private Rotation3d m_previousAngle; 043 044 private final T m_previousWheelPositions; 045 046 /** 047 * Constructs an Odometry3d object. 048 * 049 * @param kinematics The kinematics of the drivebase. 050 * @param gyroAngle The angle reported by the gyroscope. 051 * @param wheelPositions The current encoder readings. 052 * @param initialPose The starting position of the robot on the field. 053 */ 054 public Odometry3d( 055 Kinematics<T, ?, ?> kinematics, Rotation3d gyroAngle, T wheelPositions, Pose3d initialPose) { 056 m_kinematics = kinematics; 057 m_pose = initialPose; 058 // When applied extrinsically, m_gyroOffset cancels the current gyroAngle and 059 // then rotates to m_poseMeters.getRotation() 060 m_gyroOffset = gyroAngle.inverse().rotateBy(m_pose.getRotation()); 061 m_previousAngle = m_pose.getRotation(); 062 m_previousWheelPositions = m_kinematics.copy(wheelPositions); 063 } 064 065 /** 066 * Resets the robot's position on the field. 067 * 068 * <p>The gyroscope angle does not need to be reset here on the user's robot code. The library 069 * automatically takes care of offsetting the gyro angle. 070 * 071 * @param gyroAngle The angle reported by the gyroscope. 072 * @param wheelPositions The current encoder readings. 073 * @param pose The position on the field that your robot is at. 074 */ 075 public void resetPosition(Rotation3d gyroAngle, T wheelPositions, Pose3d pose) { 076 m_pose = pose; 077 // When applied extrinsically, m_gyroOffset cancels the current gyroAngle and 078 // then rotates to m_poseMeters.getRotation() 079 m_gyroOffset = gyroAngle.inverse().rotateBy(m_pose.getRotation()); 080 m_previousAngle = m_pose.getRotation(); 081 m_kinematics.copyInto(wheelPositions, m_previousWheelPositions); 082 } 083 084 /** 085 * Resets the pose. 086 * 087 * @param pose The pose to reset to. 088 */ 089 public void resetPose(Pose3d pose) { 090 // Cancel the previous m_pose.Rotation() and then rotate to the new angle 091 m_gyroOffset = 092 m_gyroOffset.rotateBy(m_pose.getRotation().inverse()).rotateBy(pose.getRotation()); 093 m_pose = pose; 094 m_previousAngle = m_pose.getRotation(); 095 } 096 097 /** 098 * Resets the translation of the pose. 099 * 100 * @param translation The translation to reset to. 101 */ 102 public void resetTranslation(Translation3d translation) { 103 m_pose = new Pose3d(translation, m_pose.getRotation()); 104 } 105 106 /** 107 * Resets the rotation of the pose. 108 * 109 * @param rotation The rotation to reset to. 110 */ 111 public void resetRotation(Rotation3d rotation) { 112 // Cancel the previous m_pose.Rotation() and then rotate to the new angle 113 m_gyroOffset = m_gyroOffset.rotateBy(m_pose.getRotation().inverse()).rotateBy(rotation); 114 m_pose = new Pose3d(m_pose.getTranslation(), rotation); 115 m_previousAngle = m_pose.getRotation(); 116 } 117 118 /** 119 * Returns the position of the robot on the field. 120 * 121 * @return The pose of the robot (x, y, and z are in meters). 122 */ 123 public Pose3d getPose() { 124 return m_pose; 125 } 126 127 /** 128 * Updates the robot's position on the field using forward kinematics and integration of the pose 129 * over time. This method takes in an angle parameter which is used instead of the angular rate 130 * that is calculated from forward kinematics, in addition to the current distance measurement at 131 * each wheel. 132 * 133 * @param gyroAngle The angle reported by the gyroscope. 134 * @param wheelPositions The current encoder readings. 135 * @return The new pose of the robot. 136 */ 137 public Pose3d update(Rotation3d gyroAngle, T wheelPositions) { 138 var angle = gyroAngle.rotateBy(m_gyroOffset); 139 var angle_difference = angle.relativeTo(m_previousAngle).toVector(); 140 141 var twist2d = m_kinematics.toTwist2d(m_previousWheelPositions, wheelPositions); 142 var twist = 143 new Twist3d( 144 twist2d.dx, 145 twist2d.dy, 146 0, 147 angle_difference.get(0), 148 angle_difference.get(1), 149 angle_difference.get(2)); 150 151 var newPose = m_pose.plus(twist.exp()); 152 153 m_kinematics.copyInto(wheelPositions, m_previousWheelPositions); 154 m_previousAngle = angle; 155 m_pose = new Pose3d(newPose.getTranslation(), angle); 156 157 return m_pose; 158 } 159}