Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
create and use SwerveConfig for robot-specific swerve parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dejabot committed Jan 21, 2024
1 parent 3bd7e41 commit dd0ff32
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/team766/robot/common/SwerveConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.team766.robot.common;

import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;

/**
* Configuration for the Swerve Drive motors on this robot.
*/
// TODO: switch from Vector2D to WPILib's Translation2D.
public record SwerveConfig(
String canBus,
Vector2D frontLeftLocation,
Vector2D frontRightLocation,
Vector2D backLeftLocation,
Vector2D backRightLocation,
double driveMotorCurentLimit,
double steerMotorCurrentLimit) {}
56 changes: 25 additions & 31 deletions src/main/java/com/team766/robot/common/mechanisms/Drive.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.team766.odometry.Odometry;
import com.team766.odometry.Point;
import com.team766.odometry.PointDir;
import com.team766.robot.common.SwerveConfig;
import com.team766.robot.gatorade.constants.OdometryInputConstants;
import com.team766.robot.gatorade.constants.SwerveDriveConstants;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;

public class Drive extends Mechanism {

private final SwerveConfig config;

// SwerveModules
private final SwerveModule swerveFR;
private final SwerveModule swerveFL;
Expand All @@ -32,9 +34,11 @@ public class Drive extends Mechanism {
// variable representing current position
private static PointDir currentPosition;

public Drive() {
public Drive(SwerveConfig config) {
loggerCategory = Category.DRIVE;

this.config = config;

// create the drive motors
MotorController driveFR = RobotProvider.instance.getMotor(DRIVE_DRIVE_FRONT_RIGHT);
MotorController driveFL = RobotProvider.instance.getMotor(DRIVE_DRIVE_FRONT_LEFT);
Expand All @@ -48,10 +52,10 @@ public Drive() {
MotorController steerBL = RobotProvider.instance.getMotor(DRIVE_STEER_BACK_LEFT);

// create the encoders
CANcoder encoderFR = new CANcoder(2, SwerveDriveConstants.SWERVE_CANBUS);
CANcoder encoderFL = new CANcoder(4, SwerveDriveConstants.SWERVE_CANBUS);
CANcoder encoderBR = new CANcoder(3, SwerveDriveConstants.SWERVE_CANBUS);
CANcoder encoderBL = new CANcoder(1, SwerveDriveConstants.SWERVE_CANBUS);
CANcoder encoderFR = new CANcoder(2, config.canBus());
CANcoder encoderFL = new CANcoder(4, config.canBus());
CANcoder encoderBR = new CANcoder(3, config.canBus());
CANcoder encoderBL = new CANcoder(1, config.canBus());

// initialize the swerve modules
swerveFR = new SwerveModule("FR", driveFR, steerFR, encoderFR);
Expand Down Expand Up @@ -106,29 +110,12 @@ public void controlRobotOriented(double x, double y, double turn) {
// Finds the vectors for turning and for translation of each module, and adds them
// Applies this for each module
swerveFL.driveAndSteer(
new Vector2D(x, y)
.add(
turn,
new Vector2D(SwerveDriveConstants.FL_Y, SwerveDriveConstants.FL_X)
.normalize()));
new Vector2D(x, y).add(turn, config.frontLeftLocation().normalize()));
swerveFR.driveAndSteer(
new Vector2D(x, y)
.add(
turn,
new Vector2D(SwerveDriveConstants.FR_Y, SwerveDriveConstants.FR_X)
.normalize()));
new Vector2D(x, y).add(turn, config.frontRightLocation().normalize()));
swerveBR.driveAndSteer(
new Vector2D(x, y)
.add(
turn,
new Vector2D(SwerveDriveConstants.BR_Y, SwerveDriveConstants.BR_X)
.normalize()));
swerveBL.driveAndSteer(
new Vector2D(x, y)
.add(
turn,
new Vector2D(SwerveDriveConstants.BL_Y, SwerveDriveConstants.BL_X)
.normalize()));
new Vector2D(x, y).add(turn, config.backRightLocation().normalize()));
swerveBL.driveAndSteer(new Vector2D(x, y).add(turn, config.backLeftLocation().normalize()));
}

/**
Expand Down Expand Up @@ -167,10 +154,17 @@ public void stopDrive() {
public void setCross() {
checkContextOwnership();

swerveFL.steer(new Vector2D(SwerveDriveConstants.FL_Y, -SwerveDriveConstants.FL_X));
swerveFR.steer(new Vector2D(SwerveDriveConstants.FR_Y, -SwerveDriveConstants.FR_X));
swerveBL.steer(new Vector2D(SwerveDriveConstants.BL_Y, -SwerveDriveConstants.BL_X));
swerveBR.steer(new Vector2D(SwerveDriveConstants.BR_Y, -SwerveDriveConstants.BR_X));
swerveFL.steer(
new Vector2D(
config.frontLeftLocation().getY(), -config.frontLeftLocation().getX()));
swerveFL.steer(
new Vector2D(
config.frontRightLocation().getY(), -config.frontRightLocation().getX()));
swerveFL.steer(
new Vector2D(config.backLeftLocation().getY(), -config.backLeftLocation().getX()));
swerveFL.steer(
new Vector2D(
config.backRightLocation().getY(), -config.backRightLocation().getX()));
}

public void resetGyro() {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/team766/robot/gatorade/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.team766.framework.AutonomousMode;
import com.team766.framework.Procedure;
import com.team766.hal.RobotConfigurator;
import com.team766.robot.common.SwerveConfig;
import com.team766.robot.common.mechanisms.Drive;
import com.team766.robot.gatorade.constants.SwerveDriveConstants;
import com.team766.robot.gatorade.mechanisms.*;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;

public class Robot implements RobotConfigurator {
// Declare mechanisms (as static fields) here
Expand All @@ -17,11 +20,20 @@ public class Robot implements RobotConfigurator {

@Override
public void initializeMechanisms() {
SwerveConfig config =
new SwerveConfig(
SwerveDriveConstants.SWERVE_CANBUS,
new Vector2D(SwerveDriveConstants.FL_X, SwerveDriveConstants.FL_Y),
new Vector2D(SwerveDriveConstants.FR_X, SwerveDriveConstants.FR_Y),
new Vector2D(SwerveDriveConstants.BL_X, SwerveDriveConstants.BL_Y),
new Vector2D(SwerveDriveConstants.BR_X, SwerveDriveConstants.BR_Y),
SwerveDriveConstants.DRIVE_MOTOR_CURRENT_LIMIT,
SwerveDriveConstants.STEER_MOTOR_CURRENT_LIMIT);
intake = new Intake();
wrist = new Wrist();
elevator = new Elevator();
shoulder = new Shoulder();
drive = new Drive();
drive = new Drive(config);
lights = new Lights();
}

Expand Down

0 comments on commit dd0ff32

Please sign in to comment.