Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_ |
| 2 | #define FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_ |
| 3 | |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 4 | #include "frc971/control_loops/swerve/swerve_drivetrain_can_position_static.h" |
James Kuszmaul | d938d33 | 2024-05-15 20:47:19 -0700 | [diff] [blame] | 5 | #include "frc971/control_loops/swerve/swerve_drivetrain_output_generated.h" |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 6 | #include "frc971/control_loops/swerve/swerve_drivetrain_position_static.h" |
| 7 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 8 | #include "frc971/wpilib/talonfx.h" |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 10 | namespace frc971::wpilib::swerve { |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 11 | |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 12 | // 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 15 | struct SwerveModule { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 16 | SwerveModule(TalonFXParams rotation_params, TalonFXParams translation_params, |
Maxwell Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 17 | std::string canbus, |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 18 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals, |
| 19 | double stator_current_limit, double supply_current_limit) |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 20 | : 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 26 | |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 27 | // 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 29 | void WriteModule( |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 30 | const frc971::control_loops::swerve::SwerveModuleOutput *module_output, |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 31 | 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 Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 44 | // 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 Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 71 | std::shared_ptr<TalonFX> rotation; |
| 72 | std::shared_ptr<TalonFX> translation; |
Nikolai Sohmers | 3f2a507 | 2024-06-08 14:05:59 -0700 | [diff] [blame] | 73 | frc971::wpilib::AbsoluteEncoder rotation_encoder; |
| 74 | }; |
| 75 | |
| 76 | // Represents all the modules in a swerve drivetrain. |
| 77 | struct 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 98 | } // namespace frc971::wpilib::swerve |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 99 | #endif // FRC971_WPILIB_SWERVE_SWERVE_MODULE_H_ |