-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDriveSubsystem.h
137 lines (113 loc) · 4.02 KB
/
DriveSubsystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <frc/ADIS16470_IMU.h>
#include <frc/filter/SlewRateLimiter.h>
#include <frc/geometry/Pose2d.h>
#include <frc/geometry/Rotation2d.h>
#include <frc/kinematics/ChassisSpeeds.h>
#include <frc/kinematics/SwerveDriveKinematics.h>
#include <frc/kinematics/SwerveDriveOdometry.h>
#include <frc2/command/SubsystemBase.h>
#include <frc/XboxController.h>
#include <frc/controller/PIDController.h>
#include <frc/controller/ProfiledPIDController.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc2/command/Command.h>
#include <frc2/command/InstantCommand.h>
#include <frc2/command/PIDCommand.h>
#include <frc2/command/ParallelRaceGroup.h>
#include <frc2/command/RunCommand.h>
#include <ctre/phoenix6/Pigeon2.hpp>
#include "Constants.h"
#include "MAXSwerveModule.h"
class DriveSubsystem : public frc2::SubsystemBase {
public:
DriveSubsystem();
/**
* Will be called periodically whenever the CommandScheduler runs.
*/
void Periodic() override;
// Subsystem methods go here.
/**
* Drives the robot at given x, y and theta speeds. Speeds range from [-1, 1]
* and the linear speeds have no effect on the angular speed.
*
* @param xSpeed Speed of the robot in the x direction
* (forward/backwards).
* @param ySpeed Speed of the robot in the y direction (sideways).
* @param rot Angular rate of the robot.
* @param fieldRelative Whether the provided x and y speeds are relative to
* the field.
*/
void Drive(units::meters_per_second_t xSpeed,
units::meters_per_second_t ySpeed, units::radians_per_second_t rot,
bool fieldRelative);
void Drive(frc::ChassisSpeeds speeds);
/**
* Sets the wheels into an X formation to prevent movement.
*/
void SetX();
/**
* Resets the drive encoders to currently read a position of 0.
*/
void ResetEncoders();
/**
* Sets the drive MotorControllers to a power from -1 to 1.
*/
void SetModuleStates(wpi::array<frc::SwerveModuleState, 4> desiredStates);
/**
* Returns the heading of the robot.
*
* @return the robot's heading in degrees, from 180 to 180
*/
units::degree_t GetHeading();
/**
* Zeroes the heading of the robot.
*/
void ZeroHeading();
/**
* Returns the turn rate of the robot.
*
* @return The turn rate of the robot, in degrees per second
*/
double GetTurnRate();
/**
* Returns the currently-estimated pose of the robot.
*
* @return The pose.
*/
frc::Pose2d GetPose();
/**
* Resets the odometry to the specified pose.
*
* @param pose The pose to which to set the odometry.
*/
void ResetOdometry(frc::Pose2d pose);
void OffsetRotation(frc::Rotation2d rot);
void ResetOdometry();
frc::SwerveDriveKinematics<4> m_driveKinematics{
frc::Translation2d{DriveConstants::kWheelBase / 2,
DriveConstants::kTrackWidth / 2},
frc::Translation2d{DriveConstants::kWheelBase / 2,
-DriveConstants::kTrackWidth / 2},
frc::Translation2d{-DriveConstants::kWheelBase / 2,
DriveConstants::kTrackWidth / 2},
frc::Translation2d{-DriveConstants::kWheelBase / 2,
-DriveConstants::kTrackWidth / 2}};
private:
// Components (e.g. motor controllers and sensors) should generally be
// declared private and exposed only through public methods.
frc::ChassisSpeeds getRobotRelativeSpeeds();
MAXSwerveModule m_frontLeft;
MAXSwerveModule m_rearLeft;
MAXSwerveModule m_frontRight;
MAXSwerveModule m_rearRight;
// Gyro Sensor
ctre::phoenix6::hardware::Pigeon2 m_pidgey{13, "rio"};
units::time::second_t currentTime{frc::Timer::GetFPGATimestamp()};
// Odometry class for tracking robot pose
// 4 defines the number of modules
frc::SwerveDriveOdometry<4> m_odometry;
};