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/SingleJointedArmSim.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
|
|
#include <units/voltage.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;
|
|
|
|
|
|
|
|
|
|
|
|
SingleJointedArmSim::SingleJointedArmSim(
|
2024-05-15 09:23:22 -04:00
|
|
|
|
const LinearSystem<2, 1, 2>& system, const DCMotor& gearbox, double gearing,
|
2020-10-16 00:00:45 -04:00
|
|
|
|
units::meter_t armLength, units::radian_t minAngle,
|
2023-01-18 23:40:39 -05:00
|
|
|
|
units::radian_t maxAngle, bool simulateGravity,
|
2023-07-18 16:00:27 -04:00
|
|
|
|
units::radian_t startingAngle,
|
2024-05-15 09:23:22 -04:00
|
|
|
|
const std::array<double, 2>& measurementStdDevs)
|
|
|
|
|
|
: LinearSystemSim<2, 1, 2>(system, measurementStdDevs),
|
2023-01-18 23:40:39 -05:00
|
|
|
|
m_armLen(armLength),
|
2020-09-20 09:39:52 -07:00
|
|
|
|
m_minAngle(minAngle),
|
|
|
|
|
|
m_maxAngle(maxAngle),
|
2020-10-16 00:00:45 -04:00
|
|
|
|
m_gearbox(gearbox),
|
|
|
|
|
|
m_gearing(gearing),
|
2023-07-18 16:00:27 -04:00
|
|
|
|
m_simulateGravity(simulateGravity) {
|
2023-08-12 18:21:07 -04:00
|
|
|
|
SetState(startingAngle, 0_rad_per_s);
|
2023-07-18 16:00:27 -04:00
|
|
|
|
}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
|
|
SingleJointedArmSim::SingleJointedArmSim(
|
2020-10-16 00:00:45 -04:00
|
|
|
|
const DCMotor& gearbox, double gearing, units::kilogram_square_meter_t moi,
|
2020-09-20 09:39:52 -07:00
|
|
|
|
units::meter_t armLength, units::radian_t minAngle,
|
2023-01-18 23:40:39 -05:00
|
|
|
|
units::radian_t maxAngle, bool simulateGravity,
|
2023-07-18 16:00:27 -04:00
|
|
|
|
units::radian_t startingAngle,
|
2024-05-15 09:23:22 -04:00
|
|
|
|
const std::array<double, 2>& measurementStdDevs)
|
2020-09-20 09:39:52 -07:00
|
|
|
|
: SingleJointedArmSim(
|
2020-10-16 00:00:45 -04:00
|
|
|
|
LinearSystemId::SingleJointedArmSystem(gearbox, moi, gearing),
|
2023-01-18 23:40:39 -05:00
|
|
|
|
gearbox, gearing, armLength, minAngle, maxAngle, simulateGravity,
|
2023-07-18 16:00:27 -04:00
|
|
|
|
startingAngle, measurementStdDevs) {}
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
2023-08-12 18:21:07 -04:00
|
|
|
|
void SingleJointedArmSim::SetState(units::radian_t angle,
|
|
|
|
|
|
units::radians_per_second_t velocity) {
|
|
|
|
|
|
SetState(Vectord<2>{std::clamp(angle, m_minAngle, m_maxAngle), velocity});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-14 00:28:00 -08:00
|
|
|
|
bool SingleJointedArmSim::WouldHitLowerLimit(units::radian_t armAngle) const {
|
2022-11-04 17:14:46 -07:00
|
|
|
|
return armAngle <= m_minAngle;
|
2020-09-20 09:39:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-14 00:28:00 -08:00
|
|
|
|
bool SingleJointedArmSim::WouldHitUpperLimit(units::radian_t armAngle) const {
|
2022-11-04 17:14:46 -07:00
|
|
|
|
return armAngle >= m_maxAngle;
|
2021-01-14 00:28:00 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SingleJointedArmSim::HasHitLowerLimit() const {
|
2022-08-17 13:42:36 -07:00
|
|
|
|
return WouldHitLowerLimit(units::radian_t{m_y(0)});
|
2021-01-14 00:28:00 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SingleJointedArmSim::HasHitUpperLimit() const {
|
2022-08-17 13:42:36 -07:00
|
|
|
|
return WouldHitUpperLimit(units::radian_t{m_y(0)});
|
2020-09-20 09:39:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
units::radian_t SingleJointedArmSim::GetAngle() const {
|
2021-01-05 20:55:44 -05:00
|
|
|
|
return units::radian_t{m_y(0)};
|
2020-09-20 09:39:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
units::radians_per_second_t SingleJointedArmSim::GetVelocity() const {
|
|
|
|
|
|
return units::radians_per_second_t{m_x(1)};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-27 00:13:59 -07:00
|
|
|
|
units::ampere_t SingleJointedArmSim::GetCurrentDraw() const {
|
|
|
|
|
|
// Reductions are greater than 1, so a reduction of 10:1 would mean the motor
|
|
|
|
|
|
// is spinning 10x faster than the output
|
|
|
|
|
|
units::radians_per_second_t motorVelocity{m_x(1) * m_gearing};
|
2020-10-16 00:00:45 -04:00
|
|
|
|
return m_gearbox.Current(motorVelocity, units::volt_t{m_u(0)}) *
|
2020-09-27 00:13:59 -07:00
|
|
|
|
wpi::sgn(m_u(0));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
|
void SingleJointedArmSim::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
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
|
Vectord<2> SingleJointedArmSim::UpdateX(const Vectord<2>& currentXhat,
|
|
|
|
|
|
const Vectord<1>& u,
|
|
|
|
|
|
units::second_t dt) {
|
2023-01-18 23:40:39 -05:00
|
|
|
|
// The torque on the arm is given by τ = F⋅r, where F is the force applied by
|
|
|
|
|
|
// gravity and r the distance from pivot to center of mass. Recall from
|
|
|
|
|
|
// dynamics that the sum of torques for a rigid body is τ = J⋅α, were τ is
|
|
|
|
|
|
// torque on the arm, J is the mass-moment of inertia about the pivot axis,
|
|
|
|
|
|
// and α is the angular acceleration in rad/s². Rearranging yields: α = F⋅r/J
|
|
|
|
|
|
//
|
|
|
|
|
|
// We substitute in F = m⋅g⋅cos(θ), where θ is the angle from horizontal:
|
|
|
|
|
|
//
|
|
|
|
|
|
// α = (m⋅g⋅cos(θ))⋅r/J
|
|
|
|
|
|
//
|
|
|
|
|
|
// Multiply RHS by cos(θ) to account for the arm angle. Further, we know the
|
|
|
|
|
|
// arm mass-moment of inertia J of our arm is given by J=1/3 mL², modeled as a
|
|
|
|
|
|
// rod rotating about it's end, where L is the overall rod length. The mass
|
|
|
|
|
|
// distribution is assumed to be uniform. Substitute r=L/2 to find:
|
|
|
|
|
|
//
|
|
|
|
|
|
// α = (m⋅g⋅cos(θ))⋅r/(1/3 mL²)
|
|
|
|
|
|
// α = (m⋅g⋅cos(θ))⋅(L/2)/(1/3 mL²)
|
|
|
|
|
|
// α = 3/2⋅g⋅cos(θ)/L
|
|
|
|
|
|
//
|
|
|
|
|
|
// This acceleration is next added to the linear system dynamics ẋ=Ax+Bu
|
|
|
|
|
|
//
|
|
|
|
|
|
// f(x, u) = Ax + Bu + [0 α]ᵀ
|
|
|
|
|
|
// f(x, u) = Ax + Bu + [0 3/2⋅g⋅cos(θ)/L]ᵀ
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
|
Vectord<2> updatedXhat = RKDP(
|
|
|
|
|
|
[&](const auto& x, const auto& u) -> Vectord<2> {
|
|
|
|
|
|
Vectord<2> xdot = m_plant.A() * x + m_plant.B() * u;
|
2020-09-27 00:13:59 -07:00
|
|
|
|
|
|
|
|
|
|
if (m_simulateGravity) {
|
2023-01-18 23:40:39 -05:00
|
|
|
|
xdot += Vectord<2>{
|
|
|
|
|
|
0.0, (3.0 / 2.0 * -9.8 / m_armLen * std::cos(x(0))).value()};
|
2020-09-27 00:13:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
return xdot;
|
2020-09-20 09:39:52 -07:00
|
|
|
|
},
|
|
|
|
|
|
currentXhat, u, dt);
|
|
|
|
|
|
|
|
|
|
|
|
// Check for collisions.
|
2022-08-17 13:42:36 -07:00
|
|
|
|
if (WouldHitLowerLimit(units::radian_t{updatedXhat(0)})) {
|
2022-04-29 22:29:20 -07:00
|
|
|
|
return Vectord<2>{m_minAngle.value(), 0.0};
|
2022-08-17 13:42:36 -07:00
|
|
|
|
} else if (WouldHitUpperLimit(units::radian_t{updatedXhat(0)})) {
|
2022-04-29 22:29:20 -07:00
|
|
|
|
return Vectord<2>{m_maxAngle.value(), 0.0};
|
2020-09-20 09:39:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
return updatedXhat;
|
|
|
|
|
|
}
|