CTRE Phoenix 6 C++ 25.0.0-beta-4
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 */
35protected:
37 public:
38 /** \brief Reference to motor simulation for drive motor */
39 frc::sim::DCMotorSim DriveMotor;
40 /** \brief Reference to motor simulation for the steer motor */
41 frc::sim::DCMotorSim SteerMotor;
42 /** \brief Reference to steer gearing for updating CANcoder */
43 units::scalar_t DriveGearing;
44 /** \brief Reference to steer gearing for updating CANcoder */
45 units::scalar_t SteerGearing;
46 /** \brief Voltage necessary for the drive motor to overcome friction */
47 units::volt_t DriveFrictionVoltage;
48 /** \brief Voltage necessary for the steer motor to overcome friction */
49 units::volt_t SteerFrictionVoltage;
50 /** \brief Whether the drive motor is inverted */
52 /** \brief Whether the steer motor is inverted */
54 /** \brief Whether the CANcoder is inverted */
56
57 SimSwerveModule(units::scalar_t driveGearing, units::kilogram_square_meter_t driveInertia,
58 units::volt_t driveFrictionVoltage, bool driveMotorInverted,
59 units::scalar_t steerGearing, units::kilogram_square_meter_t steerInertia,
60 units::volt_t steerFrictionVoltage, bool steerMotorInverted,
61 bool cancoderInverted) :
62 DriveMotor{frc::LinearSystemId::DCMotorSystem(frc::DCMotor::KrakenX60FOC(1), driveInertia, driveGearing), frc::DCMotor::KrakenX60FOC(1)},
63 SteerMotor{frc::LinearSystemId::DCMotorSystem(frc::DCMotor::KrakenX60FOC(1), steerInertia, steerGearing), frc::DCMotor::KrakenX60FOC(1)},
64 DriveGearing{driveGearing},
65 SteerGearing{steerGearing},
66 DriveFrictionVoltage{driveFrictionVoltage},
67 SteerFrictionVoltage{steerFrictionVoltage},
68 DriveMotorInverted{driveMotorInverted},
69 SteerMotorInverted{steerMotorInverted},
70 CANcoderInverted{cancoderInverted}
71 {}
72 };
73
75 std::vector<SimSwerveModule> _modules;
76
78 Rotation2d _lastAngle{};
79
80public:
81 template < typename... ModuleConstants,
82 typename = std::enable_if_t< std::conjunction_v<std::is_same<ModuleConstants, SwerveModuleConstants>...> > >
83 SimSwerveDrivetrain(std::vector<Translation2d> wheelLocations,
84 sim::Pigeon2SimState &pigeonSim,
85 ModuleConstants const &... moduleConstants) :
86 _pigeonSim{pigeonSim},
89 moduleConstants.DriveMotorGearRatio,
90 moduleConstants.DriveInertia,
91 moduleConstants.DriveFrictionVoltage,
92 moduleConstants.DriveMotorInverted,
93 moduleConstants.SteerMotorGearRatio,
94 moduleConstants.SteerInertia,
95 moduleConstants.SteerFrictionVoltage,
96 moduleConstants.SteerMotorInverted,
97 moduleConstants.CANcoderInverted
98 }...
99 },
100 _kinem{std::move(wheelLocations)}
101 {}
102
103 void Update(units::second_t dt, units::volt_t supplyVoltage, std::vector<std::unique_ptr<SwerveModule>> const &modulesToApply)
104 {
105 if (modulesToApply.size() != _modules.size()) return;
106
107 std::vector<SwerveModuleState> states(modulesToApply.size());
108 /* update our sim devices */
109 for (size_t i = 0; i < modulesToApply.size(); ++i) {
110 sim::TalonFXSimState &driveMotor = modulesToApply[i]->GetDriveMotor().GetSimState();
111 sim::TalonFXSimState &steerMotor = modulesToApply[i]->GetSteerMotor().GetSimState();
112 sim::CANcoderSimState &cancoder = modulesToApply[i]->GetCANcoder().GetSimState();
113
117
118 driveMotor.SetSupplyVoltage(supplyVoltage);
119 steerMotor.SetSupplyVoltage(supplyVoltage);
120 cancoder.SetSupplyVoltage(supplyVoltage);
121
122 _modules[i].DriveMotor.SetInputVoltage(AddFriction(driveMotor.GetMotorVoltage(), _modules[i].DriveFrictionVoltage));
123 _modules[i].SteerMotor.SetInputVoltage(AddFriction(steerMotor.GetMotorVoltage(), _modules[i].SteerFrictionVoltage));
124
125 _modules[i].DriveMotor.Update(dt);
126 _modules[i].SteerMotor.Update(dt);
127
128 driveMotor.SetRawRotorPosition(_modules[i].DriveMotor.GetAngularPosition() * _modules[i].DriveGearing);
129 driveMotor.SetRotorVelocity(_modules[i].DriveMotor.GetAngularVelocity() * _modules[i].DriveGearing);
130
131 steerMotor.SetRawRotorPosition(_modules[i].SteerMotor.GetAngularPosition() * _modules[i].SteerGearing);
132 steerMotor.SetRotorVelocity(_modules[i].SteerMotor.GetAngularVelocity() * _modules[i].SteerGearing);
133
134 /* CANcoders see the mechanism, so don't account for the steer gearing */
135 cancoder.SetRawPosition(_modules[i].SteerMotor.GetAngularPosition());
136 cancoder.SetVelocity(_modules[i].SteerMotor.GetAngularVelocity());
137
138 states[i] = modulesToApply[i]->GetCurrentState();
139 }
140
141 auto const angularVel = _kinem.ToChassisSpeeds(states).omega;
142 _lastAngle = _lastAngle + Rotation2d{angularVel * dt};
145 }
146
147protected:
148 /**
149 * \brief Applies the effects of friction to dampen the motor voltage.
150 *
151 * \param motorVoltage Voltage output by the motor
152 * \param frictionVoltage Voltage required to overcome friction
153 * \returns Friction-dampened motor voltage
154 */
155 static units::volt_t AddFriction(units::volt_t motorVoltage, units::volt_t frictionVoltage)
156 {
157 if (units::math::abs(motorVoltage) < frictionVoltage) {
158 motorVoltage = 0_V;
159 } else if (motorVoltage > 0_V) {
160 motorVoltage -= frictionVoltage;
161 } else {
162 motorVoltage += frictionVoltage;
163 }
164 return motorVoltage;
165 }
166};
167
168}
169}
170}
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
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 cancoderInverted)
Definition SimSwerveDrivetrain.hpp:57
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
bool CANcoderInverted
Whether the CANcoder is inverted.
Definition SimSwerveDrivetrain.hpp:55
units::volt_t DriveFrictionVoltage
Voltage necessary for the drive motor to overcome friction.
Definition SimSwerveDrivetrain.hpp:47
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 CANcoder.
Definition SimSwerveDrivetrain.hpp:45
bool DriveMotorInverted
Whether the drive motor is inverted.
Definition SimSwerveDrivetrain.hpp:51
units::scalar_t DriveGearing
Reference to steer gearing for updating CANcoder.
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:155
impl::SwerveDriveKinematics _kinem
Definition SimSwerveDrivetrain.hpp:77
sim::Pigeon2SimState & _pigeonSim
Definition SimSwerveDrivetrain.hpp:74
SimSwerveDrivetrain(std::vector< Translation2d > wheelLocations, sim::Pigeon2SimState &pigeonSim, ModuleConstants const &... moduleConstants)
Definition SimSwerveDrivetrain.hpp:83
std::vector< SimSwerveModule > _modules
Definition SimSwerveDrivetrain.hpp:75
Rotation2d _lastAngle
Definition SimSwerveDrivetrain.hpp:78
void Update(units::second_t dt, units::volt_t supplyVoltage, std::vector< std::unique_ptr< SwerveModule > > const &modulesToApply)
Definition SimSwerveDrivetrain.hpp:103
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 StatusCodes.h:18
Definition span.hpp:401