49 std::derived_from<hardware::traits::CommonTalon> DriveMotorT,
50 std::derived_from<hardware::traits::CommonTalon> SteerMotorT,
53 requires std::same_as<EncoderT, hardware::CANcoder> ||
54 std::same_as<EncoderT, hardware::CANdi> ||
55 std::same_as<EncoderT, hardware::TalonFXS>
94 std::vector<std::unique_ptr<SwerveModule>> _modules;
110 template <std::same_as<SwerveModuleConstants>... ModuleConstants>
128 template <std::same_as<SwerveModuleConstants>... ModuleConstants>
131 wpi::units::hertz_t odometryUpdateFrequency,
132 ModuleConstants
const &... modules
134 SwerveDrivetrain{drivetrainConstants, odometryUpdateFrequency, std::array{0.1, 0.1, 0.1}, std::array{0.9, 0.9, 0.9}, modules...}
156 template <std::same_as<SwerveModuleConstants>... ModuleConstants>
159 wpi::units::hertz_t odometryUpdateFrequency,
160 std::array<double, 3>
const &odometryStandardDeviation,
161 std::array<double, 3>
const &visionStandardDeviation,
162 ModuleConstants
const &... modules
165 drivetrainConstants, odometryUpdateFrequency,
166 odometryStandardDeviation, visionStandardDeviation,
169 _modules{CreateModuleArray(drivetrainConstants.Network, modules...)},
170 _pigeon2{drivetrainConstants.Pigeon2Id, drivetrainConstants.Network},
171 _simDrive{_drivetrain.GetModuleLocations(), _pigeon2.GetSimState(), modules...}
173 if (drivetrainConstants.Pigeon2Configs) {
174 ctre::phoenix::StatusCode retval{};
175 for (
int i = 0; i < kNumConfigAttempts; ++i) {
176 retval = GetPigeon2().GetConfigurator().Apply(*drivetrainConstants.Pigeon2Configs);
177 if (retval.
IsOK())
break;
179 if (!retval.
IsOK()) {
180 printf(
"Pigeon2 ID %d failed config with error: %s\n", GetPigeon2().GetDeviceID(), retval.
GetName());
184 GetOdometryThread().Start();
190 template <
typename... ModuleConstants>
191 std::vector<std::unique_ptr<SwerveModule>> CreateModuleArray(
193 ModuleConstants
const &... constants
195 std::vector<std::unique_ptr<SwerveModule>> modules;
196 modules.reserve(
sizeof...(ModuleConstants));
198 [&]<
size_t... Idxs>(std::index_sequence<Idxs...>) {
199 (modules.emplace_back(std::make_unique<SwerveModule>(constants, canbus,
_drivetrain.GetModule(Idxs))), ...);
200 }(std::index_sequence_for<ModuleConstants...>{});
213 virtual void UpdateSimState(wpi::units::second_t dt, wpi::units::volt_t supplyVoltage)
215 _simDrive.Update(dt, supplyVoltage, _modules);
232 std::vector<hardware::traits::CommonDevice *> devices;
233 devices.reserve(_modules.size() * 3 + 1);
235 for (
auto &module : _modules) {
236 devices.push_back(&module->GetDriveMotor());
237 devices.push_back(&module->GetSteerMotor());
238 devices.push_back(&module->GetEncoder());
260 std::vector<hardware::traits::CommonDevice *> devices;
261 devices.reserve(_modules.size() * 3 + 1);
263 for (
auto &module : _modules) {
264 devices.push_back(&module->GetDriveMotor());
265 devices.push_back(&module->GetSteerMotor());
266 devices.push_back(&module->GetEncoder());
330 template <std::derived_from<requests::SwerveRequest> Request>
331 requires (!std::is_const_v<Request>)
335 [&request](
auto const ¶ms,
auto modules)
mutable {
336 return request.Apply(params, modules);
346 template <std::derived_from<requests::SwerveRequest> Request>
347 requires (!std::is_const_v<Request>)
351 [request=std::move(request)](
auto const ¶ms,
auto modules)
mutable {
352 return request.Apply(params, modules);
384 _drivetrain.RegisterTelemetry(std::move(telemetryFunction));
409 return _drivetrain.ConfigNeutralMode(neutralMode, timeoutSeconds);
438 _drivetrain.SeedFieldCentric(rotation);
495 _drivetrain.SetOperatorForwardDirection(fieldDirection);
535 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp);
567 Pose2d visionRobotPose,
568 wpi::units::second_t timestamp,
569 std::array<double, 3> visionMeasurementStdDevs)
571 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp, visionMeasurementStdDevs);
587 _drivetrain.SetVisionMeasurementStdDevs(visionMeasurementStdDevs);
614 virtual std::optional<Pose2d>
SamplePoseAt(wpi::units::second_t timestamp)
const
628 return *_modules.at(index);
639 return *_modules.at(index);
647 std::span<std::unique_ptr<SwerveModule>
const>
GetModules()
const
667 return _pigeon2.GetRotation3d();
700#include <wpi/util/struct/Struct.hpp>
704struct wpi::util::Struct<
ctre::phoenix6::swerve::impl::SwerveDrivetrainImpl::SwerveDriveState, size_t> final {
710 static constexpr size_t GetSize(
size_t numModules)
712 return wpi::util::GetStructSize<ctre::phoenix6::swerve::Pose2d>() +
713 wpi::util::GetStructSize<ctre::phoenix6::swerve::ChassisVelocities>() +
715 wpi::util::GetStructSize<ctre::phoenix6::swerve::SwerveModulePosition>() +
716 2 * wpi::util::GetStructSize<ctre::phoenix6::swerve::SwerveModuleVelocity>()
718 wpi::util::GetStructSize<ctre::phoenix6::swerve::Rotation2d>() +
719 2 * wpi::util::GetStructSize<double>() +
720 2 * wpi::util::GetStructSize<int32_t>();
724 static SwerveDriveState
Unpack(std::span<uint8_t const> data,
size_t numModules);
725 static void Pack(std::span<uint8_t> data, SwerveDriveState
const &value,
size_t numModules);
727 static void ForEachNested(std::invocable<std::string_view, std::string_view>
auto fn,
size_t numModules)
729 wpi::util::ForEachStructSchema<ctre::phoenix6::swerve::Pose2d>(fn);
730 wpi::util::ForEachStructSchema<ctre::phoenix6::swerve::ChassisVelocities>(fn);
731 wpi::util::ForEachStructSchema<ctre::phoenix6::swerve::SwerveModulePosition>(fn);
732 wpi::util::ForEachStructSchema<ctre::phoenix6::swerve::SwerveModuleVelocity>(fn);
733 wpi::util::ForEachStructSchema<ctre::phoenix6::swerve::Rotation2d>(fn);
737static_assert(wpi::util::StructSerializable<ctre::phoenix6::swerve::impl::SwerveDrivetrainImpl::SwerveDriveState, size_t>);
738static_assert(wpi::util::HasNestedStruct<ctre::phoenix6::swerve::impl::SwerveDrivetrainImpl::SwerveDriveState, size_t>);
Class for getting information about an available CAN bus.
Definition CANBus.hpp:142
static ctre::phoenix::StatusCode OptimizeBusUtilizationForAll(Devices &... devices)
Optimizes the bus utilization of the provided devices by reducing the update frequencies of their sta...
Definition ParentDevice.hpp:315
Class description for the Pigeon 2 IMU sensor that measures orientation.
Definition Pigeon2.hpp:28
Simplified swerve drive simulation class.
Definition SimSwerveDrivetrain.hpp:42
virtual ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, wpi::units::second_t timeoutSeconds)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrain.hpp:407
OdometryThread & GetOdometryThread()
Gets a reference to the odometry thread.
Definition SwerveDrivetrain.hpp:296
virtual void UpdateSimState(wpi::units::second_t dt, wpi::units::volt_t supplyVoltage)
Updates all the simulation state variables for this drivetrain class.
Definition SwerveDrivetrain.hpp:213
virtual void ResetTranslation(Translation2d const &translation)
Resets the translation of the robot pose without affecting rotation.
Definition SwerveDrivetrain.hpp:459
virtual void TareEverything()
Zeroes this swerve drive's odometry entirely.
Definition SwerveDrivetrain.hpp:420
ctre::phoenix::StatusCode OptimizeBusUtilization()
Optimizes the bus utilization of all devices in the swerve drivetrain by reducing the update frequenc...
Definition SwerveDrivetrain.hpp:230
impl::SwerveDrivetrainImpl::OdometryThread OdometryThread
Performs swerve module updates in a separate thread to minimize latency.
Definition SwerveDrivetrain.hpp:59
swerve::SwerveModule< DriveMotorT, SteerMotorT, EncoderT > SwerveModule
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveDrivetrain.hpp:79
virtual std::optional< Pose2d > SamplePoseAt(wpi::units::second_t timestamp) const
Return the pose at a given timestamp, if the buffer is not empty.
Definition SwerveDrivetrain.hpp:614
virtual void SetStateStdDevs(std::array< double, 3 > const &stateStdDevs)
Sets the pose estimator's trust in robot odometry.
Definition SwerveDrivetrain.hpp:598
SwerveModule const & GetModule(size_t index) const
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:637
ctre::phoenix::StatusCode OptimizeBusUtilization(wpi::units::hertz_t optimizedFreqHz)
Optimizes the bus utilization of all devices in the swerve drivetrain by reducing the update frequenc...
Definition SwerveDrivetrain.hpp:258
SwerveModule & GetModule(size_t index)
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:626
std::span< std::unique_ptr< SwerveModule > const > GetModules() const
Get a reference to the full array of modules.
Definition SwerveDrivetrain.hpp:647
void SetControl(Request &request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:332
virtual void AddVisionMeasurement(Pose2d visionRobotPose, wpi::units::second_t timestamp, std::array< double, 3 > visionMeasurementStdDevs)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrain.hpp:566
void SetControl(Request &&request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:348
virtual bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrain.hpp:306
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, wpi::units::hertz_t odometryUpdateFrequency, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:129
virtual void AddVisionMeasurement(Pose2d visionRobotPose, wpi::units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrain.hpp:533
virtual wpi::math::Rotation3d GetRotation3d() const
Gets the current orientation of the robot as a wpi::math::Rotation3d from the Pigeon 2 quaternion val...
Definition SwerveDrivetrain.hpp:665
impl::SwerveDriveKinematics const & GetKinematics() const
Gets a reference to the kinematics used for the drivetrain.
Definition SwerveDrivetrain.hpp:316
virtual ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrain.hpp:395
SwerveDriveState GetState() const
Gets the current state of the swerve drivetrain.
Definition SwerveDrivetrain.hpp:364
virtual void SeedFieldCentric(Rotation2d const &rotation=Rotation2d{})
Resets the rotation of the robot pose to the given value from the requests::ForwardPerspectiveValue::...
Definition SwerveDrivetrain.hpp:436
virtual void SetOperatorForwardDirection(Rotation2d const &fieldDirection)
Takes the requests::ForwardPerspectiveValue::BlueAlliance perpective direction and treats it as the f...
Definition SwerveDrivetrain.hpp:493
virtual void SetVisionMeasurementStdDevs(std::array< double, 3 > visionMeasurementStdDevs)
Sets the pose estimator's trust of global measurements.
Definition SwerveDrivetrain.hpp:585
virtual void ResetPose(Pose2d const &pose)
Resets the pose of the robot.
Definition SwerveDrivetrain.hpp:447
bool IsOnCANFD() const
Gets whether the drivetrain is on a CAN FD bus.
Definition SwerveDrivetrain.hpp:276
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:111
impl::SwerveDrivetrainImpl::SwerveDriveState SwerveDriveState
Plain-Old-Data class holding the state of the swerve drivetrain.
Definition SwerveDrivetrain.hpp:66
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, wpi::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:157
SwerveModule::Constants SwerveModuleConstants
All constants for a swerve module.
Definition SwerveDrivetrain.hpp:84
virtual ~SwerveDrivetrain()=default
virtual void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the robot pose without affecting translation.
Definition SwerveDrivetrain.hpp:471
hardware::Pigeon2 & GetPigeon2()
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrain.hpp:678
hardware::Pigeon2 const & GetPigeon2() const
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrain.hpp:690
wpi::units::hertz_t GetOdometryFrequency() const
Gets the target odometry update frequency.
Definition SwerveDrivetrain.hpp:286
static constexpr int kNumConfigAttempts
Number of times to attempt config applies.
Definition SwerveDrivetrain.hpp:88
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:382
impl::SwerveDrivetrainImpl _drivetrain
The underlying drivetrain instance.
Definition SwerveDrivetrain.hpp:91
std::vector< Translation2d > const & GetModuleLocations() const
Gets the locations of the swerve modules.
Definition SwerveDrivetrain.hpp:657
Rotation2d GetOperatorForwardDirection() const
Returns the requests::ForwardPerspectiveValue::BlueAlliance perpective direction that is treated as t...
Definition SwerveDrivetrain.hpp:509
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:56
SwerveModuleConstants< typename DriveMotorT::Configuration, typename SteerMotorT::Configuration, typename EncoderT::Configuration > Constants
All constants for a swerve module.
Definition SwerveModule.hpp:66
Class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states ...
Definition SwerveDriveKinematics.hpp:41
Performs swerve module updates in a separate thread to minimize latency.
Definition SwerveDrivetrainImpl.hpp:33
Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
Definition SwerveDrivetrainImpl.hpp:30
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
constexpr const char * GetName() const
Gets the name of this StatusCode.
Definition StatusCodes.h:867
constexpr bool IsOK() const
Definition StatusCodes.h:860
Definition SwerveModule.hpp:28
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:1499
Common constants for a swerve drivetrain.
Definition SwerveDrivetrainConstants.hpp:19
Plain-Old-Data class holding the state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:118
static SwerveDriveState Unpack(std::span< uint8_t const > data, size_t numModules)
static std::string_view GetTypeName(size_t numModules)
static void Pack(std::span< uint8_t > data, SwerveDriveState const &value, size_t numModules)
static std::string_view GetSchema(size_t numModules)
static constexpr size_t GetSize(size_t numModules)
Definition SwerveDrivetrain.hpp:710
static void ForEachNested(std::invocable< std::string_view, std::string_view > auto fn, size_t numModules)
Definition SwerveDrivetrain.hpp:727