Skip to content

Commit

Permalink
Merge branch 'main' into autoposition
Browse files Browse the repository at this point in the history
  • Loading branch information
liulinden committed Feb 28, 2024
2 parents 3ab70d3 + 44d9f6b commit 7e1700f
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 424 deletions.
47 changes: 30 additions & 17 deletions shuffleboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,6 @@
"Colors/Color when false": "#8B0000FF"
}
},
"1,0": {
"size": [
2,
2
],
"content": {
"_type": "Gyro",
"_source0": "network_table:///SmartDashboard/ Degrees",
"_title": "Heading Degrees",
"_glyph": 139,
"_showGlyph": true,
"Visuals/Major tick spacing": 45.0,
"Visuals/Starting angle": 180.0,
"Visuals/Show tick mark ring": true,
"Visuals/Counter clockwise": false
}
},
"9,5": {
"size": [
1,
Expand Down Expand Up @@ -278,6 +261,23 @@
"Colors/Color when true": "#7CFC00FF",
"Colors/Color when false": "#8B0000FF"
}
},
"1,0": {
"size": [
2,
2
],
"content": {
"_type": "Gyro",
"_source0": "network_table:///SmartDashboard/Heading Degrees",
"_title": "Heading Degrees",
"_glyph": 148,
"_showGlyph": false,
"Visuals/Major tick spacing": 45.0,
"Visuals/Starting angle": 180.0,
"Visuals/Show tick mark ring": true,
"Visuals/Counter clockwise": false
}
}
}
}
Expand All @@ -294,6 +294,19 @@
"titleType": 0,
"tiles": {}
}
},
{
"title": "Tab 3",
"autoPopulate": false,
"autoPopulatePrefix": "",
"widgetPane": {
"gridSize": 128.0,
"showGrid": true,
"hgap": 16.0,
"vgap": 16.0,
"titleType": 0,
"tiles": {}
}
}
],
"windowGeometry": {
Expand Down
3 changes: 0 additions & 3 deletions simgui.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"Connections": {
"open": true
},
"Server": {
"open": true
},
"visible": true
},
"NetworkTables View": {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static class DriverConstants {

public static final double DEAD_ZONE = 0.25;

public static final int NUMBER_OF_SPEED_OPTIONS = 2;

// Names of options for displaying
public static final String[] maxSpeedOptionsNames = { "Precise", "Normal", "Boost" };

Expand Down
41 changes: 21 additions & 20 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import frc.robot.Constants.SwerveModuleConstants;
import frc.robot.Constants.ArmConstants;
import frc.robot.commands.Autos;
import frc.robot.commands.AimAtTag;
import frc.robot.commands.ArmRotateTo;
import frc.robot.commands.ChassisRemoteControl;
import frc.robot.Constants.VisionConstants;
Expand All @@ -15,8 +16,6 @@
import frc.robot.subsystems.arm.DummyArm;
import frc.robot.subsystems.arm.RealArm;
import frc.robot.inputs.ChassisDriveInputs;
import frc.robot.inputs.OptionButtonInput;
import frc.robot.inputs.OptionButtonInput.ActivationMode;

import com.kauailabs.navx.frc.AHRS;

Expand Down Expand Up @@ -99,13 +98,7 @@ public class RobotContainer {
* The container for the robot. Contains subsystems, OI devices, and commands.
*/
public RobotContainer() {
autoChooser.setDefaultOption("Testing Auto", Autos.testingAuto(drivetrain));
autoChooser.addOption("Follow Tag", Autos.tagFollowAuto(drivetrain, vision, 1));
autoChooser.addOption("Rotate by 90", Autos.rotateBy90Auto(drivetrain));
autoChooser.addOption("Rotate to 90", Autos.rotateTo90Auto(drivetrain));
autoChooser.addOption("Rotate by -90", Autos.rotateByNegative90Auto(drivetrain));
autoChooser.addOption("Rotate to -90", Autos.rotateToNegative90Auto(drivetrain));
autoChooser.addOption("Rotate by 10", Autos.rotateBy10Auto(drivetrain));
autoChooser.addOption("Rotate by 90", Autos.rotateTestAuto(drivetrain, 90, false));
SmartDashboard.putData("Auto Chooser", autoChooser);

configureBindings();
Expand All @@ -128,7 +121,6 @@ public void setUpDriveController() {
drivetrain.removeDefaultCommand();

ChassisDriveInputs inputs;
OptionButtonInput preciseModeButton, boostModeButton, fieldRelativeButton;

if (genericHIDType.equals(GenericHID.HIDType.kHIDJoystick)) {
final CommandJoystick joystick = new CommandJoystick(genericHID.getPort());
Expand All @@ -139,18 +131,22 @@ public void setUpDriveController() {
joystick::getTwist, -1,
Constants.DriverConstants.DEAD_ZONE);

preciseModeButton = new OptionButtonInput(joystick, 2, ActivationMode.TOGGLE);
boostModeButton = new OptionButtonInput(joystick, 1, ActivationMode.HOLD);
fieldRelativeButton = new OptionButtonInput(joystick, 3, ActivationMode.TOGGLE);
joystick.button(1).onTrue(Commands.runOnce(inputs::increaseSpeedLevel));
// joystick.button(1).onFalse(Commands.runOnce(inputs::decreaseSpeedLevel));

joystick.button(2).onTrue(Commands.runOnce(inputs::decreaseSpeedLevel));
// joystick.button(2).onFalse(Commands.runOnce(inputs::increaseSpeedLevel));

joystick.button(3).onTrue(Commands.runOnce(inputs::toggleFieldRelative));

// This bypasses arm remote control, arm remote control is incompatible with
// autonomous commands
operatorJoystick.button(4).onTrue(armToIntake);
operatorJoystick.button(5).onTrue(armToAmp);
operatorJoystick.button(6).onTrue(armToSpeaker);

joystick.button(9).onTrue(Commands.run(drivetrain::brakeMode, drivetrain));
joystick.button(10).onTrue(Commands.run(drivetrain::toDefaultStates, drivetrain));
// joystick.button(9).onTrue(Commands.run(drivetrain::brakeMode, drivetrain));
// joystick.button(10).onTrue(Commands.run(drivetrain::toDefaultStates, drivetrain));
} else {
final CommandXboxController xbox = new CommandXboxController(genericHID.getPort());

Expand All @@ -160,13 +156,18 @@ public void setUpDriveController() {
xbox::getRightX, -1,
Constants.DriverConstants.DEAD_ZONE);

preciseModeButton = new OptionButtonInput(xbox::b, ActivationMode.TOGGLE);
boostModeButton = new OptionButtonInput(xbox::leftStick, ActivationMode.HOLD);
fieldRelativeButton = new OptionButtonInput(xbox::povUp, ActivationMode.TOGGLE);
// xbox.povDown().whileTrue(Commands.run(drivetrain::brakeMode, drivetrain));
// xbox.povLeft().whileTrue(Commands.run(drivetrain::toDefaultStates,
// drivetrain));

xbox.b().onTrue(Commands.runOnce(inputs::decreaseSpeedLevel));
xbox.povUp().onTrue(Commands.runOnce(inputs::increaseSpeedLevel));
xbox.button(3).onTrue(Commands.runOnce(inputs::toggleFieldRelative));

xbox.a().whileTrue(new AimAtTag(drivetrain, vision, 1, inputs));
}

drivetrain.setDefaultCommand(
new ChassisRemoteControl(drivetrain, inputs, preciseModeButton, boostModeButton, fieldRelativeButton));
drivetrain.setDefaultCommand(new ChassisRemoteControl(drivetrain, inputs));
}

/** Use this method to define your trigger->command mappings. */
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/frc/robot/commands/AimAtTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package frc.robot.commands;

import frc.robot.Constants.RobotMovementConstants;
import frc.robot.inputs.ChassisDriveInputs;
import frc.robot.subsystems.SwerveDrivetrain;
import frc.robot.subsystems.Vision;

import org.photonvision.targeting.PhotonTrackedTarget;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.Command;

/** Command to automatically aim at a tag, ends once facing the tag */
public class AimAtTag extends Command {
private final SwerveDrivetrain drivetrain;
private final ChassisDriveInputs chassisDriveInputs;

private final Vision vision;
private final Integer tagID; // Integer as opposed to int so it can be null for best tag

private final PIDController rotatePID;

/**
* Create a new AimAtTag command. Tries to constants aim at a tag while still allowing driver to control robot.
*
* @param drivetrain the drivetrain of the robot
* @param vision the vision subsystem of the robot
* @param tagID the numerical ID of the the tag to turn to, null for best tag
* @param chassisDriveControl collection of inputs for driving
*/
public AimAtTag(SwerveDrivetrain drivetrain, Vision vision, Integer tagID, ChassisDriveInputs chassisDriveInputs) {
this.drivetrain = drivetrain;

this.vision = vision;
this.tagID = tagID;
this.chassisDriveInputs = chassisDriveInputs;

rotatePID = new PIDController(
2,
0,
0);
rotatePID.enableContinuousInput(-1, 1);
rotatePID.setTolerance(Units.radiansToDegrees(RobotMovementConstants.ANGLE_TOLERANCE_RADIANS));
rotatePID.setSetpoint(0);

addRequirements(drivetrain);
}

/**
* Create a new AimAtTag command. Tries to aim at a tag.
*
* @param drivetrain the drivetrain of the robot
* @param vision the vision subsystem of the robot
* @param tagID the numerical ID of the the tag to turn to, null for best tag
*/
public AimAtTag(SwerveDrivetrain drivetrain, Vision vision, Integer tagID) {
this(drivetrain, vision, tagID, null);
}

@Override
public void initialize() {
drivetrain.toDefaultStates();
}

@Override
public void execute() {
PhotonTrackedTarget tag = (tagID == null) ? vision.getTag() : vision.getTag(tagID);

double tagYawRadians = 0;
if (tag != null) {
tagYawRadians = Units.degreesToRadians(tag.getYaw());
}

double xSpeed = 0;
double ySpeed = 0;
if (chassisDriveInputs != null) {
xSpeed = chassisDriveInputs.getX();
ySpeed = chassisDriveInputs.getY();
}
double rotateSpeed = rotatePID.calculate(tagYawRadians);

ChassisSpeeds desiredSpeeds = new ChassisSpeeds(xSpeed, ySpeed, rotateSpeed);

drivetrain.setDesiredState(desiredSpeeds, false, true);

drivetrain.updateSmartDashboard();
}

@Override
public boolean isFinished() {
return (chassisDriveInputs == null) && (rotatePID.atSetpoint());
}

@Override
public void end(boolean interrupted) {
drivetrain.stop();
}
}
72 changes: 0 additions & 72 deletions src/main/java/frc/robot/commands/ArmRemoteControl.java

This file was deleted.

28 changes: 2 additions & 26 deletions src/main/java/frc/robot/commands/Autos.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,9 @@
*/
public final class Autos {
/** Example static factory for an autonomous command. */
public static Command testingAuto(SwerveDrivetrain drivetrain) {
public static Command rotateTestAuto(SwerveDrivetrain drivetrain, double degrees, boolean fieldRelative) {
return Commands.sequence(
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(90), false));
}

public static Command rotateTo90Auto(SwerveDrivetrain drivetrain) {
return Commands.sequence(
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(90), true));
}

public static Command rotateBy90Auto(SwerveDrivetrain drivetrain) {
return Commands.sequence(
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(90), false));
}

public static Command rotateToNegative90Auto(SwerveDrivetrain drivetrain) {
return Commands.sequence(
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(-90), true));
}

public static Command rotateByNegative90Auto(SwerveDrivetrain drivetrain) {
return Commands.sequence(
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(-90), false));
}

public static Command rotateBy10Auto(SwerveDrivetrain drivetrain) {
return Commands.sequence(new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(10), true));
new AutoRotateTo(drivetrain, Rotation2d.fromDegrees(90), fieldRelative));
}

/** Auto-mode that attempts to follow an april tag. */
Expand Down
Loading

0 comments on commit 7e1700f

Please sign in to comment.