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.
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2025-11-11 18:05:12 -08:00
|
|
|
#include "wpi/simulation/ElevatorSim.hpp"
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/hardware/motor/PWMVictorSPX.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hardware/rotation/Encoder.hpp"
|
|
|
|
|
#include "wpi/math/controller/PIDController.hpp"
|
2026-01-12 19:09:35 -08:00
|
|
|
#include "wpi/math/system/DCMotor.hpp"
|
|
|
|
|
#include "wpi/math/system/Models.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/simulation/EncoderSim.hpp"
|
|
|
|
|
#include "wpi/system/RobotController.hpp"
|
|
|
|
|
#include "wpi/units/time.hpp"
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2022-04-24 07:19:18 -07:00
|
|
|
#define EXPECT_NEAR_UNITS(val1, val2, eps) \
|
2025-11-07 20:00:05 -05:00
|
|
|
EXPECT_LE(wpi::units::math::abs(val1 - val2), eps)
|
2022-04-24 07:19:18 -07:00
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(ElevatorSimTest, StateSpaceSim) {
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::sim::ElevatorSim sim(wpi::math::DCMotor::Vex775Pro(4), 14.67, 8_kg,
|
|
|
|
|
0.75_in, 0_m, 3_m, true, 0_m, {0.01});
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::PIDController controller(10, 0.0, 0.0);
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::PWMVictorSPX motor(0);
|
|
|
|
|
wpi::Encoder encoder(0, 1);
|
|
|
|
|
wpi::sim::EncoderSim encoderSim(encoder);
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; ++i) {
|
|
|
|
|
controller.SetSetpoint(2.0);
|
|
|
|
|
auto nextVoltage = controller.Calculate(encoderSim.GetDistance());
|
2026-03-06 14:19:15 -08:00
|
|
|
motor.SetDutyCycle(nextVoltage / wpi::RobotController::GetInputVoltage());
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2026-03-06 14:19:15 -08:00
|
|
|
wpi::math::Vectord<1> u{motor.GetDutyCycle() *
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::RobotController::GetInputVoltage()};
|
2020-09-20 09:39:52 -07:00
|
|
|
sim.SetInput(u);
|
|
|
|
|
sim.Update(20_ms);
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
const auto& y = sim.GetOutput();
|
2020-09-20 09:39:52 -07:00
|
|
|
encoderSim.SetDistance(y(0));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
EXPECT_NEAR(controller.GetSetpoint(), sim.GetPosition().value(), 0.2);
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-19 01:49:28 -05:00
|
|
|
TEST(ElevatorSimTest, InitialState) {
|
|
|
|
|
constexpr auto startingHeight = 0.5_m;
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::sim::ElevatorSim sim(wpi::math::DCMotor::KrakenX60(2), 20, 8_kg, 0.1_m,
|
|
|
|
|
0_m, 1_m, true, startingHeight, {0.01, 0.0});
|
2025-02-19 01:49:28 -05:00
|
|
|
|
|
|
|
|
EXPECT_DOUBLE_EQ(startingHeight.value(), sim.GetPosition().value());
|
|
|
|
|
EXPECT_DOUBLE_EQ(0, sim.GetVelocity().value());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(ElevatorSimTest, MinMax) {
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::sim::ElevatorSim sim(wpi::math::DCMotor::Vex775Pro(4), 14.67, 8_kg,
|
|
|
|
|
0.75_in, 0_m, 1_m, true, 0_m, {0.01});
|
2020-09-20 09:39:52 -07:00
|
|
|
for (size_t i = 0; i < 100; ++i) {
|
2025-11-07 20:00:05 -05:00
|
|
|
sim.SetInput(wpi::math::Vectord<1>{0.0});
|
2020-09-20 09:39:52 -07:00
|
|
|
sim.Update(20_ms);
|
|
|
|
|
|
|
|
|
|
auto height = sim.GetPosition();
|
|
|
|
|
EXPECT_TRUE(height > -0.05_m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; ++i) {
|
2025-11-07 20:00:05 -05:00
|
|
|
sim.SetInput(wpi::math::Vectord<1>{12.0});
|
2020-09-20 09:39:52 -07:00
|
|
|
sim.Update(20_ms);
|
|
|
|
|
|
|
|
|
|
auto height = sim.GetPosition();
|
|
|
|
|
EXPECT_TRUE(height < 1.05_m);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-06 21:40:25 -08:00
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST(ElevatorSimTest, Stability) {
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::sim::ElevatorSim sim{wpi::math::DCMotor::Vex775Pro(4),
|
|
|
|
|
100,
|
|
|
|
|
4_kg,
|
|
|
|
|
0.5_in,
|
|
|
|
|
0_m,
|
|
|
|
|
10_m,
|
|
|
|
|
false,
|
|
|
|
|
0_m};
|
2021-01-06 21:40:25 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
sim.SetState(wpi::math::Vectord<2>{0.0, 0.0});
|
|
|
|
|
sim.SetInput(wpi::math::Vectord<1>{12.0});
|
2022-04-24 07:19:18 -07:00
|
|
|
for (int i = 0; i < 50; ++i) {
|
|
|
|
|
sim.Update(20_ms);
|
2021-01-06 21:40:25 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::LinearSystem<2, 1, 1> system =
|
2026-01-12 19:09:35 -08:00
|
|
|
wpi::math::Models::ElevatorFromPhysicalConstants(
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::math::DCMotor::Vex775Pro(4), 4_kg, 0.5_in, 100)
|
2024-05-15 09:23:22 -04:00
|
|
|
.Slice(0);
|
2025-11-07 20:01:58 -05:00
|
|
|
EXPECT_NEAR_UNITS(wpi::units::meter_t{system.CalculateX(
|
|
|
|
|
wpi::math::Vectord<2>{0.0, 0.0},
|
|
|
|
|
wpi::math::Vectord<1>{12.0}, 20_ms * 50)(0)},
|
|
|
|
|
sim.GetPosition(), 1_cm);
|
2021-01-06 21:40:25 -08:00
|
|
|
}
|
2025-11-13 13:48:43 -05:00
|
|
|
|
|
|
|
|
TEST(ElevatorSimTest, CurrentDraw) {
|
2025-11-29 13:59:29 -08:00
|
|
|
constexpr auto motor = wpi::math::DCMotor::KrakenX60(2);
|
|
|
|
|
wpi::sim::ElevatorSim sim(motor, 20, 8_kg, 0.1_m, 0_m, 1_m, true, 0_m,
|
2025-11-13 13:48:43 -05:00
|
|
|
{0.01, 0.0});
|
|
|
|
|
|
|
|
|
|
EXPECT_DOUBLE_EQ(0.0, sim.GetCurrentDraw().value());
|
|
|
|
|
sim.SetInputVoltage(motor.Voltage(motor.Torque(60_A), 0_rad_per_s));
|
|
|
|
|
sim.Update(100_ms);
|
2025-11-29 13:59:29 -08:00
|
|
|
// Current draw should start at 60 A and decrease as the back-EMF catches up
|
2025-11-13 13:48:43 -05:00
|
|
|
EXPECT_TRUE(0_A < sim.GetCurrentDraw() && sim.GetCurrentDraw() < 60_A);
|
|
|
|
|
}
|