WPILibC++ 2023.4.3
DifferentialDrivetrainSim.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 <frc/EigenCore.h>
11
12#include <units/length.h>
14#include <units/time.h>
15#include <units/voltage.h>
16
17namespace frc::sim {
18
20 public:
21 /**
22 * Create a SimDrivetrain.
23 *
24 * @param plant The LinearSystem representing the robot's drivetrain. This
25 * system can be created with
26 * LinearSystemId::DrivetrainVelocitySystem() or
27 * LinearSystemId::IdentifyDrivetrainSystem().
28 * @param trackWidth The robot's track width.
29 * @param driveMotor A DCMotor representing the left side of the drivetrain.
30 * @param gearingRatio The gearingRatio ratio of the left side, as output over
31 * input. This must be the same ratio as the ratio used to
32 * identify or create the plant.
33 * @param wheelRadius The radius of the wheels on the drivetrain, in meters.
34 * @param measurementStdDevs Standard deviations for measurements, in the form
35 * [x, y, heading, left velocity, right velocity,
36 * left distance, right distance]ᵀ. Can be omitted
37 * if no noise is desired. Gyro standard deviations
38 * of 0.0001 radians, velocity standard deviations
39 * of 0.05 m/s, and position measurement standard
40 * deviations of 0.005 meters are a reasonable
41 * starting point.
42 */
44 LinearSystem<2, 2, 2> plant, units::meter_t trackWidth,
45 DCMotor driveMotor, double gearingRatio, units::meter_t wheelRadius,
46 const std::array<double, 7>& measurementStdDevs = {});
47
48 /**
49 * Create a SimDrivetrain.
50 *
51 * @param driveMotor A DCMotor representing the left side of the drivetrain.
52 * @param gearing The gearing on the drive between motor and wheel, as
53 * output over input. This must be the same ratio as the
54 * ratio used to identify or create the plant.
55 * @param J The moment of inertia of the drivetrain about its
56 * center.
57 * @param mass The mass of the drivebase.
58 * @param wheelRadius The radius of the wheels on the drivetrain.
59 * @param trackWidth The robot's track width, or distance between left and
60 * right wheels.
61 * @param measurementStdDevs Standard deviations for measurements, in the form
62 * [x, y, heading, left velocity, right velocity,
63 * left distance, right distance]ᵀ. Can be omitted
64 * if no noise is desired. Gyro standard deviations
65 * of 0.0001 radians, velocity standard deviations
66 * of 0.05 m/s, and position measurement standard
67 * deviations of 0.005 meters are a reasonable
68 * starting point.
69 */
71 frc::DCMotor driveMotor, double gearing, units::kilogram_square_meter_t J,
72 units::kilogram_t mass, units::meter_t wheelRadius,
73 units::meter_t trackWidth,
74 const std::array<double, 7>& measurementStdDevs = {});
75
76 /**
77 * Clamp the input vector such that no element exceeds the battery voltage.
78 * If any does, the relative magnitudes of the input will be maintained.
79 *
80 * @param u The input vector.
81 * @return The normalized input.
82 */
84
85 /**
86 * Sets the applied voltage to the drivetrain. Note that positive voltage must
87 * make that side of the drivetrain travel forward (+X).
88 *
89 * @param leftVoltage The left voltage.
90 * @param rightVoltage The right voltage.
91 */
92 void SetInputs(units::volt_t leftVoltage, units::volt_t rightVoltage);
93
94 /**
95 * Sets the gearing reduction on the drivetrain. This is commonly used for
96 * shifting drivetrains.
97 *
98 * @param newGearing The new gear ratio, as output over input.
99 */
100 void SetGearing(double newGearing);
101
102 /**
103 * Updates the simulation.
104 *
105 * @param dt The time that's passed since the last Update(units::second_t)
106 * call.
107 */
108 void Update(units::second_t dt);
109
110 /**
111 * Returns the current gearing reduction of the drivetrain, as output over
112 * input.
113 */
114 double GetGearing() const;
115
116 /**
117 * Returns the direction the robot is pointing.
118 *
119 * Note that this angle is counterclockwise-positive, while most gyros are
120 * clockwise positive.
121 */
123
124 /**
125 * Returns the current pose.
126 */
128
129 /**
130 * Get the right encoder position in meters.
131 * @return The encoder position.
132 */
133 units::meter_t GetRightPosition() const {
134 return units::meter_t{GetOutput(State::kRightPosition)};
135 }
136
137 /**
138 * Get the right encoder velocity in meters per second.
139 * @return The encoder velocity.
140 */
141 units::meters_per_second_t GetRightVelocity() const {
142 return units::meters_per_second_t{GetOutput(State::kRightVelocity)};
143 }
144
145 /**
146 * Get the left encoder position in meters.
147 * @return The encoder position.
148 */
149 units::meter_t GetLeftPosition() const {
150 return units::meter_t{GetOutput(State::kLeftPosition)};
151 }
152
153 /**
154 * Get the left encoder velocity in meters per second.
155 * @return The encoder velocity.
156 */
157 units::meters_per_second_t GetLeftVelocity() const {
158 return units::meters_per_second_t{GetOutput(State::kLeftVelocity)};
159 }
160
161 /**
162 * Returns the currently drawn current for the right side.
163 */
164 units::ampere_t GetRightCurrentDraw() const;
165
166 /**
167 * Returns the currently drawn current for the left side.
168 */
169 units::ampere_t GetLeftCurrentDraw() const;
170
171 /**
172 * Returns the currently drawn current.
173 */
174 units::ampere_t GetCurrentDraw() const;
175
176 /**
177 * Sets the system state.
178 *
179 * @param state The state.
180 */
181 void SetState(const Vectord<7>& state);
182
183 /**
184 * Sets the system pose.
185 *
186 * @param pose The pose.
187 */
188 void SetPose(const frc::Pose2d& pose);
189
191
192 class State {
193 public:
194 static constexpr int kX = 0;
195 static constexpr int kY = 1;
196 static constexpr int kHeading = 2;
197 static constexpr int kLeftVelocity = 3;
198 static constexpr int kRightVelocity = 4;
199 static constexpr int kLeftPosition = 5;
200 static constexpr int kRightPosition = 6;
201 };
202
203 /**
204 * Represents a gearing option of the Toughbox mini.
205 * 12.75:1 -- 14:50 and 14:50
206 * 10.71:1 -- 14:50 and 16:48
207 * 8.45:1 -- 14:50 and 19:45
208 * 7.31:1 -- 14:50 and 21:43
209 * 5.95:1 -- 14:50 and 24:40
210 */
212 public:
213 static constexpr double k12p75 = 12.75;
214 static constexpr double k10p71 = 10.71;
215 static constexpr double k8p45 = 8.45;
216 static constexpr double k7p31 = 7.31;
217 static constexpr double k5p95 = 5.95;
218 };
219
221 public:
233 };
234
236 public:
237 static constexpr units::meter_t kSixInch = 6_in;
238 static constexpr units::meter_t kEightInch = 8_in;
239 static constexpr units::meter_t kTenInch = 10_in;
240 };
241
242 /**
243 * Create a sim for the standard FRC kitbot.
244 *
245 * @param motor The motors installed in the bot.
246 * @param gearing The gearing reduction used.
247 * @param wheelSize The wheel size.
248 * @param measurementStdDevs Standard deviations for measurements, in the form
249 * [x, y, heading, left velocity, right velocity, left distance, right
250 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
251 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and
252 * position measurement standard deviations of 0.005 meters are a reasonable
253 * starting point.
254 */
256 frc::DCMotor motor, double gearing, units::meter_t wheelSize,
257 const std::array<double, 7>& measurementStdDevs = {}) {
258 // MOI estimation -- note that I = mr² for point masses
259 units::kilogram_square_meter_t batteryMoi = 12.5_lb * 10_in * 10_in;
260 units::kilogram_square_meter_t gearboxMoi = (2.8_lb + 2.0_lb) *
261 2 // CIM plus toughbox per side
262 * (26_in / 2) * (26_in / 2);
263
265 motor, gearing, batteryMoi + gearboxMoi, 60_lb,
266 wheelSize / 2.0, 26_in, measurementStdDevs};
267 }
268
269 /**
270 * Create a sim for the standard FRC kitbot.
271 *
272 * @param motor The motors installed in the bot.
273 * @param gearing The gearing reduction used.
274 * @param wheelSize The wheel size.
275 * @param J The moment of inertia of the drivebase. This can be
276 * calculated using SysId.
277 * @param measurementStdDevs Standard deviations for measurements, in the form
278 * [x, y, heading, left velocity, right velocity, left distance, right
279 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
280 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and
281 * position measurement standard deviations of 0.005 meters are a reasonable
282 * starting point.
283 */
285 frc::DCMotor motor, double gearing, units::meter_t wheelSize,
286 units::kilogram_square_meter_t J,
287 const std::array<double, 7>& measurementStdDevs = {}) {
289 motor, gearing, J, 60_lb, wheelSize / 2.0, 26_in, measurementStdDevs};
290 }
291
292 private:
293 /**
294 * Returns an element of the state vector.
295 *
296 * @param output The row of the output vector.
297 */
298 double GetOutput(int output) const;
299
300 /**
301 * Returns the current output vector y.
302 */
303 Vectord<7> GetOutput() const;
304
305 /**
306 * Returns an element of the state vector. Note that this will not include
307 * noise!
308 *
309 * @param output The row of the output vector.
310 */
311 double GetState(int state) const;
312
313 /**
314 * Returns the current state vector x. Note that this will not include noise!
315 */
316 Vectord<7> GetState() const;
317
318 LinearSystem<2, 2, 2> m_plant;
319 units::meter_t m_rb;
320 units::meter_t m_wheelRadius;
321
322 DCMotor m_motor;
323
324 double m_originalGearing;
325 double m_currentGearing;
326
327 Vectord<7> m_x;
328 Vectord<2> m_u;
329 Vectord<7> m_y;
330 std::array<double, 7> m_measurementStdDevs;
331};
332} // namespace frc::sim
Holds the constants for a DC motor.
Definition: DCMotor.h:20
static constexpr DCMotor Falcon500(int numMotors=1)
Returns instance of Falcon 500 brushless motor.
Definition: DCMotor.h:194
static constexpr DCMotor CIM(int numMotors=1)
Returns instance of CIM.
Definition: DCMotor.h:124
static constexpr DCMotor NEO(int numMotors=1)
Returns instance of NEO brushless motor.
Definition: DCMotor.h:180
static constexpr DCMotor MiniCIM(int numMotors=1)
Returns instance of MiniCIM.
Definition: DCMotor.h:131
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:25
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:26
Represents a gearing option of the Toughbox mini.
Definition: DifferentialDrivetrainSim.h:211
static constexpr double k10p71
Definition: DifferentialDrivetrainSim.h:214
static constexpr double k5p95
Definition: DifferentialDrivetrainSim.h:217
static constexpr double k8p45
Definition: DifferentialDrivetrainSim.h:215
static constexpr double k7p31
Definition: DifferentialDrivetrainSim.h:216
static constexpr double k12p75
Definition: DifferentialDrivetrainSim.h:213
Definition: DifferentialDrivetrainSim.h:220
static constexpr frc::DCMotor DualNEOPerSide
Definition: DifferentialDrivetrainSim.h:232
static constexpr frc::DCMotor DualFalcon500PerSide
Definition: DifferentialDrivetrainSim.h:229
static constexpr frc::DCMotor SingleNEOPerSide
Definition: DifferentialDrivetrainSim.h:231
static constexpr frc::DCMotor SingleMiniCIMPerSide
Definition: DifferentialDrivetrainSim.h:224
static constexpr frc::DCMotor SingleCIMPerSide
Definition: DifferentialDrivetrainSim.h:222
static constexpr frc::DCMotor DualCIMPerSide
Definition: DifferentialDrivetrainSim.h:223
static constexpr frc::DCMotor DualMiniCIMPerSide
Definition: DifferentialDrivetrainSim.h:226
static constexpr frc::DCMotor SingleFalcon500PerSide
Definition: DifferentialDrivetrainSim.h:227
Definition: DifferentialDrivetrainSim.h:235
static constexpr units::meter_t kTenInch
Definition: DifferentialDrivetrainSim.h:239
static constexpr units::meter_t kSixInch
Definition: DifferentialDrivetrainSim.h:237
static constexpr units::meter_t kEightInch
Definition: DifferentialDrivetrainSim.h:238
Definition: DifferentialDrivetrainSim.h:192
static constexpr int kLeftVelocity
Definition: DifferentialDrivetrainSim.h:197
static constexpr int kX
Definition: DifferentialDrivetrainSim.h:194
static constexpr int kY
Definition: DifferentialDrivetrainSim.h:195
static constexpr int kLeftPosition
Definition: DifferentialDrivetrainSim.h:199
static constexpr int kHeading
Definition: DifferentialDrivetrainSim.h:196
static constexpr int kRightPosition
Definition: DifferentialDrivetrainSim.h:200
static constexpr int kRightVelocity
Definition: DifferentialDrivetrainSim.h:198
Definition: DifferentialDrivetrainSim.h:19
units::ampere_t GetCurrentDraw() const
Returns the currently drawn current.
DifferentialDrivetrainSim(frc::DCMotor driveMotor, double gearing, units::kilogram_square_meter_t J, units::kilogram_t mass, units::meter_t wheelRadius, units::meter_t trackWidth, const std::array< double, 7 > &measurementStdDevs={})
Create a SimDrivetrain.
void Update(units::second_t dt)
Updates the simulation.
units::meter_t GetRightPosition() const
Get the right encoder position in meters.
Definition: DifferentialDrivetrainSim.h:133
void SetGearing(double newGearing)
Sets the gearing reduction on the drivetrain.
Pose2d GetPose() const
Returns the current pose.
units::meter_t GetLeftPosition() const
Get the left encoder position in meters.
Definition: DifferentialDrivetrainSim.h:149
static DifferentialDrivetrainSim CreateKitbotSim(frc::DCMotor motor, double gearing, units::meter_t wheelSize, const std::array< double, 7 > &measurementStdDevs={})
Create a sim for the standard FRC kitbot.
Definition: DifferentialDrivetrainSim.h:255
units::meters_per_second_t GetLeftVelocity() const
Get the left encoder velocity in meters per second.
Definition: DifferentialDrivetrainSim.h:157
units::ampere_t GetLeftCurrentDraw() const
Returns the currently drawn current for the left side.
void SetState(const Vectord< 7 > &state)
Sets the system state.
units::meters_per_second_t GetRightVelocity() const
Get the right encoder velocity in meters per second.
Definition: DifferentialDrivetrainSim.h:141
void SetPose(const frc::Pose2d &pose)
Sets the system pose.
units::ampere_t GetRightCurrentDraw() const
Returns the currently drawn current for the right side.
Vectord< 2 > ClampInput(const Vectord< 2 > &u)
Clamp the input vector such that no element exceeds the battery voltage.
double GetGearing() const
Returns the current gearing reduction of the drivetrain, as output over input.
void SetInputs(units::volt_t leftVoltage, units::volt_t rightVoltage)
Sets the applied voltage to the drivetrain.
Vectord< 7 > Dynamics(const Vectord< 7 > &x, const Vectord< 2 > &u)
static DifferentialDrivetrainSim CreateKitbotSim(frc::DCMotor motor, double gearing, units::meter_t wheelSize, units::kilogram_square_meter_t J, const std::array< double, 7 > &measurementStdDevs={})
Create a sim for the standard FRC kitbot.
Definition: DifferentialDrivetrainSim.h:284
DifferentialDrivetrainSim(LinearSystem< 2, 2, 2 > plant, units::meter_t trackWidth, DCMotor driveMotor, double gearingRatio, units::meter_t wheelRadius, const std::array< double, 7 > &measurementStdDevs={})
Create a SimDrivetrain.
Rotation2d GetHeading() const
Returns the direction the robot is pointing.
Definition: AnalogOutputSim.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12