Valor 6800 1.0
Loading...
Searching...
No Matches
Swerve.h
1
2#pragma once
3
4#include <deque>
5#include <memory>
6#include <string>
7#include <vector>
8
9#include <choreo/Choreo.h>
10#include <Eigen/Core>
11#include <ctre/phoenix6/Pigeon2.hpp>
12#include <frc/DriverStation.h>
13#include <frc/EigenCore.h>
14#include <frc/Notifier.h>
15#include <frc/controller/ProfiledPIDController.h>
16#include <frc/estimator/SwerveDrivePoseEstimator.h>
17#include <frc/geometry/Pose2d.h>
18#include <frc/kinematics/ChassisSpeeds.h>
19#include <frc/kinematics/SwerveDriveKinematics.h>
20#include <frc/trajectory/TrapezoidProfile.h>
21#include <frc2/command/InstantCommand.h>
22#include <networktables/StructArrayTopic.h>
23#include <networktables/StructTopic.h>
24#include <pathplanner/lib/config/RobotConfig.h>
25#include <pathplanner/lib/util/swerve/SwerveSetpoint.h>
26#include <pathplanner/lib/util/swerve/SwerveSetpointGenerator.h>
27#include <units/acceleration.h>
28#include <units/angle.h>
29#include <units/angular_velocity.h>
30#include <units/length.h>
31#include <units/time.h>
32#include <units/velocity.h>
33
34#include "valkyrie/BaseSubsystem.h"
35#include "valkyrie/CharMode.h"
36#include "valkyrie/controllers/PIDF.h"
37#include "valkyrie/drivetrain/SwerveModule.h"
38#include "valkyrie/util/LockedAccess.h"
39
40#define MODULE_COUNT 4
41
42namespace valor {
43
45 public:
46 CharMode::MODE_OPTIONS selectedTest;
47
48 Swerve(std::string name, std::vector<std::pair<BaseController*, BaseController*>> modules, units::meter_t module_radius,
49 units::meter_t _wheelDiameter, std::vector<int> moduleCoordsX, std::vector<int> moduleCoordsY, units::meter_t driveBaseRadius);
50 ~Swerve();
51
52 void analyzeDashboard() override;
53 void assignOutputs() override;
54 void LoggablePeriodic() override;
55 void reset() override;
56
57 void SimulationPeriodic() override;
58
63 frc::Pose2d getRawPose();
64 frc::Pose2d getCalculatedPose();
65
66 void setupGyro(int, const char*, units::degree_t, units::degree_t, units::degree_t);
67 void resetGyro();
68 frc::Rotation2d getGyro();
69
75 void resetOdometry(frc::Pose2d pose);
76 void resetEncoders();
77
82 wpi::array<frc::SwerveModulePosition, MODULE_COUNT> getModulePositions();
83
88 wpi::array<frc::SwerveModuleState, MODULE_COUNT> getModuleStates();
89
96 wpi::array<frc::SwerveModuleState, MODULE_COUNT> calculateModuleStates(frc::ChassisSpeeds chassisSpeeds, bool isFOC);
97
98 void updateAngularPosition();
99
109 void drive(units::velocity::meters_per_second_t vx_mps, units::velocity::meters_per_second_t vy_mps,
110 units::angular_velocity::radians_per_second_t omega_radps, bool isFOC);
111
119 void drive(frc::ChassisSpeeds _chassisSpeeds, bool isFOC);
120
127 void driveRobotRelative(frc::ChassisSpeeds speeds);
128
129 double getSkiddingRatio();
130 bool isRobotSkidding();
131
132 frc::Pose2d cachedRawPose;
133 frc::Pose2d cachedCalculatedPose;
134 frc::Pose2d cachedPredictedPose;
135
136 frc::ChassisSpeeds cachedChassisFieldSpeed;
137 frc::ChassisSpeeds cachedRobotRelativeSpeed;
138
144 units::angular_acceleration::radians_per_second_squared_t getSmoothedAngularAcceleration();
145 double rotationLerping(double);
146 void followTrajectory(const choreo::SwerveSample& sample);
147
148 units::meter_t getDriveBaseRadius() { return driveBaseRadius; };
149
150 protected:
151 double xSpeed;
152 double ySpeed;
153 double rotSpeed;
154
155 frc::ChassisSpeeds commandedSpeeds;
156
157 units::meters_per_second_t maxDriveSpeed;
158 units::radians_per_second_t maxRotationSpeed;
159
160 std::unique_ptr<ctre::phoenix6::hardware::Pigeon2> pigeon;
161 std::unique_ptr<frc::SwerveDriveKinematics<MODULE_COUNT>> kinematics;
164
165 bool toast;
166
167 void enableCarpetGrain(double grainMultiplier, bool roughTowardsRed);
168
169 frc::ChassisSpeeds vectorLockedSpeeds(const frc::ChassisSpeeds speeds, const frc::Rotation2d heading);
170
171 frc::ChassisSpeeds getRobotRelativeSpeeds();
172 frc::ChassisSpeeds getFieldRelativeSpeeds();
173 void setSwerveDesiredState(wpi::array<frc::SwerveModuleState, MODULE_COUNT> desiredStates, bool isDriveOpenLoop);
174
175 units::radian_t angularPosition = 0_rad;
176
177 wpi::array<frc::SwerveModuleState, MODULE_COUNT> testModeDesiredStates{wpi::empty_array};
178
179 std::vector<valor::SwerveModule*> swerveModules;
180
181 void startOdomThread(units::hertz_t freq);
182 void updateOdometry();
183
184 static frc::ChassisSpeeds reflectDriveSpeeds(const frc::ChassisSpeeds unreflectedSpeeds) {
185 if (frc::DriverStation::GetAlliance().value_or(frc::DriverStation::Alliance::kBlue) == frc::DriverStation::Alliance::kRed) {
187 }
188 return unreflectedSpeeds;
189 }
190
191 frc::Timer resetOdom;
192
193 private:
194 std::deque<units::angular_acceleration::radians_per_second_squared_t> yawRateBuffer; // TODO Replace with a moving average filter
195
196 static constexpr size_t ACCEL_BUFFER_SIZE = 10; // need to adjust // TODO: Replace with an actual moving average filter
197 units::angular_velocity::radians_per_second_t lastYawRate = 0_rad_per_s; // TODO: Replace with a moving average filter
198
199 units::angular_acceleration::radians_per_second_squared_t angularAcceleration = 0_rad_per_s_sq;
200
201 double _drivetrain_accel;
202
203 bool useCarpetGrain;
204 double carpetGrainMultiplier;
205 bool roughTowardsRed;
206 void calculateCarpetPose();
207
208 bool rotTest;
209 bool strLineTest;
210
211 CharMode charac; // TODO: Switch to WPI SysID
212
213 units::meter_t driveBaseRadius;
214
215 // Cached telemetry values (computed in analyzeDashboard, published in LoggablePeriodic)
216 units::degree_t cachedGyroPitch{0_deg};
217 units::degree_t cachedGyroYaw{0_deg};
218 units::degree_t cachedGyroRoll{0_deg};
219 wpi::array<frc::SwerveModuleState, MODULE_COUNT> cachedModuleStates{wpi::empty_array};
220 units::degrees_per_second_t cachedAngularVelocity{0_deg_per_s};
221
222 frc::Notifier odometryThread{[this] { updateOdometry(); }};
223
224 frc::PIDController xChoreoController{10.0, 0.0, 0.0};
225 frc::PIDController yChoreoController{10.0, 0.0, 0.0};
226 frc::PIDController headingChoreoController{7.5, 0.0, 0.0};
227
228 // SwerveSetpointGenerator setpointGenerator;
229 // SwerveSetpoint previousSetpoint;
230};
231
232} // namespace valor
Abstract class that all Valor subsystem's should implement.
Definition BaseSubsystem.h:52
T ReadLog(std::string_view field, const T &defaultValue={})
Read a value from NetworkTables for the given field.
Definition Loggable.h:343
Definition Swerve.h:44
void assignOutputs() override
Read subsystem state and send motor commands.
wpi::array< frc::SwerveModuleState, MODULE_COUNT > getModuleStates()
Retrieves the current state (speed and angle) of each swerve module.
wpi::array< frc::SwerveModuleState, MODULE_COUNT > calculateModuleStates(frc::ChassisSpeeds chassisSpeeds, bool isFOC)
Calculates the desired swerve module states based on chassis speeds.
frc::Pose2d getRawPose()
void driveRobotRelative(frc::ChassisSpeeds speeds)
Drives the robot relative to its own orientation. This method calculates swerve module states based o...
void drive(frc::ChassisSpeeds _chassisSpeeds, bool isFOC)
Drives the robot using specified chassis speeds. This is the primary drive method that calculates and...
void analyzeDashboard() override
Synchronize dashboard data (both read and write)
void resetOdometry(frc::Pose2d pose)
void reset() override
Reset all subsystem state.
void LoggablePeriodic() override
Periodic callback for logging updates.
void updateAngularAcceleration()
Updates the calculated angular acceleration of the robot. This function uses the current and previous...
wpi::array< frc::SwerveModulePosition, MODULE_COUNT > getModulePositions()
Retrieves the current position of each swerve module.
void drive(units::velocity::meters_per_second_t vx_mps, units::velocity::meters_per_second_t vy_mps, units::angular_velocity::radians_per_second_t omega_radps, bool isFOC)
Drives the robot with given x, y, and rotational velocities. This function internally converts the in...
Definition LockedAccess.h:31