YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
yams::mechanisms::swerve::SwerveModule Class Reference

#include <SwerveModule.hpp>

Collaboration diagram for yams::mechanisms::swerve::SwerveModule:
Collaboration graph
[legend]

Public Member Functions

 SwerveModule (config::SwerveModuleConfig *config)
 
void SetupTelemetry (const std::string &mechName)
 
void SeedAzimuthEncoder ()
 
std::string GetName () const
 
wpi::units::degree_t GetRawAbsoluteEncoderAngle () const
 
const config::SwerveModuleConfig & GetConfig () const
 
wpi::math::SwerveModuleVelocity SetSwerveModuleState (wpi::math::SwerveModuleVelocity state)
 
wpi::math::SwerveModuleVelocity SetSwerveModuleState (wpi::math::SwerveModuleVelocity state, wpi::units::newton_t feedforwardForce)
 
config::SwerveModuleConfig & GetMutableConfig ()
 
wpi::math::SwerveModuleVelocity SetAzimuthAngle (wpi::math::Rotation2d angle)
 
SwerveModuleVelocityWithAzimuth GetState () const
 
wpi::math::SwerveModulePosition GetPosition () const
 
void UpdateTelemetry ()
 
void SimIterate ()
 
motorcontrollers::SmartMotorController * GetDriveMotorController () const
 
motorcontrollers::SmartMotorController * GetAzimuthMotorController () const
 
wpi::units::meters_per_second_t GetCouplingVelocity () const
 

Public Attributes

motorcontrollers::SmartMotorController * m_driveMotorController
 
motorcontrollers::SmartMotorController * m_azimuthMotorController
 

Detailed Description

A single swerve module comprising a drive motor and an azimuth (steering) motor.

Each module holds references to two SmartMotorControllers (drive and azimuth) and a SwerveModuleConfig that describes its physical properties and absolute encoder. Modules are normally owned by the subsystem and handed to SwerveDrive by pointer.

Example usage (inside a subsystem constructor)

using namespace yams::motorcontrollers;
using namespace yams::gearing;
using namespace yams::mechanisms::config;
// Declare as subsystem members:
// ctre::phoenix6::hardware::TalonFX m_driveMotor{1};
// ctre::phoenix6::hardware::TalonFX m_azimuthMotor{2};
// ctre::phoenix6::hardware::CANcoder m_encoder{3};
// std::optional<TalonFXWrapper> m_driveSMC;
// std::optional<TalonFXWrapper> m_azimuthSMC;
// std::optional<SwerveModule> m_module;
driveCfg.WithSubsystem(this)
.WithMechanismCircumference(wpi::units::meter_t{4.0_in * std::numbers::pi})
.WithFeedback(0.1, 0.0, 0.0)
.WithMotorGearing(MechanismGearing{GearBox::FromStages({"6.75:1"})})
.WithStatorCurrentLimit(40.0_A)
.WithZeroPower(Cfg::MotorMode::BRAKE)
.WithTelemetry("FL_Drive", Cfg::TelemetryVerbosity::HIGH);
azimuthCfg.WithSubsystem(this)
.WithFeedback(50.0, 0.0, 0.5)
.WithMotorGearing(MechanismGearing{GearBox::FromStages({"21.43:1"})})
.WithStatorCurrentLimit(20.0_A)
.WithTelemetry("FL_Azimuth", Cfg::TelemetryVerbosity::HIGH);
m_driveSMC.emplace(m_driveMotor, wpi::math::DCMotor::KrakenX60(1), driveCfg);
m_azimuthSMC.emplace(m_azimuthMotor, wpi::math::DCMotor::KrakenX60(1), azimuthCfg);
auto* enc = &m_encoder;
SwerveModuleConfig moduleConfig{&m_driveSMC.value(), &m_azimuthSMC.value()};
moduleConfig
.WithAbsoluteEncoder([enc]() -> wpi::units::degree_t {
return wpi::units::degree_t{wpi::units::turn_t{enc->GetAbsolutePosition().GetValue()}};
})
.WithAbsoluteEncoderOffset(15.0_deg)
.WithLocation(wpi::units::inch_t{12}, wpi::units::inch_t{12})
.WithOptimization(true)
.WithTelemetry("FrontLeft", Cfg::TelemetryVerbosity::HIGH);
m_module.emplace(&m_moduleConfig);
Definition MechanismGearing.hpp:19
Definition SwerveModuleConfig.hpp:30
SwerveModuleConfig & WithAbsoluteEncoder(std::function< wpi::units::degree_t()> supplier)
SwerveModuleConfig & WithCosineCompensation(bool compensate)
SwerveModuleConfig & WithTelemetry(const std::string &name, TelemetryVerbosity verbosity)
SwerveModuleConfig & WithLocation(wpi::math::Translation2d location)
SwerveModuleConfig & WithWheelDiameter(wpi::units::meter_t diameter)
Definition SmartMotorControllerConfig.hpp:65
SmartMotorControllerConfig & WithTelemetry(const std::string &name, TelemetryVerbosity verbosity=TelemetryVerbosity::HIGH)
SmartMotorControllerConfig & WithZeroPower(MotorMode mode)
SmartMotorControllerConfig & WithClosedLoopMode()
SmartMotorControllerConfig & WithMotorGearing(const gearing::MechanismGearing &gearing)
SmartMotorControllerConfig & WithSubsystem(wpi::cmd::SubsystemBase *subsystem)
SmartMotorControllerConfig & WithFeedback(double kP, double kI, double kD, ClosedLoopControllerSlot slot=ClosedLoopControllerSlot::SLOT_0)
SmartMotorControllerConfig & WithMechanismCircumference(wpi::units::meter_t circumference)
Definition GearBox.hpp:11
Definition ArmConfig.hpp:17
Definition TalonFXSWrapper.hpp:38
Definition SimSensorConfig.hpp:15

