2022-Robot
Robot code for 2022 FRC Season by Argos, FRC team #1756
Loading...
Searching...
No Matches
swerve_drive_subsystem.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <frc/ADIS16448_IMU.h>
8#include <frc/controller/HolonomicDriveController.h>
9#include <frc/kinematics/ChassisSpeeds.h>
10#include <frc/kinematics/SwerveDriveKinematics.h>
11#include <frc/kinematics/SwerveDriveOdometry.h>
12#include <frc/kinematics/SwerveModuleState.h>
13#include <frc2/command/SubsystemBase.h>
14
15#include <memory>
16
19#include "ctre/Phoenix.h"
23
25 public:
26 // MOTORS
27 WPI_TalonFX m_drive;
28 WPI_TalonFX m_turn;
29 // ENCODER
30 CANCoder m_encoder;
31
39 SwerveModule(const argos_lib::CANAddress& driveAddr,
40 const argos_lib::CANAddress& turnAddr,
41 const argos_lib::CANAddress& encoderAddr);
42
43 frc::SwerveModuleState GetState();
44};
45
50class SwerveDriveSubsystem : public frc2::SubsystemBase {
51 public:
57
58 explicit SwerveDriveSubsystem(std::shared_ptr<NetworkTablesWrapper> networkTable,
59 const argos_lib::RobotInstance instance);
60
64 void Disable();
65
73 void SwerveDrive(const double& fwVelocity, const double& sideVelocity, const double& rotVelocity);
74
78 void StopDrive();
79
86 void Home(const units::degree_t& angle);
87
94 void FieldHome(units::degree_t homeAngle = 0_deg, bool updateOdometry = true);
95
101 void InitializeOdometry(const frc::Pose2d& currentPose);
102
103 frc::Rotation2d GetContinuousOdometryAngle();
104
105 frc::Pose2d GetContinuousOdometry();
106
112 frc::Pose2d UpdateOdometry();
113
120 units::degree_t GetFieldCentricAngle() const;
121
127 frc::Pose2d GetPoseEstimate();
128
130
135 void InitializeMotors();
136
145 void UpdateFollowerLinearPIDParams(double kP, double kI, double kD);
146
155 void UpdateFollowerRotationalPIDParams(double kP, double kI, double kD);
156
162 void UpdateFollowerRotationalPIDConstraints(frc::TrapezoidProfile<units::degrees>::Constraints constraints);
163
170
175
181 bool ProfileIsComplete() const;
182
189 frc::ChassisSpeeds GetChassisVelocity();
190
191 private:
193
195
200
201 // GYROSCOPIC SENSORS
202 frc::ADIS16448_IMU m_imu;
203 Pigeon2 m_pigeonIMU;
204
205 units::degree_t m_fieldHomeOffset;
206
211 struct Velocities {
212 const double fwVelocity;
213 const double sideVelocity;
214 const double rotVelocity;
215 };
216
217 frc::SwerveDriveKinematics<4> m_swerveDriveKinematics;
218
219 frc::SwerveDriveOdometry<4> m_odometry;
220 units::degree_t m_prevOdometryAngle;
222
223 // POINTER TO NETWORK TABLE CLASS OBJECT
224 std::shared_ptr<NetworkTablesWrapper> m_pNetworkTable;
225
226 // std::FILE SYSTEM HOMING STORAGE
228
231 std::unique_ptr<SwerveTrapezoidalProfileSegment> m_pActiveSwerveProfile;
232 std::chrono::time_point<std::chrono::steady_clock> m_swerveProfileStartTime;
233 frc2::PIDController m_linearPID;
234 frc::ProfiledPIDController<units::radians>
236 frc::HolonomicDriveController m_followerController;
237
239
246 wpi::array<frc::SwerveModuleState, 4> GetRawModuleStates(SwerveDriveSubsystem::Velocities velocities);
247
253 wpi::array<frc::SwerveModuleState, 4> GetCurrentModuleStates();
254
261 void HomeToFS(const units::degree_t& angle);
262
268
269 units::degree_t GetIMUYaw() const;
270 void ResetIMUYaw();
271};
Accesses swerve module home positions in a file saved on the RoboRIO.
Definition: file_system_homing_storage.h:13
Subsystem for controlling the swerve drivetrain of the robot.
Definition: swerve_drive_subsystem.h:50
void Home(const units::degree_t &angle)
Save homes to persistent storage and updates module motors.
Definition: swerve_drive_subsystem.cpp:419
frc::Pose2d GetContinuousOdometry()
Definition: swerve_drive_subsystem.cpp:464
frc::Pose2d GetPoseEstimate()
Get the latest pose estimate.
Definition: swerve_drive_subsystem.cpp:487
void Disable()
Handle the robot disabling.
Definition: swerve_drive_subsystem.cpp:158
void HomeToFS(const units::degree_t &angle)
Save homes to a file.
Definition: swerve_drive_subsystem.cpp:499
void FieldHome(units::degree_t homeAngle=0_deg, bool updateOdometry=true)
Tell the robot it's in it's correct field-oriented "Front".
Definition: swerve_drive_subsystem.cpp:432
void UpdateFollowerRotationalPIDParams(double kP, double kI, double kD)
Change PID parameters for rotational follower. These adjust velocities based on angle error from path...
Definition: swerve_drive_subsystem.cpp:560
SwerveModule m_backRight
Back right swerve module.
Definition: swerve_drive_subsystem.h:198
frc::HolonomicDriveController m_followerController
Controller to follow drive profile.
Definition: swerve_drive_subsystem.h:236
SwerveModule m_frontLeft
Front left swerve module.
Definition: swerve_drive_subsystem.h:196
void SwerveDrive(const double &fwVelocity, const double &sideVelocity, const double &rotVelocity)
Main drive function for the robot.
Definition: swerve_drive_subsystem.cpp:213
std::chrono::time_point< std::chrono::steady_clock > m_swerveProfileStartTime
Time when active profile began.
Definition: swerve_drive_subsystem.h:232
frc2::PIDController m_linearPID
Correction parameters for x/y error when following drive profile.
Definition: swerve_drive_subsystem.h:233
DriveControlMode
Enumerator for either field-centric or robot centric drive modes.
Definition: swerve_drive_subsystem.h:56
@ robotCentricControl
Definition: swerve_drive_subsystem.h:56
@ fieldCentricControl
Definition: swerve_drive_subsystem.h:56
bool m_profileComplete
True once a drive profile has been completed.
Definition: swerve_drive_subsystem.h:230
void CancelDrivingProfile()
Cancel the current driving profile without marking it complete.
Definition: swerve_drive_subsystem.cpp:580
bool m_followingProfile
True when an incomplete drive profile is being run.
Definition: swerve_drive_subsystem.h:229
void UpdateFollowerRotationalPIDConstraints(frc::TrapezoidProfile< units::degrees >::Constraints constraints)
Update constraints to rotate robot along profiled path.
Definition: swerve_drive_subsystem.cpp:565
FileSystemHomingStorage m_fsStorage
Roborio filesystem access for homes.
Definition: swerve_drive_subsystem.h:227
units::degree_t m_fieldHomeOffset
Offset from IMU angle to 0 field angle (intake away from driver station)
Definition: swerve_drive_subsystem.h:205
frc::Pose2d UpdateOdometry()
Reads module states & gyro, updates odometry, and returns latest pose estimate.
Definition: swerve_drive_subsystem.cpp:469
void UpdateFollowerLinearPIDParams(double kP, double kI, double kD)
Change PID parameters for linear follower. These adjust velocities based on distance error from path ...
Definition: swerve_drive_subsystem.cpp:555
void InitializeOdometry(const frc::Pose2d &currentPose)
Set current robot position. Useful for initializing initial position before autonomous.
Definition: swerve_drive_subsystem.cpp:443
void InitializeMotorsFromFS()
Initialize motors from saved file.
Definition: swerve_drive_subsystem.cpp:513
void StartDrivingProfile(SwerveTrapezoidalProfileSegment newProfile)
Start driving a new profile. This also resets the finished flag.
Definition: swerve_drive_subsystem.cpp:572
frc::SwerveDriveKinematics< 4 > m_swerveDriveKinematics
Kinematics model for swerve drive system.
Definition: swerve_drive_subsystem.h:217
SwerveModule m_backLeft
Back left swerve module.
Definition: swerve_drive_subsystem.h:199
DriveControlMode m_controlMode
Active control mode.
Definition: swerve_drive_subsystem.h:194
frc::ProfiledPIDController< units::radians > m_rotationalPID
Correction parameters for rotational error when following drive profile.
Definition: swerve_drive_subsystem.h:235
void SetControlMode(SwerveDriveSubsystem::DriveControlMode controlMode)
Definition: swerve_drive_subsystem.cpp:491
wpi::array< frc::SwerveModuleState, 4 > GetRawModuleStates(SwerveDriveSubsystem::Velocities velocities)
Get the Raw Module States object and switch between robot-centric and field-centric.
Definition: swerve_drive_subsystem.cpp:167
units::degree_t GetIMUYaw() const
Definition: swerve_drive_subsystem.cpp:589
argos_lib::NTMotorPIDTuner m_driveMotorPIDTuner
Utility to tune drive motors.
Definition: swerve_drive_subsystem.h:238
wpi::array< frc::SwerveModuleState, 4 > GetCurrentModuleStates()
Get the active states of all swerve modules.
Definition: swerve_drive_subsystem.cpp:209
bool ProfileIsComplete() const
Check if a driving profile path has been completed.
Definition: swerve_drive_subsystem.cpp:585
void InitializeMotors()
Initialize motors from persistent storage.
Definition: swerve_drive_subsystem.cpp:495
frc::ADIS16448_IMU m_imu
Definition: swerve_drive_subsystem.h:202
frc::ChassisSpeeds GetChassisVelocity()
Get the robot velocity in chassis frame (x toward intake, y toward left) based on GetCurrentModuleSta...
Definition: swerve_drive_subsystem.cpp:597
std::shared_ptr< NetworkTablesWrapper > m_pNetworkTable
Instance of network table class.
Definition: swerve_drive_subsystem.h:224
std::unique_ptr< SwerveTrapezoidalProfileSegment > m_pActiveSwerveProfile
Profile to execute.
Definition: swerve_drive_subsystem.h:231
void StopDrive()
Stop all motors.
Definition: swerve_drive_subsystem.cpp:408
SwerveModule m_frontRight
Front right swerve module.
Definition: swerve_drive_subsystem.h:197
Pigeon2 m_pigeonIMU
Definition: swerve_drive_subsystem.h:203
frc::SwerveDriveOdometry< 4 > m_odometry
Odometry to track robot.
Definition: swerve_drive_subsystem.h:219
frc::Rotation2d GetContinuousOdometryAngle()
Definition: swerve_drive_subsystem.cpp:451
units::degree_t m_continuousOdometryOffset
Offset to convert [-180,180] odometry angle to continuous angle.
Definition: swerve_drive_subsystem.h:221
void ResetIMUYaw()
Definition: swerve_drive_subsystem.cpp:593
units::degree_t m_prevOdometryAngle
Last odometry angle used for continuous calculations.
Definition: swerve_drive_subsystem.h:220
units::degree_t GetFieldCentricAngle() const
Get the field-centric angle of the robot based on gyro and saved reference orientation.
Definition: swerve_drive_subsystem.cpp:483
argos_lib::RobotInstance m_instance
Definition: swerve_drive_subsystem.h:192
Definition: swerve_drive_subsystem.h:24
frc::SwerveModuleState GetState()
Definition: swerve_drive_subsystem.cpp:549
WPI_TalonFX m_drive
Definition: swerve_drive_subsystem.h:27
CANCoder m_encoder
Definition: swerve_drive_subsystem.h:30
WPI_TalonFX m_turn
Definition: swerve_drive_subsystem.h:28
Definition: swerve_trapezoidal_profile.h:18
Allows user to set PID parameters from network tables and update the motor configurations on updates....
Definition: nt_motor_pid_tuner.h:48
RobotInstance
Differentiates between practice robot and competition robot.
Definition: config_types.h:13
Definition: constraints.h:16
A struct for holding the 3 different input velocities, for organization.
Definition: swerve_drive_subsystem.h:211
const double sideVelocity
Definition: swerve_drive_subsystem.h:213
const double rotVelocity
Definition: swerve_drive_subsystem.h:214
const double fwVelocity
Definition: swerve_drive_subsystem.h:212
Definition: config_types.h:25