12#include <frc/simulation/DCMotorSim.h>
13#include <frc/system/plant/LinearSystemId.h>
38 typename = std::enable_if_t<std::is_base_of_v<hardware::traits::CommonTalon, DriveMotorT>>,
39 typename = std::enable_if_t<std::is_base_of_v<hardware::traits::CommonTalon, SteerMotorT>>,
40 typename = std::enable_if_t<std::disjunction_v<
41 std::is_same<hardware::CANcoder, EncoderT>,
42 std::is_same<hardware::CANdi, EncoderT>,
43 std::is_same<hardware::TalonFXS, EncoderT>
72 units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia,
73 units::volt_t driveFrictionVoltage,
bool driveMotorInverted,
75 units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia,
76 units::volt_t steerFrictionVoltage,
bool steerMotorInverted,
95 switch (driveMotorType) {
98 return frc::DCMotor::KrakenX60FOC(1);
100 return frc::DCMotor::NEO(1);
102 return frc::DCMotor::NeoVortex(1);
108 switch (steerMotorType) {
111 return frc::DCMotor::KrakenX60FOC(1);
113 return frc::DCMotor{12_V, 3.1_Nm, 202_A, 4_A, 774_rad_per_s, 1};
115 return frc::DCMotor::NEO(1);
117 return frc::DCMotor::NeoVortex(1);
119 return frc::DCMotor::NEO550(1);
123 return frc::DCMotor::CIM(1);
136 typename... ModuleConstants,
137 typename = std::enable_if_t<std::conjunction_v<
145 std::vector<Translation2d> wheelLocations,
147 ModuleConstants
const &... moduleConstants
152 moduleConstants.DriveMotorGearRatio,
153 moduleConstants.DriveInertia,
154 moduleConstants.DriveFrictionVoltage,
155 moduleConstants.DriveMotorInverted,
156 moduleConstants.DriveMotorType,
157 moduleConstants.SteerMotorGearRatio,
158 moduleConstants.SteerInertia,
159 moduleConstants.SteerFrictionVoltage,
160 moduleConstants.SteerMotorInverted,
161 moduleConstants.SteerMotorType,
162 moduleConstants.EncoderInverted,
163 moduleConstants.FeedbackSource
171 units::volt_t supplyVoltage,
174 if (modulesToApply.size() !=
_modules.size())
return;
176 std::vector<SwerveModuleState> states(modulesToApply.size());
178 for (
size_t i = 0; i < modulesToApply.size(); ++i) {
179 auto &driveMotor = modulesToApply[i]->GetDriveMotor().GetSimState();
180 auto &steerMotor = modulesToApply[i]->GetSteerMotor().GetSimState();
181 auto &encoder = modulesToApply[i]->GetEncoder().GetSimState();
183 if constexpr (std::is_same_v<hardware::TalonFXS, DriveMotorT>) {
189 if constexpr (std::is_same_v<hardware::TalonFXS, SteerMotorT>) {
196 if constexpr (std::is_same_v<hardware::CANcoder, EncoderT>) {
200 driveMotor.SetSupplyVoltage(supplyVoltage);
201 steerMotor.SetSupplyVoltage(supplyVoltage);
202 encoder.SetSupplyVoltage(supplyVoltage);
210 driveMotor.SetRawRotorPosition(
_modules[i].DriveMotor.GetAngularPosition() *
_modules[i].DriveGearing);
211 driveMotor.SetRotorVelocity(
_modules[i].DriveMotor.GetAngularVelocity() *
_modules[i].DriveGearing);
213 steerMotor.SetRawRotorPosition(
_modules[i].SteerMotor.GetAngularPosition() *
_modules[i].SteerGearing);
214 steerMotor.SetRotorVelocity(
_modules[i].SteerMotor.GetAngularVelocity() *
_modules[i].SteerGearing);
215 if constexpr (std::is_same_v<hardware::TalonFXS, SteerMotorT>) {
217 steerMotor.SetPulseWidthPosition(
_modules[i].SteerMotor.GetAngularPosition());
218 steerMotor.SetPulseWidthVelocity(
_modules[i].SteerMotor.GetAngularVelocity());
221 if constexpr (std::is_same_v<hardware::CANcoder, EncoderT>) {
223 encoder.SetRawPosition(
_modules[i].SteerMotor.GetAngularPosition());
224 encoder.SetVelocity(
_modules[i].SteerMotor.GetAngularVelocity());
225 }
else if constexpr (std::is_same_v<hardware::CANdi, EncoderT>) {
233 encoder.SetPwm1Connected(
true);
234 encoder.SetPwm1Position(
_modules[i].SteerMotor.GetAngularPosition());
235 encoder.SetPwm1Velocity(
_modules[i].SteerMotor.GetAngularVelocity());
244 encoder.SetPwm2Connected(
true);
245 encoder.SetPwm2Position(
_modules[i].SteerMotor.GetAngularPosition());
246 encoder.SetPwm2Velocity(
_modules[i].SteerMotor.GetAngularVelocity());
254 states[i] = modulesToApply[i]->GetCurrentState();
271 static units::volt_t
AddFriction(units::volt_t motorVoltage, units::volt_t frictionVoltage)
273 if (units::math::abs(motorVoltage) < frictionVoltage) {
275 }
else if (motorVoltage > 0_V) {
276 motorVoltage -= frictionVoltage;
278 motorVoltage += frictionVoltage;
Class to control the state of a simulated hardware::Pigeon2.
Definition Pigeon2SimState.hpp:31
ctre::phoenix::StatusCode SetAngularVelocityZ(units::angular_velocity::degrees_per_second_t dps)
Sets the simulated angular velocity Z component of the Pigeon2.
ctre::phoenix::StatusCode SetRawYaw(units::angle::degree_t deg)
Sets the simulated raw yaw of the Pigeon2.
Definition SimSwerveDrivetrain.hpp:48
static frc::DCMotor GetDriveMotorGearbox(DriveMotorArrangement driveMotorType)
Definition SimSwerveDrivetrain.hpp:93
SteerFeedbackType EncoderType
The type of encoder to use for the azimuth.
Definition SimSwerveDrivetrain.hpp:69
static frc::DCMotor GetSteerMotorGearbox(SteerMotorArrangement steerMotorType)
Definition SimSwerveDrivetrain.hpp:106
units::scalar_t SteerGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:57
bool SteerMotorInverted
Whether the steer motor is inverted.
Definition SimSwerveDrivetrain.hpp:65
units::scalar_t DriveGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:55
units::volt_t SteerFrictionVoltage
Voltage necessary for the steer motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:61
bool DriveMotorInverted
Whether the drive motor is inverted.
Definition SimSwerveDrivetrain.hpp:63
bool EncoderInverted
Whether the azimuth encoder is inverted.
Definition SimSwerveDrivetrain.hpp:67
SimSwerveModule(units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia, units::volt_t driveFrictionVoltage, bool driveMotorInverted, DriveMotorArrangement driveMotorType, units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia, units::volt_t steerFrictionVoltage, bool steerMotorInverted, SteerMotorArrangement steerMotorType, bool encoderInverted, SteerFeedbackType encoderType)
Definition SimSwerveDrivetrain.hpp:71
frc::sim::DCMotorSim SteerMotor
Reference to motor simulation for the steer motor.
Definition SimSwerveDrivetrain.hpp:53
units::volt_t DriveFrictionVoltage
Voltage necessary for the drive motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:59
frc::sim::DCMotorSim DriveMotor
Reference to motor simulation for drive motor.
Definition SimSwerveDrivetrain.hpp:51
Simplified swerve drive simulation class.
Definition SimSwerveDrivetrain.hpp:46
static units::volt_t AddFriction(units::volt_t motorVoltage, units::volt_t frictionVoltage)
Applies the effects of friction to dampen the motor voltage.
Definition SimSwerveDrivetrain.hpp:271
void Update(units::second_t dt, units::volt_t supplyVoltage, std::vector< std::unique_ptr< SwerveModule< DriveMotorT, SteerMotorT, EncoderT > > > const &modulesToApply)
Definition SimSwerveDrivetrain.hpp:169
sim::Pigeon2SimState & _pigeonSim
Definition SimSwerveDrivetrain.hpp:128
Rotation2d _lastAngle
Definition SimSwerveDrivetrain.hpp:132
std::vector< SimSwerveModule > _modules
Definition SimSwerveDrivetrain.hpp:129
SimSwerveDrivetrain(std::vector< Translation2d > wheelLocations, sim::Pigeon2SimState &pigeonSim, ModuleConstants const &... moduleConstants)
Definition SimSwerveDrivetrain.hpp:144
impl::SwerveDriveKinematics _kinem
Definition SimSwerveDrivetrain.hpp:131
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:45
Class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states ...
Definition SwerveDriveKinematics.hpp:42
ChassisSpeeds ToChassisSpeeds(WheelSpeeds const &moduleStates) const
Performs forward kinematics to return the resulting chassis state from the given module states.
@ Clockwise_Positive
The device should read a clockwise rotation as positive motion.
@ CounterClockwise_Positive
The device should read a counter-clockwise rotation as positive motion.
SteerMotorArrangement
Supported motor arrangements for the steer motors.
Definition SwerveModuleConstants.hpp:52
@ TalonFXS_Brushed_AC
Brushed motor connected to a Talon FXS on terminals A and C.
@ TalonFXS_Brushed_AB
Brushed motor connected to a Talon FXS on terminals A and B.
@ TalonFX_Integrated
Talon FX integrated brushless motor.
@ TalonFXS_Brushed_BC
Brushed motor connected to a Talon FXS on terminals B and C.
@ TalonFXS_NEO550_JST
Third party NEO550 brushless motor connected to a Talon FXS over JST.
@ TalonFXS_NEO_JST
Third party NEO brushless motor connected to a Talon FXS over JST.
@ TalonFXS_VORTEX_JST
Third party VORTEX brushless motor connected to a Talon FXS over JST.
@ TalonFXS_Minion_JST
CTR Electronics MinionĀ® brushless motor connected to a Talon FXS over JST.
SteerFeedbackType
Supported feedback sensors for the steer motors.
Definition SwerveModuleConstants.hpp:90
@ SyncCANdiPWM2
Requires Pro; Use signals::FeedbackSensorSourceValue::SyncCANdiPWM2 for the steer motor.
@ SyncCANdiPWM1
Requires Pro; Use signals::FeedbackSensorSourceValue::SyncCANdiPWM1 for the steer motor.
@ RemoteCANdiPWM2
Use signals::FeedbackSensorSourceValue::RemoteCANdiPWM2 for the steer motor.
@ FusedCANdiPWM2
Requires Pro; Use signals::FeedbackSensorSourceValue::FusedCANdiPWM2 for the steer motor.
@ FusedCANdiPWM1
Requires Pro; Use signals::FeedbackSensorSourceValue::FusedCANdiPWM1 for the steer motor.
@ RemoteCANdiPWM1
Use signals::FeedbackSensorSourceValue::RemoteCANdiPWM1 for the steer motor.
DriveMotorArrangement
Supported motor arrangements for the drive motors.
Definition SwerveModuleConstants.hpp:34
@ TalonFX_Integrated
Talon FX integrated brushless motor.
@ TalonFXS_NEO_JST
Third party NEO brushless motor connected to a Talon FXS over JST.
@ TalonFXS_VORTEX_JST
Third party VORTEX brushless motor connected to a Talon FXS over JST.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:154