12#include <frc/simulation/DCMotorSim.h>
13#include <frc/system/plant/LinearSystemId.h>
58 units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia,
59 units::volt_t driveFrictionVoltage,
bool driveMotorInverted,
60 units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia,
61 units::volt_t steerFrictionVoltage,
bool steerMotorInverted,
64 DriveMotor{frc::LinearSystemId::DCMotorSystem(frc::DCMotor::KrakenX60FOC(1), driveInertia, driveGearing), frc::DCMotor::KrakenX60FOC(1)},
65 SteerMotor{frc::LinearSystemId::DCMotorSystem(frc::DCMotor::KrakenX60FOC(1), steerInertia, steerGearing), frc::DCMotor::KrakenX60FOC(1)},
84 typename... ModuleConstants,
85 typename = std::enable_if_t<std::conjunction_v<
86 std::is_same< ModuleConstants, SwerveModuleConstants<configs::TalonFXConfiguration, configs::TalonFXConfiguration, configs::CANcoderConfiguration> >...
90 std::vector<Translation2d> wheelLocations,
92 ModuleConstants
const &... moduleConstants
97 moduleConstants.DriveMotorGearRatio,
98 moduleConstants.DriveInertia,
99 moduleConstants.DriveFrictionVoltage,
100 moduleConstants.DriveMotorInverted,
101 moduleConstants.SteerMotorGearRatio,
102 moduleConstants.SteerInertia,
103 moduleConstants.SteerFrictionVoltage,
104 moduleConstants.SteerMotorInverted,
105 moduleConstants.EncoderInverted
112 typename DriveMotorT,
113 typename SteerMotorT,
115 typename = std::enable_if_t<std::is_base_of_v<hardware::traits::CommonTalon, DriveMotorT>>,
116 typename = std::enable_if_t<std::is_base_of_v<hardware::traits::CommonTalon, SteerMotorT>>,
117 typename = std::enable_if_t<std::is_base_of_v<hardware::ParentDevice, EncoderT>>
121 units::volt_t supplyVoltage,
124 if (modulesToApply.size() !=
_modules.size())
return;
126 if constexpr (!(std::is_same_v<hardware::TalonFX, DriveMotorT> &&
127 std::is_same_v<hardware::TalonFX, SteerMotorT> &&
128 std::is_same_v<hardware::CANcoder, EncoderT>))
133 std::vector<SwerveModuleState> states(modulesToApply.size());
135 for (
size_t i = 0; i < modulesToApply.size(); ++i) {
164 states[i] = modulesToApply[i]->GetCurrentState();
181 static units::volt_t
AddFriction(units::volt_t motorVoltage, units::volt_t frictionVoltage)
183 if (units::math::abs(motorVoltage) < frictionVoltage) {
185 }
else if (motorVoltage > 0_V) {
186 motorVoltage -= frictionVoltage;
188 motorVoltage += frictionVoltage;
Class to control the state of a simulated hardware::CANcoder.
Definition CANcoderSimState.hpp:32
ctre::phoenix::StatusCode SetSupplyVoltage(units::voltage::volt_t volts)
Sets the simulated supply voltage of the CANcoder.
ChassisReference Orientation
The orientation of the CANcoder relative to the robot chassis.
Definition CANcoderSimState.hpp:44
ctre::phoenix::StatusCode SetVelocity(units::angular_velocity::turns_per_second_t rps)
Sets the simulated velocity of the CANcoder.
ctre::phoenix::StatusCode SetRawPosition(units::angle::turn_t rotations)
Sets the simulated raw position of the CANcoder.
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.
Class to control the state of a simulated hardware::TalonFX.
Definition TalonFXSimState.hpp:33
ctre::phoenix::StatusCode SetRawRotorPosition(units::angle::turn_t rotations)
Sets the simulated raw rotor position of the TalonFX.
units::voltage::volt_t GetMotorVoltage() const
Gets the simulated output voltage of the motor.
ctre::phoenix::StatusCode SetRotorVelocity(units::angular_velocity::turns_per_second_t rps)
Sets the simulated rotor velocity of the TalonFX.
ctre::phoenix::StatusCode SetSupplyVoltage(units::voltage::volt_t volts)
Sets the simulated supply voltage of the TalonFX.
ChassisReference Orientation
The orientation of the TalonFX relative to the robot chassis.
Definition TalonFXSimState.hpp:45
Definition SimSwerveDrivetrain.hpp:36
frc::sim::DCMotorSim DriveMotor
Reference to motor simulation for drive motor.
Definition SimSwerveDrivetrain.hpp:39
frc::sim::DCMotorSim SteerMotor
Reference to motor simulation for the steer motor.
Definition SimSwerveDrivetrain.hpp:41
units::volt_t DriveFrictionVoltage
Voltage necessary for the drive motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:47
SimSwerveModule(units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia, units::volt_t driveFrictionVoltage, bool driveMotorInverted, units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia, units::volt_t steerFrictionVoltage, bool steerMotorInverted, bool encoderInverted)
Definition SimSwerveDrivetrain.hpp:57
bool SteerMotorInverted
Whether the steer motor is inverted.
Definition SimSwerveDrivetrain.hpp:53
units::volt_t SteerFrictionVoltage
Voltage necessary for the steer motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:49
units::scalar_t SteerGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:45
bool EncoderInverted
Whether the azimuth encoder is inverted.
Definition SimSwerveDrivetrain.hpp:55
bool DriveMotorInverted
Whether the drive motor is inverted.
Definition SimSwerveDrivetrain.hpp:51
units::scalar_t DriveGearing
Reference to steer gearing for updating encoder.
Definition SimSwerveDrivetrain.hpp:43
Simplified swerve drive simulation class.
Definition SimSwerveDrivetrain.hpp:34
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:181
impl::SwerveDriveKinematics _kinem
Definition SimSwerveDrivetrain.hpp:79
sim::Pigeon2SimState & _pigeonSim
Definition SimSwerveDrivetrain.hpp:76
SimSwerveDrivetrain(std::vector< Translation2d > wheelLocations, sim::Pigeon2SimState &pigeonSim, ModuleConstants const &... moduleConstants)
Definition SimSwerveDrivetrain.hpp:89
std::vector< SimSwerveModule > _modules
Definition SimSwerveDrivetrain.hpp:77
Rotation2d _lastAngle
Definition SimSwerveDrivetrain.hpp:80
void Update(units::second_t dt, units::volt_t supplyVoltage, std::vector< std::unique_ptr< SwerveModule< DriveMotorT, SteerMotorT, EncoderT > > > const &modulesToApply)
Definition SimSwerveDrivetrain.hpp:119
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:39
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.
Definition MotionMagicExpoTorqueCurrentFOC.hpp:18