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