YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
SwerveDriveConfig.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Yet Another Software Suite
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
6#include <functional>
7#include <optional>
8#include <string>
9#include <vector>
10#include <wpi/commands2/SubsystemBase.hpp>
11#include <wpi/math/controller/AntiTipping.hpp>
12#include <wpi/math/controller/PIDController.hpp>
13#include <wpi/math/geometry/Pose2d.hpp>
14#include <wpi/math/geometry/Rotation2d.hpp>
15#include <wpi/math/geometry/Rotation3d.hpp>
16#include <wpi/math/geometry/Translation2d.hpp>
17#include <wpi/math/kinematics/ChassisVelocities.hpp>
18#include <wpi/units/angle.hpp>
19#include <wpi/units/angular_velocity.hpp>
20#include <wpi/units/length.hpp>
21#include <wpi/units/time.hpp>
22#include <wpi/units/velocity.hpp>
23#include <wpi/util/Alert.hpp>
24
30
32
39 public:
41
42 SwerveDriveConfig() = default;
43
44 // ---- Builder methods -------------------------------------------------------
45
52 SwerveDriveConfig& WithSubsystem(wpi::cmd::SubsystemBase* subsystem);
53
60 SwerveDriveConfig& WithModules(std::vector<SwerveModule*> modules);
61
71 SwerveDriveConfig& WithGyro(std::function<wpi::units::degree_t()> gyroSupplier);
72
79 SwerveDriveConfig& WithGyro(std::function<wpi::math::Rotation3d()> gyroSupplier);
80
90
102 SwerveDriveConfig& WithAntiTipping(wpi::units::meters_per_second_t kP,
103 wpi::units::degree_t tippingThreshold,
104 wpi::units::meters_per_second_t maxCorrectionSpeed);
105
112 SwerveDriveConfig& WithAntiTipping(std::optional<wpi::math::AntiTipping> antiTipping);
113
122
132 std::function<wpi::units::degrees_per_second_t()> angularVelocitySupplier);
133
140 SwerveDriveConfig& WithGyroOffset(wpi::units::degree_t offset);
141
149
156 SwerveDriveConfig& WithStartingPose(wpi::math::Pose2d pose);
157
165 SwerveDriveConfig& WithMaximumChassisSpeed(wpi::units::meters_per_second_t speed,
166 wpi::units::degrees_per_second_t angularVelocity);
167
174 SwerveDriveConfig& WithMaximumModuleSpeed(wpi::units::meters_per_second_t speed);
175
182 SwerveDriveConfig& WithCenterOfRotation(wpi::math::Translation2d center);
183
191 SwerveDriveConfig& WithCenterOfRotation(wpi::units::meter_t forward, wpi::units::meter_t left);
192
199 SwerveDriveConfig& WithDiscretizationTime(wpi::units::second_t dt);
200
210
218
228
235 SwerveDriveConfig& WithTranslationController(wpi::math::PIDController controller);
236
245 SwerveDriveConfig& WithRotationController(wpi::math::PIDController controller);
246
255 SwerveDriveConfig& WithSimTranslationController(wpi::math::PIDController controller);
256
265 SwerveDriveConfig& WithSimRotationController(wpi::math::PIDController controller);
266
274 SwerveDriveConfig& WithTelemetry(const std::string& name, TelemetryVerbosity verbosity);
275
284 SwerveDriveConfig& WithTelemetry(const std::string& name,
286
287 // ---- Getters ---------------------------------------------------------------
288
290 const std::vector<SwerveModule*>& GetModules() const;
291
293 wpi::cmd::SubsystemBase* GetSubsystem() const;
294
296 wpi::math::Pose2d GetInitialPose() const;
297
299 std::optional<wpi::units::meters_per_second_t> GetMaximumChassisLinearVelocity() const;
300
302 std::optional<wpi::units::degrees_per_second_t> GetMaximumChassisAngularVelocity() const;
304 std::optional<wpi::units::meters_per_second_t> GetMaximumModuleLinearVelocity() const;
306 std::optional<wpi::math::Translation2d> GetCenterOfRotation() const;
308 std::optional<TelemetryVerbosity> GetTelemetryVerbosity() const;
310 const std::string& GetTelemetryName() const;
319 std::optional<telemetry::SwerveDriveTelemetryConfig> GetSwerveDriveTelemetryConfig();
320
324 wpi::units::degree_t GetGyroOffset() const;
325
331 wpi::units::degree_t GetGyroAngle() const;
332
341 wpi::math::Rotation3d GetGyroRotation3d() const;
342
349 wpi::math::ChassisVelocities GetAntiTippingCorrection() const;
350
356 std::optional<bool> GetModuleStateOptimization() const;
357
363 std::optional<wpi::units::second_t> GetDiscretization() const;
364
371 wpi::math::ChassisVelocities OptimizeRobotRelativeChassisSpeeds(
372 wpi::math::ChassisVelocities speeds) const;
373
380 std::optional<std::reference_wrapper<wpi::math::PIDController>> GetTranslationPID();
381
388 std::optional<std::reference_wrapper<wpi::math::PIDController>> GetRotationPID();
389
391 static wpi::math::Translation2d CubeTranslation(wpi::math::Translation2d translation);
392
394 static wpi::math::Translation2d ScaleTranslation(wpi::math::Translation2d translation,
395 double scalar);
396
397 private:
398 std::vector<SwerveModule*> m_modules;
399 wpi::cmd::SubsystemBase* m_subsystem{nullptr};
400 std::string m_telemetryName{"swerve"};
401 std::optional<TelemetryVerbosity> m_telemetryVerbosity;
402 std::optional<telemetry::SwerveDriveTelemetryConfig> m_specifiedTelemetryConfig;
403 std::optional<std::function<wpi::math::Rotation3d()>> m_gyroSupplier;
404 config::GyroAxis m_gyroHeadingAxis{config::GyroAxis::YAW};
405 // Heading unwrapped across the half rotation boundary, for deriving the angular velocity.
406 mutable double m_continuousHeadingRadians{0};
407 mutable bool m_continuousHeadingRead{false};
408 mutable math::DerivativeTimeFilter m_gyroAngularVelocityFilter{wpi::units::millisecond_t{20}};
409 mutable std::optional<wpi::util::Alert> m_noGyroAngularVelocitySupplierAlert;
410 std::optional<std::function<wpi::units::degrees_per_second_t()>> m_gyroAngularVelocitySupplier;
411 std::optional<wpi::units::degree_t> m_gyroOffset;
412 bool m_gyroInverted{false};
413 wpi::math::Pose2d m_initialPose;
414 std::optional<wpi::units::meters_per_second_t> m_maximumChassisLinearVelocity;
415 std::optional<wpi::units::degrees_per_second_t> m_maximumChassisAngularVelocity;
416 std::optional<wpi::units::meters_per_second_t> m_maximumModuleLinearVelocity;
417 std::optional<bool> m_moduleStateOptimization;
418 std::optional<wpi::math::AntiTipping> m_antiTipping;
419 std::optional<wpi::units::second_t> m_discretizationSeconds;
420 std::optional<wpi::units::second_t> m_simDiscretizationSeconds;
421 std::optional<double> m_angularVelocityScaleFactor;
422 std::optional<double> m_simAngularVelocityScaleFactor;
423 std::optional<wpi::math::Translation2d> m_centerOfRotation;
424 std::optional<wpi::math::PIDController> m_translationController;
425 std::optional<wpi::math::PIDController> m_rotationController;
426 std::optional<wpi::math::PIDController> m_simTranslationController;
427 std::optional<wpi::math::PIDController> m_simRotationController;
428
429 wpi::math::ChassisVelocities AngularVelocitySkewCorrection(
430 wpi::math::ChassisVelocities robotRelativeSpeeds) const;
431};
432
433} // namespace yams::mechanisms::swerve
Definition SwerveDriveConfig.hpp:38
SwerveDriveConfig & WithSimRotationController(wpi::math::PIDController controller)
wpi::math::ChassisVelocities OptimizeRobotRelativeChassisSpeeds(wpi::math::ChassisVelocities speeds) const
wpi::math::Pose2d GetInitialPose() const
wpi::math::Rotation3d GetGyroRotation3d() const
SwerveDriveConfig & WithSimDiscretizationTime(wpi::units::second_t dt)
static wpi::math::Translation2d CubeTranslation(wpi::math::Translation2d translation)
std::optional< telemetry::SwerveDriveTelemetryConfig > GetSwerveDriveTelemetryConfig()
std::optional< wpi::units::meters_per_second_t > GetMaximumChassisLinearVelocity() const
wpi::units::degree_t GetGyroOffset() const
SwerveDriveConfig & WithModules(std::vector< SwerveModule * > modules)
std::optional< wpi::units::degrees_per_second_t > GetMaximumChassisAngularVelocity() const
const std::vector< SwerveModule * > & GetModules() const
std::optional< bool > GetModuleStateOptimization() const
wpi::math::ChassisVelocities GetAntiTippingCorrection() const
SwerveDriveConfig & WithSimGyroAngularVelocityScaleFactor(double scaleFactor)
std::optional< std::reference_wrapper< wpi::math::PIDController > > GetRotationPID()
SwerveDriveConfig & WithMaximumModuleSpeed(wpi::units::meters_per_second_t speed)
std::optional< wpi::units::meters_per_second_t > GetMaximumModuleLinearVelocity() const
SwerveDriveConfig & WithMaximumChassisSpeed(wpi::units::meters_per_second_t speed, wpi::units::degrees_per_second_t angularVelocity)
static wpi::math::Translation2d ScaleTranslation(wpi::math::Translation2d translation, double scalar)
SwerveDriveConfig & WithSimTranslationController(wpi::math::PIDController controller)
SwerveDriveConfig & WithCenterOfRotation(wpi::math::Translation2d center)
wpi::cmd::SubsystemBase * GetSubsystem() const
std::optional< wpi::units::second_t > GetDiscretization() const
SwerveDriveConfig & WithTelemetry(const std::string &name, telemetry::SwerveDriveTelemetryConfig telemetryConfig)
SwerveDriveConfig & WithCenterOfRotation(wpi::units::meter_t forward, wpi::units::meter_t left)
SwerveDriveConfig & WithTelemetry(const std::string &name, TelemetryVerbosity verbosity)
SwerveDriveConfig & WithDiscretizationTime(wpi::units::second_t dt)
SwerveDriveConfig & WithRotationController(wpi::math::PIDController controller)
SwerveDriveConfig & WithStartingPose(wpi::math::Pose2d pose)
SwerveDriveConfig & WithGyroHeadingAxis(config::GyroAxis axis)
SwerveDriveConfig & WithGyro(std::function< wpi::math::Rotation3d()> gyroSupplier)
SwerveDriveConfig & WithAntiTipping(wpi::units::meters_per_second_t kP, wpi::units::degree_t tippingThreshold, wpi::units::meters_per_second_t maxCorrectionSpeed)
SwerveDriveConfig & WithModuleStateOptimization(bool enabled)
SwerveDriveConfig & WithGyroAngularVelocityScaleFactor(double scaleFactor)
SwerveDriveConfig & WithGyroInverted(bool inverted)
SwerveDriveConfig & WithAntiTipping(std::optional< wpi::math::AntiTipping > antiTipping)
SwerveDriveConfig & WithGyro(std::function< wpi::units::degree_t()> gyroSupplier)
wpi::units::degree_t GetGyroAngle() const
SwerveDriveConfig & WithSubsystem(wpi::cmd::SubsystemBase *subsystem)
SwerveDriveConfig & WithGyroVelocity(std::function< wpi::units::degrees_per_second_t()> angularVelocitySupplier)
const std::string & GetTelemetryName() const
std::optional< TelemetryVerbosity > GetTelemetryVerbosity() const
SwerveDriveConfig & WithGyroOffset(wpi::units::degree_t offset)
SwerveDriveConfig & WithTranslationController(wpi::math::PIDController controller)
std::optional< std::reference_wrapper< wpi::math::PIDController > > GetTranslationPID()
std::optional< wpi::math::Translation2d > GetCenterOfRotation() const
TelemetryVerbosity
Definition SmartMotorControllerConfig.hpp:72
Definition SwerveDriveTelemetryConfig.hpp:51
GyroAxis
Definition GyroAxis.hpp:20
Definition SwerveDrive.hpp:49