Constructor & Destructor Documentation

◆ SwerveModule()

yams::mechanisms::swerve::SwerveModule::SwerveModule ( config::SwerveModuleConfig *  config)
explicit

Construct a SwerveModule from a SwerveModuleConfig.

Parameters
configModule configuration. Must contain a telemetry name and a location.
Exceptions
std::invalid_argumentif telemetry name or location is not set.

Member Function Documentation

◆ GetAzimuthMotorController()

motorcontrollers::SmartMotorController * yams::mechanisms::swerve::SwerveModule::GetAzimuthMotorController ( ) const

Get the azimuth motor controller.

◆ GetConfig()

const config::SwerveModuleConfig & yams::mechanisms::swerve::SwerveModule::GetConfig ( ) const

Get the module configuration.

◆ GetCouplingVelocity()

wpi::units::meters_per_second_t yams::mechanisms::swerve::SwerveModule::GetCouplingVelocity ( ) const

Wheel velocity the drive encoder reads because the azimuth is turning.

◆ GetDriveMotorController()

motorcontrollers::SmartMotorController * yams::mechanisms::swerve::SwerveModule::GetDriveMotorController ( ) const

Get the drive motor controller.

◆ GetMutableConfig()

config::SwerveModuleConfig & yams::mechanisms::swerve::SwerveModule::GetMutableConfig ( )

Get the module config for changing it, as WithModuleStateOptimization does.

◆ GetName()

std::string yams::mechanisms::swerve::SwerveModule::GetName ( ) const

Get the module name (from telemetry config).

◆ GetPosition()

wpi::math::SwerveModulePosition yams::mechanisms::swerve::SwerveModule::GetPosition ( ) const

Get the current measured module position (distance and heading).

◆ GetRawAbsoluteEncoderAngle()

wpi::units::degree_t yams::mechanisms::swerve::SwerveModule::GetRawAbsoluteEncoderAngle ( ) const

Get the absolute encoder angle without any offsets applied.

◆ GetState()

SwerveModuleVelocityWithAzimuth yams::mechanisms::swerve::SwerveModule::GetState ( ) const

Get the current measured module state (speed and heading).

◆ SeedAzimuthEncoder()

void yams::mechanisms::swerve::SwerveModule::SeedAzimuthEncoder ( )

Seed the azimuth encoder from the absolute encoder reading (real robot only).

◆ SetAzimuthAngle()

wpi::math::SwerveModuleVelocity yams::mechanisms::swerve::SwerveModule::SetAzimuthAngle ( wpi::math::Rotation2d  angle)

Turn the wheel to the given angle without driving it, as for locking the drive in place or tuning the azimuth motor. The velocity deadband is skipped, since it would hold the wheel where it is.

Parameters
angleAngle to turn the wheel to.
Returns
The state the module was commanded to.

◆ SetSwerveModuleState() [1/2]

wpi::math::SwerveModuleVelocity yams::mechanisms::swerve::SwerveModule::SetSwerveModuleState ( wpi::math::SwerveModuleVelocity  state)

Command the module to a target state.

Applies optimization (direction flip, velocity deadband, cosine compensation) before setting drive velocity and azimuth position.

Parameters
stateTarget module state.

◆ SetSwerveModuleState() [2/2]

wpi::math::SwerveModuleVelocity yams::mechanisms::swerve::SwerveModule::SetSwerveModuleState ( wpi::math::SwerveModuleVelocity  state,
wpi::units::newton_t  feedforwardForce 
)

Set the swerve module state with a feedforward force on the drive wheel, for following a trajectory that supplies module forces. The force follows the state: it is reversed when optimization reverses the wheel, and scaled by cosine compensation.

Parameters
stateDesired module state.
feedforwardForceForce the wheel should apply along the requested direction.
Returns
The state the module was sent, after optimization.

◆ SetupTelemetry()

void yams::mechanisms::swerve::SwerveModule::SetupTelemetry ( const std::string &  mechName)

Setup telemetry for the module; the SwerveModuleTelemetry config used is either the one supplied via SwerveModuleConfig::WithTelemetry(name, SwerveModuleTelemetryConfig) or a default built from SwerveModuleConfig::GetTelemetryVerbosity() (defaulting to TelemetryVerbosity::HIGH).

Parameters
mechNameTelemetry name of the parent SwerveDrive.

◆ SimIterate()

void yams::mechanisms::swerve::SwerveModule::SimIterate ( )

Advance the simulation model for both drive and azimuth motors.

◆ UpdateTelemetry()

void yams::mechanisms::swerve::SwerveModule::UpdateTelemetry ( )

Publish drive and azimuth motor telemetry to NetworkTables.

Member Data Documentation

◆ m_azimuthMotorController

motorcontrollers::SmartMotorController* yams::mechanisms::swerve::SwerveModule::m_azimuthMotorController

◆ m_driveMotorController

motorcontrollers::SmartMotorController* yams::mechanisms::swerve::SwerveModule::m_driveMotorController

The documentation for this class was generated from the following file: