Valkyrie 2026
Loading...
Searching...
No Matches
SwerveModule.h
1#pragma once
2
3#include <numbers>
4
5#include <ctre/phoenix6/CANcoder.hpp>
6#include <frc/DutyCycleEncoder.h>
7#include <frc/Filesystem.h>
8#include <frc/geometry/Rotation2d.h>
9#include <frc/geometry/Translation2d.h>
10#include <frc/kinematics/SwerveModulePosition.h>
11#include <frc/kinematics/SwerveModuleState.h>
12#include <frc/simulation/DCMotorSim.h>
13#include <units/voltage.h>
14#include <valkyrie/Loggable.h>
15#include <valkyrie/controllers/BaseController.h>
16#include <valkyrie/drivetrain/swerve/Requests.h>
17
18namespace valor {
19namespace swerve {
20
29class SwerveModule : public valor::Loggable {
30 public:
31 struct Config {
32 std::unique_ptr<BaseController> azimuthMotor;
33 std::unique_ptr<BaseController> driveMotor;
34 frc::Translation2d location;
35
36 // Simulation constants
37 units::kilogram_square_meter_t driveInertia = 0.035_kg_sq_m;
38 units::kilogram_square_meter_t azimuthInertia = 0.01_kg_sq_m;
39 };
40
41 SwerveModule(Config config, units::meter_t wheelDiameter);
42
43 frc::SwerveModuleState GetState() const;
44
45 constexpr frc::SwerveModuleState GetDesiredState() const { return desiredState; }
46
47 frc::SwerveModulePosition GetPosition() const;
48
49 units::meters_per_second_t GetMaxSpeed() const;
50
51 frc::Translation2d GetLocation() const { return location; }
52
53 constexpr units::meter_t GetWheelDiameter() const { return wheelConversion * 1_tr / std::numbers::pi; }
54
55 void ApplyRequest(requests::ApplyState);
56
57 inline BaseController& GetDriveMotor() const { return *driveMotor; }
58
59 inline BaseController& GetAzimuthMotor() const { return *azimuthMotor; }
60
61 void Reset();
62
63 void LoggablePeriodic() override;
64
65 // Doesn't override anything, just for convention - called explicitly from
66 // Swerve
67 void SimulationPeriodic();
68
69 private:
70 std::unique_ptr<BaseController> azimuthMotor;
71 std::unique_ptr<BaseController> driveMotor;
72
73 decltype(1_m / 1_tr) wheelConversion;
74 frc::Translation2d location;
75
76 frc::sim::DCMotorSim driveSim;
77 frc::sim::DCMotorSim azimuthSim;
78
79 frc::SwerveModuleState desiredState;
80};
81
82} // namespace swerve
83} // namespace valor
Base helper for publishing and subscribing values to NetworkTables.
Definition Loggable.h:53
void LoggablePeriodic() override
Periodic callback for logging updates.
Definition SwerveModule.h:31