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
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include <units/angular_acceleration.h>
|
|
|
|
|
#include <units/angular_velocity.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/Encoder.h"
|
|
|
|
|
#include "frc/RobotController.h"
|
|
|
|
|
#include "frc/controller/PIDController.h"
|
|
|
|
|
#include "frc/controller/SimpleMotorFeedforward.h"
|
2021-04-17 11:27:16 -07:00
|
|
|
#include "frc/motorcontrol/PWMVictorSPX.h"
|
2020-09-20 09:39:52 -07:00
|
|
|
#include "frc/simulation/BatterySim.h"
|
|
|
|
|
#include "frc/simulation/DifferentialDrivetrainSim.h"
|
|
|
|
|
#include "frc/simulation/ElevatorSim.h"
|
|
|
|
|
#include "frc/simulation/EncoderSim.h"
|
|
|
|
|
#include "frc/simulation/FlywheelSim.h"
|
|
|
|
|
#include "frc/simulation/LinearSystemSim.h"
|
|
|
|
|
#include "frc/simulation/PWMSim.h"
|
|
|
|
|
#include "frc/simulation/RoboRioSim.h"
|
|
|
|
|
#include "frc/simulation/SingleJointedArmSim.h"
|
|
|
|
|
#include "frc/system/plant/LinearSystemId.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
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);
|
2020-09-20 09:39:52 -07:00
|
|
|
frc::sim::FlywheelSim sim{plant, frc::DCMotor::NEO(2), 1.0};
|
|
|
|
|
frc2::PIDController controller{0.2, 0.0, 0.0};
|
|
|
|
|
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);
|
|
|
|
|
motor.SetVoltage(units::volt_t(voltageOut) +
|
|
|
|
|
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()}));
|
2021-08-19 00:23:48 -07:00
|
|
|
sim.SetInput(Eigen::Vector<double, 1>{
|
|
|
|
|
motor.Get() * frc::RobotController::GetInputVoltage()});
|
2020-09-20 09:39:52 -07:00
|
|
|
sim.Update(20_ms);
|
|
|
|
|
encoderSim.SetRate(sim.GetAngularVelocity().to<double>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(std::abs(200 - encoder.GetRate()) < 0.1);
|
|
|
|
|
}
|