Skip to content

Commit

Permalink
Initial YAGSL based swerve implementation (#13)
Browse files Browse the repository at this point in the history
* Basic Swerve YAGSL implementation

---------

Co-authored-by: Alex <[email protected]>
  • Loading branch information
BrownGenius and CodeSalvageO authored Jan 25, 2024
1 parent 07e6709 commit 350645a
Show file tree
Hide file tree
Showing 20 changed files with 968 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/deploy/swervePracticeBot/controllerproperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"angleJoystickRadiusDeadband": 0.5,
"heading": {
"p": 0.4,
"i": 0,
"d": 0.01
}
}
30 changes: 30 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/backleft.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"location": {
"front": -9.75,
"left": 9.75
},
"absoluteEncoderOffset": -257.2,
"drive": {
"type": "sparkmax",
"id": 12,
"canbus": null
},
"inverted": {
"drive": false,
"angle": true
},
"angle": {
"type": "sparkmax",
"id": 16,
"canbus": null
},
"conversionFactor": {
"angle": 0,
"drive": 0
},
"encoder": {
"type": "cancoder",
"id": 20,
"canbus": null
}
}
30 changes: 30 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/backright.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"location": {
"front": -9.75,
"left": -9.75
},
"absoluteEncoderOffset": -293,
"drive": {
"type": "sparkmax",
"id": 13,
"canbus": null
},
"inverted": {
"drive": true,
"angle": true
},
"angle": {
"type": "sparkmax",
"id": 17,
"canbus": null
},
"conversionFactor": {
"angle": 0,
"drive": 0
},
"encoder": {
"type": "cancoder",
"id": 21,
"canbus": null
}
}
30 changes: 30 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/frontleft.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"location": {
"front": 9.75,
"left": 9.75
},
"absoluteEncoderOffset": -230,
"drive": {
"type": "sparkmax",
"id": 10,
"canbus": null
},
"inverted": {
"drive": false,
"angle": true
},
"angle": {
"type": "sparkmax",
"id": 14,
"canbus": null
},
"conversionFactor": {
"angle": 0,
"drive": 0
},
"encoder": {
"type": "cancoder",
"id": 18,
"canbus": null
}
}
30 changes: 30 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/frontright.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"location": {
"front": 9.75,
"left": -9.75
},
"absoluteEncoderOffset": -290,
"drive": {
"type": "sparkmax",
"id": 11,
"canbus": null
},
"inverted": {
"drive": false,
"angle": true
},
"angle": {
"type": "sparkmax",
"id": 15,
"canbus": null
},
"conversionFactor": {
"angle": 0,
"drive": 0
},
"encoder": {
"type": "cancoder",
"id": 19,
"canbus": null
}
}
16 changes: 16 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/physicalproperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"optimalVoltage": 12,
"wheelGripCoefficientOfFriction": 1.19,
"currentLimit": {
"drive": 40,
"angle": 20
},
"conversionFactor": {
"angle": 16.8,
"drive": 0.047286787200699704
},
"rampRate": {
"drive": 0.25,
"angle": 0.25
}
}
16 changes: 16 additions & 0 deletions src/main/deploy/swervePracticeBot/modules/pidfproperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"drive": {
"p": 0.0020645,
"i": 0,
"d": 0,
"f": 0,
"iz": 0
},
"angle": {
"p": 0.01,
"i": 0,
"d": 0,
"f": 0,
"iz": 0
}
}
14 changes: 14 additions & 0 deletions src/main/deploy/swervePracticeBot/swervedrive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"imu": {
"type": "navx_mxp",
"id": 0,
"canbus": null
},
"invertedIMU": true,
"modules": [
"frontleft.json",
"frontright.json",
"backleft.json",
"backright.json"
]
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void robotInit() {
} else {*/
if (isReal()) {
// Running on real robot
Logger.addDataReceiver(new WPILOGWriter("/media/sda1/")); // Log to a USB stick
// Logger.addDataReceiver(new WPILOGWriter("/media/sda1/")); // Log to a USB stick
Logger.addDataReceiver(new NT4Publisher()); // Publish data to NetworkTables
} else {
// Running in simulation
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

package frc.robot;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.commands.IntakeBaseCommand;
import frc.robot.commands.ShooterEnable;
import frc.robot.commands.drive.DriveCommand;
import frc.robot.subsystems.drive.DriveSwerveYAGSL;
import frc.robot.subsystems.intake.IntakeBase;
import frc.robot.subsystems.intake.IntakeIOSim;
import frc.robot.subsystems.intake.IntakeIOSparkMax;
Expand All @@ -21,6 +24,7 @@ public class RobotContainer {
public final CommandXboxController controller;
public final ShooterSubsystem shooter;
public final IntakeBase intake;
public final DriveSwerveYAGSL drive;

public RobotContainer() {
controller = new CommandXboxController(0);
Expand All @@ -40,6 +44,8 @@ public RobotContainer() {
intake = new IntakeBase(new IntakeIOSim());
}

drive = new DriveSwerveYAGSL();

configureBindings();
}

Expand All @@ -52,6 +58,19 @@ private void configureBindings() {
intake,
() -> controller.rightBumper().getAsBoolean(),
() -> controller.leftBumper().getAsBoolean()));

drive.setDefaultCommand(
new DriveCommand(
drive,
() -> MathUtil.applyDeadband(-controller.getLeftY(), 0.01),
() -> MathUtil.applyDeadband(-controller.getLeftX(), 0.01),
() -> MathUtil.applyDeadband(-controller.getRightX(), 0.01)));
// TODO: Move deadband to constants file

controller
.start()
.onTrue(
new InstantCommand(() -> drive.setFieldOrientedDrive(!drive.isFieldOrientedDrive())));
}

public Command getAutonomousCommand() {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/frc/robot/commands/drive/DriveCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package frc.robot.commands.drive;

import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.drive.DriveBase;
import java.util.function.DoubleSupplier;

public class DriveCommand extends Command {
DriveBase drive;
DoubleSupplier speedX;
DoubleSupplier speedY;
DoubleSupplier rot;

public DriveCommand(
DriveBase drive, DoubleSupplier speedX, DoubleSupplier speedY, DoubleSupplier rot) {
this.drive = drive;
this.speedX = speedX;
this.speedY = speedY;
this.rot = rot;

addRequirements(drive);
}

@Override
public void execute() {
ChassisSpeeds speeds =
new ChassisSpeeds(
speedX.getAsDouble() * drive.getMaxLinearSpeed(),
speedY.getAsDouble() * drive.getMaxLinearSpeed(),
rot.getAsDouble() * drive.getMaxAngularSpeed());

drive.runVelocity(speeds);
}
}
26 changes: 26 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/Drive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package frc.robot.subsystems.drive;

import edu.wpi.first.math.kinematics.ChassisSpeeds;

public interface Drive {
/**
* Sets the desired chassis speed
*
* @param velocity specifies the setpoint velocity for the chassis
*/
public void runVelocity(ChassisSpeeds velocity);

/**
* This returns the robot's max linear speed in meter/sec
*
* @return speed in meter/sec
*/
public double getMaxLinearSpeed();

/**
* This returns the robot's max angular speed in radian/sec
*
* @return speed in radian/sec
*/
public double getMaxAngularSpeed();
}
19 changes: 19 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/DriveBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package frc.robot.subsystems.drive;

import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class DriveBase extends SubsystemBase implements Drive {
@Override
public void runVelocity(ChassisSpeeds velocity) {}

@Override
public double getMaxLinearSpeed() {
return 0;
}

@Override
public double getMaxAngularSpeed() {
return 0;
}
}
52 changes: 52 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/DriveSwerveYAGSL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package frc.robot.subsystems.drive;

import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.Filesystem;
import java.io.File;
import swervelib.SwerveDrive;
import swervelib.parser.SwerveParser;

public class DriveSwerveYAGSL extends DriveBase {
private final double maximumSpeed =
Units.feetToMeters(4.5); // * TODO: Calculate actual max speed */
private final File swerveJsonDirectory =
new File(Filesystem.getDeployDirectory(), "swervePracticeBot");
private SwerveDrive swerveDrive;
private boolean fieldOrientedDrive = false;

public DriveSwerveYAGSL() {
try {
swerveDrive = new SwerveParser(swerveJsonDirectory).createSwerveDrive(maximumSpeed);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void runVelocity(ChassisSpeeds velocity) {
if (fieldOrientedDrive) {
swerveDrive.driveFieldOriented(velocity);
} else {
swerveDrive.drive(velocity);
}
}

@Override
public double getMaxLinearSpeed() {
return swerveDrive.getMaximumVelocity();
}

@Override
public double getMaxAngularSpeed() {
return swerveDrive.getMaximumAngularVelocity();
}

public void setFieldOrientedDrive(boolean enable) {
fieldOrientedDrive = enable;
}

public boolean isFieldOrientedDrive() {
return fieldOrientedDrive;
}
}
Loading

0 comments on commit 350645a

Please sign in to comment.