12#include <wpi/simulation/DCMotorSim.hpp>
13#include <wpi/math/system/Models.hpp>
35 std::derived_from<hardware::traits::CommonTalon> DriveMotorT,
36 std::derived_from<hardware::traits::CommonTalon> SteerMotorT,
39 requires std::same_as<EncoderT, hardware::CANcoder> ||
40 std::same_as<EncoderT, hardware::CANdi> ||
41 std::same_as<EncoderT, hardware::TalonFXS>
70 wpi::units::scalar_t driveGearing, wpi::units::kilogram_square_meter_t driveInertia,
71 wpi::units::volt_t driveFrictionVoltage,
bool driveMotorInverted,
73 wpi::units::scalar_t steerGearing, wpi::units::kilogram_square_meter_t steerInertia,
74 wpi::units::volt_t steerFrictionVoltage,
bool steerMotorInverted,
77 wpi::units::turn_t encoderOffset,
81 wpi::math::Models::SingleJointedArmFromPhysicalConstants(
GetDriveMotorGearbox(driveMotorType), driveInertia, driveGearing),
85 wpi::math::Models::SingleJointedArmFromPhysicalConstants(
GetSteerMotorGearbox(steerMotorType), steerInertia, steerGearing),
101 switch (driveMotorType) {
104 return wpi::math::DCMotor::KrakenX60FOC(1);
106 return wpi::math::DCMotor::NEO(1);
108 return wpi::math::DCMotor::NeoVortex(1);
114 switch (steerMotorType) {
117 return wpi::math::DCMotor::KrakenX60FOC(1);
119 return wpi::math::DCMotor{12_V, 3.1_Nm, 202_A, 4_A, 774_rad_per_s, 1};
121 return wpi::math::DCMotor::NEO(1);
123 return wpi::math::DCMotor::NeoVortex(1);
125 return wpi::math::DCMotor::NEO550(1);
129 return wpi::math::DCMotor::CIM(1);
143 typename DriveMotorT::Configuration,
144 typename SteerMotorT::Configuration,
145 typename EncoderT::Configuration
146 >>... ModuleConstants
149 std::vector<Translation2d> wheelLocations,
151 ModuleConstants
const &... moduleConstants
156 moduleConstants.DriveMotorGearRatio,
157 moduleConstants.DriveInertia,
158 moduleConstants.DriveFrictionVoltage,
159 moduleConstants.DriveMotorInverted,
160 moduleConstants.DriveMotorType,
161 moduleConstants.SteerMotorGearRatio,
162 moduleConstants.SteerInertia,
163 moduleConstants.SteerFrictionVoltage,
164 moduleConstants.SteerMotorInverted,
165 moduleConstants.SteerMotorType,
166 moduleConstants.EncoderInverted,
167 moduleConstants.EncoderOffset,
168 moduleConstants.FeedbackSource
171 _kinem{std::move(wheelLocations)}
175 wpi::units::second_t dt,
176 wpi::units::volt_t supplyVoltage,
179 if (modulesToApply.size() !=
_modules.size())
return;
181 std::vector<SwerveModuleVelocity> velocities(modulesToApply.size());
183 for (
size_t i = 0; i < modulesToApply.size(); ++i) {
184 auto &driveMotor = modulesToApply[i]->GetDriveMotor().GetSimState();
185 auto &steerMotor = modulesToApply[i]->GetSteerMotor().GetSimState();
186 auto &encoder = modulesToApply[i]->GetEncoder().GetSimState();
188 if constexpr (std::same_as<hardware::TalonFXS, DriveMotorT>) {
194 if constexpr (std::same_as<hardware::TalonFXS, SteerMotorT>) {
197 steerMotor.PulseWidthSensorOffset =
_modules[i].EncoderOffset;
202 if constexpr (std::same_as<hardware::CANcoder, EncoderT>) {
204 encoder.SensorOffset =
_modules[i].EncoderOffset;
207 driveMotor.SetSupplyVoltage(supplyVoltage);
208 steerMotor.SetSupplyVoltage(supplyVoltage);
209 encoder.SetSupplyVoltage(supplyVoltage);
217 driveMotor.SetRawRotorPosition(
_modules[i].DriveMotor.GetAngularPosition() *
_modules[i].DriveGearing);
218 driveMotor.SetRotorVelocity(
_modules[i].DriveMotor.GetAngularVelocity() *
_modules[i].DriveGearing);
219 driveMotor.SetRotorAcceleration(
_modules[i].DriveMotor.GetAngularAcceleration() *
_modules[i].DriveGearing);
221 steerMotor.SetRawRotorPosition(
_modules[i].SteerMotor.GetAngularPosition() *
_modules[i].SteerGearing);
222 steerMotor.SetRotorVelocity(
_modules[i].SteerMotor.GetAngularVelocity() *
_modules[i].SteerGearing);
223 steerMotor.SetRotorAcceleration(
_modules[i].SteerMotor.GetAngularAcceleration() *
_modules[i].SteerGearing);
224 if constexpr (std::same_as<hardware::TalonFXS, SteerMotorT>) {
226 steerMotor.SetPulseWidthPosition(
_modules[i].SteerMotor.GetAngularPosition());
227 steerMotor.SetPulseWidthVelocity(
_modules[i].SteerMotor.GetAngularVelocity());
230 if constexpr (std::same_as<hardware::CANcoder, EncoderT>) {
232 encoder.SetRawPosition(
_modules[i].SteerMotor.GetAngularPosition());
233 encoder.SetVelocity(
_modules[i].SteerMotor.GetAngularVelocity());
234 }
else if constexpr (std::same_as<hardware::CANdi, EncoderT>) {
240 encoder.Pwm1SensorOffset =
_modules[i].EncoderOffset;
243 encoder.SetPwm1Connected(
true);
244 encoder.SetPwm1Position(
_modules[i].SteerMotor.GetAngularPosition());
245 encoder.SetPwm1Velocity(
_modules[i].SteerMotor.GetAngularVelocity());
252 encoder.Pwm1SensorOffset =
_modules[i].EncoderOffset;
255 encoder.SetPwm2Connected(
true);
256 encoder.SetPwm2Position(
_modules[i].SteerMotor.GetAngularPosition());
257 encoder.SetPwm2Velocity(
_modules[i].SteerMotor.GetAngularVelocity());
265 velocities[i] = modulesToApply[i]->GetCurrentVelocity();
268 auto const angularVel =
_kinem.ToChassisVelocities(velocities).omega;
282 static wpi::units::volt_t
AddFriction(wpi::units::volt_t motorVoltage, wpi::units::volt_t frictionVoltage)
284 if (wpi::units::math::abs(motorVoltage) < frictionVoltage) {
286 }
else if (motorVoltage > 0_V) {
287 motorVoltage -= frictionVoltage;
289 motorVoltage += frictionVoltage;
Class to control the state of a simulated hardware::Pigeon2.
Definition Pigeon2SimState.hpp:31
Definition SimSwerveDrivetrain.hpp:44
wpi::sim::DCMotorSim DriveMotor
Reference to motor simulation for drive motor.
Definition SimSwerveDrivetrain.hpp:47
bool SteerMotorInverted
Whether the steer motor is inverted.
Definition SimSwerveDrivetrain.hpp:61
wpi::units::scalar_t SteerGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:53
wpi::sim::DCMotorSim SteerMotor
Reference to motor simulation for the steer motor.
Definition SimSwerveDrivetrain.hpp:49
wpi::units::turn_t EncoderOffset
Offset of the azimuth encoder.
Definition SimSwerveDrivetrain.hpp:65
SimSwerveModule(wpi::units::scalar_t driveGearing, wpi::units::kilogram_square_meter_t driveInertia, wpi::units::volt_t driveFrictionVoltage, bool driveMotorInverted, DriveMotorArrangement driveMotorType, wpi::units::scalar_t steerGearing, wpi::units::kilogram_square_meter_t steerInertia, wpi::units::volt_t steerFrictionVoltage, bool steerMotorInverted, SteerMotorArrangement steerMotorType, bool encoderInverted, wpi::units::turn_t encoderOffset, SteerFeedbackType encoderType)
Definition SimSwerveDrivetrain.hpp:69
wpi::units::scalar_t DriveGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:51
bool DriveMotorInverted
Whether the drive motor is inverted.
Definition SimSwerveDrivetrain.hpp:59
static wpi::math::DCMotor GetSteerMotorGearbox(SteerMotorArrangement steerMotorType)
Definition SimSwerveDrivetrain.hpp:112
wpi::units::volt_t SteerFrictionVoltage
Voltage necessary for the steer motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:57
bool EncoderInverted
Whether the azimuth encoder is inverted.
Definition SimSwerveDrivetrain.hpp:63
wpi::units::volt_t DriveFrictionVoltage
Voltage necessary for the drive motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:55
static wpi::math::DCMotor GetDriveMotorGearbox(DriveMotorArrangement driveMotorType)
Definition SimSwerveDrivetrain.hpp:99
SteerFeedbackType EncoderType
The type of encoder to use for the azimuth.
Definition SimSwerveDrivetrain.hpp:67
std::vector< SimSwerveModule > _modules
Definition SimSwerveDrivetrain.hpp:135
SimSwerveDrivetrain(std::vector< Translation2d > wheelLocations, sim::Pigeon2SimState &pigeonSim, ModuleConstants const &... moduleConstants)
Definition SimSwerveDrivetrain.hpp:148
impl::SwerveDriveKinematics _kinem
Definition SimSwerveDrivetrain.hpp:137
void Update(wpi::units::second_t dt, wpi::units::volt_t supplyVoltage, std::span< std::unique_ptr< SwerveModule< DriveMotorT, SteerMotorT, EncoderT > > const > modulesToApply)
Definition SimSwerveDrivetrain.hpp:174
static wpi::units::volt_t AddFriction(wpi::units::volt_t motorVoltage, wpi::units::volt_t frictionVoltage)
Applies the effects of friction to dampen the motor voltage.
Definition SimSwerveDrivetrain.hpp:282
Rotation2d _lastAngle
Definition SimSwerveDrivetrain.hpp:138
sim::Pigeon2SimState & _pigeonSim
Definition SimSwerveDrivetrain.hpp:134
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
@ Clockwise_Positive
The device should read a clockwise rotation as positive motion.
Definition ChassisReference.hpp:20
@ CounterClockwise_Positive
The device should read a counter-clockwise rotation as positive motion.
Definition ChassisReference.hpp:18
Definition SwerveModule.hpp:28
SteerMotorArrangement
Supported motor arrangements for the steer motors.
Definition SwerveModuleConstants.hpp:49
@ TalonFXS_Brushed_AC
Brushed motor connected to a Talon FXS on terminals A and C.
Definition SwerveModuleConstants.hpp:77
@ TalonFXS_Brushed_AB
Brushed motor connected to a Talon FXS on terminals A and B.
Definition SwerveModuleConstants.hpp:73
@ TalonFX_Integrated
Talon FX integrated brushless motor.
Definition SwerveModuleConstants.hpp:53
@ TalonFXS_Brushed_BC
Brushed motor connected to a Talon FXS on terminals B and C.
Definition SwerveModuleConstants.hpp:81
@ TalonFXS_NEO550_JST
Third party NEO550 brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:69
@ TalonFXS_NEO_JST
Third party NEO brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:61
@ TalonFXS_VORTEX_JST
Third party VORTEX brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:65
@ TalonFXS_Minion_JST
CTR Electronics MinionĀ® brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:57
SteerFeedbackType
Supported feedback sensors for the steer motors.
Definition SwerveModuleConstants.hpp:87
@ SyncCANdiPWM2
Requires Pro; Use signals::FeedbackSensorSourceValue::SyncCANdiPWM2 for the steer motor.
Definition SwerveModuleConstants.hpp:122
@ SyncCANdiPWM1
Requires Pro; Use signals::FeedbackSensorSourceValue::SyncCANdiPWM1 for the steer motor.
Definition SwerveModuleConstants.hpp:117
@ RemoteCANdiPWM2
Use signals::FeedbackSensorSourceValue::RemoteCANdiPWM2 for the steer motor.
Definition SwerveModuleConstants.hpp:132
@ FusedCANdiPWM2
Requires Pro; Use signals::FeedbackSensorSourceValue::FusedCANdiPWM2 for the steer motor.
Definition SwerveModuleConstants.hpp:112
@ FusedCANdiPWM1
Requires Pro; Use signals::FeedbackSensorSourceValue::FusedCANdiPWM1 for the steer motor.
Definition SwerveModuleConstants.hpp:107
@ RemoteCANdiPWM1
Use signals::FeedbackSensorSourceValue::RemoteCANdiPWM1 for the steer motor.
Definition SwerveModuleConstants.hpp:127
DriveMotorArrangement
Supported motor arrangements for the drive motors.
Definition SwerveModuleConstants.hpp:31
@ TalonFX_Integrated
Talon FX integrated brushless motor.
Definition SwerveModuleConstants.hpp:35
@ TalonFXS_NEO_JST
Third party NEO brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:39
@ TalonFXS_VORTEX_JST
Third party VORTEX brushless motor connected to a Talon FXS over JST.
Definition SwerveModuleConstants.hpp:43
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:148