FRC Paradigm Shift #1259
Documentation for the 2021 Robot
SwerveModule.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <frc/controller/PIDController.h>
11 #include <frc/controller/ProfiledPIDController.h>
12 #include <frc/geometry/Rotation2d.h>
13 #include <frc/kinematics/SwerveModuleState.h>
14 #include <frc/trajectory/TrapezoidProfile.h>
15 #include <frc/SmartDashboard/SmartDashboard.h>
16 #include <frc/Timer.h>
17 #include <networktables/NetworkTableEntry.h>
18 #include <wpi/math>
19 
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wattributes"
22 
23 #include <rev\CANSparkMax.h>
24 
25 #pragma GCC diagnostic pop
26 
27 #include <ctre/phoenix/motorcontrol/can/TalonFX.h>
28 #include <ctre/phoenix/motorcontrol/StatorCurrentLimitConfiguration.h>
29 
30 #include <string>
31 
32 #include "Constants.h"
33 #include "common/Util.h"
34 #include "common/PIDLoaderFalcon.h"
35 #include "common/PIDLoaderNEO.h"
36 
37 // Uncomment this to prevent swerve modules from driving
38 //#define DISABLE_DRIVE
39 
40 using namespace rev;
41 using namespace ctre::phoenix::motorcontrol;
42 using namespace ctre::phoenix::motorcontrol::can;
43 using namespace ModuleConstants;
44 using namespace units;
45 using namespace std;
46 using namespace frc;
47 
48 using GetPulseWidthCallback = function<double (CANifier::PWMChannel)>;
49 
50 
52 {
53  using radians_per_second_squared_t = compound_unit<radians, inverse<squared<second>>>;
54 
55 public:
56  SwerveModule( int driveMotorChannel
57  , int turningMotorChannel
58  , GetPulseWidthCallback pulseWidthCallback
59  , CANifier::PWMChannel pwmChannel
60  , bool driveEncoderReversed
61  , double offSet
62  , const string& name);
63 
64  void Periodic();
65 
69  SwerveModuleState GetState();
70 
73  void SetDesiredState(SwerveModuleState &state);
75  void ResetEncoders();
77  void ResetRelativeToAbsolute();
78 
79 private:
81  double CalcAbsoluteAngle();
86  double MinTurnRads(double init, double final, bool& bOutputReverse);
89  meters_per_second_t CalcMetersPerSec();
93  double CalcTicksPer100Ms(meters_per_second_t speed);
94 
96  double m_offset;
98  string m_name;
99 
101  TalonFX m_driveMotor;
104  CANSparkMax m_turningMotor;
105  CANEncoder m_turnRelativeEncoder = m_turningMotor.GetAlternateEncoder(CANEncoder::AlternateEncoderType::kQuadrature,
107  CANPIDController m_turnPIDController = m_turningMotor.GetPIDController();
109 
114 
118  CANifier::PWMChannel m_pwmChannel;
119 
121  Timer m_timer;
122 };
PIDLoaderFalcon m_drivePIDLoader
PID param loader for the TalonFX.
Definition: SwerveModule.h:111
STL namespace.
Timer m_timer
Timer used to sync absolute and relative encoders on robot turn on.
Definition: SwerveModule.h:121
string m_name
String used to identify each pod, used for SmartDashboard prints.
Definition: SwerveModule.h:98
GetPulseWidthCallback m_pulseWidthCallback
Definition: SwerveModule.h:117
constexpr double kTurnEncoderCPR
Definition: Constants.h:130
CANifier::PWMChannel m_pwmChannel
Definition: SwerveModule.h:118
PIDLoaderNEO m_turnPIDLoader
PID param loader for the CANSparkMax.
Definition: SwerveModule.h:113
CANSparkMax m_turningMotor
Definition: SwerveModule.h:104
function< double(CANifier::PWMChannel)> GetPulseWidthCallback
Definition: SwerveModule.h:48
double m_offset
The offset, in pulse widths, for syncing the relative encoder to the absolute encoder.
Definition: SwerveModule.h:96
compound_unit< radians, inverse< squared< second >>> radians_per_second_squared_t
Definition: SwerveModule.h:53
TalonFX m_driveMotor
Falon 500 that drives the pod.
Definition: SwerveModule.h:101