WPILibC++ 2025.0.0-alpha-1-9-ga2beb75
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, 2> {
22 public:
23 /**
24 * Creates a simulated arm mechanism.
25 *
26 * @param system The system representing this arm. This system can
27 * be created with
28 * LinearSystemId::SingleJointedArmSystem().
29 * @param gearbox The type and number of motors on the arm gearbox.
30 * @param gearing The gear ratio of the arm (numbers greater than 1
31 * represent reductions).
32 * @param armLength The length of the arm.
33 * @param minAngle The minimum angle that the arm is capable of.
34 * @param maxAngle The maximum angle that the arm is capable of.
35 * @param simulateGravity Whether gravity should be simulated or not.
36 * @param startingAngle The initial position of the arm.
37 * @param measurementStdDevs The standard deviations of the measurements.
38 */
40 const DCMotor& gearbox, double gearing,
41 units::meter_t armLength, units::radian_t minAngle,
42 units::radian_t maxAngle, bool simulateGravity,
43 units::radian_t startingAngle,
44 const std::array<double, 2>& measurementStdDevs = {0.0,
45 0.0});
46 /**
47 * Creates a simulated arm mechanism.
48 *
49 * @param gearbox The type and number of motors on the arm gearbox.
50 * @param gearing The gear ratio of the arm (numbers greater than 1
51 * represent reductions).
52 * @param moi The moment of inertia of the arm. This can be
53 * calculated from CAD software.
54 * @param armLength The length of the arm.
55 * @param minAngle The minimum angle that the arm is capable of.
56 * @param maxAngle The maximum angle that the arm is capable of.
57 * @param simulateGravity Whether gravity should be simulated or not.
58 * @param startingAngle The initial position of the arm.
59 * @param measurementStdDevs The standard deviation of the measurement noise.
60 */
61 SingleJointedArmSim(const DCMotor& gearbox, double gearing,
62 units::kilogram_square_meter_t moi,
63 units::meter_t armLength, units::radian_t minAngle,
64 units::radian_t maxAngle, bool simulateGravity,
65 units::radian_t startingAngle,
66 const std::array<double, 2>& measurementStdDevs = {0.0,
67 0.0});
68
70
71 /**
72 * Sets the arm's state. The new angle will be limited between the minimum and
73 * maximum allowed limits.
74 *
75 * @param angle The new angle.
76 * @param velocity The new angular velocity.
77 */
78 void SetState(units::radian_t angle, units::radians_per_second_t velocity);
79
80 /**
81 * Returns whether the arm would hit the lower limit.
82 *
83 * @param armAngle The arm height.
84 * @return Whether the arm would hit the lower limit.
85 */
86 bool WouldHitLowerLimit(units::radian_t armAngle) const;
87
88 /**
89 * Returns whether the arm would hit the upper limit.
90 *
91 * @param armAngle The arm height.
92 * @return Whether the arm would hit the upper limit.
93 */
94 bool WouldHitUpperLimit(units::radian_t armAngle) const;
95
96 /**
97 * Returns whether the arm has hit the lower limit.
98 *
99 * @return Whether the arm has hit the lower limit.
100 */
101 bool HasHitLowerLimit() const;
102
103 /**
104 * Returns whether the arm has hit the upper limit.
105 *
106 * @return Whether the arm has hit the upper limit.
107 */
108 bool HasHitUpperLimit() const;
109
110 /**
111 * Returns the current arm angle.
112 *
113 * @return The current arm angle.
114 */
115 units::radian_t GetAngle() const;
116
117 /**
118 * Returns the current arm velocity.
119 *
120 * @return The current arm velocity.
121 */
122 units::radians_per_second_t GetVelocity() const;
123
124 /**
125 * Returns the arm current draw.
126 *
127 * @return The arm current draw.
128 */
129 units::ampere_t GetCurrentDraw() const;
130
131 /**
132 * Sets the input voltage for the arm.
133 *
134 * @param voltage The input voltage.
135 */
136 void SetInputVoltage(units::volt_t voltage);
137
138 /**
139 * Calculates a rough estimate of the moment of inertia of an arm given its
140 * length and mass.
141 *
142 * @param length The length of the arm.
143 * @param mass The mass of the arm.
144 *
145 * @return The calculated moment of inertia.
146 */
147 static constexpr units::kilogram_square_meter_t EstimateMOI(
148 units::meter_t length, units::kilogram_t mass) {
149 return 1.0 / 3.0 * mass * length * length;
150 }
151
152 protected:
153 /**
154 * Updates the state estimate of the arm.
155 *
156 * @param currentXhat The current state estimate.
157 * @param u The system inputs (voltage).
158 * @param dt The time difference between controller updates.
159 */
160 Vectord<2> UpdateX(const Vectord<2>& currentXhat, const Vectord<1>& u,
161 units::second_t dt) override;
162
163 private:
164 units::meter_t m_armLen;
165 units::radian_t m_minAngle;
166 units::radian_t m_maxAngle;
167 const DCMotor m_gearbox;
168 double m_gearing;
169 bool m_simulateGravity;
170};
171} // namespace frc::sim
Holds the constants for a DC motor.
Definition: DCMotor.h:20
A plant defined using state-space notation.
Definition: LinearSystem.h:35
This class helps simulate linear systems.
Definition: LinearSystemSim.h:31
void SetState(const Vectord< States > &state)
Sets the system state.
Definition: LinearSystemSim.h:119
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.
void SetState(units::radian_t angle, units::radians_per_second_t velocity)
Sets the arm's state.
units::ampere_t GetCurrentDraw() const
Returns the arm current draw.
units::radians_per_second_t GetVelocity() const
Returns the current arm velocity.
units::radian_t GetAngle() const
Returns the current arm angle.
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:147
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.
bool HasHitLowerLimit() const
Returns whether the arm has hit the lower limit.
SingleJointedArmSim(const LinearSystem< 2, 1, 2 > &system, const DCMotor &gearbox, double gearing, units::meter_t armLength, units::radian_t minAngle, units::radian_t maxAngle, bool simulateGravity, units::radian_t startingAngle, const std::array< double, 2 > &measurementStdDevs={0.0, 0.0})
Creates a simulated arm mechanism.
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, units::radian_t startingAngle, const std::array< double, 2 > &measurementStdDevs={0.0, 0.0})
Creates a simulated arm mechanism.
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: ADXL345Sim.h:14
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12