FRC Paradigm Shift #1259
Documentation for the 2021 Robot
DriveSubsystem.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <frc/Encoder.h>
11 #include <frc/geometry/Pose2d.h>
12 #include <frc/geometry/Rotation2d.h>
13 #include <frc/kinematics/ChassisSpeeds.h>
14 #include <frc/kinematics/SwerveDriveKinematics.h>
15 #include <frc/kinematics/SwerveDriveOdometry.h>
16 #include <frc/SmartDashBoard/SmartDashboard.h>
17 #include <frc2/command/SubsystemBase.h>
18 
19 #include <ctre/phoenix/CANifier.h>
20 
21 #include "common/Util.h"
22 #include "common/Gyro.h"
23 
24 #include "Constants.h"
25 #include "SwerveModule.h"
26 
27 // Uncomment to directly set states to each module
28 //#define MANUAL_MODULE_STATES
29 // Uncomment to tune Rotation Drive PIDs
30 //#define TUNE_ROTATION_DRIVE
31 
32 using namespace ctre::phoenix;
33 using namespace DriveConstants;
34 using namespace std;
35 using namespace frc;
36 
37 class DriveSubsystem : public frc2::SubsystemBase
38 {
39 public:
41  {
45  kRearRight
46  };
47 
48  DriveSubsystem(Gyro *gyro);
49 
51  void Periodic() override;
52 
53  // Subsystem methods go here.
54 
64  void Drive(meters_per_second_t xSpeed, meters_per_second_t ySpeed, radians_per_second_t rot, bool fieldRelative);
65 
66  // Drives the robot with the right stick controlling the position angle of the robot
73  void RotationDrive(meters_per_second_t xSpeed, meters_per_second_t ySpeed, radian_t rot, bool fieldRelative);
74 
75  // Drives the robot with the right stick controlling the position angle of the robot
83  void RotationDrive(meters_per_second_t xSpeed, meters_per_second_t ySpeed, double xRot, double yRot, bool fieldRelative);
84 
92  void HeadingDrive(meters_per_second_t xSpeed, meters_per_second_t ySpeed, radians_per_second_t rot, bool fieldRelative);
93 
95  void UpdateLastHeading();
96 
98  void ResetEncoders();
99 
101  using SwerveModuleStates = wpi::array<SwerveModuleState, DriveConstants::kNumSwerveModules>;
103  void SetModuleStates(SwerveModuleStates desiredStates);
104 
107  Pose2d GetPose();
108 
112  double PWMToPulseWidth(CANifier::PWMChannel pwmChannel);
113 
116  void ResetOdometry(Pose2d pose);
117 
119  void WheelsForward();
120 
122  void ResetRelativeToAbsolute();
123 
125  SwerveDriveKinematics<kNumSwerveModules> kDriveKinematics{
126  Translation2d( kWheelBase / 2, kTrackWidth / 2), // +x, +y FL
127  Translation2d( kWheelBase / 2, -kTrackWidth / 2), // +x, -y FR
128  Translation2d(-kWheelBase / 2, kTrackWidth / 2), // -x, +y RL
129  Translation2d(-kWheelBase / 2, -kTrackWidth / 2)}; // -x, -y RR
130 
131 private:
134  {
135  SwerveModuleStates sms({
136  m_frontLeft.GetState(),
137  m_frontRight.GetState(),
138  m_rearLeft.GetState(),
139  m_rearRight.GetState()
140  });
141  return sms;
142  }
143 
152 
154  CANifier m_canifier;
158  SwerveDriveOdometry<DriveConstants::kNumSwerveModules> m_odometry;
159 
161  frc2::PIDController m_rotationPIDController{
165  };
170 };
ModuleLocation
< Order as returned by kDriveKinematics.ToSwerveModuleStates
constexpr double kRotationDriveI
Definition: Constants.h:105
constexpr double kRotationDriveD
Definition: Constants.h:107
Definition: Gyro.h:14
STL namespace.
constexpr meter_t kTrackWidth
Distance between centers of left and right wheels on robot.
Definition: Constants.h:31
SwerveDriveOdometry< DriveConstants::kNumSwerveModules > m_odometry
Odometry class for tracking robot pose.
CANifier m_canifier
Reads the absolute encoder pulse widths.
SwerveModule m_frontRight
wpi::array< SwerveModuleState, DriveConstants::kNumSwerveModules > SwerveModuleStates
Readable alias for array of swerve modules.
double m_lastHeading
Last maintained heading, used for HeadingDrive.
SwerveModuleStates getCurrentWheelSpeeds()
Get all 4 swerve module wheel speed to update the odometry with.
Gyro * m_gyro
Gyro to determine field relative driving, from RobotContainer.
constexpr meter_t kWheelBase
Distance between centers of front and back wheels on robot.
Definition: Constants.h:33
constexpr double kRotationDriveP
Definition: Constants.h:104
SwerveModule m_rearRight
SwerveModule m_frontLeft
bool m_rotationalInput
Whether or not rotation input was provided, used for HeadingDrive.
SwerveModule m_rearLeft