blob: c01df24569889a84bed7ba5ebc369bf65e1a1e5e [file] [log] [blame]
Maxwell Hendersonf8c96892023-06-28 19:55:59 -07001#ifndef FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_
2#define FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_
3
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07004#include "frc971/control_loops/swerve/swerve_drivetrain_can_position_static.h"
James Kuszmauld938d332024-05-15 20:47:19 -07005#include "frc971/control_loops/swerve/swerve_drivetrain_output_generated.h"
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07006#include "frc971/control_loops/swerve/swerve_drivetrain_position_static.h"
7#include "frc971/wpilib/encoder_and_potentiometer.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08008#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf8c96892023-06-28 19:55:59 -07009
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace frc971::wpilib::swerve {
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070011
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070012// Contains the objects for interacting with the hardware for a given swerve
13// module, assuming that the module uses two TalonFX-based motor controllers and
14// has a CTRE mag encoder on the rotation of the module.
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070015struct SwerveModule {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080016 SwerveModule(TalonFXParams rotation_params, TalonFXParams translation_params,
Maxwell Hendersonacf63682023-08-19 22:18:09 -070017 std::string canbus,
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070018 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
19 double stator_current_limit, double supply_current_limit)
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080020 : rotation(std::make_shared<TalonFX>(rotation_params, canbus, signals,
21 stator_current_limit,
22 supply_current_limit)),
23 translation(std::make_shared<TalonFX>(translation_params, canbus,
24 signals, stator_current_limit,
25 supply_current_limit)) {}
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070026
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070027 // Writes the requested torque currents from the module_output to the motors,
28 // setting the maximum voltage of the motor outputs to the requested value.
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070029 void WriteModule(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070030 const frc971::control_loops::swerve::SwerveModuleOutput *module_output,
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070031 double max_voltage) {
32 double rotation_current = 0.0;
33 double translation_current = 0.0;
34
35 if (module_output != nullptr) {
36 rotation_current = module_output->rotation_current();
37 translation_current = module_output->translation_current();
38 }
39
40 rotation->WriteCurrent(rotation_current, max_voltage);
41 translation->WriteCurrent(translation_current, max_voltage);
42 }
43
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070044 // Used during initialization to set the WPILib objects used by the mag
45 // encoder on the rotation joint.
46 void set_rotation_encoder(std::unique_ptr<frc::Encoder> encoder,
47 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
48 rotation_encoder.set_encoder(std::move(encoder));
49 rotation_encoder.set_absolute_pwm(std::move(absolute_pwm));
50 }
51
52 // Populates the Position message with the mag encoder values.
53 void PopulatePosition(
54 frc971::control_loops::swerve::SwerveModulePositionStatic *fbs) {
55 auto rotation_position = fbs->add_rotation_position();
56 rotation_position->set_encoder(rotation_encoder.ReadRelativeEncoder());
57 rotation_position->set_absolute_encoder(
58 rotation_encoder.ReadAbsoluteEncoder());
59 }
60
61 // Populates a CAN-position message with the CAN-based devices (currently,
62 // just the motors themselves).
63 void PopulateCanPosition(
64 frc971::control_loops::swerve::SwerveModuleCanPositionStatic
65 *can_position) {
66 // TODO(james): Source these numbers from the constants.json.
67 rotation->SerializePosition(can_position->add_rotation(), 1.0);
68 translation->SerializePosition(can_position->add_translation(), 1.0);
69 }
70
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080071 std::shared_ptr<TalonFX> rotation;
72 std::shared_ptr<TalonFX> translation;
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070073 frc971::wpilib::AbsoluteEncoder rotation_encoder;
74};
75
76// Represents all the modules in a swerve drivetrain.
77struct SwerveModules {
78 void PopulateFalconsVector(std::vector<std::shared_ptr<TalonFX>> *falcons) {
79 CHECK_NOTNULL(falcons)->push_back(front_left->rotation);
80 falcons->push_back(front_left->translation);
81
82 falcons->push_back(front_right->rotation);
83 falcons->push_back(front_right->translation);
84
85 falcons->push_back(back_left->rotation);
86 falcons->push_back(back_left->translation);
87
88 falcons->push_back(back_right->rotation);
89 falcons->push_back(back_right->translation);
90 }
91
92 std::shared_ptr<SwerveModule> front_left;
93 std::shared_ptr<SwerveModule> front_right;
94 std::shared_ptr<SwerveModule> back_left;
95 std::shared_ptr<SwerveModule> back_right;
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070096};
97
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080098} // namespace frc971::wpilib::swerve
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070099#endif // FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_