2019-09-08 00:11:49 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "frc/kinematics/DifferentialDriveOdometry.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
DifferentialDriveOdometry::DifferentialDriveOdometry(
|
2019-11-15 20:34:10 -05:00
|
|
|
DifferentialDriveKinematics kinematics, const Rotation2d& gyroAngle,
|
|
|
|
|
const Pose2d& initialPose)
|
2019-09-08 00:11:49 -04:00
|
|
|
: m_kinematics(kinematics), m_pose(initialPose) {
|
|
|
|
|
m_previousAngle = m_pose.Rotation();
|
2019-11-15 20:34:10 -05:00
|
|
|
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Pose2d& DifferentialDriveOdometry::UpdateWithTime(
|
2019-11-15 20:34:10 -05:00
|
|
|
units::second_t currentTime, const Rotation2d& gyroAngle,
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveWheelSpeeds& wheelSpeeds) {
|
|
|
|
|
units::second_t deltaTime =
|
|
|
|
|
(m_previousTime >= 0_s) ? currentTime - m_previousTime : 0_s;
|
|
|
|
|
m_previousTime = currentTime;
|
|
|
|
|
|
2019-11-15 20:34:10 -05:00
|
|
|
auto angle = gyroAngle + m_gyroOffset;
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
auto [dx, dy, dtheta] = m_kinematics.ToChassisSpeeds(wheelSpeeds);
|
|
|
|
|
static_cast<void>(dtheta);
|
|
|
|
|
|
|
|
|
|
auto newPose = m_pose.Exp(
|
|
|
|
|
{dx * deltaTime, dy * deltaTime, (angle - m_previousAngle).Radians()});
|
|
|
|
|
|
|
|
|
|
m_previousAngle = angle;
|
|
|
|
|
m_pose = {newPose.Translation(), angle};
|
|
|
|
|
|
|
|
|
|
return m_pose;
|
|
|
|
|
}
|