WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DifferentialDrivetrainSim.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
12#include "wpi/units/length.hpp"
13#include "wpi/units/moment_of_inertia.hpp"
14#include "wpi/units/time.hpp"
15#include "wpi/units/velocity.hpp"
16#include "wpi/units/voltage.hpp"
17
18namespace wpi::sim {
19
21 public:
22 /**
23 * Creates a simulated differential drivetrain.
24 *
25 * @param plant The wpi::math::LinearSystem representing the robot's
26 * drivetrain. This system can be created with
27 * wpi::math::Models::DifferentialDriveFromPhysicalConstants() or
28 * wpi::math::Models::DifferentialDriveFromSysId().
29 * @param trackwidth The robot's trackwidth.
30 * @param driveMotor A wpi::math::DCMotor representing the left side of the
31 * drivetrain.
32 * @param gearingRatio The gearingRatio ratio of the left side, as output over
33 * input. This must be the same ratio as the ratio used to identify or
34 * create the plant.
35 * @param wheelRadius The radius of the wheels on the drivetrain, in meters.
36 * @param measurementStdDevs Standard deviations for measurements, in the form
37 * [x, y, heading, left velocity, right velocity, left distance, right
38 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
39 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s,
40 * and position measurement standard deviations of 0.005 meters are a
41 * reasonable starting point.
42 */
44 wpi::math::LinearSystem<2, 2, 2> plant, wpi::units::meter_t trackwidth,
45 wpi::math::DCMotor driveMotor, double gearingRatio,
46 wpi::units::meter_t wheelRadius,
47 const std::array<double, 7>& measurementStdDevs = {});
48
49 /**
50 * Creates a simulated differential drivetrain.
51 *
52 * @param driveMotor A wpi::math::DCMotor representing the left side of the
53 * drivetrain.
54 * @param gearing The gearing on the drive between motor and wheel, as output
55 * over input. This must be the same ratio as the ratio used to identify
56 * or create the plant.
57 * @param J The moment of inertia of the drivetrain about its center.
58 * @param mass The mass of the drivebase.
59 * @param wheelRadius The radius of the wheels on the drivetrain.
60 * @param trackwidth The robot's trackwidth, or distance between left and
61 * right wheels.
62 * @param measurementStdDevs Standard deviations for measurements, in the form
63 * [x, y, heading, left velocity, right velocity, left distance, right
64 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
65 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s,
66 * and position measurement standard deviations of 0.005 meters are a
67 * reasonable starting point.
68 */
70 wpi::math::DCMotor driveMotor, double gearing,
71 wpi::units::kilogram_square_meter_t J, wpi::units::kilogram_t mass,
72 wpi::units::meter_t wheelRadius, wpi::units::meter_t trackwidth,
73 const std::array<double, 7>& measurementStdDevs = {});
74
75 /**
76 * Clamp the input vector such that no element exceeds the battery voltage.
77 * If any does, the relative magnitudes of the input will be maintained.
78 *
79 * @param u The input vector.
80 * @return The normalized input.
81 */
82 Eigen::Vector2d ClampInput(const Eigen::Vector2d& u);
83
84 /**
85 * Sets the applied voltage to the drivetrain. Note that positive voltage must
86 * make that side of the drivetrain travel forward (+X).
87 *
88 * @param leftVoltage The left voltage.
89 * @param rightVoltage The right voltage.
90 */
91 void SetInputs(wpi::units::volt_t leftVoltage,
92 wpi::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
106 * Update(wpi::units::second_t) call.
107 */
108 void Update(wpi::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 wpi::units::meter_t GetRightPosition() const {
134 return wpi::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 wpi::units::meters_per_second_t GetRightVelocity() const {
142 return wpi::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 wpi::units::meter_t GetLeftPosition() const {
150 return wpi::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 wpi::units::meters_per_second_t GetLeftVelocity() const {
158 return wpi::units::meters_per_second_t{GetOutput(State::kLeftVelocity)};
159 }
160
161 /**
162 * Returns the currently drawn current for the right side.
163 */
164 wpi::units::ampere_t GetRightCurrentDraw() const;
165
166 /**
167 * Returns the currently drawn current for the left side.
168 */
169 wpi::units::ampere_t GetLeftCurrentDraw() const;
170
171 /**
172 * Returns the currently drawn current.
173 */
174 wpi::units::ampere_t GetCurrentDraw() const;
175
176 /**
177 * Sets the system state.
178 *
179 * @param state The state.
180 */
181 void SetState(const wpi::math::Vectord<7>& state);
182
183 /**
184 * Sets the system pose.
185 *
186 * @param pose The pose.
187 */
188 void SetPose(const wpi::math::Pose2d& pose);
189
190 /**
191 * The differential drive dynamics function.
192 *
193 * @param x The state.
194 * @param u The input.
195 * @return The state derivative with respect to time.
196 */
198 const Eigen::Vector2d& u);
199
200 class State {
201 public:
202 static constexpr int kX = 0;
203 static constexpr int kY = 1;
204 static constexpr int kHeading = 2;
205 static constexpr int kLeftVelocity = 3;
206 static constexpr int kRightVelocity = 4;
207 static constexpr int kLeftPosition = 5;
208 static constexpr int kRightPosition = 6;
209 };
210
211 /**
212 * Represents a gearing option of the Toughbox mini.
213 * 12.75:1 -- 14:50 and 14:50
214 * 10.71:1 -- 14:50 and 16:48
215 * 8.45:1 -- 14:50 and 19:45
216 * 7.31:1 -- 14:50 and 21:43
217 * 5.95:1 -- 14:50 and 24:40
218 */
220 public:
221 /// Gear ratio of 12.75:1.
222 static constexpr double k12p75 = 12.75;
223 /// Gear ratio of 10.71:1.
224 static constexpr double k10p71 = 10.71;
225 /// Gear ratio of 8.45:1.
226 static constexpr double k8p45 = 8.45;
227 /// Gear ratio of 7.31:1.
228 static constexpr double k7p31 = 7.31;
229 /// Gear ratio of 5.95:1.
230 static constexpr double k5p95 = 5.95;
231 };
232
233 /**
234 * Represents common motor layouts of the kit drivetrain.
235 */
237 public:
238 /// One CIM motor per drive side.
241 /// Two CIM motors per drive side.
244 /// One Mini CIM motor per drive side.
247 /// Two Mini CIM motors per drive side.
250 /// One Falcon 500 motor per drive side.
253 /// Two Falcon 500 motors per drive side.
256 /// One NEO motor per drive side.
259 /// Two NEO motors per drive side.
262 };
263
264 /**
265 * Represents common wheel sizes of the kit drivetrain.
266 */
268 public:
269 /// Six inch diameter wheels.
270 static constexpr wpi::units::meter_t kSixInch = 6_in;
271 /// Eight inch diameter wheels.
272 static constexpr wpi::units::meter_t kEightInch = 8_in;
273 /// Ten inch diameter wheels.
274 static constexpr wpi::units::meter_t kTenInch = 10_in;
275 };
276
277 /**
278 * Create a sim for the standard FRC kitbot.
279 *
280 * @param motor The motors installed in the bot.
281 * @param gearing The gearing reduction used.
282 * @param wheelSize The wheel size.
283 * @param measurementStdDevs Standard deviations for measurements, in the form
284 * [x, y, heading, left velocity, right velocity, left distance, right
285 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
286 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and
287 * position measurement standard deviations of 0.005 meters are a reasonable
288 * starting point.
289 */
291 wpi::math::DCMotor motor, double gearing, wpi::units::meter_t wheelSize,
292 const std::array<double, 7>& measurementStdDevs = {}) {
293 // MOI estimation -- note that I = mr² for point masses
294 wpi::units::kilogram_square_meter_t batteryMoi = 12.5_lb * 10_in * 10_in;
295 wpi::units::kilogram_square_meter_t gearboxMoi =
296 (2.8_lb + 2.0_lb) * 2 // CIM plus toughbox per side
297 * (26_in / 2) * (26_in / 2);
298
300 motor, gearing, batteryMoi + gearboxMoi, 60_lb,
301 wheelSize / 2.0, 26_in, measurementStdDevs};
302 }
303
304 /**
305 * Create a sim for the standard FRC kitbot.
306 *
307 * @param motor The motors installed in the bot.
308 * @param gearing The gearing reduction used.
309 * @param wheelSize The wheel size.
310 * @param J The moment of inertia of the drivebase. This can be
311 * calculated using SysId.
312 * @param measurementStdDevs Standard deviations for measurements, in the form
313 * [x, y, heading, left velocity, right velocity, left distance, right
314 * distance]ᵀ. Can be omitted if no noise is desired. Gyro standard
315 * deviations of 0.0001 radians, velocity standard deviations of 0.05 m/s, and
316 * position measurement standard deviations of 0.005 meters are a reasonable
317 * starting point.
318 */
320 wpi::math::DCMotor motor, double gearing, wpi::units::meter_t wheelSize,
321 wpi::units::kilogram_square_meter_t J,
322 const std::array<double, 7>& measurementStdDevs = {}) {
324 motor, gearing, J, 60_lb, wheelSize / 2.0, 26_in, measurementStdDevs};
325 }
326
327 private:
328 /**
329 * Returns an element of the state vector.
330 *
331 * @param output The row of the output vector.
332 */
333 double GetOutput(int output) const;
334
335 /**
336 * Returns the current output vector y.
337 */
338 wpi::math::Vectord<7> GetOutput() const;
339
340 /**
341 * Returns an element of the state vector. Note that this will not include
342 * noise!
343 *
344 * @param output The row of the output vector.
345 */
346 double GetState(int state) const;
347
348 /**
349 * Returns the current state vector x. Note that this will not include noise!
350 */
351 wpi::math::Vectord<7> GetState() const;
352
354 wpi::units::meter_t m_rb;
355 wpi::units::meter_t m_wheelRadius;
356
357 wpi::math::DCMotor m_motor;
358
359 double m_originalGearing;
360 double m_currentGearing;
361
363 Eigen::Vector2d m_u;
365 std::array<double, 7> m_measurementStdDevs;
366};
367} // namespace wpi::sim
Holds the constants for a DC motor.
Definition DCMotor.hpp:19
static constexpr DCMotor MiniCIM(int numMotors=1)
Returns a gearbox of MiniCIM motors.
Definition DCMotor.hpp:155
static constexpr DCMotor Falcon500(int numMotors=1)
Returns a gearbox of Falcon 500 brushless motors.
Definition DCMotor.hpp:218
static constexpr DCMotor NEO(int numMotors=1)
Returns a gearbox of NEO brushless motors.
Definition DCMotor.hpp:204
static constexpr DCMotor CIM(int numMotors=1)
Returns a gearbox of CIM motors.
Definition DCMotor.hpp:148
A plant defined using state-space notation.
Definition LinearSystem.hpp:35
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition Rotation2d.hpp:26
Represents a gearing option of the Toughbox mini.
Definition DifferentialDrivetrainSim.hpp:219
static constexpr double k10p71
Gear ratio of 10.71:1.
Definition DifferentialDrivetrainSim.hpp:224
static constexpr double k5p95
Gear ratio of 5.95:1.
Definition DifferentialDrivetrainSim.hpp:230
static constexpr double k12p75
Gear ratio of 12.75:1.
Definition DifferentialDrivetrainSim.hpp:222
static constexpr double k7p31
Gear ratio of 7.31:1.
Definition DifferentialDrivetrainSim.hpp:228
static constexpr double k8p45
Gear ratio of 8.45:1.
Definition DifferentialDrivetrainSim.hpp:226
Represents common motor layouts of the kit drivetrain.
Definition DifferentialDrivetrainSim.hpp:236
static constexpr wpi::math::DCMotor SingleCIMPerSide
One CIM motor per drive side.
Definition DifferentialDrivetrainSim.hpp:239
static constexpr wpi::math::DCMotor DualMiniCIMPerSide
Two Mini CIM motors per drive side.
Definition DifferentialDrivetrainSim.hpp:248
static constexpr wpi::math::DCMotor DualFalcon500PerSide
Two Falcon 500 motors per drive side.
Definition DifferentialDrivetrainSim.hpp:254
static constexpr wpi::math::DCMotor DualNEOPerSide
Two NEO motors per drive side.
Definition DifferentialDrivetrainSim.hpp:260
static constexpr wpi::math::DCMotor DualCIMPerSide
Two CIM motors per drive side.
Definition DifferentialDrivetrainSim.hpp:242
static constexpr wpi::math::DCMotor SingleNEOPerSide
One NEO motor per drive side.
Definition DifferentialDrivetrainSim.hpp:257
static constexpr wpi::math::DCMotor SingleMiniCIMPerSide
One Mini CIM motor per drive side.
Definition DifferentialDrivetrainSim.hpp:245
static constexpr wpi::math::DCMotor SingleFalcon500PerSide
One Falcon 500 motor per drive side.
Definition DifferentialDrivetrainSim.hpp:251
Represents common wheel sizes of the kit drivetrain.
Definition DifferentialDrivetrainSim.hpp:267
static constexpr wpi::units::meter_t kSixInch
Six inch diameter wheels.
Definition DifferentialDrivetrainSim.hpp:270
static constexpr wpi::units::meter_t kEightInch
Eight inch diameter wheels.
Definition DifferentialDrivetrainSim.hpp:272
static constexpr wpi::units::meter_t kTenInch
Ten inch diameter wheels.
Definition DifferentialDrivetrainSim.hpp:274
Definition DifferentialDrivetrainSim.hpp:200
static constexpr int kX
Definition DifferentialDrivetrainSim.hpp:202
static constexpr int kHeading
Definition DifferentialDrivetrainSim.hpp:204
static constexpr int kLeftVelocity
Definition DifferentialDrivetrainSim.hpp:205
static constexpr int kRightPosition
Definition DifferentialDrivetrainSim.hpp:208
static constexpr int kY
Definition DifferentialDrivetrainSim.hpp:203
static constexpr int kLeftPosition
Definition DifferentialDrivetrainSim.hpp:207
static constexpr int kRightVelocity
Definition DifferentialDrivetrainSim.hpp:206
Definition DifferentialDrivetrainSim.hpp:20
static DifferentialDrivetrainSim CreateKitbotSim(wpi::math::DCMotor motor, double gearing, wpi::units::meter_t wheelSize, const std::array< double, 7 > &measurementStdDevs={})
Create a sim for the standard FRC kitbot.
Definition DifferentialDrivetrainSim.hpp:290
wpi::units::ampere_t GetLeftCurrentDraw() const
Returns the currently drawn current for the left side.
double GetGearing() const
Returns the current gearing reduction of the drivetrain, as output over input.
Eigen::Vector2d ClampInput(const Eigen::Vector2d &u)
Clamp the input vector such that no element exceeds the battery voltage.
wpi::math::Pose2d GetPose() const
Returns the current pose.
wpi::units::ampere_t GetRightCurrentDraw() const
Returns the currently drawn current for the right side.
wpi::math::Rotation2d GetHeading() const
Returns the direction the robot is pointing.
void SetGearing(double newGearing)
Sets the gearing reduction on the drivetrain.
void SetInputs(wpi::units::volt_t leftVoltage, wpi::units::volt_t rightVoltage)
Sets the applied voltage to the drivetrain.
wpi::units::ampere_t GetCurrentDraw() const
Returns the currently drawn current.
static DifferentialDrivetrainSim CreateKitbotSim(wpi::math::DCMotor motor, double gearing, wpi::units::meter_t wheelSize, wpi::units::kilogram_square_meter_t J, const std::array< double, 7 > &measurementStdDevs={})
Create a sim for the standard FRC kitbot.
Definition DifferentialDrivetrainSim.hpp:319
wpi::units::meters_per_second_t GetRightVelocity() const
Get the right encoder velocity in meters per second.
Definition DifferentialDrivetrainSim.hpp:141
wpi::units::meters_per_second_t GetLeftVelocity() const
Get the left encoder velocity in meters per second.
Definition DifferentialDrivetrainSim.hpp:157
void SetPose(const wpi::math::Pose2d &pose)
Sets the system pose.
void SetState(const wpi::math::Vectord< 7 > &state)
Sets the system state.
void Update(wpi::units::second_t dt)
Updates the simulation.
wpi::math::Vectord< 7 > Dynamics(const wpi::math::Vectord< 7 > &x, const Eigen::Vector2d &u)
The differential drive dynamics function.
wpi::units::meter_t GetLeftPosition() const
Get the left encoder position in meters.
Definition DifferentialDrivetrainSim.hpp:149
DifferentialDrivetrainSim(wpi::math::LinearSystem< 2, 2, 2 > plant, wpi::units::meter_t trackwidth, wpi::math::DCMotor driveMotor, double gearingRatio, wpi::units::meter_t wheelRadius, const std::array< double, 7 > &measurementStdDevs={})
Creates a simulated differential drivetrain.
DifferentialDrivetrainSim(wpi::math::DCMotor driveMotor, double gearing, wpi::units::kilogram_square_meter_t J, wpi::units::kilogram_t mass, wpi::units::meter_t wheelRadius, wpi::units::meter_t trackwidth, const std::array< double, 7 > &measurementStdDevs={})
Creates a simulated differential drivetrain.
wpi::units::meter_t GetRightPosition() const
Get the right encoder position in meters.
Definition DifferentialDrivetrainSim.hpp:133
Eigen::Vector< double, Size > Vectord
Definition EigenCore.hpp:12
Definition CTREPCMSim.hpp:13