WPILibC++ 2023.4.3
SingleJointedArmSim.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <array>
8
9#include <units/angle.h>
10#include <units/length.h>
11#include <units/mass.h>
13
16
17namespace frc::sim {
18/**
19 * Represents a simulated arm mechanism.
20 */
21class SingleJointedArmSim : public LinearSystemSim<2, 1, 1> {
22 public:
23 /**
24 * Creates a simulated arm mechanism.
25 *
26 * @param system The system representing this arm.
27 * @param gearbox The type and number of motors on the arm gearbox.
28 * @param gearing The gear ratio of the arm (numbers greater than 1
29 * represent reductions).
30 * @param armLength The length of the arm.
31 * @param minAngle The minimum angle that the arm is capable of.
32 * @param maxAngle The maximum angle that the arm is capable of.
33 * @param simulateGravity Whether gravity should be simulated or not.
34 * @param measurementStdDevs The standard deviations of the measurements.
35 */
37 const DCMotor& gearbox, double gearing,
38 units::meter_t armLength, units::radian_t minAngle,
39 units::radian_t maxAngle, bool simulateGravity,
40 const std::array<double, 1>& measurementStdDevs = {0.0});
41 /**
42 * Creates a simulated arm mechanism.
43 *
44 * @param gearbox The type and number of motors on the arm gearbox.
45 * @param gearing The gear ratio of the arm (numbers greater than 1
46 * represent reductions).
47 * @param moi The moment of inertia of the arm. This can be
48 * calculated from CAD software.
49 * @param armLength The length of the arm.
50 * @param minAngle The minimum angle that the arm is capable of.
51 * @param maxAngle The maximum angle that the arm is capable of.
52 * @param simulateGravity Whether gravity should be simulated or not.
53 * @param measurementStdDevs The standard deviation of the measurement noise.
54 */
55 SingleJointedArmSim(const DCMotor& gearbox, double gearing,
56 units::kilogram_square_meter_t moi,
57 units::meter_t armLength, units::radian_t minAngle,
58 units::radian_t maxAngle, bool simulateGravity,
59 const std::array<double, 1>& measurementStdDevs = {0.0});
60
61 /**
62 * Returns whether the arm would hit the lower limit.
63 *
64 * @param armAngle The arm height.
65 * @return Whether the arm would hit the lower limit.
66 */
67 bool WouldHitLowerLimit(units::radian_t armAngle) const;
68
69 /**
70 * Returns whether the arm would hit the upper limit.
71 *
72 * @param armAngle The arm height.
73 * @return Whether the arm would hit the upper limit.
74 */
75 bool WouldHitUpperLimit(units::radian_t armAngle) const;
76
77 /**
78 * Returns whether the arm has hit the lower limit.
79 *
80 * @return Whether the arm has hit the lower limit.
81 */
82 bool HasHitLowerLimit() const;
83
84 /**
85 * Returns whether the arm has hit the upper limit.
86 *
87 * @return Whether the arm has hit the upper limit.
88 */
89 bool HasHitUpperLimit() const;
90
91 /**
92 * Returns the current arm angle.
93 *
94 * @return The current arm angle.
95 */
96 units::radian_t GetAngle() const;
97
98 /**
99 * Returns the current arm velocity.
100 *
101 * @return The current arm velocity.
102 */
103 units::radians_per_second_t GetVelocity() const;
104
105 /**
106 * Returns the arm current draw.
107 *
108 * @return The arm current draw.
109 */
110 units::ampere_t GetCurrentDraw() const override;
111
112 /**
113 * Sets the input voltage for the arm.
114 *
115 * @param voltage The input voltage.
116 */
117 void SetInputVoltage(units::volt_t voltage);
118
119 /**
120 * Calculates a rough estimate of the moment of inertia of an arm given its
121 * length and mass.
122 *
123 * @param length The length of the arm.
124 * @param mass The mass of the arm.
125 *
126 * @return The calculated moment of inertia.
127 */
128 static constexpr units::kilogram_square_meter_t EstimateMOI(
129 units::meter_t length, units::kilogram_t mass) {
130 return 1.0 / 3.0 * mass * length * length;
131 }
132
133 protected:
134 /**
135 * Updates the state estimate of the arm.
136 *
137 * @param currentXhat The current state estimate.
138 * @param u The system inputs (voltage).
139 * @param dt The time difference between controller updates.
140 */
141 Vectord<2> UpdateX(const Vectord<2>& currentXhat, const Vectord<1>& u,
142 units::second_t dt) override;
143
144 private:
145 units::meter_t m_armLen;
146 units::radian_t m_minAngle;
147 units::radian_t m_maxAngle;
148 const DCMotor m_gearbox;
149 double m_gearing;
150 bool m_simulateGravity;
151};
152} // namespace frc::sim
Holds the constants for a DC motor.
Definition: DCMotor.h:20
A plant defined using state-space notation.
Definition: LinearSystem.h:31
This class helps simulate linear systems.
Definition: LinearSystemSim.h:31
Represents a simulated arm mechanism.
Definition: SingleJointedArmSim.h:21
bool WouldHitLowerLimit(units::radian_t armAngle) const
Returns whether the arm would hit the lower limit.
units::radians_per_second_t GetVelocity() const
Returns the current arm velocity.
units::radian_t GetAngle() const
Returns the current arm angle.
SingleJointedArmSim(const DCMotor &gearbox, double gearing, units::kilogram_square_meter_t moi, units::meter_t armLength, units::radian_t minAngle, units::radian_t maxAngle, bool simulateGravity, const std::array< double, 1 > &measurementStdDevs={0.0})
Creates a simulated arm mechanism.
units::ampere_t GetCurrentDraw() const override
Returns the arm current draw.
static constexpr units::kilogram_square_meter_t EstimateMOI(units::meter_t length, units::kilogram_t mass)
Calculates a rough estimate of the moment of inertia of an arm given its length and mass.
Definition: SingleJointedArmSim.h:128
bool HasHitUpperLimit() const
Returns whether the arm has hit the upper limit.
bool WouldHitUpperLimit(units::radian_t armAngle) const
Returns whether the arm would hit the upper limit.
SingleJointedArmSim(const LinearSystem< 2, 1, 1 > &system, const DCMotor &gearbox, double gearing, units::meter_t armLength, units::radian_t minAngle, units::radian_t maxAngle, bool simulateGravity, const std::array< double, 1 > &measurementStdDevs={0.0})
Creates a simulated arm mechanism.
bool HasHitLowerLimit() const
Returns whether the arm has hit the lower limit.
void SetInputVoltage(units::volt_t voltage)
Sets the input voltage for the arm.
Vectord< 2 > UpdateX(const Vectord< 2 > &currentXhat, const Vectord< 1 > &u, units::second_t dt) override
Updates the state estimate of the arm.
Definition: AnalogOutputSim.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12