Files
allwpilib/wpilibc/src/main/native/cpp/simulation/DCMotorSim.cpp

99 lines
3.2 KiB
C++
Raw Normal View History

2022-01-22 04:42:06 +00: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.
2025-11-07 19:56:21 -05:00
#include "wpi/simulation/DCMotorSim.hpp"
2022-01-22 04:42:06 +00:00
2025-11-07 19:56:21 -05:00
#include "wpi/system/RobotController.hpp"
2025-11-07 19:57:55 -05:00
#include "wpi/util/MathExtras.hpp"
2022-01-22 04:42:06 +00:00
2025-11-07 20:00:05 -05:00
using namespace wpi;
using namespace wpi::sim;
2022-01-22 04:42:06 +00:00
2025-11-07 20:00:05 -05:00
DCMotorSim::DCMotorSim(const wpi::math::LinearSystem<2, 1, 2>& plant,
const wpi::math::DCMotor& gearbox,
2022-01-22 04:42:06 +00:00
const std::array<double, 2>& measurementStdDevs)
: LinearSystemSim<2, 1, 2>(plant, measurementStdDevs),
m_gearbox(gearbox),
// 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(1, 1) / m_plant.B(1, 0)),
m_j(m_gearing * gearbox.Kt.value() /
(gearbox.R.value() * m_plant.B(1, 0))) {}
2022-01-22 04:42:06 +00:00
2025-11-07 20:00:05 -05:00
void DCMotorSim::SetState(wpi::units::radian_t angularPosition,
wpi::units::radians_per_second_t angularVelocity) {
SetState(wpi::math::Vectord<2>{angularPosition, angularVelocity});
}
2025-11-07 20:00:05 -05:00
void DCMotorSim::SetAngle(wpi::units::radian_t angularPosition) {
SetState(angularPosition, GetAngularVelocity());
}
void DCMotorSim::SetAngularVelocity(
2025-11-07 20:00:05 -05:00
wpi::units::radians_per_second_t angularVelocity) {
SetState(GetAngularPosition(), angularVelocity);
}
2025-11-07 20:00:05 -05:00
wpi::units::radian_t DCMotorSim::GetAngularPosition() const {
return wpi::units::radian_t{GetOutput(0)};
2022-01-22 04:42:06 +00:00
}
2025-11-07 20:00:05 -05:00
wpi::units::radians_per_second_t DCMotorSim::GetAngularVelocity() const {
return wpi::units::radians_per_second_t{GetOutput(1)};
2022-01-22 04:42:06 +00:00
}
2025-11-07 20:00:05 -05:00
wpi::units::radians_per_second_squared_t DCMotorSim::GetAngularAcceleration() const {
return wpi::units::radians_per_second_squared_t{
(m_plant.A() * m_x + m_plant.B() * m_u)(1, 0)};
}
2025-11-07 20:00:05 -05:00
wpi::units::newton_meter_t DCMotorSim::GetTorque() const {
return wpi::units::newton_meter_t{GetAngularAcceleration().value() * m_j.value()};
}
2025-11-07 20:00:05 -05:00
wpi::units::ampere_t DCMotorSim::GetCurrentDraw() const {
2022-01-22 04:42:06 +00:00
// 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.
2025-11-07 20:00:05 -05:00
return m_gearbox.Current(wpi::units::radians_per_second_t{m_x(1)} * m_gearing,
wpi::units::volt_t{m_u(0)}) *
wpi::util::sgn(m_u(0));
2022-01-22 04:42:06 +00:00
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t DCMotorSim::GetInputVoltage() const {
return wpi::units::volt_t{GetInput(0)};
}
2025-11-07 20:00:05 -05:00
void DCMotorSim::SetInputVoltage(wpi::units::volt_t voltage) {
SetInput(wpi::math::Vectord<1>{voltage.value()});
ClampInput(wpi::RobotController::GetBatteryVoltage().value());
2022-01-22 04:42:06 +00:00
}
2025-11-07 20:00:05 -05:00
const wpi::math::DCMotor& DCMotorSim::GetGearbox() const {
return m_gearbox;
}
double DCMotorSim::GetGearing() const {
return m_gearing;
}
2025-11-07 20:00:05 -05:00
wpi::units::kilogram_square_meter_t DCMotorSim::GetJ() const {
return m_j;
}