librmb 1.0
Rambunction 4330 Utility Library
Loading...
Searching...
No Matches
SwerveDrive.h
1#pragma once
2
3#include <array>
4#include <initializer_list>
5#include <memory>
6#include <mutex>
7#include <string>
8
9#include <units/angle.h>
10#include <units/velocity.h>
11
12#include <frc/controller/HolonomicDriveController.h>
13#include <frc/estimator/SwerveDrivePoseEstimator.h>
14#include <frc/geometry/Rotation2d.h>
15#include <frc/interfaces/Gyro.h>
16#include <frc/kinematics/ChassisSpeeds.h>
17#include <frc/kinematics/SwerveDriveKinematics.h>
18#include <frc/kinematics/SwerveModulePosition.h>
19#include <frc/kinematics/SwerveModuleState.h>
20#include <frc/trajectory/Trajectory.h>
21
22#include <pathplanner/lib/path/PathPlannerTrajectory.h>
23
24#include "frc/geometry/Pose2d.h"
25#include "frc/geometry/Translation2d.h"
26#include "frc2/command/Commands.h"
27#include "networktables/DoubleArrayTopic.h"
28#include "pathplanner/lib/commands/FollowPathHolonomic.h"
29#include "pathplanner/lib/path/PathConstraints.h"
30#include "pathplanner/lib/path/PathPlannerPath.h"
31#include "rmb/drive/BaseDrive.h"
32#include "rmb/drive/SwerveModule.h"
33#include "units/angular_velocity.h"
34
35#include <frc2/command/Command.h>
36#include <frc2/command/CommandPtr.h>
37
38#include "networktables/DoubleTopic.h"
39#include "units/time.h"
40
41#include <rmb/sensors/gyro.h>
42#include <vector>
43
44namespace rmb {
45
54template <size_t NumModules> class SwerveDrive : public BaseDrive {
55public:
56 SwerveDrive(const SwerveDrive &) = delete;
57 SwerveDrive(SwerveDrive &&) = delete;
58
73 SwerveDrive(std::array<SwerveModule, NumModules> modules,
74 std::shared_ptr<const rmb::Gyro> gyro,
75 frc::HolonomicDriveController holonomicController,
76 std::string visionTable,
77 units::meters_per_second_t maxModuleSpeed,
78 const frc::Pose2d &initialPose = frc::Pose2d());
79
91 SwerveDrive(std::array<SwerveModule, NumModules> modules,
92 std::shared_ptr<const rmb::Gyro> gyro,
93 frc::HolonomicDriveController holonomicController,
94 units::meters_per_second_t maxModuleSpeed,
95 const frc::Pose2d &initialPose = frc::Pose2d());
96
97 virtual ~SwerveDrive() = default;
98
99 void driveCartesian(double xSpeed, double ySpeed, double zRotation,
100 bool fieldOriented);
101
102 void drivePolar(double speed, const frc::Rotation2d &angle, double zRotation,
103 bool fieldOriented);
104
105 void driveModulePowers(std::array<SwerveModulePower, NumModules> powers);
106
107 void driveModuleStates(std::array<frc::SwerveModuleState, NumModules> states);
108
109 std::array<frc::SwerveModuleState, NumModules> getModuleStates() const;
110
111 std::array<frc::SwerveModulePosition, NumModules> getModulePositions() const;
112
113 const std::array<rmb::SwerveModule, NumModules> &getModules() const {
114 return modules;
115 }
116
122 void driveChassisSpeeds(frc::ChassisSpeeds chassisSpeeds) override;
123
127 frc::ChassisSpeeds getChassisSpeeds() const override;
128
129 //------------------
130 // Odometry Methods
131 //------------------
132
136 frc::Pose2d getPose() const override;
137
138 std::array<frc::SwerveModuleState, NumModules> getTargetModuleStates() const;
139
149 frc::Pose2d updatePose() override;
150
156 void resetPose(const frc::Pose2d &pose = frc::Pose2d()) override;
157
175 void addVisionMeasurments(const frc::Pose2d &poseEstimate,
176 units::second_t time) override;
177
187 void setVisionSTDevs(wpi::array<double, 3> standardDevs) override;
188
189 //----------------------
190 // Trajectory Following
191 //----------------------
192
198 bool isHolonomic() const override { return true; }
199
200 // /**
201 // * Generates a command to follow WPILib Trajectory.
202 // *
203 // * @param trajectory The trajectory to follow.
204 // * @param driveRequirments The subsystems required for driving the robot
205 // * (ie. the one that contains this class)
206 // *
207 // * @return The command to follow a trajectory.
208 // */
209 frc2::CommandPtr followWPILibTrajectory(
210 frc::Trajectory trajectory,
211 std::initializer_list<frc2::Subsystem *> driveRequirements) override;
212
213 // /**
214 // * Generates a command to follow PathPlanner Trajectory.
215 // *
216 // * @param trajectory The trajectory to follow.
217 // * @param driveRequirments The subsystems required for driving the robot
218 // * (ie. the one that contains this class)
219 // *
220 // * @return The command to follow a trajectory.
221 // */
222 frc2::CommandPtr followPPPath(
223 std::shared_ptr<pathplanner::PathPlannerPath> path,
224 std::initializer_list<frc2::Subsystem *> driveRequirements) override;
225
226 frc2::CommandPtr FollowGeneratedPPPath(
227 frc::Pose2d targetPose, pathplanner::PathConstraints constraints,
228 std::initializer_list<frc2::Subsystem *> driveRequirements);
229
230 void updateNTDebugInfo(bool openLoopVelocity = false);
231
232 void stop();
233
234private:
235 void recomputeOpenloopInverseKinematicsMatrix();
236
237 //-----------------
238 // Network Tables Debugging
239 //-----------------
240
241 // std::array<nt::DoublePublisher, NumModules> ntVelocityErrorTopics;
242 // std::array<nt::DoublePublisher, NumModules> ntPositionErrorTopics;
243 // std::array<nt::DoublePublisher, NumModules> ntPositionTopics;
244 // std::array<nt::DoublePublisher, NumModules> ntVelocityTopics;
245
246 nt::DoubleArrayPublisher ntPositionTopics;
247 nt::DoubleArrayPublisher ntVelocityTopics;
248
249 nt::DoubleArrayPublisher ntPositionErrorTopics;
250 nt::DoubleArrayPublisher ntVelocityErrorTopics;
251
252 nt::DoubleArrayPublisher ntTargetPositionTopics;
253 nt::DoubleArrayPublisher ntTargetVelocityTopics;
254
255 //-----------------
256 // Drive Variables
257 //-----------------
258
262 std::array<SwerveModule, NumModules> modules;
263
267 std::shared_ptr<const rmb::Gyro> gyro;
268
272 frc::SwerveDriveKinematics<NumModules> kinematics;
273
278 Eigen::Matrix<float, 2 * NumModules, 3> openLoopInverseKinematics;
279
283 frc::HolonomicDriveController holonomicController;
284
285 //-------------------
286 // Odometry Variables
287 //-------------------
288
292 frc::SwerveDrivePoseEstimator<NumModules> poseEstimator;
293
297 mutable std::mutex visionThreadMutex;
298
299 units::meters_per_second_t maxModuleSpeed;
300
301 units::meter_t largestModuleDistance = 1.0_m;
302};
303} // namespace rmb
304
305#include "SwerveDrive.inl"
Definition BaseDrive.h:32
Definition SwerveDrive.h:54
void driveCartesian(double xSpeed, double ySpeed, double zRotation, bool fieldOriented)
Definition SwerveDrive.inl:141
void driveChassisSpeeds(frc::ChassisSpeeds chassisSpeeds) override
Definition SwerveDrive.inl:238
frc::Pose2d updatePose() override
Definition SwerveDrive.inl:257
frc::Pose2d getPose() const override
Definition SwerveDrive.inl:252
frc2::CommandPtr followPPPath(std::shared_ptr< pathplanner::PathPlannerPath > path, std::initializer_list< frc2::Subsystem * > driveRequirements) override
Definition SwerveDrive.inl:373
bool isHolonomic() const override
Definition SwerveDrive.h:198
void resetPose(const frc::Pose2d &pose=frc::Pose2d()) override
Definition SwerveDrive.inl:338
void setVisionSTDevs(wpi::array< double, 3 > standardDevs) override
Definition SwerveDrive.inl:351
void addVisionMeasurments(const frc::Pose2d &poseEstimate, units::second_t time) override
Definition SwerveDrive.inl:344
frc2::CommandPtr followWPILibTrajectory(frc::Trajectory trajectory, std::initializer_list< frc2::Subsystem * > driveRequirements) override
Definition SwerveDrive.inl:358
frc::ChassisSpeeds getChassisSpeeds() const override
Definition SwerveDrive.inl:246
the master namespace of librmb
Definition LogitechGamepad.h:9