mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Use gyro angle instead of robot angle for odometry (#2081)
The odometry classes previously took in the robot angle as an argument, meaning that users had to take care of offsetting the gyro themselves to accurately report the robot angle. This change will make it so that users will not have to worry about resetting gyros and adding offsets themselves, as this will be handled by the odometry classes.
This commit is contained in:
committed by
Peter Johnson
parent
1b66ead49d
commit
841ef91c0f
@@ -33,19 +33,26 @@ class DifferentialDriveOdometry {
|
||||
* Constructs a DifferentialDriveOdometry object.
|
||||
*
|
||||
* @param kinematics The differential drive kinematics for your drivetrain.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param initialPose The starting position of the robot on the field.
|
||||
*/
|
||||
explicit DifferentialDriveOdometry(DifferentialDriveKinematics kinematics,
|
||||
const Rotation2d& gyroAngle,
|
||||
const Pose2d& initialPose = Pose2d());
|
||||
|
||||
/**
|
||||
* Resets the robot's position on the field.
|
||||
*
|
||||
* The gyroscope angle does not need to be reset here on the user's robot
|
||||
* code. The library automatically takes care of offsetting the gyro angle.
|
||||
*
|
||||
* @param pose The position on the field that your robot is at.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
*/
|
||||
void ResetPosition(const Pose2d& pose) {
|
||||
void ResetPosition(const Pose2d& pose, const Rotation2d& gyroAngle) {
|
||||
m_pose = pose;
|
||||
m_previousAngle = pose.Rotation();
|
||||
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,13 +70,13 @@ class DifferentialDriveOdometry {
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param currentTime The current time.
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param wheelSpeeds The current wheel speeds.
|
||||
*
|
||||
* @return The new pose of the robot.
|
||||
*/
|
||||
const Pose2d& UpdateWithTime(units::second_t currentTime,
|
||||
const Rotation2d& angle,
|
||||
const Rotation2d& gyroAngle,
|
||||
const DifferentialDriveWheelSpeeds& wheelSpeeds);
|
||||
|
||||
/**
|
||||
@@ -80,14 +87,15 @@ class DifferentialDriveOdometry {
|
||||
* This also takes in an angle parameter which is used instead of the
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param wheelSpeeds The current wheel speeds.
|
||||
*
|
||||
* @return The new pose of the robot.
|
||||
*/
|
||||
const Pose2d& Update(const Rotation2d& angle,
|
||||
const Pose2d& Update(const Rotation2d& gyroAngle,
|
||||
const DifferentialDriveWheelSpeeds& wheelSpeeds) {
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), angle, wheelSpeeds);
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), gyroAngle,
|
||||
wheelSpeeds);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -95,6 +103,7 @@ class DifferentialDriveOdometry {
|
||||
Pose2d m_pose;
|
||||
|
||||
units::second_t m_previousTime = -1_s;
|
||||
Rotation2d m_gyroOffset;
|
||||
Rotation2d m_previousAngle;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -31,19 +31,26 @@ class MecanumDriveOdometry {
|
||||
* Constructs a MecanumDriveOdometry object.
|
||||
*
|
||||
* @param kinematics The mecanum drive kinematics for your drivetrain.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param initialPose The starting position of the robot on the field.
|
||||
*/
|
||||
explicit MecanumDriveOdometry(MecanumDriveKinematics kinematics,
|
||||
const Rotation2d& gyroAngle,
|
||||
const Pose2d& initialPose = Pose2d());
|
||||
|
||||
/**
|
||||
* Resets the robot's position on the field.
|
||||
*
|
||||
* The gyroscope angle does not need to be reset here on the user's robot
|
||||
* code. The library automatically takes care of offsetting the gyro angle.
|
||||
*
|
||||
* @param pose The position on the field that your robot is at.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
*/
|
||||
void ResetPosition(const Pose2d& pose) {
|
||||
void ResetPosition(const Pose2d& pose, const Rotation2d& gyroAngle) {
|
||||
m_pose = pose;
|
||||
m_previousAngle = pose.Rotation();
|
||||
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,13 +68,13 @@ class MecanumDriveOdometry {
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param currentTime The current time.
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param wheelSpeeds The current wheel speeds.
|
||||
*
|
||||
* @return The new pose of the robot.
|
||||
*/
|
||||
const Pose2d& UpdateWithTime(units::second_t currentTime,
|
||||
const Rotation2d& angle,
|
||||
const Rotation2d& gyroAngle,
|
||||
MecanumDriveWheelSpeeds wheelSpeeds);
|
||||
|
||||
/**
|
||||
@@ -78,14 +85,15 @@ class MecanumDriveOdometry {
|
||||
* This also takes in an angle parameter which is used instead of the
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param wheelSpeeds The current wheel speeds.
|
||||
*
|
||||
* @return The new pose of the robot.
|
||||
*/
|
||||
const Pose2d& Update(const Rotation2d& angle,
|
||||
const Pose2d& Update(const Rotation2d& gyroAngle,
|
||||
MecanumDriveWheelSpeeds wheelSpeeds) {
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), angle, wheelSpeeds);
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), gyroAngle,
|
||||
wheelSpeeds);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -94,6 +102,7 @@ class MecanumDriveOdometry {
|
||||
|
||||
units::second_t m_previousTime = -1_s;
|
||||
Rotation2d m_previousAngle;
|
||||
Rotation2d m_gyroOffset;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -36,19 +36,26 @@ class SwerveDriveOdometry {
|
||||
* Constructs a SwerveDriveOdometry object.
|
||||
*
|
||||
* @param kinematics The swerve drive kinematics for your drivetrain.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param initialPose The starting position of the robot on the field.
|
||||
*/
|
||||
SwerveDriveOdometry(SwerveDriveKinematics<NumModules> kinematics,
|
||||
const Rotation2d& gyroAngle,
|
||||
const Pose2d& initialPose = Pose2d());
|
||||
|
||||
/**
|
||||
* Resets the robot's position on the field.
|
||||
*
|
||||
* The gyroscope angle does not need to be reset here on the user's robot
|
||||
* code. The library automatically takes care of offsetting the gyro angle.
|
||||
*
|
||||
* @param pose The position on the field that your robot is at.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
*/
|
||||
void ResetPosition(const Pose2d& pose) {
|
||||
void ResetPosition(const Pose2d& pose, const Rotation2d& gyroAngle) {
|
||||
m_pose = pose;
|
||||
m_previousAngle = pose.Rotation();
|
||||
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +73,7 @@ class SwerveDriveOdometry {
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param currentTime The current time.
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param moduleStates The current state of all swerve modules. Please provide
|
||||
* the states in the same order in which you instantiated
|
||||
* your SwerveDriveKinematics.
|
||||
@@ -75,7 +82,7 @@ class SwerveDriveOdometry {
|
||||
*/
|
||||
template <typename... ModuleStates>
|
||||
const Pose2d& UpdateWithTime(units::second_t currentTime,
|
||||
const Rotation2d& angle,
|
||||
const Rotation2d& gyroAngle,
|
||||
ModuleStates&&... moduleStates);
|
||||
|
||||
/**
|
||||
@@ -86,7 +93,7 @@ class SwerveDriveOdometry {
|
||||
* This also takes in an angle parameter which is used instead of the
|
||||
* angular rate that is calculated from forward kinematics.
|
||||
*
|
||||
* @param angle The angle of the robot.
|
||||
* @param gyroAngle The angle reported by the gyroscope.
|
||||
* @param moduleStates The current state of all swerve modules. Please provide
|
||||
* the states in the same order in which you instantiated
|
||||
* your SwerveDriveKinematics.
|
||||
@@ -94,9 +101,9 @@ class SwerveDriveOdometry {
|
||||
* @return The new pose of the robot.
|
||||
*/
|
||||
template <typename... ModuleStates>
|
||||
const Pose2d& Update(const Rotation2d& angle,
|
||||
const Pose2d& Update(const Rotation2d& gyroAngle,
|
||||
ModuleStates&&... moduleStates) {
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), angle,
|
||||
return UpdateWithTime(frc2::Timer::GetFPGATimestamp(), gyroAngle,
|
||||
moduleStates...);
|
||||
}
|
||||
|
||||
@@ -106,6 +113,7 @@ class SwerveDriveOdometry {
|
||||
|
||||
units::second_t m_previousTime = -1_s;
|
||||
Rotation2d m_previousAngle;
|
||||
Rotation2d m_gyroOffset;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -10,20 +10,24 @@
|
||||
namespace frc {
|
||||
template <size_t NumModules>
|
||||
SwerveDriveOdometry<NumModules>::SwerveDriveOdometry(
|
||||
SwerveDriveKinematics<NumModules> kinematics, const Pose2d& initialPose)
|
||||
SwerveDriveKinematics<NumModules> kinematics, const Rotation2d& gyroAngle,
|
||||
const Pose2d& initialPose)
|
||||
: m_kinematics(kinematics), m_pose(initialPose) {
|
||||
m_previousAngle = m_pose.Rotation();
|
||||
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
||||
}
|
||||
|
||||
template <size_t NumModules>
|
||||
template <typename... ModuleStates>
|
||||
const Pose2d& frc::SwerveDriveOdometry<NumModules>::UpdateWithTime(
|
||||
units::second_t currentTime, const Rotation2d& angle,
|
||||
units::second_t currentTime, const Rotation2d& gyroAngle,
|
||||
ModuleStates&&... moduleStates) {
|
||||
units::second_t deltaTime =
|
||||
(m_previousTime >= 0_s) ? currentTime - m_previousTime : 0_s;
|
||||
m_previousTime = currentTime;
|
||||
|
||||
auto angle = gyroAngle + m_gyroOffset;
|
||||
|
||||
auto [dx, dy, dtheta] = m_kinematics.ToChassisSpeeds(moduleStates...);
|
||||
static_cast<void>(dtheta);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user