@@ -60,6 +60,7 @@ public class Drivetrain extends SubsystemBase {
6060 CANMappings .REAR_RIGHT_TURNING ,
6161 DrivetrainConfig .BACK_RIGHT_CHASSIS_ANGULAR_OFFSET );
6262
63+ // creates PID controllers
6364 public final PIDController xController =
6465 new PIDController (
6566 OrbitConfig .ORBIT_DISTANCE_P , OrbitConfig .ORBIT_DISTANCE_I , OrbitConfig .ORBIT_DISTANCE_D );
@@ -70,31 +71,38 @@ public class Drivetrain extends SubsystemBase {
7071 new PIDController (
7172 OrbitConfig .ORBIT_ROTATION_P , OrbitConfig .ORBIT_ROTATION_I , OrbitConfig .ORBIT_ROTATION_D );
7273
74+ // Creates PhotonPoseEstimator that estimates the robot position from one camera
7375 private PhotonPoseEstimator vision =
7476 new PhotonPoseEstimator (
7577 VisionConfig .LAYOUT , VisionConfig .STRATEGY , VisionConfig .CAMERA_POSITION );
7678
79+ // Creates PhotonPoseEstimator that estimates the robot position from another camera
7780 private PhotonPoseEstimator driverVision =
7881 new PhotonPoseEstimator (
7982 VisionConfig .LAYOUT , VisionConfig .STRATEGY , VisionConfig .DRIVER_CAMERA_POSITION );
8083
84+ // creates Ultrasonic rangeFinder that measures distance (currently not in use)
8185 Ultrasonic rangeFinder = new Ultrasonic (1 , 2 );
8286
8387 // The gyro sensor
8488 @ NotLogged private final AHRS gyro = new AHRS (AHRS .NavXComType .kMXP_SPI );
8589
90+ // creates new field 2d to be shared with smart dashboard
8691 @ NotLogged private final Field2d field = new Field2d ();
8792
8893 // Slew rate filter variables for controlling lateral acceleration
8994 @ NotLogged
9095 private SlewRateLimiter magLimiter = new SlewRateLimiter (DrivetrainConfig .MAX_ACCELERATION );
9196
97+ // slew rate filter variables for controlling rotation acceleration
9298 @ NotLogged
9399 private SlewRateLimiter rotLimiter =
94100 new SlewRateLimiter (DrivetrainConfig .MAX_ROTATIONAL_ACCELERATION );
95101
102+ // creates new robot chassis speeds
96103 @ Logged private ChassisSpeeds desiredChassisSpeeds = new ChassisSpeeds ();
97104
105+ // creates new swerve drive pose estimator
98106 @ NotLogged
99107 public SwerveDrivePoseEstimator poseEstimator =
100108 new SwerveDrivePoseEstimator (
@@ -109,8 +117,11 @@ public class Drivetrain extends SubsystemBase {
109117 new Pose2d (0 , 0 , getHeading ()));
110118
111119 public Drivetrain () {
120+ // sends field data to smart dashboard
112121 SmartDashboard .putData (field );
122+ // makes sure that rotController automatically maps shortest route to setpoint
113123 rotController .enableContinuousInput (-Math .PI , Math .PI );
124+ // sets xController, yController, and rotController position and velocity tolerance
114125 xController .setTolerance (
115126 DrivetrainConfig .DISTANCE_POSITION_TOLERANCE , DrivetrainConfig .DISTANCE_VELOCITY_TOLERANCE );
116127 yController .setTolerance (
@@ -119,6 +130,7 @@ public Drivetrain() {
119130 DrivetrainConfig .ROTATION_POSITION_TOLERANCE , DrivetrainConfig .ROTATION_VELOCITY_TOLERANCE );
120131 }
121132
133+ // Logs desired swerve module states
122134 @ Logged (name = "desiredStates" )
123135 public SwerveModuleState [] getDesiredStates () {
124136 return new SwerveModuleState [] {
@@ -129,6 +141,7 @@ public SwerveModuleState[] getDesiredStates() {
129141 };
130142 }
131143
144+ // Logs actual swerve module states
132145 @ Logged (name = "actualStates" )
133146 public SwerveModuleState [] getActualStates () {
134147 return new SwerveModuleState [] {
@@ -139,6 +152,7 @@ public SwerveModuleState[] getActualStates() {
139152 };
140153 }
141154
155+ // logs actual chassis speeds
142156 @ Logged (name = "actualChassisSpeeds" )
143157 public ChassisSpeeds getChassisSpeeds () {
144158 return DrivetrainConfig .DRIVE_KINEMATICS .toChassisSpeeds (
@@ -150,25 +164,31 @@ public ChassisSpeeds getChassisSpeeds() {
150164 });
151165 }
152166
167+ // sets desired chassis speeds given a speed and gives swerveModuleStates new module states
153168 public void setChassisSpeeds (ChassisSpeeds speeds ) {
154169 desiredChassisSpeeds = speeds ;
155170 var swerveModuleStates = DrivetrainConfig .DRIVE_KINEMATICS .toSwerveModuleStates (speeds );
156171
172+ // renormalizes wheel speeds if any individual speed is above the specified maximum given the
173+ // current swerve module states and the absolute maximum speed the swerve modules can reach
157174 SwerveDriveKinematics .desaturateWheelSpeeds (
158175 swerveModuleStates , DrivetrainConfig .MAX_SPEED_METERS_PER_SECOND );
159176
177+ // sets the desired states of the swerve modules
160178 this .frontLeft .setDesiredState (swerveModuleStates [0 ]);
161179 this .frontRight .setDesiredState (swerveModuleStates [1 ]);
162180 this .rearLeft .setDesiredState (swerveModuleStates [2 ]);
163181 this .rearRight .setDesiredState (swerveModuleStates [3 ]);
164182 }
165183
184+ // the range measured from the rangefinder in millimeters
166185 public double getDistance () {
167186 return rangeFinder .getRangeMM ();
168187 }
169188
170189 @ Override
171190 public void periodic () {
191+ // has ultrasonic sensor send ping and get distance measurement periodically
172192 rangeFinder .ping ();
173193 // Update the odometry in the periodic block
174194
@@ -182,11 +202,22 @@ public void periodic() {
182202 this .rearRight .getPosition ()
183203 }));
184204
205+ // Updates the stored reference pose for use when using the CLOSEST_TO_REFERENCE_POSE_STRATEGY
206+ // (not in use)
185207 vision .setReferencePose (this .poseEstimator .getEstimatedPosition ());
186208 driverVision .setReferencePose (this .poseEstimator .getEstimatedPosition ());
187209
210+ // Puts the pose data from one camera into a list
188211 List <PhotonPipelineResult > results = Vision .camera .getAllUnreadResults ();
189212
213+ // If there is pose data from the cameras, get the latest estimated pose and update the 'vision'
214+ // photon pose estimator
215+ // If there is no multi tag result and the distance from the camera to the target is greater
216+ // than
217+ // 4 meters, return
218+ // Otherwise, add the latest vision pose estimate to a filter with the odometry pose estimate
219+ // and set
220+ // the guessed pose from that to the current pose
190221 if (!results .isEmpty ()) {
191222 PhotonPipelineResult result = results .get (results .size () - 1 );
192223 vision
@@ -249,6 +280,7 @@ public Command followLine(
249280 this );
250281 }
251282
283+ // Initializes and logs targetAngle and lastDistance
252284 @ Logged public Rotation2d targetAngle ;
253285 @ Logged public double lastDistance ;
254286
0 commit comments