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 "frc/simulation/FlywheelSim.h"
|
|
|
|
|
|
|
|
|
|
#include <wpi/MathExtras.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/system/plant/LinearSystemId.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
using namespace frc::sim;
|
|
|
|
|
|
|
|
|
|
FlywheelSim::FlywheelSim(const LinearSystem<1, 1, 1>& plant,
|
2020-10-16 00:00:45 -04:00
|
|
|
const DCMotor& gearbox, double gearing,
|
2020-09-20 09:39:52 -07:00
|
|
|
const std::array<double, 1>& measurementStdDevs)
|
2020-10-16 00:00:45 -04:00
|
|
|
: LinearSystemSim<1, 1, 1>(plant, measurementStdDevs),
|
|
|
|
|
m_gearbox(gearbox),
|
2020-09-20 09:39:52 -07:00
|
|
|
m_gearing(gearing) {}
|
|
|
|
|
|
|
|
|
|
FlywheelSim::FlywheelSim(const DCMotor& gearbox, double gearing,
|
2020-10-16 00:00:45 -04:00
|
|
|
units::kilogram_square_meter_t moi,
|
2020-09-20 09:39:52 -07:00
|
|
|
const std::array<double, 1>& measurementStdDevs)
|
|
|
|
|
: FlywheelSim(LinearSystemId::FlywheelSystem(gearbox, moi, gearing),
|
2020-10-16 00:00:45 -04:00
|
|
|
gearbox, gearing, measurementStdDevs) {}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
units::radians_per_second_t FlywheelSim::GetAngularVelocity() const {
|
2020-10-16 00:00:45 -04:00
|
|
|
return units::radians_per_second_t{GetOutput(0)};
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
units::ampere_t FlywheelSim::GetCurrentDraw() const {
|
|
|
|
|
// I = V / R - omega / (Kv * R)
|
|
|
|
|
// Reductions are greater than 1, so a reduction of 10:1 would mean the motor
|
|
|
|
|
// is spinning 10x faster than the output.
|
2020-10-16 00:00:45 -04:00
|
|
|
return m_gearbox.Current(GetAngularVelocity() * m_gearing,
|
|
|
|
|
units::volt_t{m_u(0)}) *
|
2020-09-20 09:39:52 -07:00
|
|
|
wpi::sgn(m_u(0));
|
|
|
|
|
}
|
2020-10-16 00:00:45 -04:00
|
|
|
|
|
|
|
|
void FlywheelSim::SetInputVoltage(units::volt_t voltage) {
|
2021-10-25 08:58:12 -07:00
|
|
|
SetInput(Eigen::Vector<double, 1>{voltage.value()});
|
2020-10-16 00:00:45 -04:00
|
|
|
}
|