-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriveSubsystem.h
More file actions
221 lines (181 loc) · 7.06 KB
/
DriveSubsystem.h
File metadata and controls
221 lines (181 loc) · 7.06 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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/estimator/SwerveDrivePoseEstimator.h>
#include <frc/estimator/SwerveDrivePoseEstimator.h>
#include <frc/filter/SlewRateLimiter.h>
#include <frc/geometry/Pose2d.h>
#include <frc/geometry/Rotation2d.h>
#include <frc/smartdashboard/Field2d.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc/smartdashboard/Field2d.h>
#include <frc/smartdashboard/SmartDashboard.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 <networktables/StructArrayTopic.h>
#include <ctre/phoenix6/sim/Pigeon2SimState.hpp>
#include <subzero/vision/PhotonVisionEstimators.h>
#include <subzero/target/ITurnToTarget.h>
#include <subzero/logging/ConsoleLogger.h>
#include <networktables/StructArrayTopic.h>
#include <ctre/phoenix6/sim/Pigeon2SimState.hpp>
#include <subzero/vision/PhotonVisionEstimators.h>
#include <subzero/target/ITurnToTarget.h>
#include <subzero/logging/ConsoleLogger.h>
#include "Constants.h"
#include "MAXSwerveModule.h"
class DriveSubsystem : public frc2::SubsystemBase {
public:
explicit 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);
frc2::CommandPtr MoveToAngle(units::degree_t angle);
/**
* 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 ResetRotation();
void SimulationPeriodic() override;
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}};
void AddVisionMeasurement(const frc::Pose2d& visionMeasurement,
units::second_t timestamp);
void AddVisionMeasurement(const frc::Pose2d& visionMeasurement,
units::second_t timestamp,
const Eigen::Vector3d& stdDevs);
wpi::array<frc::SwerveModulePosition, 4U> GetModulePositions() const;
frc::ChassisSpeeds GetSpeedsFromJoystick(
units::meters_per_second_t xSpeed, units::meters_per_second_t ySpeed,
units::radians_per_second_t rot, bool fieldRelative);
void logDrivebase();
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{DriveConstants::kPigeonCanId, "rio"};
ctre::phoenix6::sim::Pigeon2SimState& m_simPidgey = m_pidgey.GetSimState();
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;
frc::SwerveDrivePoseEstimator<4> poseEstimator{
m_driveKinematics,
GetHeading(),
{m_frontLeft.GetPosition(), m_rearLeft.GetPosition(),
m_frontRight.GetPosition(), m_rearRight.GetPosition()},
frc::Pose2d{0_m, 0_m, 0_rad},
VisionConstants::kDrivetrainStd,
VisionConstants::kVisionStd};
nt::StructArrayPublisher<frc::SwerveModuleState> m_publisher;
nt::StructArrayPublisher<frc::SwerveModuleState> m_desiredPublisher;
// Pose viewing
frc::Field2d m_field;
frc::Pose2d m_lastGoodPosition;
photon::PhotonPoseEstimator poseLeft{
// layout
VisionConstants::kTagLayout,
// strategy
VisionConstants::kPoseStrategy,
// offsets
VisionConstants::kRobotToCamLeft};
// photon::PhotonPoseEstimator poseRight{
// // layout
// VisionConstants::kTagLayout,
// // strategy
// VisionConstants::kPoseStrategy,
// // offsets
// VisionConstants::kRobotToCamRight};
photon::PhotonCamera m_leftCamera{VisionConstants::kLeftCameraName};
// photon::PhotonCamera m_rightCamera{VisionConstants::kRightCameraName};
std::vector<subzero::PhotonVisionEstimators::PhotonCameraEstimator>
poseCameras{
subzero::PhotonVisionEstimators::PhotonCameraEstimator(poseLeft, m_leftCamera),
// subzero::PhotonVisionEstimators::PhotonCameraEstimator(poseRight, m_rightCamera),
};
subzero::PhotonVisionEstimators m_vision{poseCameras,
VisionConstants::kSingleTagStdDevs,
VisionConstants::kMultiTagStdDevs
};
units::second_t m_lastUpdatedTime;
};