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
|
|
|
|
2021-05-26 00:09:36 -07:00
|
|
|
#include <wpi/numbers>
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
#include "frc/kinematics/ChassisSpeeds.h"
|
|
|
|
|
#include "frc/kinematics/DifferentialDriveKinematics.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
2020-08-06 23:57:39 -07:00
|
|
|
#include "units/angular_velocity.h"
|
|
|
|
|
#include "units/length.h"
|
|
|
|
|
#include "units/velocity.h"
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
static constexpr double kEpsilon = 1E-9;
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, InverseKinematicsFromZero) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
|
|
|
|
const ChassisSpeeds chassisSpeeds;
|
|
|
|
|
const auto wheelSpeeds = kinematics.ToWheelSpeeds(chassisSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(wheelSpeeds.left.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(wheelSpeeds.right.value(), 0, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, ForwardKinematicsFromZero) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
|
|
|
|
const DifferentialDriveWheelSpeeds wheelSpeeds;
|
|
|
|
|
const auto chassisSpeeds = kinematics.ToChassisSpeeds(wheelSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(chassisSpeeds.vx.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.vy.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.omega.value(), 0, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, InverseKinematicsForStraightLine) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
|
|
|
|
const ChassisSpeeds chassisSpeeds{3.0_mps, 0_mps, 0_rad_per_s};
|
|
|
|
|
const auto wheelSpeeds = kinematics.ToWheelSpeeds(chassisSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(wheelSpeeds.left.value(), 3, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(wheelSpeeds.right.value(), 3, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, ForwardKinematicsForStraightLine) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
|
|
|
|
const DifferentialDriveWheelSpeeds wheelSpeeds{3.0_mps, 3.0_mps};
|
|
|
|
|
const auto chassisSpeeds = kinematics.ToChassisSpeeds(wheelSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(chassisSpeeds.vx.value(), 3, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.vy.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.omega.value(), 0, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, InverseKinematicsForRotateInPlace) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
2021-05-26 00:09:36 -07:00
|
|
|
const ChassisSpeeds chassisSpeeds{
|
|
|
|
|
0.0_mps, 0.0_mps, units::radians_per_second_t{wpi::numbers::pi}};
|
2019-09-08 00:11:49 -04:00
|
|
|
const auto wheelSpeeds = kinematics.ToWheelSpeeds(chassisSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(wheelSpeeds.left.value(), -0.381 * wpi::numbers::pi, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(wheelSpeeds.right.value(), +0.381 * wpi::numbers::pi, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(DifferentialDriveKinematicsTest, ForwardKinematicsForRotateInPlace) {
|
2019-09-08 00:11:49 -04:00
|
|
|
const DifferentialDriveKinematics kinematics{0.381_m * 2};
|
|
|
|
|
const DifferentialDriveWheelSpeeds wheelSpeeds{
|
2022-08-17 13:42:36 -07:00
|
|
|
units::meters_per_second_t{+0.381 * wpi::numbers::pi},
|
|
|
|
|
units::meters_per_second_t{-0.381 * wpi::numbers::pi}};
|
2019-09-08 00:11:49 -04:00
|
|
|
const auto chassisSpeeds = kinematics.ToChassisSpeeds(wheelSpeeds);
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(chassisSpeeds.vx.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.vy.value(), 0, kEpsilon);
|
|
|
|
|
EXPECT_NEAR(chassisSpeeds.omega.value(), -wpi::numbers::pi, kEpsilon);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|