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
|
|
|
|
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"
|
|
|
|
|
#include "wpi/math/controller/SimpleMotorFeedforward.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/math/system/plant/LinearSystemId.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/simulation/BatterySim.hpp"
|
|
|
|
|
#include "wpi/simulation/DifferentialDrivetrainSim.hpp"
|
|
|
|
|
#include "wpi/simulation/ElevatorSim.hpp"
|
|
|
|
|
#include "wpi/simulation/EncoderSim.hpp"
|
|
|
|
|
#include "wpi/simulation/FlywheelSim.hpp"
|
|
|
|
|
#include "wpi/simulation/LinearSystemSim.hpp"
|
|
|
|
|
#include "wpi/simulation/PWMSim.hpp"
|
|
|
|
|
#include "wpi/simulation/RoboRioSim.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/system/RobotController.hpp"
|
|
|
|
|
#include "wpi/units/angular_acceleration.hpp"
|
|
|
|
|
#include "wpi/units/angular_velocity.hpp"
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2021-09-17 22:51:51 -07:00
|
|
|
TEST(StateSpaceSimTest, FlywheelSim) {
|
2020-09-20 09:39:52 -07:00
|
|
|
const frc::LinearSystem<1, 1, 1> plant =
|
2020-11-12 01:33:04 -05:00
|
|
|
frc::LinearSystemId::IdentifyVelocitySystem<units::radian>(
|
|
|
|
|
0.02_V / 1_rad_per_s, 0.01_V / 1_rad_per_s_sq);
|
2024-05-24 19:05:14 -04:00
|
|
|
frc::sim::FlywheelSim sim{plant, frc::DCMotor::NEO(2)};
|
2023-09-15 19:57:31 -07:00
|
|
|
frc::PIDController controller{0.2, 0.0, 0.0};
|
2020-09-20 09:39:52 -07:00
|
|
|
frc::SimpleMotorFeedforward<units::radian> feedforward{
|
|
|
|
|
0_V, 0.02_V / 1_rad_per_s, 0.01_V / 1_rad_per_s_sq};
|
|
|
|
|
frc::Encoder encoder{0, 1};
|
|
|
|
|
frc::sim::EncoderSim encoderSim{encoder};
|
|
|
|
|
frc::PWMVictorSPX motor{0};
|
|
|
|
|
|
2021-08-05 22:04:51 -04:00
|
|
|
frc::sim::RoboRioSim::ResetData();
|
|
|
|
|
encoderSim.ResetData();
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
|
// RobotPeriodic runs first
|
|
|
|
|
auto voltageOut = controller.Calculate(encoder.GetRate(), 200.0);
|
2022-08-17 13:42:36 -07:00
|
|
|
motor.SetVoltage(units::volt_t{voltageOut} +
|
2020-09-20 09:39:52 -07:00
|
|
|
feedforward.Calculate(200_rad_per_s));
|
|
|
|
|
|
|
|
|
|
// Then, SimulationPeriodic runs
|
|
|
|
|
frc::sim::RoboRioSim::SetVInVoltage(
|
2020-11-12 01:33:49 -05:00
|
|
|
frc::sim::BatterySim::Calculate({sim.GetCurrentDraw()}));
|
2022-04-29 22:29:20 -07:00
|
|
|
sim.SetInput(
|
|
|
|
|
frc::Vectord<1>{motor.Get() * frc::RobotController::GetInputVoltage()});
|
2020-09-20 09:39:52 -07:00
|
|
|
sim.Update(20_ms);
|
2021-10-25 08:58:12 -07:00
|
|
|
encoderSim.SetRate(sim.GetAngularVelocity().value());
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(std::abs(200 - encoder.GetRate()) < 0.1);
|
|
|
|
|
}
|