mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Position Delta Odometry for Mecanum (#4514)
This commit is contained in:
@@ -49,6 +49,18 @@ ChassisSpeeds MecanumDriveKinematics::ToChassisSpeeds(
|
||||
units::radians_per_second_t{chassisSpeedsVector(2)}};
|
||||
}
|
||||
|
||||
Twist2d MecanumDriveKinematics::ToTwist2d(
|
||||
const MecanumDriveWheelPositions& wheelDeltas) const {
|
||||
Vectord<4> wheelDeltasVector{
|
||||
wheelDeltas.frontLeft.value(), wheelDeltas.frontRight.value(),
|
||||
wheelDeltas.rearLeft.value(), wheelDeltas.rearRight.value()};
|
||||
|
||||
Eigen::Vector3d twistVector = m_forwardKinematics.solve(wheelDeltasVector);
|
||||
|
||||
return {units::meter_t{twistVector(0)}, // NOLINT
|
||||
units::meter_t{twistVector(1)}, units::radian_t{twistVector(2)}};
|
||||
}
|
||||
|
||||
void MecanumDriveKinematics::SetInverseKinematics(Translation2d fl,
|
||||
Translation2d fr,
|
||||
Translation2d rl,
|
||||
|
||||
@@ -8,32 +8,37 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
MecanumDriveOdometry::MecanumDriveOdometry(MecanumDriveKinematics kinematics,
|
||||
const Rotation2d& gyroAngle,
|
||||
const Pose2d& initialPose)
|
||||
: m_kinematics(kinematics), m_pose(initialPose) {
|
||||
MecanumDriveOdometry::MecanumDriveOdometry(
|
||||
MecanumDriveKinematics kinematics, const Rotation2d& gyroAngle,
|
||||
const MecanumDriveWheelPositions wheelPositions, const Pose2d& initialPose)
|
||||
: m_kinematics(kinematics),
|
||||
m_pose(initialPose),
|
||||
m_previousWheelPositions(wheelPositions) {
|
||||
m_previousAngle = m_pose.Rotation();
|
||||
m_gyroOffset = m_pose.Rotation() - gyroAngle;
|
||||
wpi::math::MathSharedStore::ReportUsage(
|
||||
wpi::math::MathUsageId::kOdometry_MecanumDrive, 1);
|
||||
}
|
||||
|
||||
const Pose2d& MecanumDriveOdometry::UpdateWithTime(
|
||||
units::second_t currentTime, const Rotation2d& gyroAngle,
|
||||
MecanumDriveWheelSpeeds wheelSpeeds) {
|
||||
units::second_t deltaTime =
|
||||
(m_previousTime >= 0_s) ? currentTime - m_previousTime : 0_s;
|
||||
m_previousTime = currentTime;
|
||||
|
||||
const Pose2d& MecanumDriveOdometry::Update(
|
||||
const Rotation2d& gyroAngle,
|
||||
const MecanumDriveWheelPositions wheelPositions) {
|
||||
auto angle = gyroAngle + m_gyroOffset;
|
||||
|
||||
auto [dx, dy, dtheta] = m_kinematics.ToChassisSpeeds(wheelSpeeds);
|
||||
static_cast<void>(dtheta);
|
||||
MecanumDriveWheelPositions wheelDeltas{
|
||||
wheelPositions.frontLeft - m_previousWheelPositions.frontLeft,
|
||||
wheelPositions.frontRight - m_previousWheelPositions.frontRight,
|
||||
wheelPositions.rearLeft - m_previousWheelPositions.rearLeft,
|
||||
wheelPositions.rearRight - m_previousWheelPositions.rearRight,
|
||||
};
|
||||
|
||||
auto newPose = m_pose.Exp(
|
||||
{dx * deltaTime, dy * deltaTime, (angle - m_previousAngle).Radians()});
|
||||
auto twist = m_kinematics.ToTwist2d(wheelDeltas);
|
||||
twist.dtheta = (angle - m_previousAngle).Radians();
|
||||
|
||||
auto newPose = m_pose.Exp(twist);
|
||||
|
||||
m_previousAngle = angle;
|
||||
m_previousWheelPositions = wheelPositions;
|
||||
m_pose = {newPose.Translation(), angle};
|
||||
|
||||
return m_pose;
|
||||
|
||||
Reference in New Issue
Block a user