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/ElevatorSim.h"
|
|
|
|
|
|
|
|
|
|
#include <wpi/MathExtras.h>
|
|
|
|
|
|
2021-01-06 21:40:25 -08:00
|
|
|
#include "frc/system/NumericalIntegration.h"
|
2020-09-20 09:39:52 -07:00
|
|
|
#include "frc/system/plant/LinearSystemId.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
using namespace frc::sim;
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
ElevatorSim::ElevatorSim(const LinearSystem<2, 1, 1>& plant,
|
|
|
|
|
const DCMotor& gearbox, double gearing,
|
|
|
|
|
units::meter_t drumRadius, units::meter_t minHeight,
|
2022-04-24 07:19:18 -07:00
|
|
|
units::meter_t maxHeight, bool simulateGravity,
|
2023-07-18 16:00:27 -04:00
|
|
|
units::meter_t startingHeight,
|
2020-10-16 00:00:45 -04:00
|
|
|
const std::array<double, 1>& measurementStdDevs)
|
|
|
|
|
: LinearSystemSim(plant, measurementStdDevs),
|
|
|
|
|
m_gearbox(gearbox),
|
|
|
|
|
m_drumRadius(drumRadius),
|
|
|
|
|
m_minHeight(minHeight),
|
|
|
|
|
m_maxHeight(maxHeight),
|
2022-04-24 07:19:18 -07:00
|
|
|
m_gearing(gearing),
|
2023-07-18 16:00:27 -04:00
|
|
|
m_simulateGravity(simulateGravity) {
|
|
|
|
|
SetState(
|
|
|
|
|
frc::Vectord<2>{std::clamp(startingHeight, minHeight, maxHeight), 0.0});
|
|
|
|
|
}
|
2020-10-16 00:00:45 -04:00
|
|
|
|
|
|
|
|
ElevatorSim::ElevatorSim(const DCMotor& gearbox, double gearing,
|
|
|
|
|
units::kilogram_t carriageMass,
|
|
|
|
|
units::meter_t drumRadius, units::meter_t minHeight,
|
2022-04-24 07:19:18 -07:00
|
|
|
units::meter_t maxHeight, bool simulateGravity,
|
2023-07-18 16:00:27 -04:00
|
|
|
units::meter_t startingHeight,
|
2020-10-16 00:00:45 -04:00
|
|
|
const std::array<double, 1>& measurementStdDevs)
|
2023-07-18 16:00:27 -04:00
|
|
|
: ElevatorSim(LinearSystemId::ElevatorSystem(gearbox, carriageMass,
|
|
|
|
|
drumRadius, gearing),
|
|
|
|
|
gearbox, gearing, drumRadius, minHeight, maxHeight,
|
|
|
|
|
simulateGravity, startingHeight, measurementStdDevs) {}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2021-01-14 00:28:00 -08:00
|
|
|
bool ElevatorSim::WouldHitLowerLimit(units::meter_t elevatorHeight) const {
|
2023-02-05 14:58:53 -05:00
|
|
|
return elevatorHeight <= m_minHeight;
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 00:28:00 -08:00
|
|
|
bool ElevatorSim::WouldHitUpperLimit(units::meter_t elevatorHeight) const {
|
2023-02-05 14:58:53 -05:00
|
|
|
return elevatorHeight >= m_maxHeight;
|
2021-01-14 00:28:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ElevatorSim::HasHitLowerLimit() const {
|
2022-08-17 13:42:36 -07:00
|
|
|
return WouldHitLowerLimit(units::meter_t{m_y(0)});
|
2021-01-14 00:28:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ElevatorSim::HasHitUpperLimit() const {
|
2022-08-17 13:42:36 -07:00
|
|
|
return WouldHitUpperLimit(units::meter_t{m_y(0)});
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
units::meter_t ElevatorSim::GetPosition() const {
|
2021-01-05 20:55:44 -05:00
|
|
|
return units::meter_t{m_y(0)};
|
2020-10-16 00:00:45 -04:00
|
|
|
}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
units::meters_per_second_t ElevatorSim::GetVelocity() const {
|
|
|
|
|
return units::meters_per_second_t{m_x(1)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
units::ampere_t ElevatorSim::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.
|
|
|
|
|
|
|
|
|
|
// v = r w, so w = v / r
|
|
|
|
|
units::meters_per_second_t velocity{m_x(1)};
|
|
|
|
|
units::radians_per_second_t motorVelocity =
|
|
|
|
|
velocity / m_drumRadius * m_gearing * 1_rad;
|
|
|
|
|
|
|
|
|
|
// Perform calculation and return.
|
2020-10-16 00:00:45 -04:00
|
|
|
return m_gearbox.Current(motorVelocity, 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 ElevatorSim::SetInputVoltage(units::volt_t voltage) {
|
2022-04-29 22:29:20 -07:00
|
|
|
SetInput(Vectord<1>{voltage.value()});
|
2020-10-16 00:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<2> ElevatorSim::UpdateX(const Vectord<2>& currentXhat,
|
|
|
|
|
const Vectord<1>& u, units::second_t dt) {
|
2021-07-11 10:42:33 -04:00
|
|
|
auto updatedXhat = RKDP(
|
2022-04-29 22:29:20 -07:00
|
|
|
[&](const Vectord<2>& x, const Vectord<1>& u_) -> Vectord<2> {
|
|
|
|
|
Vectord<2> xdot = m_plant.A() * x + m_plant.B() * u;
|
2022-04-24 07:19:18 -07:00
|
|
|
|
|
|
|
|
if (m_simulateGravity) {
|
2022-04-29 22:29:20 -07:00
|
|
|
xdot += Vectord<2>{0.0, -9.8};
|
2022-04-24 07:19:18 -07:00
|
|
|
}
|
|
|
|
|
return xdot;
|
2020-09-20 09:39:52 -07:00
|
|
|
},
|
|
|
|
|
currentXhat, u, dt);
|
|
|
|
|
// Check for collision after updating x-hat.
|
2022-08-17 13:42:36 -07:00
|
|
|
if (WouldHitLowerLimit(units::meter_t{updatedXhat(0)})) {
|
2022-04-29 22:29:20 -07:00
|
|
|
return Vectord<2>{m_minHeight.value(), 0.0};
|
2020-10-16 00:00:45 -04:00
|
|
|
}
|
2022-08-17 13:42:36 -07:00
|
|
|
if (WouldHitUpperLimit(units::meter_t{updatedXhat(0)})) {
|
2022-04-29 22:29:20 -07:00
|
|
|
return Vectord<2>{m_maxHeight.value(), 0.0};
|
2020-09-20 09:39:52 -07:00
|
|
|
}
|
|
|
|
|
return updatedXhat;
|
|
|
|
|
}
|