-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
264 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.subsystems.Intake; | ||
import frc.robot.utils.UserAnalog; | ||
|
||
public class IntakeCommand extends CommandBase { | ||
|
||
private final double indexSpeed = 0.8; | ||
|
||
private final Intake intake; | ||
private final UserAnalog grabberSpeed; | ||
|
||
public IntakeCommand(Intake intake, UserAnalog grabberSpeed) { | ||
this.intake = intake; | ||
this.grabberSpeed = grabberSpeed; | ||
|
||
addRequirements(intake); | ||
intake.setDefaultCommand(this); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
|
||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (intake.getSwitch()) { | ||
intake.index(indexSpeed); | ||
} else { | ||
intake.index(0); | ||
} | ||
intake.intake(grabberSpeed.get()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,16 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.wpilibj.kinematics.DifferentialDriveKinematics; | ||
import edu.wpi.first.wpilibj.kinematics.DifferentialDriveWheelSpeeds; | ||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.Constants; | ||
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup; | ||
import frc.robot.commands.vision.PositionVisionCommand; | ||
import frc.robot.commands.vision.ShootVisionCommand; | ||
import frc.robot.subsystems.DriveTrain; | ||
import frc.robot.subsystems.Shooter; | ||
|
||
public class VisionCommand extends CommandBase { | ||
|
||
private final DriveTrain driveTrain; | ||
private final Shooter shooter; | ||
|
||
public VisionCommand(Shooter shooter, DriveTrain driveTrain) { | ||
this.driveTrain=driveTrain; | ||
this.shooter=shooter; | ||
|
||
} | ||
|
||
@Override | ||
public void initialize() { | ||
|
||
} | ||
|
||
@Override | ||
public void execute() { | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return false; | ||
} | ||
|
||
private void turnByAngle(double radians) { | ||
DifferentialDriveKinematics kinematics = new DifferentialDriveKinematics(Constants.K_TRACK_WIDTH_METERS); | ||
//kinematics works for speeds, but by inputing a position a position can also be yielded | ||
DifferentialDriveWheelSpeeds turnPositions = kinematics.toWheelSpeeds(new ChassisSpeeds(0, 0, radians)); | ||
driveTrain.positionDrive(driveTrain.metersToRotations(turnPositions.rightMetersPerSecond),driveTrain.metersToRotations(turnPositions.leftMetersPerSecond)); | ||
public class VisionCommand extends SequentialCommandGroup { | ||
public VisionCommand(DriveTrain driveTrain, Shooter shooter) { | ||
super(); | ||
PositionVisionCommand position = new PositionVisionCommand(shooter, driveTrain); | ||
ShootVisionCommand shoot = new ShootVisionCommand(shooter); | ||
addCommands(position, shoot); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/frc/robot/commands/vision/PositionVisionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package frc.robot.commands.vision; | ||
|
||
import edu.wpi.first.networktables.EntryListenerFlags; | ||
import edu.wpi.first.networktables.EntryNotification; | ||
import edu.wpi.first.networktables.NetworkTable; | ||
import edu.wpi.first.networktables.NetworkTableEntry; | ||
import edu.wpi.first.networktables.NetworkTableInstance; | ||
import edu.wpi.first.wpilibj.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.wpilibj.kinematics.DifferentialDriveKinematics; | ||
import edu.wpi.first.wpilibj.kinematics.DifferentialDriveWheelSpeeds; | ||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.Constants; | ||
import frc.robot.subsystems.DriveTrain; | ||
import frc.robot.subsystems.Shooter; | ||
|
||
public class PositionVisionCommand extends CommandBase { | ||
|
||
private final DriveTrain driveTrain; | ||
private final Shooter shooter; | ||
private final NetworkTableEntry angleEntry; | ||
private int angleListenerHandle; | ||
private final NetworkTableEntry distanceEntry; | ||
private int distanceListenerHandle; | ||
private boolean turning = false; | ||
private double distance; | ||
private double angle; | ||
|
||
public PositionVisionCommand(Shooter shooter, DriveTrain driveTrain) { | ||
this.driveTrain = driveTrain; | ||
this.shooter = shooter; | ||
|
||
NetworkTableInstance ntInst = NetworkTableInstance.getDefault(); | ||
NetworkTable table = ntInst.getTable("Vision"); | ||
angleEntry = table.getEntry("angle"); | ||
angleListenerHandle=angleEntry.addListener(this::listenAngle, EntryListenerFlags.kUpdate); | ||
distanceEntry = table.getEntry("distance"); | ||
distanceListenerHandle=distanceEntry.addListener(this::listenDistance, EntryListenerFlags.kUpdate); | ||
|
||
} | ||
|
||
@Override | ||
public void initialize() { | ||
angleListenerHandle=angleEntry.addListener(this::listenAngle, EntryListenerFlags.kUpdate); | ||
distanceListenerHandle=distanceEntry.addListener(this::listenDistance, EntryListenerFlags.kUpdate); } | ||
|
||
@Override | ||
public void execute() { | ||
|
||
} | ||
|
||
@Override | ||
public void end(boolean interupted){ | ||
angleEntry.removeListener(angleListenerHandle); | ||
distanceEntry.removeListener(distanceListenerHandle); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return !turning && angle<=0.01; | ||
} | ||
|
||
private void listenAngle(EntryNotification n){ | ||
angle=n.value.getDouble(); | ||
if(!turning && angle>0.01){ | ||
turnByAngle(angle); | ||
} | ||
} | ||
|
||
private void listenDistance(EntryNotification n){ | ||
distance = n.value.getDouble(); | ||
} | ||
|
||
private void turnByAngle(double radians) { | ||
DifferentialDriveKinematics kinematics = new DifferentialDriveKinematics(Constants.K_TRACK_WIDTH_METERS); | ||
// kinematics works for speeds, but by inputing a position a position can also | ||
// be yielded | ||
DifferentialDriveWheelSpeeds turnPositions = kinematics.toWheelSpeeds(new ChassisSpeeds(0, 0, radians)); | ||
driveTrain.positionDrive(driveTrain.metersToRotations(turnPositions.rightMetersPerSecond), | ||
driveTrain.metersToRotations(turnPositions.leftMetersPerSecond)); | ||
turning=true; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/frc/robot/commands/vision/ShootVisionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package frc.robot.commands.vision; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.subsystems.Shooter; | ||
|
||
public class ShootVisionCommand extends CommandBase { | ||
|
||
private final Shooter shooter; | ||
|
||
public ShootVisionCommand(Shooter shooter) { | ||
this.shooter = shooter; | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
|
||
} | ||
|
||
@Override | ||
public void execute(){ | ||
} | ||
|
||
@Override | ||
public void end(boolean isInterupted){ | ||
|
||
} | ||
|
||
@Override | ||
public boolean isFinished(){ | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package frc.robot.subsystems; | ||
|
||
import edu.wpi.first.wpilibj.DigitalInput; | ||
import edu.wpi.first.wpilibj.Spark; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import frc.robot.Constants; | ||
|
||
public class Intake extends SubsystemBase { | ||
|
||
private final Spark indexMotor; | ||
private final Spark intakeMotor; | ||
|
||
private final DigitalInput indexSwitch; | ||
|
||
public Intake() { | ||
indexMotor = new Spark(Constants.K_INTAKE_INDEXER_SPARK); | ||
intakeMotor = new Spark(Constants.K_INTAKE_GRABBER_SPARK); | ||
|
||
indexSwitch = new DigitalInput(Constants.K_INTAKE_INDEX_SWITCH); | ||
} | ||
|
||
/** | ||
* Indexes a ball once intaked. | ||
* | ||
* @param speed - double from 1 (in) to -1 (out) | ||
*/ | ||
public void index(double speed) { | ||
indexMotor.set(speed); | ||
} | ||
|
||
/** | ||
* Returns whether the indexing switch is pressed | ||
*/ | ||
public boolean getSwitch() { | ||
return indexSwitch.get(); | ||
} | ||
|
||
/** | ||
* runs the intake bar | ||
* | ||
* @param double speed - | ||
*/ | ||
public void intake(double speed) { | ||
intakeMotor.set(speed); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
package frc.robot.utils; | ||
|
||
public interface UserAnalog { | ||
|
||
public static UserAnalog fromDigital(UserDigital digital, double trueVal, double falseVal){ | ||
return ()->{ | ||
if(digital.get()){ | ||
return trueVal; | ||
}else{ | ||
return falseVal; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @return value - a [-1,1] value based on the input from the user or controller | ||
*/ | ||
public double get(); | ||
} | ||
|
Oops, something went wrong.