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>
|
|
|
|
|
|
2024-11-07 13:02:11 -08:00
|
|
|
#include "frc/RobotController.h"
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
using namespace frc::sim;
|
|
|
|
|
|
|
|
|
|
FlywheelSim::FlywheelSim(const LinearSystem<1, 1, 1>& plant,
|
2024-05-24 19:05:14 -04:00
|
|
|
const DCMotor& gearbox,
|
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),
|
2024-05-24 19:05:14 -04:00
|
|
|
// By theorem 6.10.1 of
|
|
|
|
|
// https://file.tavsys.net/control/controls-engineering-in-frc.pdf, the
|
|
|
|
|
// flywheel state-space model is:
|
|
|
|
|
//
|
|
|
|
|
// dx/dt = -G²Kₜ/(KᵥRJ)x + (GKₜ)/(RJ)u
|
|
|
|
|
// A = -G²Kₜ/(KᵥRJ)
|
|
|
|
|
// B = GKₜ/(RJ)
|
|
|
|
|
//
|
|
|
|
|
// Solve for G.
|
|
|
|
|
//
|
|
|
|
|
// A/B = -G/Kᵥ
|
|
|
|
|
// G = -KᵥA/B
|
|
|
|
|
//
|
|
|
|
|
// Solve for J.
|
|
|
|
|
//
|
|
|
|
|
// B = GKₜ/(RJ)
|
|
|
|
|
// J = GKₜ/(RB)
|
|
|
|
|
m_gearing(-gearbox.Kv.value() * m_plant.A(0, 0) / m_plant.B(0, 0)),
|
|
|
|
|
m_j(m_gearing * gearbox.Kt.value() /
|
|
|
|
|
(gearbox.R.value() * m_plant.B(0, 0))) {}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2024-05-24 19:05:14 -04:00
|
|
|
void FlywheelSim::SetVelocity(units::radians_per_second_t velocity) {
|
2023-08-12 18:21:07 -04:00
|
|
|
LinearSystemSim::SetState(Vectord<1>{velocity.value()});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-24 19:05:14 -04:00
|
|
|
units::radians_per_second_squared_t FlywheelSim::GetAngularAcceleration()
|
|
|
|
|
const {
|
|
|
|
|
return units::radians_per_second_squared_t{
|
|
|
|
|
(m_plant.A() * m_x + m_plant.B() * m_u)(0, 0)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
units::newton_meter_t FlywheelSim::GetTorque() const {
|
|
|
|
|
return units::newton_meter_t{GetAngularAcceleration().value() * m_j.value()};
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2024-05-15 09:23:22 -04:00
|
|
|
return m_gearbox.Current(units::radians_per_second_t{m_x(0)} * m_gearing,
|
2020-10-16 00:00:45 -04:00
|
|
|
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
|
|
|
|
2024-05-24 19:05:14 -04:00
|
|
|
units::volt_t FlywheelSim::GetInputVoltage() const {
|
|
|
|
|
return units::volt_t{GetInput(0)};
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
void FlywheelSim::SetInputVoltage(units::volt_t voltage) {
|
2022-04-29 22:29:20 -07:00
|
|
|
SetInput(Vectord<1>{voltage.value()});
|
2024-05-15 13:40:30 -04:00
|
|
|
ClampInput(frc::RobotController::GetBatteryVoltage().value());
|
2020-10-16 00:00:45 -04:00
|
|
|
}
|