Skip to content

Commit

Permalink
Don't reconstruct color sensor if not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
ky28059 committed Mar 26, 2023
1 parent f8d1f97 commit 1c578e1
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions src/main/java/frc/robot/subsystems/RollerSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import edu.wpi.first.networktables.GenericEntry;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab;
import edu.wpi.first.wpilibj.util.Color;
Expand Down Expand Up @@ -39,7 +38,6 @@ public enum HeldPiece {
private final TrackingTimer openTimer = new TrackingTimer();
private final TrackingTimer closeTimer = new TrackingTimer();
private final TrackingTimer cooldownTimer = new TrackingTimer();
private final TrackingTimer colorTimer = new TrackingTimer();

private static final double OPEN_TIME_SECONDS = 1.0;
private static final double CLOSE_TIME_SECONDS = 0.5;
Expand All @@ -64,7 +62,7 @@ public enum HeldPiece {
//for tuning
private final ShuffleboardTab shuffleboardTab;
private final GenericEntry limitEntry, proximityEntry, colorEntry, heldPieceEntry;
private final GenericEntry rEntry, gEntry, bEntry;
private final GenericEntry rEntry, gEntry, bEntry, connectedEntry;

private double rollPower = 0.0;

Expand Down Expand Up @@ -110,6 +108,9 @@ public RollerSubsystem() {
bEntry = shuffleboardTab.add("B", 0)
.withPosition(2, 1)
.getEntry();
connectedEntry = shuffleboardTab.add("Sensor connected", 0)
.withPosition(3, 1)
.getEntry();
}

/**
Expand Down Expand Up @@ -223,46 +224,30 @@ private HeldPiece getProximitySensorPiece() {
* @return The piece detected by the color sensor.
*/
private HeldPiece getColorSensorPiece() {
// If we're not connected, return EMPTY and fall through to limit switch.
connectedEntry.setBoolean(crolorSensor.isConnected());
if (!crolorSensor.isConnected()) return HeldPiece.EMPTY;

Color detectedColor = crolorSensor.getColor();
double red = detectedColor.red * 255;
double green = detectedColor.green * 255;
double blue = detectedColor.blue * 255;

// for tuning
rEntry.setValue(red);
gEntry.setValue(green);
bEntry.setValue(blue);

//if the color is 0 0 0, the sensor is broken and should be rebooted
//only do this every couple seconds
if(red == 0 && green == 0 && blue == 0){
if(!colorTimer.hasStarted()){
crolorSensor = new ColorSensorV3(I2C.Port.kMXP);
colorTimer.start();
} else if (colorTimer.advanceIfElapsed(2)){
crolorSensor = new ColorSensorV3(I2C.Port.kMXP);
}
} else if(colorTimer.hasStarted()){
colorTimer.stop();
colorTimer.reset();
}

//calculate 3d distance between measured point and each set points
// Calculate 3d distance between measured RGB and the EMPTY, CONE, and CUBE RGB values.
double emptyDist = Math.pow(EMPTY_RED - red, 2) + Math.pow(EMPTY_GREEN - green, 2) + Math.pow(EMPTY_BLUE - blue, 2);
double coneDist = Math.pow(CONE_RED - red, 2) + Math.pow(CONE_GREEN - green, 2) + Math.pow(CONE_BLUE - blue, 2);
double cubeDist = Math.pow(CUBE_RED - red, 2) + Math.pow(CUBE_GREEN - green, 2) + Math.pow(CUBE_BLUE - blue, 2);

// System.out.println("empty - " + emptyDist + " cone - " + coneDist + " cube - " + cubeDist);

//determine that the piece is the one with the least 3d distance
if (cubeDist < coneDist && cubeDist < emptyDist){
// System.out.print("CUBE ");
// Return the piece with the least 3d distance.
if (cubeDist < coneDist && cubeDist < emptyDist) {
return HeldPiece.CUBE;
} else if (coneDist < emptyDist){
// System.out.print("CONE ");
} else if (coneDist < emptyDist) {
return HeldPiece.CONE;
} else {
// System.out.print("NONE ");
return HeldPiece.EMPTY;
}
}
Expand Down

0 comments on commit 1c578e1

Please sign in to comment.