71 _isRunning.store(
true, std::memory_order_relaxed);
83 _isRunning.store(
false, std::memory_order_relaxed);
175 std::vector<std::unique_ptr<SwerveModuleImpl>> _modules;
177 std::vector<Translation2d> _moduleLocations;
178 std::vector<SwerveModulePosition> _modulePositions;
179 std::vector<SwerveModuleVelocity> _moduleVelocities;
184 Rotation2d _operatorForwardDirection{};
187 ControlParameters _requestParameters{};
189 mutable std::recursive_mutex _stateLock;
190 SwerveDriveState _cachedState{};
191 std::function<void(SwerveDriveState
const &)> _telemetryFunction{};
194 wpi::units::hertz_t _updateFrequency;
196 std::unique_ptr<OdometryThread> _odometryThread;
209 std::derived_from<configs::ParentConfiguration> DriveMotorConfigsT,
210 std::derived_from<configs::ParentConfiguration> SteerMotorConfigsT,
211 std::derived_from<configs::ParentConfiguration> EncoderConfigsT
233 std::derived_from<configs::ParentConfiguration> DriveMotorConfigsT,
234 std::derived_from<configs::ParentConfiguration> SteerMotorConfigsT,
235 std::derived_from<configs::ParentConfiguration> EncoderConfigsT
239 wpi::units::hertz_t odometryUpdateFrequency,
242 SwerveDrivetrainImpl{drivetrainConstants, odometryUpdateFrequency, std::array{0.1, 0.1, 0.1}, std::array{0.9, 0.9, 0.9}, modules}
264 std::derived_from<configs::ParentConfiguration> DriveMotorConfigsT,
265 std::derived_from<configs::ParentConfiguration> SteerMotorConfigsT,
266 std::derived_from<configs::ParentConfiguration> EncoderConfigsT
270 wpi::units::hertz_t odometryUpdateFrequency,
271 std::array<double, 3>
const &odometryStandardDeviation,
272 std::array<double, 3>
const &visionStandardDeviation,
304 return _odometryThread->IsOdometryValid();
321 std::lock_guard<std::recursive_mutex> lock{_stateLock};
323 _requestToApply = std::move(request);
340 std::lock_guard<std::recursive_mutex> lock{_stateLock};
341 return request(_requestParameters, _modules);
353 std::lock_guard<std::recursive_mutex> lock{_stateLock};
372 std::lock_guard<std::recursive_mutex> lock{_stateLock};
373 _telemetryFunction = std::move(telemetryFunction);
386 for (
auto &module : _modules) {
387 auto status =
module->ConfigNeutralMode(neutralMode, timeoutSeconds);
405 std::lock_guard<std::recursive_mutex> lock{_stateLock};
407 for (
size_t i = 0; i < _modules.size(); ++i) {
408 _modules[i]->ResetPosition();
410 _odometryThread->_allSignals.WaitForAll(2.0 / _updateFrequency);
412 for (
size_t i = 0; i < _modules.size(); ++i) {
413 _modulePositions[i] = _modules[i]->GetPosition(
false);
414 _cachedState.ModulePositions[i] = _modulePositions[i];
416 _odometry.ResetPosition({_pigeonYaw.GetValue()}, _modulePositions, Pose2d{});
418 _cachedState.Pose = _odometry.GetEstimatedPosition();
445 std::lock_guard<std::recursive_mutex> lock{_stateLock};
447 _odometry.ResetPose(pose);
449 _cachedState.Pose = _odometry.GetEstimatedPosition();
461 std::lock_guard<std::recursive_mutex> lock{_stateLock};
463 _odometry.ResetTranslation(translation);
465 _cachedState.Pose = _odometry.GetEstimatedPosition();
477 std::lock_guard<std::recursive_mutex> lock{_stateLock};
479 _odometry.ResetRotation(rotation);
481 _cachedState.Pose = _odometry.GetEstimatedPosition();
503 std::lock_guard<std::recursive_mutex> lock{_stateLock};
504 _operatorForwardDirection = fieldDirection;
520 std::lock_guard<std::recursive_mutex> lock{_stateLock};
521 return _operatorForwardDirection;
549 std::lock_guard<std::recursive_mutex> lock{_stateLock};
550 _odometry.AddVisionMeasurement(visionRobotPose, timestamp);
586 Pose2d visionRobotPose,
587 wpi::units::second_t timestamp,
588 std::array<double, 3>
const &visionMeasurementStdDevs)
590 std::lock_guard<std::recursive_mutex> lock{_stateLock};
591 _odometry.AddVisionMeasurement(visionRobotPose, timestamp, visionMeasurementStdDevs);
607 std::lock_guard<std::recursive_mutex> lock{_stateLock};
608 _odometry.SetVisionMeasurementStdDevs(visionMeasurementStdDevs);
621 std::lock_guard<std::recursive_mutex> lock{_stateLock};
622 _odometry.SetStateStdDevs(stateStdDevs);
639 std::optional<Pose2d>
SamplePoseAt(wpi::units::second_t timestamp)
const
641 std::lock_guard<std::recursive_mutex> lock{_stateLock};
642 return _odometry.SampleAt(timestamp);
667 std::span<std::unique_ptr<SwerveModuleImpl>
const>
GetModules()
const {
return _modules; }
Class for getting information about an available CAN bus.
Definition CANBus.hpp:142
Class to manage bulk refreshing device status signals.
Definition StatusSignalCollection.hpp:21
Represents a status signal with data of type T, and operations available to retrieve information abou...
Definition StatusSignal.hpp:523
Class for the Pigeon 2 IMU sensor that measures orientation.
Definition CorePigeon2.hpp:1058
Class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states ...
Definition SwerveDriveKinematics.hpp:41
This class wraps Swerve Drive Odometry to fuse latency-compensated vision measurements with swerve dr...
Definition SwerveDrivePoseEstimator.hpp:143
Performs swerve module updates in a separate thread to minimize latency.
Definition SwerveDrivetrainImpl.hpp:33
std::thread _thread
Definition SwerveDrivetrainImpl.hpp:43
std::atomic< bool > _isRunning
Definition SwerveDrivetrainImpl.hpp:45
OdometryThread(SwerveDrivetrainImpl &drivetrain)
StatusSignalCollection _encSignals
Definition SwerveDrivetrainImpl.hpp:48
~OdometryThread()
Definition SwerveDrivetrainImpl.hpp:59
bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrainImpl.hpp:93
void Stop()
Stops the odometry thread.
Definition SwerveDrivetrainImpl.hpp:79
StatusSignalCollection _allSignals
Definition SwerveDrivetrainImpl.hpp:47
std::atomic< int > _threadPriorityToSet
Definition SwerveDrivetrainImpl.hpp:54
friend class SwerveDrivetrainImpl
Definition SwerveDrivetrainImpl.hpp:34
static constexpr int START_THREAD_PRIORITY
Definition SwerveDrivetrainImpl.hpp:37
std::atomic< int32_t > _failedDaqs
Definition SwerveDrivetrainImpl.hpp:52
void Start()
Starts the odometry thread.
Definition SwerveDrivetrainImpl.hpp:67
wpi::units::second_t _averageLoopTime
Definition SwerveDrivetrainImpl.hpp:50
SwerveDrivetrainImpl * _drivetrain
Definition SwerveDrivetrainImpl.hpp:41
void SetThreadPriority(int priority)
Sets the odometry thread priority to a real time priority under the specified priority level.
Definition SwerveDrivetrainImpl.hpp:104
std::atomic< int32_t > _successfulDaqs
Definition SwerveDrivetrainImpl.hpp:51
std::mutex _threadMtx
Definition SwerveDrivetrainImpl.hpp:44
int _lastThreadPriority
Definition SwerveDrivetrainImpl.hpp:55
void SetOperatorForwardDirection(Rotation2d const &fieldDirection)
Takes the requests::ForwardPerspectiveValue::BlueAlliance perpective direction and treats it as the f...
Definition SwerveDrivetrainImpl.hpp:501
void TareEverything()
Zeroes this swerve drive's odometry entirely.
Definition SwerveDrivetrainImpl.hpp:403
void SetControl(SwerveRequestFunc &&request)
Applies the specified control function to this swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:319
std::vector< Translation2d > const & GetModuleLocations() const
Gets the locations of the swerve modules.
Definition SwerveDrivetrainImpl.hpp:674
ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, wpi::units::second_t timeoutSeconds=0.100_s)
Configures the neutral mode to use for all modules' drive motors.
Definition SwerveDrivetrainImpl.hpp:383
SwerveDriveState GetState() const
Gets the current state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:351
SwerveDrivetrainImpl(SwerveDrivetrainConstants const &drivetrainConstants, wpi::units::hertz_t odometryUpdateFrequency, std::span< SwerveModuleConstants< DriveMotorConfigsT, SteerMotorConfigsT, EncoderConfigsT > const > modules)
Constructs a CTRE SwerveDrivetrainImpl using the specified constants.
Definition SwerveDrivetrainImpl.hpp:237
SwerveModuleImpl const & GetModule(size_t index) const
Get a reference to the module at the specified index.
Definition SwerveDrivetrainImpl.hpp:660
bool IsOdometryValid() const
Check if the odometry is currently valid.
Definition SwerveDrivetrainImpl.hpp:302
void ResetRotation(Rotation2d const &rotation)
Resets the rotation of the robot pose without affecting translation.
Definition SwerveDrivetrainImpl.hpp:475
hardware::core::CorePigeon2 & GetPigeon2()
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrainImpl.hpp:684
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:370
void SetVisionMeasurementStdDevs(std::array< double, 3 > const &visionMeasurementStdDevs)
Sets the pose estimator's trust of global measurements.
Definition SwerveDrivetrainImpl.hpp:605
void SetStateStdDevs(std::array< double, 3 > const &stateStdDevs)
Sets the pose estimator's trust in robot odometry.
Definition SwerveDrivetrainImpl.hpp:619
std::span< std::unique_ptr< SwerveModuleImpl > const > GetModules() const
Get a reference to the full array of modules.
Definition SwerveDrivetrainImpl.hpp:667
SwerveDriveKinematics const & GetKinematics() const
Gets a reference to the kinematics used for the drivetrain.
Definition SwerveDrivetrainImpl.hpp:312
wpi::units::hertz_t GetOdometryFrequency() const
Gets the target odometry update frequency.
Definition SwerveDrivetrainImpl.hpp:288
SwerveModuleImpl & GetModule(size_t index)
Get a reference to the module at the specified index.
Definition SwerveDrivetrainImpl.hpp:652
std::function< ctre::phoenix::StatusCode(ControlParameters const &, std::span< std::unique_ptr< SwerveModuleImpl > const >)> SwerveRequestFunc
Definition SwerveDrivetrainImpl.hpp:164
Rotation2d GetOperatorForwardDirection() const
Returns the requests::ForwardPerspectiveValue::BlueAlliance perpective direction that is treated as t...
Definition SwerveDrivetrainImpl.hpp:518
OdometryThread & GetOdometryThread()
Gets a reference to the odometry thread.
Definition SwerveDrivetrainImpl.hpp:295
hardware::core::CorePigeon2 const & GetPigeon2() const
Gets this drivetrain's Pigeon 2 reference.
Definition SwerveDrivetrainImpl.hpp:693
bool IsOnCANFD() const
Gets whether the drivetrain is on a CAN FD bus.
Definition SwerveDrivetrainImpl.hpp:281
std::optional< Pose2d > SamplePoseAt(wpi::units::second_t timestamp) const
Return the pose at a given timestamp, if the buffer is not empty.
Definition SwerveDrivetrainImpl.hpp:639
void SeedFieldCentric(Rotation2d const &rotation=Rotation2d{})
Resets the rotation of the robot pose to the given value from the requests::ForwardPerspectiveValue::...
Definition SwerveDrivetrainImpl.hpp:432
ctre::phoenix::StatusCode RunTempRequest(SwerveRequestFunc &&request) const
Immediately runs the provided temporary control function.
Definition SwerveDrivetrainImpl.hpp:338
void AddVisionMeasurement(Pose2d visionRobotPose, wpi::units::second_t timestamp, std::array< double, 3 > const &visionMeasurementStdDevs)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrainImpl.hpp:585
SwerveDrivetrainImpl(SwerveDrivetrainConstants const &drivetrainConstants, wpi::units::hertz_t odometryUpdateFrequency, std::array< double, 3 > const &odometryStandardDeviation, std::array< double, 3 > const &visionStandardDeviation, std::span< SwerveModuleConstants< DriveMotorConfigsT, SteerMotorConfigsT, EncoderConfigsT > const > modules)
Constructs a CTRE SwerveDrivetrainImpl using the specified constants.
void ResetPose(Pose2d const &pose)
Resets the pose of the robot.
Definition SwerveDrivetrainImpl.hpp:443
void AddVisionMeasurement(Pose2d visionRobotPose, wpi::units::second_t timestamp)
Adds a vision measurement to the Kalman Filter.
Definition SwerveDrivetrainImpl.hpp:547
void ResetTranslation(Translation2d const &translation)
Resets the translation of the robot pose without affecting rotation.
Definition SwerveDrivetrainImpl.hpp:459
SwerveDrivetrainImpl(SwerveDrivetrainConstants const &drivetrainConstants, std::span< SwerveModuleConstants< DriveMotorConfigsT, SteerMotorConfigsT, EncoderConfigsT > const > modules)
Constructs a CTRE SwerveDrivetrainImpl using the specified constants.
Definition SwerveDrivetrainImpl.hpp:213
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModuleImpl.hpp:61
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
static constexpr int OK
No Error.
Definition StatusCodes.h:35
constexpr bool IsOK() const
Definition StatusCodes.h:860
Definition SwerveDrivetrainImpl.hpp:21
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
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:152
Contains everything the control requests need to calculate the module state.
Definition SwerveDrivetrainImpl.hpp:144
wpi::units::second_t updatePeriod
The update period of control apply.
Definition SwerveDrivetrainImpl.hpp:161
ChassisVelocities currentChassisVelocity
The current robot-centric chassis velocity.
Definition SwerveDrivetrainImpl.hpp:155
wpi::units::meters_per_second_t kMaxSpeed
The max speed of the robot at 12 V output.
Definition SwerveDrivetrainImpl.hpp:150
impl::SwerveDriveKinematics * kinematics
The kinematics object used for control.
Definition SwerveDrivetrainImpl.hpp:146
Pose2d currentPose
The current pose of the robot.
Definition SwerveDrivetrainImpl.hpp:157
Rotation2d operatorForwardDirection
The forward direction from the operator perspective.
Definition SwerveDrivetrainImpl.hpp:153
Translation2d const * moduleLocations
The locations of the swerve modules.
Definition SwerveDrivetrainImpl.hpp:148
wpi::units::second_t timestamp
The timestamp of the current control apply, in the timebase of utils::GetCurrentTime().
Definition SwerveDrivetrainImpl.hpp:159
Plain-Old-Data class holding the state of the swerve drivetrain.
Definition SwerveDrivetrainImpl.hpp:118
wpi::units::second_t OdometryPeriod
The measured odometry update period.
Definition SwerveDrivetrainImpl.hpp:134
ChassisVelocities Velocity
The current robot-centric velocity.
Definition SwerveDrivetrainImpl.hpp:122
std::vector< SwerveModuleVelocity > ModuleVelocities
The current module velocities.
Definition SwerveDrivetrainImpl.hpp:126
int32_t SuccessfulDaqs
Number of successful data acquisitions.
Definition SwerveDrivetrainImpl.hpp:136
std::vector< SwerveModulePosition > ModulePositions
The current module positions.
Definition SwerveDrivetrainImpl.hpp:124
Pose2d Pose
The current pose of the robot.
Definition SwerveDrivetrainImpl.hpp:120
std::vector< SwerveModuleVelocity > ModuleTargets
The target module velocities.
Definition SwerveDrivetrainImpl.hpp:128
wpi::units::second_t Timestamp
The timestamp of the state capture, in the timebase of utils::GetCurrentTime().
Definition SwerveDrivetrainImpl.hpp:132
int32_t FailedDaqs
Number of failed data acquisitions.
Definition SwerveDrivetrainImpl.hpp:138
Rotation2d RawHeading
The raw heading of the robot, unaffected by vision updates and odometry resets.
Definition SwerveDrivetrainImpl.hpp:130