2023-Robot
Robot code for 2023 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/Timer.h>
9#include <frc/controller/HolonomicDriveController.h>
10#include <frc/kinematics/ChassisSpeeds.h>
11#include <frc/kinematics/SwerveDriveKinematics.h>
12#include <frc/kinematics/SwerveDriveOdometry.h>
13#include <frc/kinematics/SwerveModulePosition.h>
14#include <frc/kinematics/SwerveModuleState.h>
15#include <frc2/command/SubsystemBase.h>
16
17#include <memory>
18
23#include "ctre/Phoenix.h"
24#include "frc/StateSpaceUtil.h"
25#include "frc/estimator/SwerveDrivePoseEstimator.h"
28
30 public:
31 // MOTORS
32 WPI_TalonFX m_drive;
33 WPI_TalonFX m_turn;
34 // ENCODER
35 CANCoder m_encoder;
36
44 SwerveModule(const argos_lib::CANAddress& driveAddr,
45 const argos_lib::CANAddress& turnAddr,
46 const argos_lib::CANAddress& encoderAddr);
47
48 frc::SwerveModuleState GetState();
49 frc::SwerveModulePosition GetPosition();
50};
51
56class SwerveDriveSubsystem : public frc2::SubsystemBase {
57 public:
63
64 explicit SwerveDriveSubsystem(const argos_lib::RobotInstance instance);
65
69 void Disable();
70
78 void SwerveDrive(const double fwVelocity, const double sideVelocity, const double rotVelocity);
79
84 void SwerveDrive(const units::degree_t& velAngle, const double& velocity, const double& rotVelocity);
85
89 void SwerveDrive(const units::degree_t& velAngle, const double& velocity);
90
94 void StopDrive();
95
102 void Home(const units::degree_t& angle);
103
110 void FieldHome(units::degree_t homeAngle = 0_deg, bool updateOdometry = true);
111
117 void InitializeOdometry(const frc::Pose2d& currentPose);
118
119 frc::Rotation2d GetContinuousOdometryAngle();
120
121 frc::Rotation2d GetNearestSquareAngle();
122
123 frc::Pose2d GetContinuousOdometry();
124
130 frc::Pose2d UpdateEstimatedPose();
131
138 units::degree_t GetFieldCentricAngle() const;
139
145 frc::Pose2d GetPoseEstimate(const frc::Pose2d& robotPose, const units::millisecond_t& latency);
146
148
153 void InitializeMotors();
154
163 void UpdateFollowerLinearPIDParams(double kP, double kI, double kD);
164
173 void UpdateFollowerRotationalPIDParams(double kP, double kI, double kD);
174
180 void UpdateFollowerRotationalPIDConstraints(frc::TrapezoidProfile<units::radians>::Constraints constraints);
181 void UpdateFollowerRotationalPIDConstraints(frc::TrapezoidProfile<units::degrees>::Constraints constraints);
182
189
196
201
207 bool ProfileIsComplete() const;
208
214 bool IsFollowingProfile() const;
215
222 frc::ChassisSpeeds GetChassisVelocity();
223
227 void LockWheels();
228
229 bool GetManualOverride();
230
236 units::degree_t GetRobotPitch() const { return units::degree_t{m_pigeonIMU.GetRoll()}; }
237
243 units::degrees_per_second_t GetRobotPitchRate();
244
245 private:
247
249
254
255 // GYROSCOPIC SENSORS
256 frc::ADIS16448_IMU m_imu;
257 Pigeon2 m_pigeonIMU;
258
259 units::degree_t m_fieldHomeOffset;
260
265 struct Velocities {
266 const double fwVelocity;
267 const double sideVelocity;
268 const double rotVelocity;
269 };
270
271 frc::SwerveDriveKinematics<4> m_swerveDriveKinematics;
272
273 frc::SwerveDriveOdometry<4> m_odometry;
274 units::degree_t m_prevOdometryAngle;
276
277 frc::SwerveDrivePoseEstimator<4> m_poseEstimator;
278
279 // std::FILE SYSTEM HOMING STORAGE
281
285 std::unique_ptr<SwerveTrapezoidalProfileSegment> m_pActiveSwerveProfile;
286 std::unique_ptr<SwerveTrapezoidalSpline> m_pActiveSwerveSplineProfile;
287 std::chrono::time_point<std::chrono::steady_clock> m_swerveProfileStartTime;
288 frc::ProfiledPIDController<units::radians>::Constraints m_rotationalPIDConstraints;
289 frc2::PIDController m_linearPID;
290 frc::HolonomicDriveController m_followerController;
291
301
308 wpi::array<frc::SwerveModuleState, 4> GetRawModuleStates(SwerveDriveSubsystem::Velocities velocities);
309
315 wpi::array<frc::SwerveModuleState, 4> GetCurrentModuleStates();
316
322 wpi::array<frc::SwerveModulePosition, 4> GetCurrentModulePositions();
323
330 void HomeToFS(const units::degree_t& angle);
331
337
338 units::degree_t GetIMUYaw() const;
339 void ResetIMUYaw();
340};
Subsystem for controlling the swerve drivetrain of the robot.
Definition swerve_drive_subsystem.h:56
void Home(const units::degree_t &angle)
Save homes to persistent storage and updates module motors.
Definition swerve_drive_subsystem.cpp:541
frc::Pose2d GetContinuousOdometry()
Definition swerve_drive_subsystem.cpp:636
void Disable()
Handle the robot disabling.
Definition swerve_drive_subsystem.cpp:220
void HomeToFS(const units::degree_t &angle)
Save homes to a file.
Definition swerve_drive_subsystem.cpp:690
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:594
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:759
SwerveModule m_backRight
Back right swerve module.
Definition swerve_drive_subsystem.h:252
std::unique_ptr< SwerveTrapezoidalSpline > m_pActiveSwerveSplineProfile
Profile to execute.
Definition swerve_drive_subsystem.h:286
frc::HolonomicDriveController m_followerController
Controller to follow drive profile.
Definition swerve_drive_subsystem.h:290
SwerveModule m_frontLeft
Front left swerve module.
Definition swerve_drive_subsystem.h:250
frc::Pose2d UpdateEstimatedPose()
Reads module states & gyro, updates pose estimator, and returns latest pose estimate.
Definition swerve_drive_subsystem.cpp:641
std::chrono::time_point< std::chrono::steady_clock > m_swerveProfileStartTime
Time when active profile began.
Definition swerve_drive_subsystem.h:287
frc2::PIDController m_linearPID
Correction parameters for x/y error when following drive profile.
Definition swerve_drive_subsystem.h:289
DriveControlMode
Enumerator for either field-centric or robot centric drive modes.
Definition swerve_drive_subsystem.h:62
@ robotCentricControl
Definition swerve_drive_subsystem.h:62
@ fieldCentricControl
Definition swerve_drive_subsystem.h:62
bool m_profileComplete
True once a drive profile has been completed.
Definition swerve_drive_subsystem.h:283
void CancelDrivingProfile()
Cancel the current driving profile without marking it complete.
Definition swerve_drive_subsystem.cpp:798
bool m_followingProfile
True when an incomplete drive profile is being run.
Definition swerve_drive_subsystem.h:282
bool IsFollowingProfile() const
Check if drivetrain is following a profile.
Definition swerve_drive_subsystem.cpp:807
units::degree_t m_fieldHomeOffset
Offset from IMU angle to 0 field angle (intake away from driver station)
Definition swerve_drive_subsystem.h:259
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:752
argos_lib::NTSubscriber m_rotationalFollowerConstraintTuner_accel
Definition swerve_drive_subsystem.h:300
void InitializeOdometry(const frc::Pose2d &currentPose)
Set current robot position. Useful for initializing initial position before autonomous.
Definition swerve_drive_subsystem.cpp:606
void InitializeMotorsFromFS()
Initialize motors from saved file.
Definition swerve_drive_subsystem.cpp:704
frc::Pose2d GetPoseEstimate(const frc::Pose2d &robotPose, const units::millisecond_t &latency)
Get the latest pose estimate.
Definition swerve_drive_subsystem.cpp:655
void StartDrivingProfile(SwerveTrapezoidalProfileSegment newProfile)
Start driving a new profile. This also resets the finished flag.
Definition swerve_drive_subsystem.cpp:777
frc::SwerveDriveKinematics< 4 > m_swerveDriveKinematics
Kinematics model for swerve drive system.
Definition swerve_drive_subsystem.h:271
SwerveModule m_backLeft
Back left swerve module.
Definition swerve_drive_subsystem.h:253
DriveControlMode m_controlMode
Active control mode.
Definition swerve_drive_subsystem.h:248
wpi::array< frc::SwerveModulePosition, 4 > GetCurrentModulePositions()
Get the active positions of all swerve modules.
Definition swerve_drive_subsystem.cpp:274
void SetControlMode(SwerveDriveSubsystem::DriveControlMode controlMode)
Definition swerve_drive_subsystem.cpp:682
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:228
argos_lib::NTSubscriber m_linearFollowerTuner_P
Definition swerve_drive_subsystem.h:293
units::degree_t GetIMUYaw() const
Definition swerve_drive_subsystem.cpp:811
argos_lib::NTSubscriber m_linearFollowerTuner_D
Definition swerve_drive_subsystem.h:295
argos_lib::NTMotorPIDTuner m_driveMotorPIDTuner
Utility to tune drive motors.
Definition swerve_drive_subsystem.h:292
wpi::array< frc::SwerveModuleState, 4 > GetCurrentModuleStates()
Get the active states of all swerve modules.
Definition swerve_drive_subsystem.cpp:270
bool ProfileIsComplete() const
Check if a driving profile path has been completed.
Definition swerve_drive_subsystem.cpp:803
void SwerveDrive(const double fwVelocity, const double sideVelocity, const double rotVelocity)
Main drive function for the robot.
Definition swerve_drive_subsystem.cpp:278
void InitializeMotors()
Initialize motors from persistent storage.
Definition swerve_drive_subsystem.cpp:686
frc::ADIS16448_IMU m_imu
Definition swerve_drive_subsystem.h:256
units::degrees_per_second_t GetRobotPitchRate()
Get the rate of robot pitch.
Definition swerve_drive_subsystem.cpp:554
frc::ChassisSpeeds GetChassisVelocity()
Get the robot velocity in chassis frame (x toward intake, y toward left) based on GetCurrentModuleSta...
Definition swerve_drive_subsystem.cpp:819
argos_lib::NTSubscriber m_rotationalFollowerConstraintTuner_vel
Definition swerve_drive_subsystem.h:299
units::degree_t GetRobotPitch() const
Get the robot pitch as determined by the pigeon IMU.
Definition swerve_drive_subsystem.h:236
frc::ProfiledPIDController< units::radians >::Constraints m_rotationalPIDConstraints
Definition swerve_drive_subsystem.h:288
void LockWheels()
Put the robot wheels in an x shape where it locks the movement of it.
Definition swerve_drive_subsystem.cpp:560
void UpdateFollowerRotationalPIDConstraints(frc::TrapezoidProfile< units::radians >::Constraints constraints)
Update constraints to rotate robot along profiled path.
Definition swerve_drive_subsystem.cpp:764
argos_lib::NTSubscriber m_rotationalFollowerTuner_I
Definition swerve_drive_subsystem.h:297
std::unique_ptr< SwerveTrapezoidalProfileSegment > m_pActiveSwerveProfile
Profile to execute.
Definition swerve_drive_subsystem.h:285
void StopDrive()
Stop all motors.
Definition swerve_drive_subsystem.cpp:530
SwerveModule m_frontRight
Front right swerve module.
Definition swerve_drive_subsystem.h:251
Pigeon2 m_pigeonIMU
Definition swerve_drive_subsystem.h:257
frc::Rotation2d GetNearestSquareAngle()
Definition swerve_drive_subsystem.cpp:631
frc::SwerveDriveOdometry< 4 > m_odometry
Odometry to track robot.
Definition swerve_drive_subsystem.h:273
bool m_manualOverride
Definition swerve_drive_subsystem.h:284
argos_lib::NTSubscriber m_rotationalFollowerTuner_P
Definition swerve_drive_subsystem.h:296
frc::Rotation2d GetContinuousOdometryAngle()
Definition swerve_drive_subsystem.cpp:614
units::degree_t m_continuousOdometryOffset
Offset to convert [-180,180] odometry angle to continuous angle.
Definition swerve_drive_subsystem.h:275
void ResetIMUYaw()
Definition swerve_drive_subsystem.cpp:815
argos_lib::SwerveFSHomingStorage m_fsStorage
Roborio filesystem access for homes.
Definition swerve_drive_subsystem.h:280
argos_lib::NTSubscriber m_linearFollowerTuner_I
Definition swerve_drive_subsystem.h:294
bool GetManualOverride()
Definition swerve_drive_subsystem.cpp:590
argos_lib::NTSubscriber m_rotationalFollowerTuner_D
Definition swerve_drive_subsystem.h:298
frc::SwerveDrivePoseEstimator< 4 > m_poseEstimator
accounts vision-based measurements for odometry
Definition swerve_drive_subsystem.h:277
units::degree_t m_prevOdometryAngle
Last odometry angle used for continuous calculations.
Definition swerve_drive_subsystem.h:274
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:651
argos_lib::RobotInstance m_instance
Definition swerve_drive_subsystem.h:246
Definition swerve_drive_subsystem.h:29
frc::SwerveModuleState GetState()
Definition swerve_drive_subsystem.cpp:740
WPI_TalonFX m_drive
Definition swerve_drive_subsystem.h:32
CANCoder m_encoder
Definition swerve_drive_subsystem.h:35
WPI_TalonFX m_turn
Definition swerve_drive_subsystem.h:33
frc::SwerveModulePosition GetPosition()
Definition swerve_drive_subsystem.cpp:746
Definition swerve_trapezoidal_profile.h:18
Definition swerve_trapezoidal_spline.h:19
Allows user to set PID parameters from network tables and update the motor configurations on updates....
Definition nt_motor_pid_tuner.h:48
Subscribes to Network Tables entry updates and calls a specified callback to use the new value.
Definition nt_subscriber.h:17
Definition fs_homing.h:17
RobotInstance
Differentiates between practice robot and competition robot.
Definition config_types.h:13
A struct for holding the 3 different input velocities, for organization.
Definition swerve_drive_subsystem.h:265
const double sideVelocity
Definition swerve_drive_subsystem.h:267
const double rotVelocity
Definition swerve_drive_subsystem.h:268
const double fwVelocity
Definition swerve_drive_subsystem.h:266
Definition config_types.h:25