mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Also update copyright to include "and other WPILib contributors" and clarify license referral language to not be restricted to FIRST teams.
40 lines
1.4 KiB
C++
40 lines
1.4 KiB
C++
// Copyright (c) FIRST and other WPILib contributors.
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
#include "frc/kinematics/DifferentialDriveOdometry.h"
|
|
|
|
#include "wpimath/MathShared.h"
|
|
|
|
using namespace frc;
|
|
|
|
DifferentialDriveOdometry::DifferentialDriveOdometry(
|
|
const Rotation2d& gyroAngle, const Pose2d& initialPose)
|
|
: m_pose(initialPose) {
|
|
m_previousAngle = m_pose.Rotation();
|
|
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
|
wpi::math::MathSharedStore::ReportUsage(
|
|
wpi::math::MathUsageId::kOdometry_DifferentialDrive, 1);
|
|
}
|
|
|
|
const Pose2d& DifferentialDriveOdometry::Update(const Rotation2d& gyroAngle,
|
|
units::meter_t leftDistance,
|
|
units::meter_t rightDistance) {
|
|
auto deltaLeftDistance = leftDistance - m_prevLeftDistance;
|
|
auto deltaRightDistance = rightDistance - m_prevRightDistance;
|
|
|
|
m_prevLeftDistance = leftDistance;
|
|
m_prevRightDistance = rightDistance;
|
|
|
|
auto averageDeltaDistance = (deltaLeftDistance + deltaRightDistance) / 2.0;
|
|
auto angle = gyroAngle + m_gyroOffset;
|
|
|
|
auto newPose = m_pose.Exp(
|
|
{averageDeltaDistance, 0_m, (angle - m_previousAngle).Radians()});
|
|
|
|
m_previousAngle = angle;
|
|
m_pose = {newPose.Translation(), angle};
|
|
|
|
return m_pose;
|
|
}
|