CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
SwerveDrivetrain.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
14
15namespace ctre {
16namespace phoenix6 {
17namespace swerve {
18
19/**
20 * \brief Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
21 *
22 * This class handles the kinematics, configuration, and odometry of a
23 * swerve drive utilizing CTR Electronics devices. We recommend using
24 * the Swerve Project Generator in Tuner X to create a template project
25 * that demonstrates how to use this class.
26 *
27 * This class performs pose estimation internally using a separate odometry
28 * thread. Vision measurements can be added using AddVisionMeasurement.
29 * Other odometry APIs such as ResetPose are also available. The resulting
30 * pose estimate can be retrieved along with module states and other
31 * information using GetState. Additionally, the odometry thread synchronously
32 * provides all new state updates to a telemetry function registered with
33 * RegisterTelemetry.
34 *
35 * This class will construct the hardware devices internally, so the user
36 * only specifies the constants (IDs, PID gains, gear ratios, etc).
37 * Getters for these hardware devices are available.
38 *
39 * If using the generator, the order in which modules are constructed is
40 * Front Left, Front Right, Back Left, Back Right. This means if you need
41 * the Back Left module, call \c GetModule(2); to get the third (0-indexed)
42 * module.
43 */
45public:
46 /** \brief Performs swerve module updates in a separate thread to minimize latency. */
48
49 /**
50 * \brief Plain-Old-Data class holding the state of the swerve drivetrain.
51 * This encapsulates most data that is relevant for telemetry or
52 * decision-making from the Swerve Drive.
53 */
55
56protected:
57 /** \brief Number of times to attempt config applies. */
58 static constexpr int kNumConfigAttempts = 2;
59
60 /** \brief The underlying drivetrain instance. */
62
63private:
64 std::vector<std::unique_ptr<SwerveModule>> _modules;
65
66 hardware::Pigeon2 _pigeon2;
67 SimSwerveDrivetrain _simDrive;
68
69public:
70 /**
71 * \brief Constructs a CTRE SwerveDrivetrain using the specified constants.
72 *
73 * This constructs the underlying hardware devices, so users should not construct
74 * the devices themselves. If they need the devices, they can access them
75 * through getters in the classes.
76 *
77 * \param drivetrainConstants Drivetrain-wide constants for the swerve drive
78 * \param modules Constants for each specific module
79 */
80 template < typename... ModuleConstants,
81 typename = std::enable_if_t< std::conjunction_v<std::is_same<ModuleConstants, SwerveModuleConstants>...> > >
82 SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, ModuleConstants const &... modules) :
83 SwerveDrivetrain{drivetrainConstants, 0_Hz, modules...}
84 {}
85
86 /**
87 * \brief Constructs a CTRE SwerveDrivetrain using the specified constants.
88 *
89 * This constructs the underlying hardware devices, so users should not construct
90 * the devices themselves. If they need the devices, they can access them
91 * through getters in the classes.
92 *
93 * \param drivetrainConstants Drivetrain-wide constants for the swerve drive
94 * \param odometryUpdateFrequency The frequency to run the odometry loop. If
95 * unspecified or set to 0 Hz, this is 250 Hz on
96 * CAN FD, and 100 Hz on CAN 2.0.
97 * \param modules Constants for each specific module
98 */
99 template < typename... ModuleConstants,
100 typename = std::enable_if_t< std::conjunction_v<std::is_same<ModuleConstants, SwerveModuleConstants>...> > >
101 SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, units::hertz_t odometryUpdateFrequency,
102 ModuleConstants const &... modules) :
103 SwerveDrivetrain{drivetrainConstants, odometryUpdateFrequency, std::array{0.1, 0.1, 0.1}, std::array{0.9, 0.9, 0.9}, modules...}
104 {}
105
106 /**
107 * \brief Constructs a CTRE SwerveDrivetrain using the specified constants.
108 *
109 * This constructs the underlying hardware devices, so users should not construct
110 * the devices themselves. If they need the devices, they can access them
111 * through getters in the classes.
112 *
113 * \param drivetrainConstants Drivetrain-wide constants for the swerve drive
114 * \param odometryUpdateFrequency The frequency to run the odometry loop. If
115 * unspecified or set to 0 Hz, this is 250 Hz on
116 * CAN FD, and 100 Hz on CAN 2.0.
117 * \param odometryStandardDeviation The standard deviation for odometry calculation
118 * in the form [x, y, theta]ᵀ, with units in meters
119 * and radians
120 * \param visionStandardDeviation The standard deviation for vision calculation
121 * in the form [x, y, theta]ᵀ, with units in meters
122 * and radians
123 * \param modules Constants for each specific module
124 */
125 template < typename... ModuleConstants,
126 typename = std::enable_if_t< std::conjunction_v<std::is_same<ModuleConstants, SwerveModuleConstants>...> > >
127 SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, units::hertz_t odometryUpdateFrequency,
128 std::array<double, 3> const &odometryStandardDeviation, std::array<double, 3> const &visionStandardDeviation,
129 ModuleConstants const &... modules) :
130 _drivetrain{drivetrainConstants, odometryUpdateFrequency,
131 odometryStandardDeviation, visionStandardDeviation,
132 modules...},
133 _modules{CreateModuleArray(drivetrainConstants.CANBusName, modules...)},
134 _pigeon2{drivetrainConstants.Pigeon2Id, std::string{drivetrainConstants.CANBusName}},
135 _simDrive{_drivetrain.GetModuleLocations(), _pigeon2.GetSimState(), modules...}
136 {
137 if (drivetrainConstants.Pigeon2Configs) {
139 for (int i = 0; i < kNumConfigAttempts; ++i) {
140 retval = GetPigeon2().GetConfigurator().Apply(*drivetrainConstants.Pigeon2Configs);
141 if (retval.IsOK()) break;
142 }
143 if (!retval.IsOK()) {
144 printf("Pigeon2 ID %d failed config with error: %s\n", GetPigeon2().GetDeviceID(), retval.GetName());
145 }
146 }
147 /* do not start thread until after applying Pigeon 2 configs */
149 }
150
151 virtual ~SwerveDrivetrain() = default;
152
153private:
154 template <typename... ModuleConstants>
155 std::vector<std::unique_ptr<SwerveModule>> CreateModuleArray(std::string_view canbusName, ModuleConstants const &... constants)
156 {
157 std::vector<std::unique_ptr<SwerveModule>> modules;
158 modules.reserve(sizeof...(ModuleConstants));
159
160 [&]<size_t... Idxs>(std::index_sequence<Idxs...>) {
161 (modules.emplace_back(std::make_unique<SwerveModule>(constants, canbusName, _drivetrain.GetModule(Idxs))), ...);
162 }(std::index_sequence_for<ModuleConstants...>{});
163
164 return modules;
165 }
166
167public:
168 /**
169 * \brief Updates all the simulation state variables for this
170 * drivetrain class. User provides the update variables for the simulation.
171 *
172 * \param dt time since last update call
173 * \param supplyVoltage voltage as seen at the motor controllers
174 */
175 virtual void UpdateSimState(units::second_t dt, units::volt_t supplyVoltage)
176 {
177 _simDrive.Update(dt, supplyVoltage, _modules);
178 }
179
180 /**
181 * \brief Gets whether the drivetrain is on a CAN FD bus.
182 *
183 * \returns true if on CAN FD
184 */
185 bool IsOnCANFD() const
186 {
187 return _drivetrain.IsOnCANFD();
188 }
189
190 /**
191 * \brief Gets the target odometry update frequency.
192 *
193 * \returns Target odometry update frequency
194 */
195 units::hertz_t GetOdometryFrequency() const
196 {
198 }
199
200 /**
201 * \brief Gets a reference to the odometry thread.
202 *
203 * \returns Odometry thread
204 */
209
210 /**
211 * \brief Check if the odometry is currently valid.
212 *
213 * \returns True if odometry is valid
214 */
215 virtual bool IsOdometryValid() const
216 {
218 }
219
220 /**
221 * \brief Gets a reference to the kinematics used for the drivetrain.
222 *
223 * \returns Swerve kinematics
224 */
226 {
227 return _drivetrain.GetKinematics();
228 }
229
230 /**
231 * \brief Applies the specified control request to this swerve drivetrain.
232 *
233 * This captures the swerve request by reference, so it must live for
234 * at least as long as the drivetrain. This can be done by storing the
235 * request as a member variable of your drivetrain subsystem or robot.
236 *
237 * \param request Request to apply
238 */
239 template < typename Request,
240 typename = std::enable_if_t<std::is_base_of_v<requests::SwerveRequest, Request>>,
241 typename = std::enable_if_t<!std::is_const_v<Request>> >
242 void SetControl(Request &request)
243 {
245 [&request](auto const &params, auto const &modules) mutable {
246 return request.Apply(params, modules);
247 }
248 );
249 }
250
251 /**
252 * \brief Applies the specified control request to this swerve drivetrain.
253 *
254 * \param request Request to apply
255 */
256 template < typename Request,
257 typename = std::enable_if_t<std::is_base_of_v<requests::SwerveRequest, Request>>,
258 typename = std::enable_if_t<!std::is_const_v<Request>> >
259 void SetControl(Request &&request)
260 {
262 [request=std::move(request)](auto const &params, auto const &modules) mutable {
263 return request.Apply(params, modules);
264 }
265 );
266 }
267
268 /**
269 * \brief Gets the current state of the swerve drivetrain.
270 * This includes information such as the pose estimate,
271 * module states, and chassis speeds.
272 *
273 * \returns Current state of the drivetrain
274 */
276 {
277 return _drivetrain.GetState();
278 }
279
280 /**
281 * \brief Register the specified lambda to be executed whenever the SwerveDriveState
282 * is updated in the odometry thread.
283 *
284 * It is imperative that this function is cheap, as it will be executed synchronously
285 * with the odometry call; if this takes a long time, it may negatively impact the
286 * odometry of this stack.
287 *
288 * This can also be used for logging data if the function performs logging instead of telemetry.
289 * Additionally, the SwerveDriveState object can be cloned and stored for later processing.
290 *
291 * \param telemetryFunction Function to call for telemetry or logging
292 */
293 virtual void RegisterTelemetry(std::function<void(SwerveDriveState const &)> telemetryFunction)
294 {
295 _drivetrain.RegisterTelemetry(std::move(telemetryFunction));
296 }
297
298 /**
299 * \brief Configures the neutral mode to use for all modules' drive motors.
300 *
301 * \param neutralMode The drive motor neutral mode
302 * \returns Status code of the first failed config call, or OK if all succeeded
303 */
308
309 /**
310 * \brief Zero's this swerve drive's odometry entirely.
311 *
312 * This will zero the entire odometry, and place the robot at 0,0
313 */
314 virtual void TareEverything()
315 {
317 }
318
319 /**
320 * \brief Resets the rotation of the robot pose to 0 from the
321 * requests#ForwardPerspectiveValue#OperatorPerspective perspective.
322 * This makes the current orientation of the robot X forward for
323 * field-centric maneuvers.
324 *
325 * This is equivalent to calling ResetRotation with the operator
326 * perspective rotation.
327 */
328 virtual void SeedFieldCentric()
329 {
331 }
332
333 /**
334 * \brief Resets the pose of the robot. The pose should be from the
335 * requests#ForwardPerspectiveValue#BlueAlliance perspective.
336 *
337 * \param pose Pose to make the current pose
338 */
339 virtual void ResetPose(Pose2d const &pose)
340 {
342 }
343
344 /**
345 * \brief Resets the translation of the robot pose without affecting rotation.
346 * The translation should be from the requests#ForwardPerspectiveValue#BlueAlliance
347 * perspective.
348 *
349 * \param translation Translation to make the current translation
350 */
351 virtual void ResetTranslation(Translation2d const &translation)
352 {
353 _drivetrain.ResetTranslation(translation);
354 }
355
356 /**
357 * \brief Resets the rotation of the robot pose without affecting translation.
358 * The rotation should be from the requests#ForwardPerspectiveValue#BlueAlliance
359 * perspective.
360 *
361 * \param rotation Rotation to make the current rotation
362 */
363 virtual void ResetRotation(Rotation2d const &rotation)
364 {
365 _drivetrain.ResetRotation(rotation);
366 }
367
368 /**
369 * \brief Takes the requests#ForwardPerspectiveValue#BlueAlliance perpective
370 * direction and treats it as the forward direction for
371 * requests#ForwardPerspectiveValue#OperatorPerspective.
372 *
373 * If the operator is in the Blue Alliance Station, this should be 0 degrees.
374 * If the operator is in the Red Alliance Station, this should be 180 degrees.
375 *
376 * This does not change the robot pose, which is in the
377 * requests#ForwardPerspectiveValue#BlueAlliance perspective.
378 * As a result, the robot pose may need to be reset using ResetPose.
379 *
380 * \param fieldDirection Heading indicating which direction is forward from
381 * the requests#ForwardPerspectiveValue#BlueAlliance perspective
382 */
383 virtual void SetOperatorPerspectiveForward(Rotation2d fieldDirection)
384 {
386 }
387
388 /**
389 * \brief Returns the requests#ForwardPerspectiveValue#BlueAlliance perpective
390 * direction that is treated as the forward direction for
391 * requests#ForwardPerspectiveValue#OperatorPerspective.
392 *
393 * If the operator is in the Blue Alliance Station, this should be 0 degrees.
394 * If the operator is in the Red Alliance Station, this should be 180 degrees.
395 *
396 * \returns Heading indicating which direction is forward from
397 * the requests#ForwardPerspectiveValue#BlueAlliance perspective
398 */
400 {
402 }
403
404 /**
405 * \brief Adds a vision measurement to the Kalman Filter. This will correct the
406 * odometry pose estimate while still accounting for measurement noise.
407 *
408 * This method can be called as infrequently as you want, as long as you are
409 * calling impl#SwerveDrivePoseEstimator#Update every loop.
410 *
411 * To promote stability of the pose estimate and make it robust to bad vision
412 * data, we recommend only adding vision measurements that are already within
413 * one meter or so of the current pose estimate.
414 *
415 * \param visionRobotPose The pose of the robot as measured by the
416 * vision camera.
417 * \param timestamp The timestamp of the vision measurement in
418 * seconds. Note that you must use a timestamp with an
419 * epoch since system startup (i.e., the epoch of this
420 * timestamp is the same epoch as utils#GetCurrentTime).
421 * This means that you should use utils#GetCurrentTime
422 * as your time source in this case.
423 * An FPGA timestamp can be converted to the correct
424 * timebase using utils#FPGAToCurrentTime.
425 */
426 virtual void AddVisionMeasurement(Pose2d visionRobotPose, units::second_t timestamp)
427 {
428 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp);
429 }
430
431 /**
432 * \brief Adds a vision measurement to the Kalman Filter. This will correct the
433 * odometry pose estimate while still accounting for measurement noise.
434 *
435 * This method can be called as infrequently as you want, as long as you are
436 * calling impl#SwerveDrivePoseEstimator#Update every loop.
437 *
438 * To promote stability of the pose estimate and make it robust to bad vision
439 * data, we recommend only adding vision measurements that are already within
440 * one meter or so of the current pose estimate.
441 *
442 * Note that the vision measurement standard deviations passed into this method
443 * will continue to apply to future measurements until a subsequent call to
444 * #SetVisionMeasurementStdDevs or this method.
445 *
446 * \param visionRobotPose The pose of the robot as measured by the
447 * vision camera.
448 * \param timestamp The timestamp of the vision measurement in
449 * seconds. Note that you must use a timestamp with an
450 * epoch since system startup (i.e., the epoch of this
451 * timestamp is the same epoch as utils#GetCurrentTime).
452 * This means that you should use utils#GetCurrentTime
453 * as your time source in this case.
454 * An FPGA timestamp can be converted to the correct
455 * timebase using utils#FPGAToCurrentTime.
456 * \param visionMeasurementStdDevs Standard deviations of the vision pose
457 * measurement (x position in meters, y position
458 * in meters, and heading in radians). Increase
459 * these numbers to trust the vision pose
460 * measurement less.
461 */
463 Pose2d visionRobotPose,
464 units::second_t timestamp,
465 std::array<double, 3> visionMeasurementStdDevs)
466 {
467 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp, visionMeasurementStdDevs);
468 }
469
470 /**
471 * \brief Sets the pose estimator's trust of global measurements. This might be used to
472 * change trust in vision measurements after the autonomous period, or to change
473 * trust as distance to a vision target increases.
474 *
475 * \param visionMeasurementStdDevs Standard deviations of the vision
476 * measurements. Increase these
477 * numbers to trust global measurements from
478 * vision less. This matrix is in the form [x,
479 * y, theta]ᵀ, with units in meters and radians.
480 */
481 virtual void SetVisionMeasurementStdDevs(std::array<double, 3> visionMeasurementStdDevs)
482 {
483 _drivetrain.SetVisionMeasurementStdDevs(visionMeasurementStdDevs);
484 }
485
486 /**
487 * \brief Return the pose at a given timestamp, if the buffer is not empty.
488 *
489 * \param timestamp The pose's timestamp. Note that you must use a timestamp
490 * with an epoch since system startup (i.e., the epoch of
491 * this timestamp is the same epoch as utils#GetCurrentTime).
492 * This means that you should use utils#GetCurrentTime
493 * as your time source in this case.
494 * An FPGA timestamp can be converted to the correct
495 * timebase using utils#FPGAToCurrentTime.
496 * \returns The pose at the given timestamp (or std::nullopt if the buffer is
497 * empty).
498 */
499 virtual std::optional<Pose2d> SamplePoseAt(units::second_t timestamp)
500 {
501 return _drivetrain.SamplePoseAt(timestamp);
502 }
503
504 /**
505 * \brief Get a reference to the module at the specified index.
506 * The index corresponds to the module described in the constructor.
507 *
508 * \param index Which module to get
509 * \returns Reference to SwerveModule
510 */
511 SwerveModule &GetModule(size_t index) { return *_modules.at(index); }
512 /**
513 * \brief Get a reference to the module at the specified index.
514 * The index corresponds to the module described in the constructor.
515 *
516 * \param index Which module to get
517 * \returns Reference to SwerveModule
518 */
519 SwerveModule const &GetModule(size_t index) const { return *_modules.at(index); }
520 /**
521 * \brief Get a reference to the full array of modules.
522 * The indexes correspond to the module described in the constructor.
523 *
524 * \returns Reference to the SwerveModule array
525 */
526 std::vector<std::unique_ptr<SwerveModule>> const &GetModules() const { return _modules; }
527
528 /**
529 * \brief Gets the locations of the swerve modules.
530 *
531 * \returns Reference to the array of swerve module locations
532 */
533 std::vector<Translation2d> const &GetModuleLocations() const { return _drivetrain.GetModuleLocations(); }
534
535 /**
536 * \brief Gets the current orientation of the robot as a frc#Rotation3d from
537 * the Pigeon 2 quaternion values.
538 *
539 * \returns The robot orientation as a frc#Rotation3d
540 */
541 frc::Rotation3d GetRotation3d() const
542 {
543 return _pigeon2.GetRotation3d();
544 }
545
546 /**
547 * \brief Gets this drivetrain's Pigeon 2 reference.
548 *
549 * This should be used only to access signals and change configurations that the
550 * swerve drivetrain does not configure itself.
551 *
552 * \returns This drivetrain's Pigeon 2 reference
553 */
555 {
556 return _pigeon2;
557 }
558};
559
560}
561}
562}
ctre::phoenix::StatusCode Apply(const Pigeon2Configuration &configs)
Applies the contents of the specified config to the device.
Definition CorePigeon2.hpp:270
Class description for the Pigeon 2 IMU sensor that measures orientation.
Definition Pigeon2.hpp:29
frc::Rotation3d GetRotation3d() const
Returns the orientation of the robot as a frc::Rotation3d created from the quaternion signals.
configs::Pigeon2Configurator & GetConfigurator()
Gets the configurator for this Pigeon2.
Definition CorePigeon2.hpp:1154
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:2202
Simplified swerve drive simulation class.
Definition SimSwerveDrivetrain.hpp:34
void Update(units::second_t dt, units::volt_t supplyVoltage, std::vector< std::unique_ptr< SwerveModule > > const &modulesToApply)
Definition SimSwerveDrivetrain.hpp:103
Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
Definition SwerveDrivetrain.hpp:44
virtual void UpdateSimState(units::second_t dt, units::volt_t supplyVoltage)
Updates all the simulation state variables for this drivetrain class.
Definition SwerveDrivetrain.hpp:175
void SetControl(Request &request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:242
OdometryThread & GetOdometryThread()
Gets a reference to the odometry thread.
Definition SwerveDrivetrain.hpp:205
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, units::hertz_t odometryUpdateFrequency, std::array< double, 3 > const &odometryStandardDeviation, std::array< double, 3 > const &visionStandardDeviation, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:127
SwerveModule & GetModule(size_t index)
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:511
hardware::Pigeon2 & GetPigeon2()
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrain.hpp:554
virtual ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrain.hpp:304
void SetControl(Request &&request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:259
std::vector< std::unique_ptr< SwerveModule > > const & GetModules() const
Get a reference to the full array of modules.
Definition SwerveDrivetrain.hpp:526
virtual void ResetTranslation(Translation2d const &translation)
Resets the translation of the robot pose without affecting rotation.
Definition SwerveDrivetrain.hpp:351
virtual std::optional< Pose2d > SamplePoseAt(units::second_t timestamp)
Return the pose at a given timestamp, if the buffer is not empty.
Definition SwerveDrivetrain.hpp:499
virtual void AddVisionMeasurement(Pose2d visionRobotPose, units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrain.hpp:426
virtual void SetVisionMeasurementStdDevs(std::array< double, 3 > visionMeasurementStdDevs)
Sets the pose estimator's trust of global measurements.
Definition SwerveDrivetrain.hpp:481
virtual void AddVisionMeasurement(Pose2d visionRobotPose, units::second_t timestamp, std::array< double, 3 > visionMeasurementStdDevs)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrain.hpp:462
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, units::hertz_t odometryUpdateFrequency, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:101
virtual void TareEverything()
Zero's this swerve drive's odometry entirely.
Definition SwerveDrivetrain.hpp:314
virtual void RegisterTelemetry(std::function< void(SwerveDriveState const &)> telemetryFunction)
Register the specified lambda to be executed whenever the SwerveDriveState is updated in the odometry...
Definition SwerveDrivetrain.hpp:293
frc::Rotation3d GetRotation3d() const
Gets the current orientation of the robot as a frc::Rotation3d from the Pigeon 2 quaternion values.
Definition SwerveDrivetrain.hpp:541
SwerveDriveState GetState() const
Gets the current state of the swerve drivetrain.
Definition SwerveDrivetrain.hpp:275
SwerveModule const & GetModule(size_t index) const
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:519
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:82
bool IsOnCANFD() const
Gets whether the drivetrain is on a CAN FD bus.
Definition SwerveDrivetrain.hpp:185
virtual void SeedFieldCentric()
Resets the rotation of the robot pose to 0 from the requests::ForwardPerspectiveValue::OperatorPerspe...
Definition SwerveDrivetrain.hpp:328
std::vector< Translation2d > const & GetModuleLocations() const
Gets the locations of the swerve modules.
Definition SwerveDrivetrain.hpp:533
virtual void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the robot pose without affecting translation.
Definition SwerveDrivetrain.hpp:363
units::hertz_t GetOdometryFrequency() const
Gets the target odometry update frequency.
Definition SwerveDrivetrain.hpp:195
Rotation2d GetOperatorForwardDirection() const
Returns the requests::ForwardPerspectiveValue::BlueAlliance perpective direction that is treated as t...
Definition SwerveDrivetrain.hpp:399
virtual void ResetPose(Pose2d const &pose)
Resets the pose of the robot.
Definition SwerveDrivetrain.hpp:339
static constexpr int kNumConfigAttempts
Number of times to attempt config applies.
Definition SwerveDrivetrain.hpp:58
virtual bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrain.hpp:215
virtual void SetOperatorPerspectiveForward(Rotation2d fieldDirection)
Takes the requests::ForwardPerspectiveValue::BlueAlliance perpective direction and treats it as the f...
Definition SwerveDrivetrain.hpp:383
impl::SwerveDrivetrainImpl _drivetrain
The underlying drivetrain instance.
Definition SwerveDrivetrain.hpp:61
impl::SwerveDriveKinematics const & GetKinematics() const
Gets a reference to the kinematics used for the drivetrain.
Definition SwerveDrivetrain.hpp:225
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:28
Class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states ...
Definition SwerveDriveKinematics.hpp:42
Performs swerve module updates in a separate thread to minimize latency.
Definition SwerveDrivetrainImpl.hpp:33
void Start()
Starts the odometry thread.
Definition SwerveDrivetrainImpl.hpp:70
Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
Definition SwerveDrivetrainImpl.hpp:30
void TareEverything()
Zero's this swerve drive's odometry entirely.
Definition SwerveDrivetrainImpl.hpp:435
void SetControl(SwerveRequestFunc &&request)
Applies the specified control function to this swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:355
std::vector< Translation2d > const & GetModuleLocations() const
Gets the locations of the swerve modules.
Definition SwerveDrivetrainImpl.hpp:683
units::hertz_t GetOdometryFrequency() const
Gets the target odometry update frequency.
Definition SwerveDrivetrainImpl.hpp:324
SwerveDriveState GetState() const
Gets the current state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:387
void SeedFieldCentric()
Resets the rotation of the robot pose to 0 from the requests::ForwardPerspectiveValue::OperatorPerspe...
Definition SwerveDrivetrainImpl.hpp:457
bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrainImpl.hpp:338
void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the robot pose without affecting translation.
Definition SwerveDrivetrainImpl.hpp:500
friend class OdometryThread
Definition SwerveDrivetrainImpl.hpp:170
void RegisterTelemetry(std::function< void(SwerveDriveState const &)> telemetryFunction)
Register the specified lambda to be executed whenever the SwerveDriveState is updated in the odometry...
Definition SwerveDrivetrainImpl.hpp:406
ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrainImpl.hpp:418
SwerveDriveKinematics const & GetKinematics() const
Gets a reference to the kinematics used for the drivetrain.
Definition SwerveDrivetrainImpl.hpp:348
SwerveModuleImpl & GetModule(size_t index)
Get a reference to the module at the specified index.
Definition SwerveDrivetrainImpl.hpp:661
void SetOperatorPerspectiveForward(Rotation2d fieldDirection)
Takes the requests::ForwardPerspectiveValue::BlueAlliance perpective direction and treats it as the f...
Definition SwerveDrivetrainImpl.hpp:524
Rotation2d GetOperatorForwardDirection() const
Returns the requests::ForwardPerspectiveValue::BlueAlliance perpective direction that is treated as t...
Definition SwerveDrivetrainImpl.hpp:541
OdometryThread & GetOdometryThread()
Gets a reference to the odometry thread.
Definition SwerveDrivetrainImpl.hpp:331
std::optional< Pose2d > SamplePoseAt(units::second_t timestamp)
Return the pose at a given timestamp, if the buffer is not empty.
Definition SwerveDrivetrainImpl.hpp:648
bool IsOnCANFD() const
Gets whether the drivetrain is on a CAN FD bus.
Definition SwerveDrivetrainImpl.hpp:317
void SetVisionMeasurementStdDevs(std::array< double, 3 > visionMeasurementStdDevs)
Sets the pose estimator's trust of global measurements.
Definition SwerveDrivetrainImpl.hpp:628
void ResetPose(Pose2d const &pose)
Resets the pose of the robot.
Definition SwerveDrivetrainImpl.hpp:468
void ResetTranslation(Translation2d const &translation)
Resets the translation of the robot pose without affecting rotation.
Definition SwerveDrivetrainImpl.hpp:484
void AddVisionMeasurement(Pose2d visionRobotPose, units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrainImpl.hpp:570
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition StatusCodes.h:18
Definition span.hpp:401
Common constants for a swerve drivetrain.
Definition SwerveDrivetrainConstants.hpp:19
std::optional< configs::Pigeon2Configuration > Pigeon2Configs
The configuration object to apply to the Pigeon2.
Definition SwerveDrivetrainConstants.hpp:43
Plain-Old-Data class holding the state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:121