2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
#include "frc/kinematics/SwerveDriveKinematics.h"
|
|
|
|
|
#include "frc/kinematics/SwerveDriveOdometry.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
static constexpr double kEpsilon = 0.01;
|
|
|
|
|
|
|
|
|
|
class SwerveDriveOdometryTest : public ::testing::Test {
|
|
|
|
|
protected:
|
|
|
|
|
Translation2d m_fl{12_m, 12_m};
|
|
|
|
|
Translation2d m_fr{12_m, -12_m};
|
|
|
|
|
Translation2d m_bl{-12_m, 12_m};
|
|
|
|
|
Translation2d m_br{-12_m, -12_m};
|
|
|
|
|
|
|
|
|
|
SwerveDriveKinematics<4> m_kinematics{m_fl, m_fr, m_bl, m_br};
|
2019-11-15 20:34:10 -05:00
|
|
|
SwerveDriveOdometry<4> m_odometry{m_kinematics, 0_rad};
|
2019-09-08 00:11:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(SwerveDriveOdometryTest, TwoIterations) {
|
2022-08-17 13:42:36 -07:00
|
|
|
SwerveModuleState state{5_mps, 0_deg};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
m_odometry.ResetPosition(Pose2d{}, 0_rad);
|
|
|
|
|
m_odometry.UpdateWithTime(0_s, 0_deg, SwerveModuleState{},
|
|
|
|
|
SwerveModuleState{}, SwerveModuleState{},
|
|
|
|
|
SwerveModuleState{});
|
|
|
|
|
auto pose =
|
|
|
|
|
m_odometry.UpdateWithTime(0.1_s, 0_deg, state, state, state, state);
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(0.5, pose.X().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(0.0, pose.Y().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(0.0, pose.Rotation().Degrees().value(), kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SwerveDriveOdometryTest, 90DegreeTurn) {
|
2022-08-17 13:42:36 -07:00
|
|
|
SwerveModuleState fl{18.85_mps, 90_deg};
|
|
|
|
|
SwerveModuleState fr{42.15_mps, 26.565_deg};
|
|
|
|
|
SwerveModuleState bl{18.85_mps, -90_deg};
|
|
|
|
|
SwerveModuleState br{42.15_mps, -26.565_deg};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
SwerveModuleState zero{0_mps, 0_deg};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
m_odometry.ResetPosition(Pose2d{}, 0_rad);
|
|
|
|
|
m_odometry.UpdateWithTime(0_s, 0_deg, zero, zero, zero, zero);
|
|
|
|
|
auto pose = m_odometry.UpdateWithTime(1_s, 90_deg, fl, fr, bl, br);
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(12.0, pose.X().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(12.0, pose.Y().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(90.0, pose.Rotation().Degrees().value(), kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
2019-11-15 20:34:10 -05:00
|
|
|
|
|
|
|
|
TEST_F(SwerveDriveOdometryTest, GyroAngleReset) {
|
2022-08-17 13:42:36 -07:00
|
|
|
m_odometry.ResetPosition(Pose2d{}, 90_deg);
|
2019-11-15 20:34:10 -05:00
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
SwerveModuleState state{5_mps, 0_deg};
|
2019-11-15 20:34:10 -05:00
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
m_odometry.UpdateWithTime(0_s, 90_deg, SwerveModuleState{},
|
|
|
|
|
SwerveModuleState{}, SwerveModuleState{},
|
|
|
|
|
SwerveModuleState{});
|
|
|
|
|
auto pose =
|
|
|
|
|
m_odometry.UpdateWithTime(0.1_s, 90_deg, state, state, state, state);
|
2019-11-15 20:34:10 -05:00
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(0.5, pose.X().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(0.0, pose.Y().value(), kEpsilon);
|
|
|
|
|
EXPECT_NEAR(0.0, pose.Rotation().Degrees().value(), kEpsilon);
|
2019-11-15 20:34:10 -05:00
|
|
|
}
|