-
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.
Merge pull request #6 from MOERobotics/lehigh
Lehigh
- Loading branch information
Showing
26 changed files
with
1,554 additions
and
338 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
80 changes: 80 additions & 0 deletions
80
src/main/java/frc/robot/commands/AutoDriveToNoteCommand.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,80 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.geometry.Transform2d; | ||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.math.util.Units; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.SwerveDrive; | ||
import frc.robot.vision.Vision; | ||
|
||
|
||
public class AutoDriveToNoteCommand extends Command { | ||
|
||
private final Vision vision; | ||
private final SwerveDrive subsystem; | ||
|
||
private Translation2d target = null; | ||
private int idleLoopCount = 0; | ||
private double speed; | ||
private double speedMultiplier; | ||
public AutoDriveToNoteCommand(SwerveDrive subsystem, Vision vision, double SpeedMultiplier){ | ||
this.vision = vision; | ||
this.subsystem = subsystem; | ||
this.speedMultiplier=SpeedMultiplier; | ||
addRequirements(subsystem); | ||
} | ||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() { | ||
this.target = null; | ||
idleLoopCount = 0; | ||
} | ||
|
||
private void updateTarget() { | ||
var robotPose = subsystem.getEstimatedPose(); | ||
var detectionMaxThreshold = Double.POSITIVE_INFINITY; | ||
if (target != null) { | ||
detectionMaxThreshold = robotPose.getTranslation().getDistance(target) + Units.feetToMeters(2); | ||
} | ||
|
||
var detections = vision.detections(); | ||
for (var detection : detections){ | ||
var distance = detection.getNorm(); | ||
if (distance < detectionMaxThreshold){ | ||
var detectionFieldCoord = robotPose.transformBy(new Transform2d(detection, new Rotation2d())).getTranslation(); | ||
target = detectionFieldCoord; | ||
detectionMaxThreshold = distance; | ||
} | ||
} | ||
} | ||
|
||
// Called every time the scheduler runs while the command is scheduled. | ||
@Override | ||
public void execute() { | ||
updateTarget(); | ||
if (target == null){ | ||
idleLoopCount += 1; | ||
} | ||
// Drive towards target | ||
var robotPose = subsystem.getEstimatedPose(); | ||
var delta = target.minus(robotPose.getTranslation()); | ||
speed = speedMultiplier*target.getDistance(robotPose.getTranslation())/Units.inchesToMeters(12); | ||
var unitDelta = delta.div(delta.getNorm()).times(speed); | ||
var robotAngle = unitDelta.getAngle(); | ||
subsystem.setDesiredYaw(robotAngle.getDegrees()); | ||
subsystem.driveAtSpeed(unitDelta.getX(), unitDelta.getY(), 0, true); | ||
} | ||
|
||
// Called once the command ends or is interrupted. | ||
@Override | ||
public void end(boolean interrupted) { | ||
subsystem.stopModules(); | ||
} | ||
|
||
// Returns true when the command should end. | ||
@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
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
129 changes: 129 additions & 0 deletions
129
src/main/java/frc/robot/commands/DriveToNoteCollectCommand.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,129 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.geometry.Transform2d; | ||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.math.util.Units; | ||
import edu.wpi.first.wpilibj.Joystick; | ||
import edu.wpi.first.wpilibj.Timer; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.CollectorSubsystem; | ||
import frc.robot.subsystems.SwerveDrive; | ||
import frc.robot.vision.Vision; | ||
|
||
import java.util.function.DoubleConsumer; | ||
import java.util.function.DoubleSupplier; | ||
import java.util.function.Supplier; | ||
|
||
public class DriveToNoteCollectCommand extends Command { | ||
|
||
private final Vision vision; | ||
private final SwerveDrive subsystem; | ||
private final DoubleSupplier speedSupplier; | ||
private final DoubleConsumer rumbleCallback; | ||
private boolean shouldStop = false; | ||
private Timer timer; | ||
|
||
private final CollectorSubsystem collector; | ||
|
||
private Translation2d target = null; | ||
private int idleLoopCount = 0; | ||
|
||
// public DriveToNoteCollectCommand(SwerveDrive subsystem, Vision vision, DoubleSupplier speedSupplier) { | ||
// this(subsystem, vision, speedSupplier, null,); | ||
// } | ||
public DriveToNoteCollectCommand(SwerveDrive subsystem, Vision vision, DoubleSupplier speedSupplier, DoubleConsumer rumbleCallback, CollectorSubsystem collector){ | ||
this.speedSupplier = speedSupplier; | ||
this.vision = vision; | ||
this.subsystem = subsystem; | ||
this.rumbleCallback = rumbleCallback; | ||
this.collector = collector; | ||
shouldStop=false; | ||
addRequirements(subsystem,collector); | ||
} | ||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() { | ||
this.target = null; | ||
idleLoopCount = 0; | ||
shouldStop=false; | ||
} | ||
|
||
private void updateTarget() { | ||
var robotPose = subsystem.getEstimatedPose(); | ||
var detectionMaxThreshold = Double.POSITIVE_INFINITY; | ||
if (target != null) { | ||
detectionMaxThreshold = robotPose.getTranslation().getDistance(target) + Units.feetToMeters(2); | ||
} | ||
|
||
var detections = vision.detections(); | ||
for (var detection : detections){ | ||
var distance = detection.getNorm(); | ||
if (distance < detectionMaxThreshold){ | ||
var detectionFieldCoord = robotPose.transformBy(new Transform2d(detection, new Rotation2d())).getTranslation(); | ||
target = detectionFieldCoord; | ||
detectionMaxThreshold = distance; | ||
} | ||
} | ||
} | ||
|
||
// Called every time the scheduler runs while the command is scheduled. | ||
@Override | ||
public void execute() { | ||
double finalSpeed = 0; | ||
updateTarget(); | ||
if (target == null){ | ||
idleLoopCount += 1; | ||
|
||
if (idleLoopCount >= 5 && rumbleCallback != null){ | ||
rumbleCallback.accept(1); | ||
} | ||
return; | ||
} | ||
// Drive towards target | ||
var robotPose = subsystem.getEstimatedPose(); | ||
var delta = target.minus(robotPose.getTranslation()); | ||
var unitDelta = delta.div(delta.getNorm()).times(speedSupplier.getAsDouble()); | ||
|
||
var robotAngle = unitDelta.getAngle(); | ||
subsystem.setDesiredYaw(robotAngle.getDegrees()); | ||
|
||
subsystem.driveAtSpeed(unitDelta.getX(), unitDelta.getY(), 0, true); | ||
|
||
if (delta.getNorm()<=Units.feetToMeters(1)&& !shouldStop && !collector.isCollected()) { //within 1 ft, collector in, no note | ||
finalSpeed = 0.4; | ||
timer.restart(); | ||
} | ||
/* if (collector.isCollected() && timer.get() <= .1){ | ||
shouldStop = true; | ||
finalSpeed = -0.4; | ||
}*/ | ||
collector.updateCollectorSpeed(finalSpeed); | ||
SmartDashboard.putBoolean("started collector", collector.getCollectorState()); | ||
SmartDashboard.putNumber("collector speed", finalSpeed); | ||
|
||
SmartDashboard.putBoolean("Should Stop", shouldStop); | ||
} | ||
|
||
// Called once the command ends or is interrupted. | ||
@Override | ||
public void end(boolean interrupted) { | ||
if (rumbleCallback != null){ | ||
rumbleCallback.accept(0); | ||
} | ||
shouldStop=false; | ||
collector.stopCollector(); | ||
subsystem.stopModules(); | ||
} | ||
|
||
// Returns true when the command should end. | ||
@Override | ||
public boolean isFinished() { | ||
if(shouldStop && timer.get() >= .1){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.