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
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
|
|
#include <units/length.h>
|
|
|
|
|
#include <units/mass.h>
|
|
|
|
|
#include <units/velocity.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/simulation/LinearSystemSim.h"
|
|
|
|
|
#include "frc/system/plant/DCMotor.h"
|
|
|
|
|
|
|
|
|
|
namespace frc::sim {
|
|
|
|
|
/**
|
|
|
|
|
* Represents a simulated elevator mechanism.
|
|
|
|
|
*/
|
2024-05-15 09:23:22 -04:00
|
|
|
class ElevatorSim : public LinearSystemSim<2, 1, 2> {
|
2020-09-20 09:39:52 -07:00
|
|
|
public:
|
2023-11-02 12:10:57 -04:00
|
|
|
template <typename Distance>
|
|
|
|
|
using Velocity_t = units::unit_t<
|
|
|
|
|
units::compound_unit<Distance, units::inverse<units::seconds>>>;
|
|
|
|
|
|
|
|
|
|
template <typename Distance>
|
|
|
|
|
using Acceleration_t = units::unit_t<units::compound_unit<
|
|
|
|
|
units::compound_unit<Distance, units::inverse<units::seconds>>,
|
|
|
|
|
units::inverse<units::seconds>>>;
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs a simulated elevator mechanism.
|
|
|
|
|
*
|
2020-10-16 00:00:45 -04:00
|
|
|
* @param plant The linear system that represents the elevator.
|
2023-07-31 19:18:17 -07:00
|
|
|
* This system can be created with
|
|
|
|
|
* LinearSystemId::ElevatorSystem().
|
2020-10-16 00:00:45 -04:00
|
|
|
* @param gearbox The type of and number of motors in your
|
|
|
|
|
* elevator gearbox.
|
2020-09-20 09:39:52 -07:00
|
|
|
* @param minHeight The minimum allowed height of the elevator.
|
|
|
|
|
* @param maxHeight The maximum allowed height of the elevator.
|
2022-04-24 07:19:18 -07:00
|
|
|
* @param simulateGravity Whether gravity should be simulated or not.
|
2023-07-18 16:00:27 -04:00
|
|
|
* @param startingHeight The starting height of the elevator.
|
2020-10-16 00:00:45 -04:00
|
|
|
* @param measurementStdDevs The standard deviation of the measurements.
|
2020-09-20 09:39:52 -07:00
|
|
|
*/
|
2024-05-15 09:23:22 -04:00
|
|
|
ElevatorSim(const LinearSystem<2, 1, 2>& plant, const DCMotor& gearbox,
|
2020-09-20 09:39:52 -07:00
|
|
|
units::meter_t minHeight, units::meter_t maxHeight,
|
2023-07-18 16:00:27 -04:00
|
|
|
bool simulateGravity, units::meter_t startingHeight,
|
2024-05-15 09:23:22 -04:00
|
|
|
const std::array<double, 2>& measurementStdDevs = {0.0, 0.0});
|
2020-10-16 00:00:45 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a simulated elevator mechanism.
|
|
|
|
|
*
|
|
|
|
|
* @param gearbox The type of and number of motors in your
|
|
|
|
|
* elevator gearbox.
|
|
|
|
|
* @param gearing The gearing of the elevator (numbers greater
|
|
|
|
|
* than 1 represent reductions).
|
|
|
|
|
* @param carriageMass The mass of the elevator carriage.
|
|
|
|
|
* @param drumRadius The radius of the drum that your cable is
|
|
|
|
|
* wrapped around.
|
|
|
|
|
* @param minHeight The minimum allowed height of the elevator.
|
|
|
|
|
* @param maxHeight The maximum allowed height of the elevator.
|
2022-04-24 07:19:18 -07:00
|
|
|
* @param simulateGravity Whether gravity should be simulated or not.
|
2023-07-18 16:00:27 -04:00
|
|
|
* @param startingHeight The starting height of the elevator.
|
2020-10-16 00:00:45 -04:00
|
|
|
* @param measurementStdDevs The standard deviation of the measurements.
|
|
|
|
|
*/
|
|
|
|
|
ElevatorSim(const DCMotor& gearbox, double gearing,
|
|
|
|
|
units::kilogram_t carriageMass, units::meter_t drumRadius,
|
|
|
|
|
units::meter_t minHeight, units::meter_t maxHeight,
|
2023-07-18 16:00:27 -04:00
|
|
|
bool simulateGravity, units::meter_t startingHeight,
|
2024-05-15 09:23:22 -04:00
|
|
|
const std::array<double, 2>& measurementStdDevs = {0.0, 0.0});
|
2020-09-20 09:39:52 -07:00
|
|
|
|
2023-11-02 12:10:57 -04:00
|
|
|
/**
|
|
|
|
|
* Constructs a simulated elevator mechanism.
|
|
|
|
|
*
|
|
|
|
|
* @param kV The velocity gain.
|
|
|
|
|
* @param kA The acceleration gain.
|
|
|
|
|
* @param gearbox The type of and number of motors in your
|
|
|
|
|
* elevator gearbox.
|
|
|
|
|
* @param minHeight The minimum allowed height of the elevator.
|
|
|
|
|
* @param maxHeight The maximum allowed height of the elevator.
|
|
|
|
|
* @param simulateGravity Whether gravity should be simulated or not.
|
|
|
|
|
* @param startingHeight The starting height of the elevator.
|
|
|
|
|
* @param measurementStdDevs The standard deviation of the measurements.
|
|
|
|
|
*/
|
|
|
|
|
template <typename Distance>
|
|
|
|
|
requires std::same_as<units::meter, Distance> ||
|
|
|
|
|
std::same_as<units::radian, Distance>
|
|
|
|
|
ElevatorSim(decltype(1_V / Velocity_t<Distance>(1)) kV,
|
|
|
|
|
decltype(1_V / Acceleration_t<Distance>(1)) kA,
|
|
|
|
|
const DCMotor& gearbox, units::meter_t minHeight,
|
|
|
|
|
units::meter_t maxHeight, bool simulateGravity,
|
|
|
|
|
units::meter_t startingHeight,
|
2024-05-15 09:23:22 -04:00
|
|
|
const std::array<double, 2>& measurementStdDevs = {0.0, 0.0});
|
2023-08-12 18:21:07 -04:00
|
|
|
using LinearSystemSim::SetState;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the elevator's state. The new position will be limited between the
|
|
|
|
|
* minimum and maximum allowed heights.
|
|
|
|
|
* @param position The new position
|
|
|
|
|
* @param velocity The new velocity
|
|
|
|
|
*/
|
|
|
|
|
void SetState(units::meter_t position, units::meters_per_second_t velocity);
|
|
|
|
|
|
2021-01-14 00:28:00 -08:00
|
|
|
/**
|
|
|
|
|
* Returns whether the elevator would hit the lower limit.
|
|
|
|
|
*
|
|
|
|
|
* @param elevatorHeight The elevator height.
|
|
|
|
|
* @return Whether the elevator would hit the lower limit.
|
|
|
|
|
*/
|
|
|
|
|
bool WouldHitLowerLimit(units::meter_t elevatorHeight) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the elevator would hit the upper limit.
|
|
|
|
|
*
|
|
|
|
|
* @param elevatorHeight The elevator height.
|
|
|
|
|
* @return Whether the elevator would hit the upper limit.
|
|
|
|
|
*/
|
|
|
|
|
bool WouldHitUpperLimit(units::meter_t elevatorHeight) const;
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
/**
|
|
|
|
|
* Returns whether the elevator has hit the lower limit.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the elevator has hit the lower limit.
|
|
|
|
|
*/
|
2021-01-14 00:28:00 -08:00
|
|
|
bool HasHitLowerLimit() const;
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the elevator has hit the upper limit.
|
|
|
|
|
*
|
2020-10-16 00:00:45 -04:00
|
|
|
* @return Whether the elevator has hit the upper limit.
|
2020-09-20 09:39:52 -07:00
|
|
|
*/
|
2021-01-14 00:28:00 -08:00
|
|
|
bool HasHitUpperLimit() const;
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the position of the elevator.
|
|
|
|
|
*
|
|
|
|
|
* @return The position of the elevator.
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t GetPosition() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the velocity of the elevator.
|
|
|
|
|
*
|
|
|
|
|
* @return The velocity of the elevator.
|
|
|
|
|
*/
|
|
|
|
|
units::meters_per_second_t GetVelocity() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the elevator current draw.
|
|
|
|
|
*
|
|
|
|
|
* @return The elevator current draw.
|
|
|
|
|
*/
|
|
|
|
|
units::ampere_t GetCurrentDraw() const override;
|
|
|
|
|
|
2020-10-16 00:00:45 -04:00
|
|
|
/**
|
|
|
|
|
* Sets the input voltage for the elevator.
|
|
|
|
|
*
|
|
|
|
|
* @param voltage The input voltage.
|
|
|
|
|
*/
|
|
|
|
|
void SetInputVoltage(units::volt_t voltage);
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* Updates the state estimate of the elevator.
|
|
|
|
|
*
|
|
|
|
|
* @param currentXhat The current state estimate.
|
|
|
|
|
* @param u The system inputs (voltage).
|
|
|
|
|
* @param dt The time difference between controller updates.
|
|
|
|
|
*/
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<2> UpdateX(const Vectord<2>& currentXhat, const Vectord<1>& u,
|
|
|
|
|
units::second_t dt) override;
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-10-16 00:00:45 -04:00
|
|
|
DCMotor m_gearbox;
|
2020-09-20 09:39:52 -07:00
|
|
|
units::meter_t m_minHeight;
|
|
|
|
|
units::meter_t m_maxHeight;
|
2022-04-24 07:19:18 -07:00
|
|
|
bool m_simulateGravity;
|
2020-09-20 09:39:52 -07:00
|
|
|
};
|
|
|
|
|
} // namespace frc::sim
|