Skip to content

Commit 0226e8e

Browse files
authored
Merge pull request #238 from strykeforce/rkalnins/vision
Rkalnins/iri
2 parents 9ceb902 + 2cbf266 commit 0226e8e

22 files changed

+242
-105
lines changed
10 Bytes
Binary file not shown.

src/main/java/frc/team2767/deepspace/Robot.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import edu.wpi.first.wpilibj.command.CommandGroup;
77
import edu.wpi.first.wpilibj.command.Scheduler;
88
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
9-
import frc.team2767.deepspace.command.approach.sandstorm.SandstormCommandGroup;
109
import frc.team2767.deepspace.command.sequences.pickup.SandstormHatchPickupCommandGroup;
1110
import frc.team2767.deepspace.control.AutonChooser;
1211
import frc.team2767.deepspace.control.Controls;
1312
import frc.team2767.deepspace.subsystem.*;
1413
import frc.team2767.deepspace.subsystem.safety.SafetySubsystem;
15-
import org.slf4j.Logger;
16-
import org.slf4j.LoggerFactory;
1714
import org.strykeforce.thirdcoast.telemetry.TelemetryController;
1815
import org.strykeforce.thirdcoast.telemetry.TelemetryService;
1916
import org.strykeforce.thirdcoast.trapper.Session;
@@ -31,20 +28,16 @@ public class Robot extends TimedRobot {
3128
public static ClimbSubsystem CLIMB;
3229
public static Controls CONTROLS;
3330
public static StartLevel startLevel = StartLevel.ONE;
34-
public static boolean isAuton;
3531
private static AutonChooser AUTON;
3632
private static boolean isEvent;
37-
private static CommandGroup sandstorm;
3833
private static CommandGroup noAutoSandstorm;
39-
private Logger logger;
4034

4135
public static boolean isEvent() {
4236
return isEvent;
4337
}
4438

4539
@Override
4640
public void robotInit() {
47-
isAuton = true;
4841
DigitalInput di = new DigitalInput(7);
4942
isEvent = di.get();
5043

@@ -53,7 +46,6 @@ public void robotInit() {
5346
System.out.println("Event flag removed - switching logging to log file");
5447
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "logback-event.xml");
5548
}
56-
logger = LoggerFactory.getLogger(this.getClass());
5749

5850
TELEMETRY = new TelemetryService(TelemetryController::new);
5951
DRIVE = new DriveSubsystem();
@@ -79,7 +71,6 @@ public void robotInit() {
7971
BISCUIT.zero();
8072
INTAKE.zero();
8173

82-
sandstorm = new SandstormCommandGroup();
8374
noAutoSandstorm = new SandstormHatchPickupCommandGroup();
8475

8576
SmartDashboard.putBoolean("Game/SandstormPickUp", false);
@@ -103,11 +94,7 @@ public void autonomousInit() {
10394
BISCUIT.setPosition(BISCUIT.getPosition());
10495
DRIVE.sandstormAxisFlip(true);
10596
DRIVE.setAngleAdjustment(true);
106-
if (isAuton) {
107-
// sandstorm.start();
108-
} else {
109-
// noAutoSandstorm.start();
110-
}
97+
noAutoSandstorm.start();
11198
}
11299

113100
@Override
@@ -134,6 +121,6 @@ public void teleopPeriodic() {
134121

135122
public enum StartLevel {
136123
ONE,
137-
TWO;
124+
TWO
138125
}
139126
}

src/main/java/frc/team2767/deepspace/command/approach/VisionAutoAlignPickupCommand.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,22 @@
1515
import org.strykeforce.thirdcoast.util.RateLimit;
1616

1717
public class VisionAutoAlignPickupCommand extends Command {
18-
public static final double kP_STRAFE = 0.06; // 0.1
18+
public static final double kP_STRAFE = 0.1; // 0.1
1919
private static final double kP_YAW = 0.01; // 0.00625 tuning for NT method, 0.01 pyeye
2020
private static final double MAX_YAW = 0.3;
2121
private static final double goodEnoughYaw = 1.5;
2222
private static final double DRIVE_EXPO = 0.5;
23-
private static final double YAW_EXPO = 0.5;
2423
private static final double DEADBAND = 0.05;
2524
private static final double MIN_RANGE = 35.0;
26-
private static final double FWD_SCALE = 0.5;
27-
private static final double FWD_SCALE_FAST = 0.5;
28-
private static final double AUTON_OUTPUT = -0.40;
25+
private static final double FWD_SCALE = 0.6;
26+
private static final double FWD_SCALE_FAST = 0.6;
27+
private static final double AUTON_OUTPUT = 0.35;
2928

3029
private static final DriveSubsystem DRIVE = Robot.DRIVE;
3130
private static final VisionSubsystem VISION = Robot.VISION;
3231
private final ExpoScale driveExpo;
33-
private final ExpoScale yawExpo;
3432
private final Logger logger = LoggerFactory.getLogger(this.getClass());
3533
private double range;
36-
private double strafeError;
37-
private double yawError;
3834
private DriverControls controls;
3935
private double strafeCorrection;
4036
private boolean isAuton;
@@ -45,7 +41,6 @@ public class VisionAutoAlignPickupCommand extends Command {
4541
public VisionAutoAlignPickupCommand() {
4642
requires(DRIVE);
4743
this.driveExpo = new ExpoScale(DEADBAND, DRIVE_EXPO);
48-
this.yawExpo = new ExpoScale(DEADBAND, YAW_EXPO);
4944
strafeRateLimit = new RateLimit(0.04); // 0.015
5045
}
5146

@@ -73,7 +68,8 @@ protected void execute() {
7368
isGood = range >= 0; // check if range is good (we have a target), not -1
7469

7570
// Calculate Yaw Term based on gyro
76-
yawError = targetYaw - Math.IEEEremainder(DRIVE.getGyro().getAngle(), 360.0);
71+
double yawError = targetYaw - Math.IEEEremainder(DRIVE.getGyro().getAngle(), 360.0);
72+
DRIVE.setYawError(yawError);
7773
double yaw = kP_YAW * yawError;
7874
if (yaw > MAX_YAW) yaw = MAX_YAW;
7975
if (yaw < -MAX_YAW) yaw = -MAX_YAW;
@@ -84,24 +80,25 @@ protected void execute() {
8480
double forward;
8581
// forward is still normal
8682
if (isAuton) {
87-
forward = AUTON_OUTPUT;
83+
forward = driveExpo.apply(controls.getForward()) * AUTON_OUTPUT;
8884
} else if (isGood) {
8985
forward = driveExpo.apply(controls.getForward()) * FWD_SCALE;
9086
} else {
9187
forward = driveExpo.apply(controls.getForward()) * FWD_SCALE_FAST;
9288
}
9389

9490
double strafe;
95-
strafeError = Math.sin(Math.toRadians(VISION.getCorrectedBearing())) * range - strafeCorrection;
91+
double strafeError =
92+
Math.sin(Math.toRadians(VISION.getCorrectedBearing())) * range - strafeCorrection;
9693

97-
logger.info("strafe error = {}", strafeError);
94+
VISION.setStrafeError(strafeError);
9895

9996
// Only take over strafe control if pyeye has a target and the robot is straight to the field
10097
if (isGood && onTarget) strafe = strafeError * kP_STRAFE * forward;
10198
else strafe = driveExpo.apply(controls.getStrafe());
10299

103100
double strafeOutput = strafeRateLimit.apply(strafe);
104-
logger.debug("{} {} {}", forward, strafeOutput, yaw);
101+
105102
DRIVE.drive(forward, strafeOutput, yaw);
106103
}
107104

@@ -112,11 +109,8 @@ protected boolean isFinished() {
112109

113110
@Override
114111
protected void end() {
115-
logger.info("End Auto Align Pickup Vision");
116-
}
117112

118-
private double deadband(double value) {
119-
if (Math.abs(value) < DEADBAND) return 0.0;
120-
return value;
113+
logger.debug("range = {}", range);
114+
logger.info("End Auto Align Pickup Vision");
121115
}
122116
}

src/main/java/frc/team2767/deepspace/command/approach/VisionAutoAlignPlaceCommand.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
11
package frc.team2767.deepspace.command.approach;
22

3-
import static frc.team2767.deepspace.subsystem.FieldDirection.LEFT;
4-
3+
import edu.wpi.first.wpilibj.DriverStation;
54
import edu.wpi.first.wpilibj.command.Command;
65
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
76
import frc.team2767.deepspace.Robot;
7+
import frc.team2767.deepspace.control.Controls;
88
import frc.team2767.deepspace.subsystem.DriveSubsystem;
99
import frc.team2767.deepspace.subsystem.VisionSubsystem;
10+
import frc.team2767.deepspace.util.AutoPlaceSideChooser;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213
import org.strykeforce.thirdcoast.swerve.SwerveDrive;
14+
import org.strykeforce.thirdcoast.util.ExpoScale;
1315
import org.strykeforce.thirdcoast.util.RateLimit;
1416

1517
public class VisionAutoAlignPlaceCommand extends Command {
16-
private static final double kP_STRAFE = 0.07; // 0.11
18+
private static final double kP_STRAFE = 0.1; // 0.11
1719
private static final double kP_YAW = 0.01; // 0.00625 tuning for NT method, 0.01 pyeye
1820
private static final double MAX_YAW = 0.3;
1921
private static final double goodEnoughYaw = 1.5;
2022
private static final double MIN_RANGE = 35.0;
21-
private static final double FORWARD_OUTPUT = 0.30;
23+
private static final double FORWARD_SCALE = 0.35;
24+
private static final double DEADBAND = 0.05;
25+
private static final double DRIVE_EXPO = 0.5;
2226

2327
private static final DriveSubsystem DRIVE = Robot.DRIVE;
2428
private static final VisionSubsystem VISION = Robot.VISION;
29+
private static Controls CONTROLS;
2530
private final Logger logger = LoggerFactory.getLogger(this.getClass());
26-
private final double gyroOffset;
31+
private AutoPlaceSideChooser autoPlaceSideChooser = new AutoPlaceSideChooser();
32+
private double gyroOffset;
2733
private double range;
2834
private double forward;
2935
private double strafeError;
3036
private double yawError;
3137
private double strafeCorrection;
3238
private double targetYaw;
39+
private boolean isSandstorm;
3340
private boolean isGood = false;
3441
private RateLimit strafeRateLimit;
42+
private final ExpoScale driveExpo;
3543

36-
public VisionAutoAlignPlaceCommand(double gyroOffset) {
44+
public VisionAutoAlignPlaceCommand() {
3745
requires(DRIVE);
38-
this.gyroOffset = gyroOffset;
3946
strafeRateLimit = new RateLimit(0.05); // 0.015
47+
this.driveExpo = new ExpoScale(DEADBAND, DRIVE_EXPO);
4048
}
4149

4250
@Override
4351
protected void initialize() {
52+
gyroOffset =
53+
autoPlaceSideChooser.determineGyroOffset(
54+
VISION.direction, Math.IEEEremainder(DRIVE.getGyro().getAngle(), 360.0));
55+
targetYaw = autoPlaceSideChooser.determineTargetYaw(VISION.direction);
56+
57+
logger.debug("offset = {} target = {}", gyroOffset, targetYaw);
58+
59+
CONTROLS = Robot.CONTROLS;
60+
isSandstorm = DriverStation.getInstance().isAutonomous();
4461
DRIVE.setGyroOffset(gyroOffset);
4562
SmartDashboard.putBoolean("Game/haveHatch", true);
4663
logger.info("Begin Vision Auto Align Place");
4764
DRIVE.setDriveMode(SwerveDrive.DriveMode.CLOSED_LOOP);
4865
strafeCorrection = VISION.getStrafeCorrection();
49-
if (VISION.direction == LEFT) {
50-
targetYaw = 90.0;
51-
} else {
52-
targetYaw = -90.0;
53-
}
66+
5467
DRIVE.setTargetYaw(targetYaw);
55-
forward = FORWARD_OUTPUT;
5668
logger.debug("forward = {}", forward);
5769
logger.info("target yaw: {}", targetYaw);
5870
}
@@ -78,19 +90,30 @@ protected void execute() {
7890
double strafe;
7991
strafeError = Math.sin(Math.toRadians(VISION.getCorrectedBearing())) * range - strafeCorrection;
8092
VISION.setStrafeError(strafeError);
81-
logger.info("strafe error = {}", strafeError);
93+
forward = driveExpo.apply(CONTROLS.getDriverControls().getForward()) * FORWARD_SCALE;
94+
8295
// Only take over strafe control if pyeye has a target and the robot is straight to the field
8396
if (isGood && onTarget) strafe = strafeError * kP_STRAFE * forward;
84-
else strafe = 0;
97+
else strafe = driveExpo.apply(CONTROLS.getDriverControls().getStrafe());
8598

86-
DRIVE.drive(forward, strafeRateLimit.apply(strafe), yaw);
99+
strafe = strafeRateLimit.apply(strafe);
100+
101+
DRIVE.setGraphableStrafe(strafe);
102+
logger.info("strafe = {}", strafe);
103+
104+
DRIVE.drive(forward, strafe, yaw);
87105
}
88106

89107
@Override
90108
protected boolean isFinished() {
91109
return (range <= MIN_RANGE && isGood);
92110
}
93111

112+
@Override
113+
protected void interrupted() {
114+
DRIVE.undoGyroOffset();
115+
}
116+
94117
@Override
95118
protected void end() {
96119
logger.info("End Auto Align Place Vision");

src/main/java/frc/team2767/deepspace/command/approach/sandstorm/AutoPlaceTestLeftCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class AutoPlaceTestLeftCommand extends CommandGroup {
1111
public AutoPlaceTestLeftCommand() {
1212
addSequential(new SandstormHatchPickupCommandGroup(), 0.5);
1313
addSequential(new SetFieldDirectionCommand(FieldDirection.LEFT));
14-
addSequential(new AutoHatchPlaceCommandGroup(0.0));
14+
addSequential(new AutoHatchPlaceCommandGroup());
1515
}
1616
}

src/main/java/frc/team2767/deepspace/command/approach/sandstorm/AutoPlaceTestRightCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class AutoPlaceTestRightCommand extends CommandGroup {
1111
public AutoPlaceTestRightCommand() {
1212
addSequential(new SandstormHatchPickupCommandGroup(), 0.5);
1313
addSequential(new SetFieldDirectionCommand(FieldDirection.RIGHT));
14-
addSequential(new AutoHatchPlaceCommandGroup(0.0));
14+
addSequential(new AutoHatchPlaceCommandGroup());
1515
}
1616
}

src/main/java/frc/team2767/deepspace/command/approach/sandstorm/SandstormCommandGroup.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public SandstormCommandGroup() {
2424
addParallel(new LightsOnCommand());
2525
}
2626
});
27-
addSequential(new AutoHatchPlaceCommandGroup(0.0));
27+
addSequential(new AutoHatchPlaceCommandGroup());
2828
addSequential(new ApproachDirectionCommand());
2929
addSequential(
3030
new CommandGroup() {
@@ -50,8 +50,6 @@ public SandstormCommandGroup() {
5050
addParallel(new LightsOnCommand());
5151
}
5252
});
53-
addSequential(
54-
new ChooseAutonFieldSideCommand(
55-
new AutoHatchPlaceCommandGroup(-90.0), new AutoHatchPlaceCommandGroup(90.0)));
53+
addSequential(new AutoHatchPlaceCommandGroup());
5654
}
5755
}

src/main/java/frc/team2767/deepspace/command/approach/sequences/AutoHatchPlaceCommandGroup.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
public class AutoHatchPlaceCommandGroup extends CommandGroup {
2222

23-
public AutoHatchPlaceCommandGroup(double gyroOffset) {
23+
public AutoHatchPlaceCommandGroup() {
2424

2525
addSequential(new LogCommand("BEGIN AUTO HATCH PLACE"));
2626
addSequential(new FlipSandstormControlsCommand(false));
@@ -33,9 +33,14 @@ public AutoHatchPlaceCommandGroup(double gyroOffset) {
3333
addParallel(new SetLevelCommand(ElevatorLevel.ONE));
3434
}
3535
});
36-
addSequential(new ElevatorSetPositionCommand(ElevatorSubsystem.kHatchLowPositionInches));
37-
addSequential(new BiscuitPositionAboveCameraCommand());
38-
addSequential(new VisionAutoAlignPlaceCommand(gyroOffset));
36+
addSequential(
37+
new CommandGroup() {
38+
{
39+
addParallel(new ElevatorSetPositionCommand(ElevatorSubsystem.kHatchLowPositionInches));
40+
addParallel(new BiscuitPositionAboveCameraCommand());
41+
addSequential(new VisionAutoAlignPlaceCommand());
42+
}
43+
});
3944
addSequential(new BiscuitExecutePlanCommand());
4045
addSequential(new HoldHeadingUntilCompressionCommand());
4146
addSequential(new SandstormSwapIfAutonConditionalCommand(true));

src/main/java/frc/team2767/deepspace/command/sequences/StowAllCommandGroup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import frc.team2767.deepspace.command.log.LogCommand;
66
import frc.team2767.deepspace.command.teleop.StowElevatorConditionalCommand;
77
import frc.team2767.deepspace.command.vacuum.StowValveControlCommand;
8+
import frc.team2767.deepspace.command.vision.LightsOffCommand;
89
import frc.team2767.deepspace.subsystem.IntakeSubsystem;
910

1011
public class StowAllCommandGroup extends CommandGroup {
@@ -16,6 +17,7 @@ public StowAllCommandGroup() {
1617
addParallel(new StowValveControlCommand());
1718
addParallel(new StowElevatorConditionalCommand());
1819
addParallel(new IntakePositionCommand(IntakeSubsystem.kStowPositionDeg));
20+
addParallel(new LightsOffCommand());
1921
}
2022
});
2123
addSequential(new LogCommand("END STOW ALL"));

src/main/java/frc/team2767/deepspace/command/sequences/pickup/CoconutPickupAutoRetryCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class CoconutPickupAutoRetryCommand extends Command {
1616
private static final double STABLE_COUNTS = 5;
1717
private static final double WAIT_TIME = 1000; // ms
1818
private static final double RESET_HEIGHT = 20.25;
19-
private static final double STRING_LENGTH = 260.0;
19+
private static final double STRING_COMPRESSED_LENGTH = 223.0; // full length is 248
2020
private static PickupState state;
2121
private static double initialPressure;
2222
private static double startSealTime;
@@ -45,7 +45,7 @@ protected void initialize() {
4545
protected void execute() {
4646
switch (state) {
4747
case DOWN:
48-
if (BISCUIT.getCompression() <= STRING_LENGTH) {
48+
if (BISCUIT.getCompression() <= STRING_COMPRESSED_LENGTH) {
4949
state = PickupState.WAIT_FOR_PRESSURE;
5050
ELEVATOR.setPosition(ELEVATOR.getPosition()); // inches
5151
startSealTime = System.currentTimeMillis();

0 commit comments

Comments
 (0)