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