45 std::derived_from<hardware::traits::CommonTalon> DriveMotorT,
46 std::derived_from<hardware::traits::CommonTalon> SteerMotorT,
49 requires std::same_as<EncoderT, hardware::CANcoder> ||
50 std::same_as<EncoderT, hardware::CANdi> ||
51 std::same_as<EncoderT, hardware::TalonFXS>
72 std::vector<std::unique_ptr<SwerveModule<DriveMotorT, SteerMotorT, EncoderT>>> _modules;
90 typename DriveMotorT::Configuration,
91 typename SteerMotorT::Configuration,
92 typename EncoderT::Configuration
114 typename DriveMotorT::Configuration,
115 typename SteerMotorT::Configuration,
116 typename EncoderT::Configuration
117 >>... ModuleConstants
121 units::hertz_t odometryUpdateFrequency,
122 ModuleConstants
const &... modules
124 SwerveDrivetrain{drivetrainConstants, odometryUpdateFrequency, std::array{0.1, 0.1, 0.1}, std::array{0.9, 0.9, 0.9}, modules...}
148 typename DriveMotorT::Configuration,
149 typename SteerMotorT::Configuration,
150 typename EncoderT::Configuration
151 >>... ModuleConstants
155 units::hertz_t odometryUpdateFrequency,
156 std::array<double, 3>
const &odometryStandardDeviation,
157 std::array<double, 3>
const &visionStandardDeviation,
158 ModuleConstants
const &... modules
161 drivetrainConstants, odometryUpdateFrequency,
162 odometryStandardDeviation, visionStandardDeviation,
163 std::span<
SwerveModuleConstants<typename DriveMotorT::Configuration, typename SteerMotorT::Configuration, typename EncoderT::Configuration> const>{
164 std::array{modules...}
167 _modules{CreateModuleArray(
CANBus{drivetrainConstants.CANBusName}, modules...)},
168 _pigeon2{drivetrainConstants.Pigeon2Id, CANBus{drivetrainConstants.CANBusName}},
169 _simDrive{_drivetrain.GetModuleLocations(), _pigeon2.GetSimState(), modules...}
171 if (drivetrainConstants.Pigeon2Configs) {
173 for (
int i = 0; i < kNumConfigAttempts; ++i) {
174 retval = GetPigeon2().GetConfigurator().Apply(*drivetrainConstants.Pigeon2Configs);
175 if (retval.IsOK())
break;
177 if (!retval.IsOK()) {
178 printf(
"Pigeon2 ID %d failed config with error: %s\n", GetPigeon2().GetDeviceID(), retval.GetName());
182 GetOdometryThread().Start();
188 template <
typename... ModuleConstants>
189 std::vector<std::unique_ptr<SwerveModule<DriveMotorT, SteerMotorT, EncoderT>>> CreateModuleArray(
191 ModuleConstants
const &... constants
193 std::vector<std::unique_ptr<SwerveModule<DriveMotorT, SteerMotorT, EncoderT>>> modules;
194 modules.reserve(
sizeof...(ModuleConstants));
196 [&]<
size_t... Idxs>(std::index_sequence<Idxs...>) {
198 }(std::index_sequence_for<ModuleConstants...>{});
213 _simDrive.Update(dt, supplyVoltage, _modules);
230 std::vector<hardware::traits::CommonDevice *> devices;
231 devices.reserve(_modules.size() * 3 + 1);
232 devices.push_back(&GetPigeon2());
233 for (
auto &module : _modules) {
234 devices.push_back(&module->GetDriveMotor());
235 devices.push_back(&module->GetSteerMotor());
236 devices.push_back(&module->GetEncoder());
258 std::vector<hardware::traits::CommonDevice *> devices;
259 devices.reserve(_modules.size() * 3 + 1);
260 devices.push_back(&GetPigeon2());
261 for (
auto &module : _modules) {
262 devices.push_back(&module->GetDriveMotor());
263 devices.push_back(&module->GetSteerMotor());
264 devices.push_back(&module->GetEncoder());
276 return _drivetrain.IsOnCANFD();
286 return _drivetrain.GetOdometryFrequency();
296 return _drivetrain.GetOdometryThread();
306 return _drivetrain.IsOdometryValid();
316 return _drivetrain.GetKinematics();
328 template <std::derived_from<requests::SwerveRequest> Request>
329 requires (!std::is_const_v<Request>)
332 _drivetrain.SetControl(
333 [&request](
auto const ¶ms,
auto modules)
mutable {
334 return request.Apply(params, modules);
344 template <std::derived_from<requests::SwerveRequest> Request>
345 requires (!std::is_const_v<Request>)
348 _drivetrain.SetControl(
349 [request=std::move(request)](
auto const ¶ms,
auto modules)
mutable {
350 return request.Apply(params, modules);
364 return _drivetrain.GetState();
382 _drivetrain.RegisterTelemetry(std::move(telemetryFunction));
395 return _drivetrain.ConfigNeutralMode(neutralMode);
407 return _drivetrain.ConfigNeutralMode(neutralMode, timeoutSeconds);
417 _drivetrain.TareEverything();
433 _drivetrain.SeedFieldCentric(rotation);
444 _drivetrain.ResetPose(pose);
456 _drivetrain.ResetTranslation(translation);
468 _drivetrain.ResetRotation(rotation);
488 _drivetrain.SetOperatorPerspectiveForward(fieldDirection);
504 return _drivetrain.GetOperatorForwardDirection();
530 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp);
564 Pose2d visionRobotPose,
565 units::second_t timestamp,
566 std::array<double, 3> visionMeasurementStdDevs)
568 _drivetrain.AddVisionMeasurement(std::move(visionRobotPose), timestamp, visionMeasurementStdDevs);
584 _drivetrain.SetVisionMeasurementStdDevs(visionMeasurementStdDevs);
597 _drivetrain.SetStateStdDevs(stateStdDevs);
613 virtual std::optional<Pose2d>
SamplePoseAt(units::second_t timestamp)
const
615 return _drivetrain.SamplePoseAt(timestamp);
627 return *_modules.at(index);
638 return *_modules.at(index);
646 std::span<std::unique_ptr<SwerveModule<DriveMotorT, SteerMotorT, EncoderT>>
const>
GetModules()
const
656 std::vector<Translation2d>
const &
GetModuleLocations()
const {
return _drivetrain.GetModuleLocations(); }
666 return _pigeon2.GetRotation3d();
Class for getting information about an available CAN bus.
Definition CANBus.hpp:19
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:290
Class description for the Pigeon 2 IMU sensor that measures orientation.
Definition Pigeon2.hpp:34
Simplified swerve drive simulation class.
Definition SimSwerveDrivetrain.hpp:42
Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
Definition SwerveDrivetrain.hpp:52
virtual void SetOperatorPerspectiveForward(Rotation2d fieldDirection)
Takes the requests::ForwardPerspectiveValue::BlueAlliance perpective direction and treats it as the f...
Definition SwerveDrivetrain.hpp:486
virtual void AddVisionMeasurement(Pose2d visionRobotPose, units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrain.hpp:528
std::span< std::unique_ptr< SwerveModule< DriveMotorT, SteerMotorT, EncoderT > > const > GetModules() const
Get a reference to the full array of modules.
Definition SwerveDrivetrain.hpp:646
virtual void UpdateSimState(units::second_t dt, units::volt_t supplyVoltage)
Updates all the simulation state variables for this drivetrain class.
Definition SwerveDrivetrain.hpp:211
SwerveModule< DriveMotorT, SteerMotorT, EncoderT > & GetModule(size_t index)
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:625
OdometryThread & GetOdometryThread()
Gets a reference to the odometry thread.
Definition SwerveDrivetrain.hpp:294
virtual void ResetTranslation(Translation2d const &translation)
Resets the translation of the robot pose without affecting rotation.
Definition SwerveDrivetrain.hpp:454
virtual void TareEverything()
Zero's this swerve drive's odometry entirely.
Definition SwerveDrivetrain.hpp:415
ctre::phoenix::StatusCode OptimizeBusUtilization()
Optimizes the bus utilization of all devices in the swerve drivetrain by reducing the update frequenc...
Definition SwerveDrivetrain.hpp:228
virtual void SetStateStdDevs(std::array< double, 3 > const &stateStdDevs)
Sets the pose estimator's trust in robot odometry.
Definition SwerveDrivetrain.hpp:595
ctre::phoenix::StatusCode OptimizeBusUtilization(units::frequency::hertz_t optimizedFreqHz)
Optimizes the bus utilization of all devices in the swerve drivetrain by reducing the update frequenc...
Definition SwerveDrivetrain.hpp:256
void SetControl(Request &request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:330
void SetControl(Request &&request)
Applies the specified control request to this swerve drivetrain.
Definition SwerveDrivetrain.hpp:346
virtual std::optional< Pose2d > SamplePoseAt(units::second_t timestamp) const
Return the pose at a given timestamp, if the buffer is not empty.
Definition SwerveDrivetrain.hpp:613
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:563
virtual bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrain.hpp:304
impl::SwerveDriveKinematics const & GetKinematics() const
Gets a reference to the kinematics used for the drivetrain.
Definition SwerveDrivetrain.hpp:314
virtual ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrain.hpp:393
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, units::hertz_t odometryUpdateFrequency, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:119
virtual ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrain.hpp:405
SwerveDriveState GetState() const
Gets the current state of the swerve drivetrain.
Definition SwerveDrivetrain.hpp:362
units::hertz_t GetOdometryFrequency() const
Gets the target odometry update frequency.
Definition SwerveDrivetrain.hpp:284
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:431
virtual frc::Rotation3d GetRotation3d() const
Gets the current orientation of the robot as a frc#Rotation3d from the Pigeon 2 quaternion values.
Definition SwerveDrivetrain.hpp:664
virtual void SetVisionMeasurementStdDevs(std::array< double, 3 > visionMeasurementStdDevs)
Sets the pose estimator's trust of global measurements.
Definition SwerveDrivetrain.hpp:582
virtual void ResetPose(Pose2d const &pose)
Resets the pose of the robot.
Definition SwerveDrivetrain.hpp:442
bool IsOnCANFD() const
Gets whether the drivetrain is on a CAN FD bus.
Definition SwerveDrivetrain.hpp:274
SwerveDrivetrain(SwerveDrivetrainConstants const &drivetrainConstants, ModuleConstants const &... modules)
Constructs a CTRE SwerveDrivetrain using the specified constants.
Definition SwerveDrivetrain.hpp:95
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:153
virtual ~SwerveDrivetrain()=default
virtual void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the robot pose without affecting translation.
Definition SwerveDrivetrain.hpp:466
hardware::Pigeon2 & GetPigeon2()
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrain.hpp:677
SwerveModule< DriveMotorT, SteerMotorT, EncoderT > const & GetModule(size_t index) const
Get a reference to the module at the specified index.
Definition SwerveDrivetrain.hpp:636
hardware::Pigeon2 const & GetPigeon2() const
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrain.hpp:689
static constexpr int kNumConfigAttempts
Number of times to attempt config applies.
Definition SwerveDrivetrain.hpp:66
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:380
impl::SwerveDrivetrainImpl _drivetrain
The underlying drivetrain instance.
Definition SwerveDrivetrain.hpp:69
std::vector< Translation2d > const & GetModuleLocations() const
Gets the locations of the swerve modules.
Definition SwerveDrivetrain.hpp:656
Rotation2d GetOperatorForwardDirection() const
Returns the requests::ForwardPerspectiveValue::BlueAlliance perpective direction that is treated as t...
Definition SwerveDrivetrain.hpp:502
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:52
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
friend class OdometryThread
Definition SwerveDrivetrainImpl.hpp:165
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:1603
Common constants for a swerve drivetrain.
Definition SwerveDrivetrainConstants.hpp:19
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:148
Plain-Old-Data class holding the state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:116