Code Setup
Last updated
Was this helpful?
Was this helpful?
import java.io.File;
import edu.wpi.first.wpilibj.Filesystem;
import swervelib.parser.SwerveParser;
import swervelib.SwerveDrive;
import edu.wpi.first.math.util.Units;
double maximumSpeed = Units.feetToMeters(4.5)
File swerveJsonDirectory = new File(Filesystem.getDeployDirectory(),"swerve");
SwerveDrive swerveDrive = new SwerveParser(directory).createSwerveDrive(maximumSpeed);
import swervelib.telemetry.SwerveDriveTelemetry;
import swervelib.telemetry.SwerveDriveTelemetry.TelemetryVerbosity;
SwerveDriveTelemetry.verbosity = TelemetryVerbosity.HIGH; /**
* Command to drive the robot using translative values and heading as a setpoint.
*
* @param translationX Translation in the X direction.
* @param translationY Translation in the Y direction.
* @param headingX Heading X to calculate angle of the joystick.
* @param headingY Heading Y to calculate angle of the joystick.
* @return Drive command.
*/
public Command driveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier headingX,
DoubleSupplier headingY)
{
return run(() -> {
Translation2d scaledInputs = SwerveMath.scaleTranslation(new Translation2d(translationX.getAsDouble(),
translationY.getAsDouble()), 0.8);
// Make the robot move
driveFieldOriented(swerveDrive.swerveController.getTargetSpeeds(scaledInputs.getX(), scaledInputs.getY(),
headingX.getAsDouble(),
headingY.getAsDouble(),
swerveDrive.getOdometryHeading().getRadians(),
swerveDrive.getMaximumVelocity()));
});
}
/**
* Command to drive the robot using translative values and heading as angular velocity.
*
* @param translationX Translation in the X direction.
* @param translationY Translation in the Y direction.
* @param angularRotationX Rotation of the robot to set
* @return Drive command.
*/
public Command driveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier angularRotationX)
{
return run(() -> {
// Make the robot move
swerveDrive.drive(new Translation2d(translationX.getAsDouble() * swerveDrive.getMaximumVelocity(),
translationY.getAsDouble() * swerveDrive.getMaximumVelocity()),
angularRotationX.getAsDouble() * swerveDrive.getMaximumAngularVelocity(),
true,
false);
});
}