CTRE Phoenix 6 C++ 25.3.0
Loading...
Searching...
No Matches
SimSwerveDrivetrain.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.Ā  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
12#include <frc/simulation/DCMotorSim.h>
13#include <frc/system/plant/LinearSystemId.h>
14
15namespace ctre {
16namespace phoenix6 {
17namespace swerve {
18
19/**
20 * \brief Simplified swerve drive simulation class.
21 *
22 * This class assumes that the swerve drive is perfect, meaning
23 * that there is no scrub and the wheels do not slip.
24 *
25 * In addition, it assumes the inertia of the robot is governed only
26 * by the inertia of the steer module and the individual drive wheels.
27 * Robot-wide inertia is not accounted for, and neither is translational
28 * vs rotational inertia of the robot.
29 *
30 * These assumptions provide a simplified example that can demonstrate the
31 * behavior of a swerve drive in simulation. Users are encouraged to
32 * expand this model for their own use.
33 */
34template <
35 typename DriveMotorT,
36 typename SteerMotorT,
37 typename EncoderT,
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>
44 >>
45>
47protected:
49 public:
50 /** \brief Reference to motor simulation for drive motor */
51 frc::sim::DCMotorSim DriveMotor;
52 /** \brief Reference to motor simulation for the steer motor */
53 frc::sim::DCMotorSim SteerMotor;
54 /** \brief Reference to steer gearing for updating encoder */
55 units::scalar_t DriveGearing;
56 /** \brief Reference to steer gearing for updating encoder */
57 units::scalar_t SteerGearing;
58 /** \brief Voltage necessary for the drive motor to overcome friction */
59 units::volt_t DriveFrictionVoltage;
60 /** \brief Voltage necessary for the steer motor to overcome friction */
61 units::volt_t SteerFrictionVoltage;
62 /** \brief Whether the drive motor is inverted */
64 /** \brief Whether the steer motor is inverted */
66 /** \brief Whether the azimuth encoder is inverted */
68 /** \brief The type of encoder to use for the azimuth */
70
72 units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia,
73 units::volt_t driveFrictionVoltage, bool driveMotorInverted,
74 DriveMotorArrangement driveMotorType,
75 units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia,
76 units::volt_t steerFrictionVoltage, bool steerMotorInverted,
77 SteerMotorArrangement steerMotorType,
78 bool encoderInverted,
79 SteerFeedbackType encoderType
80 ) :
81 DriveMotor{frc::LinearSystemId::DCMotorSystem(GetDriveMotorGearbox(driveMotorType), driveInertia, driveGearing), GetDriveMotorGearbox(driveMotorType)},
82 SteerMotor{frc::LinearSystemId::DCMotorSystem(GetSteerMotorGearbox(steerMotorType), steerInertia, steerGearing), GetSteerMotorGearbox(steerMotorType)},
83 DriveGearing{driveGearing},
84 SteerGearing{steerGearing},
85 DriveFrictionVoltage{driveFrictionVoltage},
86 SteerFrictionVoltage{steerFrictionVoltage},
87 DriveMotorInverted{driveMotorInverted},
88 SteerMotorInverted{steerMotorInverted},
89 EncoderInverted{encoderInverted},
90 EncoderType{encoderType}
91 {}
92
93 static frc::DCMotor GetDriveMotorGearbox(DriveMotorArrangement driveMotorType)
94 {
95 switch (driveMotorType) {
97 default:
98 return frc::DCMotor::KrakenX60FOC(1);
100 return frc::DCMotor::NEO(1);
102 return frc::DCMotor::NeoVortex(1);
103 }
104 }
105
106 static frc::DCMotor GetSteerMotorGearbox(SteerMotorArrangement steerMotorType)
107 {
108 switch (steerMotorType) {
110 default:
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);
124 }
125 }
126 };
127
129 std::vector<SimSwerveModule> _modules;
130
132 Rotation2d _lastAngle{};
133
134public:
135 template <
136 typename... ModuleConstants,
137 typename = std::enable_if_t<std::conjunction_v<
138 std::is_same<
139 ModuleConstants,
141 >...
142 >>
143 >
145 std::vector<Translation2d> wheelLocations,
146 sim::Pigeon2SimState &pigeonSim,
147 ModuleConstants const &... moduleConstants
148 ) :
149 _pigeonSim{pigeonSim},
150 _modules{
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
164 }...
165 },
166 _kinem{std::move(wheelLocations)}
167 {}
168
169 void Update(
170 units::second_t dt,
171 units::volt_t supplyVoltage,
172 std::vector<std::unique_ptr<SwerveModule<DriveMotorT, SteerMotorT, EncoderT>>> const &modulesToApply
173 ) {
174 if (modulesToApply.size() != _modules.size()) return;
175
176 std::vector<SwerveModuleState> states(modulesToApply.size());
177 /* update our sim devices */
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();
182
183 if constexpr (std::is_same_v<hardware::TalonFXS, DriveMotorT>) {
185 } else {
187 }
188
189 if constexpr (std::is_same_v<hardware::TalonFXS, SteerMotorT>) {
191 steerMotor.ExtSensorOrientation = _modules[i].SteerMotorInverted != _modules[i].EncoderInverted ? sim::ChassisReference::Clockwise_Positive : sim::ChassisReference::CounterClockwise_Positive;
192 } else {
194 }
195
196 if constexpr (std::is_same_v<hardware::CANcoder, EncoderT>) {
198 }
199
200 driveMotor.SetSupplyVoltage(supplyVoltage);
201 steerMotor.SetSupplyVoltage(supplyVoltage);
202 encoder.SetSupplyVoltage(supplyVoltage);
203
204 _modules[i].DriveMotor.SetInputVoltage(AddFriction(driveMotor.GetMotorVoltage(), _modules[i].DriveFrictionVoltage));
205 _modules[i].SteerMotor.SetInputVoltage(AddFriction(steerMotor.GetMotorVoltage(), _modules[i].SteerFrictionVoltage));
206
207 _modules[i].DriveMotor.Update(dt);
208 _modules[i].SteerMotor.Update(dt);
209
210 driveMotor.SetRawRotorPosition(_modules[i].DriveMotor.GetAngularPosition() * _modules[i].DriveGearing);
211 driveMotor.SetRotorVelocity(_modules[i].DriveMotor.GetAngularVelocity() * _modules[i].DriveGearing);
212
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>) {
216 /* azimuth encoders see the mechanism, so don't account for the steer gearing */
217 steerMotor.SetPulseWidthPosition(_modules[i].SteerMotor.GetAngularPosition());
218 steerMotor.SetPulseWidthVelocity(_modules[i].SteerMotor.GetAngularVelocity());
219 }
220
221 if constexpr (std::is_same_v<hardware::CANcoder, EncoderT>) {
222 /* azimuth encoders see the mechanism, so don't account for the steer gearing */
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>) {
226 switch (_modules[i].EncoderType) {
231
232 /* azimuth encoders see the mechanism, so don't account for the steer gearing */
233 encoder.SetPwm1Connected(true);
234 encoder.SetPwm1Position(_modules[i].SteerMotor.GetAngularPosition());
235 encoder.SetPwm1Velocity(_modules[i].SteerMotor.GetAngularVelocity());
236 break;
237
242
243 /* azimuth encoders see the mechanism, so don't account for the steer gearing */
244 encoder.SetPwm2Connected(true);
245 encoder.SetPwm2Position(_modules[i].SteerMotor.GetAngularPosition());
246 encoder.SetPwm2Velocity(_modules[i].SteerMotor.GetAngularVelocity());
247 break;
248
249 default:
250 break;
251 }
252 }
253
254 states[i] = modulesToApply[i]->GetCurrentState();
255 }
256
257 auto const angularVel = _kinem.ToChassisSpeeds(states).omega;
258 _lastAngle = _lastAngle + Rotation2d{angularVel * dt};
261 }
262
263protected:
264 /**
265 * \brief Applies the effects of friction to dampen the motor voltage.
266 *
267 * \param motorVoltage Voltage output by the motor
268 * \param frictionVoltage Voltage required to overcome friction
269 * \returns Friction-dampened motor voltage
270 */
271 static units::volt_t AddFriction(units::volt_t motorVoltage, units::volt_t frictionVoltage)
272 {
273 if (units::math::abs(motorVoltage) < frictionVoltage) {
274 motorVoltage = 0_V;
275 } else if (motorVoltage > 0_V) {
276 motorVoltage -= frictionVoltage;
277 } else {
278 motorVoltage += frictionVoltage;
279 }
280 return motorVoltage;
281 }
282};
283
284}
285}
286}
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.
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
Definition span.hpp:401
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:154