Lights work

This commit is contained in:
2026-02-10 20:44:12 -06:00
parent 127995c5fe
commit fab0d6346d
6 changed files with 349 additions and 165 deletions

View File

@@ -6,6 +6,11 @@ import com.acmerobotics.dashboard.config.Config;
public class Color {
public static boolean redAlliance = true;
public static double Light0 = 0.28, Light1 = 0.67, Light2 = 0.36, Light3 = 0.5;
public static double LightGreen = 0.5;
public static double LightPurple = 0.715;
public static double LightOrange = 0.33;
public static double LightRed = 0.28;
public static double LightBlue = 0.6;
public static double colorFilterAlpha = 1;
}

View File

@@ -1,10 +1,19 @@
package org.firstinspires.ftc.teamcode.constants;
public class Types {
public class StateEnums {
public enum Motif {
NONE,
GPP, // Green, Purple, Purple
PGP, // Purple, Green, Purple
PPG // Purple, Purple, Green
}
public enum LightState {
BALL_COUNT,
BALL_COLOR,
GOAL_LOCK,
MANUAL,
DISABLED,
OFF
}
}

View File

@@ -16,9 +16,12 @@ import com.qualcomm.hardware.lynx.LynxModule;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import org.firstinspires.ftc.teamcode.constants.Color;
import org.firstinspires.ftc.teamcode.constants.StateEnums;
import org.firstinspires.ftc.teamcode.libs.RR.MecanumDrive;
import org.firstinspires.ftc.teamcode.utils.Drivetrain;
import org.firstinspires.ftc.teamcode.utils.Flywheel;
import org.firstinspires.ftc.teamcode.utils.Light;
import org.firstinspires.ftc.teamcode.utils.Robot;
import org.firstinspires.ftc.teamcode.utils.Servos;
import org.firstinspires.ftc.teamcode.utils.Spindexer;
@@ -47,6 +50,7 @@ public class TeleopV3 extends LinearOpMode {
boolean fixedTurret = false;
Robot robot;
MultipleTelemetry TELE;
Light light;
Servos servo;
Flywheel flywheel;
MecanumDrive drive;
@@ -63,7 +67,6 @@ public class TeleopV3 extends LinearOpMode {
double headingOffset = 0.0;
int ticker = 0;
boolean autoSpintake = false;
boolean enableSpindexerManager = true;
@@ -99,19 +102,27 @@ public class TeleopV3 extends LinearOpMode {
tController.setTolerance(0.001);
Turret turret = new Turret(robot, TELE, robot.limelight);
robot.light.setPosition(1);
light = Light.getInstance();
light.init(robot.light, spindexer, turret);
light.setState(StateEnums.LightState.MANUAL);
while (opModeInInit()) {
robot.limelight.start();
if (redAlliance) {
robot.limelight.pipelineSwitch(4);
light.setManualLightColor(Color.LightRed);
} else {
robot.limelight.pipelineSwitch(2);
light.setManualLightColor(Color.LightBlue);
}
light.update();
}
limelightUsed = true;
waitForStart();
if (isStopRequested()) return;
@@ -121,9 +132,6 @@ public class TeleopV3 extends LinearOpMode {
//TELE.addData("Is limelight on?", robot.limelight.getStatus());
// LIGHT COLORS
spindexer.ballCounterLight();
//DRIVETRAIN:
drivetrain.drive(
@@ -134,9 +142,17 @@ public class TeleopV3 extends LinearOpMode {
);
if (gamepad1.right_bumper) {
shootAll = false;
robot.transferServo.setPosition(transferServo_out);
light.setState(StateEnums.LightState.BALL_COUNT);
} else if (gamepad2.triangle){
light.setState(StateEnums.LightState.BALL_COLOR);
} else {
light.setState(StateEnums.LightState.GOAL_LOCK);
}
robot.transfer.setPower(1);
@@ -227,8 +243,6 @@ public class TeleopV3 extends LinearOpMode {
drive = new MecanumDrive(hardwareMap, new Pose2d(0, 0, 0));
}
if (enableSpindexerManager) {
//if (!shootAll) {
spindexer.processIntake();
@@ -342,6 +356,8 @@ public class TeleopV3 extends LinearOpMode {
TELE.update();
light.update();
ticker++;
}
}

View File

@@ -0,0 +1,108 @@
package org.firstinspires.ftc.teamcode.utils;
import com.acmerobotics.dashboard.config.Config;
import com.qualcomm.robotcore.hardware.Servo;
import org.firstinspires.ftc.teamcode.constants.Color;
import org.firstinspires.ftc.teamcode.constants.StateEnums.LightState;
@Config
public final class Light {
private static Light instance;
public static double ballColorCycleTime = 1000; //in ms
public static double restingTime = 150; //in ms
private Servo lightServo;
private LightState state = LightState.DISABLED;
// References to other systems (NOT static)
private Spindexer spindexer;
private Turret turret;
private double manualLightColor = Color.Light0;
private double lightColor = Color.Light0;
private double previousLightColor = lightColor;
private Light() {
}
public static synchronized Light getInstance() {
if (instance == null) {
instance = new Light();
}
return instance;
}
// Call once in OpMode init()
public void init(
Servo servo,
Spindexer spin,
Turret turr
) {
this.lightServo = servo;
this.spindexer = spin;
this.turret = turr;
}
public void setManualLightColor(double value) {
this.manualLightColor = value;
}
public void setState(LightState newState) {
state = newState;
}
public void update() {
if (lightServo == null) return;
switch (state) {
case BALL_COUNT:
lightColor = spindexer.ballCounterLight();
break;
case BALL_COLOR:
if ((System.currentTimeMillis() % ballColorCycleTime) < ((ballColorCycleTime / 3) - restingTime)) {
lightColor = spindexer.getRearCenterLight();
} else if ((System.currentTimeMillis() % ballColorCycleTime) < (ballColorCycleTime / 3)) {
lightColor = 0;
} else if ((System.currentTimeMillis() % ballColorCycleTime) < ((2 * ballColorCycleTime / 3) - restingTime)) {
lightColor = spindexer.getDriverLight();
} else if ((System.currentTimeMillis() % ballColorCycleTime) < (2 * ballColorCycleTime / 3)) {
lightColor = 0;
} else if ((System.currentTimeMillis() % ballColorCycleTime) < (ballColorCycleTime - restingTime)) {
lightColor = spindexer.getPassengerLight();
} else {
lightColor = 0;
}
break;
case GOAL_LOCK:
lightColor = turret.getLightColor();
break;
case MANUAL:
lightColor = manualLightColor;
break;
case DISABLED:
break;
case OFF:
lightColor = 0;
break;
}
if (lightColor != previousLightColor) {
lightServo.setPosition(lightColor);
}
previousLightColor = lightColor;
}
}

View File

@@ -1,10 +1,13 @@
package org.firstinspires.ftc.teamcode.utils;
import com.acmerobotics.dashboard.telemetry.MultipleTelemetry;
import com.arcrobotics.ftclib.controller.PIDFController;
import com.qualcomm.robotcore.hardware.HardwareMap;
import static org.firstinspires.ftc.teamcode.constants.Color.*;
import static org.firstinspires.ftc.teamcode.constants.Color.Light0;
import static org.firstinspires.ftc.teamcode.constants.Color.Light1;
import static org.firstinspires.ftc.teamcode.constants.Color.Light2;
import static org.firstinspires.ftc.teamcode.constants.Color.Light3;
import static org.firstinspires.ftc.teamcode.constants.Color.LightGreen;
import static org.firstinspires.ftc.teamcode.constants.Color.LightOrange;
import static org.firstinspires.ftc.teamcode.constants.Color.LightPurple;
import static org.firstinspires.ftc.teamcode.constants.Color.colorFilterAlpha;
import static org.firstinspires.ftc.teamcode.constants.ServoPositions.shootAllSpindexerSpeedIncrease;
import static org.firstinspires.ftc.teamcode.constants.ServoPositions.spinEndPos;
import static org.firstinspires.ftc.teamcode.constants.ServoPositions.spinStartPos;
@@ -19,89 +22,51 @@ import static org.firstinspires.ftc.teamcode.utils.Servos.spinF;
import static org.firstinspires.ftc.teamcode.utils.Servos.spinI;
import static org.firstinspires.ftc.teamcode.utils.Servos.spinP;
import com.acmerobotics.dashboard.telemetry.MultipleTelemetry;
import com.arcrobotics.ftclib.controller.PIDFController;
import com.qualcomm.robotcore.hardware.HardwareMap;
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
import org.firstinspires.ftc.teamcode.constants.Types;
import org.firstinspires.ftc.teamcode.constants.StateEnums;
import org.firstinspires.ftc.teamcode.libs.RR.MecanumDrive;
import java.util.Objects;
public class Spindexer {
// Array of commandedIntakePositions with contents
// {RearCenter, FrontDriver, FrontPassenger}
static final int[][] RotatedBallPositions = {{0, 2, 1}, {1, 0, 2}, {2, 1, 0}};
public int commandedIntakePosition = 0;
public double distanceRearCenter = 0.0;
public double distanceFrontDriver = 0.0;
public double distanceFrontPassenger = 0.0;
public double spindexerWiggle = 0.01;
public double spindexerOuttakeWiggle = 0.01;
public double spindexerPosOffset = 0.00;
public StateEnums.Motif desiredMotif = StateEnums.Motif.NONE;
public IntakeState currentIntakeState = IntakeState.UNKNOWN_START;
public IntakeState prevIntakeState = IntakeState.UNKNOWN_START;
public int unknownColorDetect = 0;
public int counter = 0;
Robot robot;
Servos servos;
Flywheel flywheel;
MecanumDrive drive;
double lastKnownSpinPos = 0.0;
MultipleTelemetry TELE;
PIDFController spinPID = new PIDFController(spinP, spinI, spinD, spinF);
double spinCurrentPos = 0.0;
public int commandedIntakePosition = 0;
public double distanceRearCenter = 0.0;
public double distanceFrontDriver = 0.0;
public double distanceFrontPassenger = 0.0;
public double spindexerWiggle = 0.01;
public double spindexerOuttakeWiggle = 0.01;
private double prevPos = 0.0;
public double spindexerPosOffset = 0.00;
public Types.Motif desiredMotif = Types.Motif.NONE;
// For Use
enum RotatedBallPositionNames {
REARCENTER,
FRONTDRIVER,
FRONTPASSENGER
}
// Array of commandedIntakePositions with contents
// {RearCenter, FrontDriver, FrontPassenger}
static final int[][] RotatedBallPositions = {{0,2,1}, {1,0,2}, {2,1,0}};
class spindexerBallRoatation {
int rearCenter = 0; // aka commanded Position
int frontDriver = 0;
int frontPassenger = 0;
}
enum IntakeState {
UNKNOWN_START,
UNKNOWN_MOVE,
UNKNOWN_DETECT,
INTAKE,
FINDNEXT,
MOVING,
FULL,
SHOOTNEXT,
SHOOTMOVING,
SHOOTWAIT,
SHOOT_ALL_PREP,
SHOOT_ALL_READY,
SHOOT_PREP_CONTINOUS,
SHOOT_CONTINOUS
}
int shootWaitCount = 0;
public IntakeState currentIntakeState = IntakeState.UNKNOWN_START;
public IntakeState prevIntakeState = IntakeState.UNKNOWN_START;
public int unknownColorDetect = 0;
public enum BallColor {
UNKNOWN,
GREEN,
PURPLE
}
class BallPosition {
boolean isEmpty = true;
int foundEmpty = 0;
BallColor ballColor = BallColor.UNKNOWN;
}
BallPosition[] ballPositions = new BallPosition[3];
public boolean init () {
return true;
}
double[] outakePositions =
{spindexer_outtakeBall1 + spindexerPosOffset,
spindexer_outtakeBall2 + spindexerPosOffset,
spindexer_outtakeBall3 + spindexerPosOffset};
double[] intakePositions =
{spindexer_intakePos1, spindexer_intakePos2, spindexer_intakePos3};
private double prevPos = 0.0;
private double intakeTicker = 0;
public Spindexer(HardwareMap hardwareMap) {
robot = new Robot(hardwareMap);
@@ -115,34 +80,11 @@ public class Spindexer {
ballPositions[1] = new BallPosition();
ballPositions[2] = new BallPosition();
}
double[] outakePositions =
{spindexer_outtakeBall1+spindexerPosOffset,
spindexer_outtakeBall2+spindexerPosOffset,
spindexer_outtakeBall3+spindexerPosOffset};
double[] intakePositions =
{spindexer_intakePos1, spindexer_intakePos2, spindexer_intakePos3};
public int counter = 0;
// private double getTimeSeconds ()
// {
// return (double) System.currentTimeMillis()/1000.0;
// }
// public double getPos() {
// robot.spin1Pos.getVoltage();
// robot.spin1Pos.getMaxVoltage();
// return (robot.spin1Pos.getVoltage()/robot.spin1Pos.getMaxVoltage());
// }
// public void manageSpindexer() {
//
// }
public boolean init() {
return true;
}
public void resetBallPosition(int pos) {
ballPositions[pos].isEmpty = true;
@@ -267,6 +209,7 @@ public class Spindexer {
return newPos1Detection;
}
// Has code to unjam spindexer
private void moveSpindexerToPos(double pos) {
robot.spin1.setPosition(pos);
@@ -284,11 +227,59 @@ public class Spindexer {
// prevPos = currentPos;
}
// private double getTimeSeconds ()
// {
// return (double) System.currentTimeMillis()/1000.0;
// }
// public double getPos() {
// robot.spin1Pos.getVoltage();
// robot.spin1Pos.getMaxVoltage();
// return (robot.spin1Pos.getVoltage()/robot.spin1Pos.getMaxVoltage());
// }
// public void manageSpindexer() {
//
// }
public void stopSpindexer() {
}
public void ballCounterLight(){
public double getRearCenterLight() {
BallColor color = GetRearCenterColor();
if (Objects.equals(color, BallColor.GREEN)) {
return LightGreen;
} else if (Objects.equals(color, BallColor.PURPLE)) {
return LightPurple;
} else {
return LightOrange;
}
}
public double getDriverLight() {
BallColor color = GetFrontDriverColor();
if (Objects.equals(color, BallColor.GREEN)) {
return LightGreen;
} else if (Objects.equals(color, BallColor.PURPLE)) {
return LightPurple;
} else {
return LightOrange;
}
}
public double getPassengerLight() {
BallColor color = GetFrontPassengerColor();
if (Objects.equals(color, BallColor.GREEN)) {
return LightGreen;
} else if (Objects.equals(color, BallColor.PURPLE)) {
return LightPurple;
} else {
return LightOrange;
}
}
public double ballCounterLight() {
int counter = 0;
if (!ballPositions[0].isEmpty) {
counter++;
@@ -299,14 +290,15 @@ public class Spindexer {
if (!ballPositions[2].isEmpty) {
counter++;
}
if (counter == 3) {
robot.light.setPosition(Light3);
return Light3;
} else if (counter == 2) {
robot.light.setPosition(Light2);
return Light2;
} else if (counter == 1) {
robot.light.setPosition(Light1);
return Light1;
} else {
robot.light.setPosition(Light0);
return Light0;
}
}
@@ -318,7 +310,6 @@ public class Spindexer {
return (!ballPositions[0].isEmpty && !ballPositions[1].isEmpty && !ballPositions[2].isEmpty);
}
private double intakeTicker = 0;
public boolean processIntake() {
switch (currentIntakeState) {
@@ -529,7 +520,7 @@ public class Spindexer {
return false;
}
public void setDesiredMotif (Types.Motif newMotif) {
public void setDesiredMotif(StateEnums.Motif newMotif) {
desiredMotif = newMotif;
}
@@ -577,15 +568,18 @@ public class Spindexer {
public void prepareShootAll() {
currentIntakeState = Spindexer.IntakeState.SHOOT_ALL_PREP;
}
public void prepareShootAllContinous() {
currentIntakeState = Spindexer.IntakeState.SHOOT_PREP_CONTINOUS;
}
public void shootAll() {
ballPositions[0].isEmpty = false;
ballPositions[1].isEmpty = false;
ballPositions[2].isEmpty = false;
currentIntakeState = Spindexer.IntakeState.SHOOTNEXT;
}
public void shootAllContinous() {
ballPositions[0].isEmpty = false;
ballPositions[1].isEmpty = false;
@@ -593,8 +587,7 @@ public class Spindexer {
currentIntakeState = Spindexer.IntakeState.SHOOT_CONTINOUS;
}
public boolean shootAllComplete ()
{
public boolean shootAllComplete() {
return ((currentIntakeState != Spindexer.IntakeState.SHOOT_ALL_PREP) &&
(currentIntakeState != Spindexer.IntakeState.SHOOT_ALL_READY) &&
(currentIntakeState != Spindexer.IntakeState.SHOOTMOVING) &&
@@ -608,8 +601,7 @@ public class Spindexer {
currentIntakeState = Spindexer.IntakeState.FINDNEXT;
}
public void update()
{
public void update() {
}
public BallColor GetFrontDriverColor() {
@@ -623,4 +615,46 @@ public class Spindexer {
public BallColor GetRearCenterColor() {
return ballPositions[RotatedBallPositions[commandedIntakePosition][RotatedBallPositionNames.REARCENTER.ordinal()]].ballColor;
}
// For Use
enum RotatedBallPositionNames {
REARCENTER,
FRONTDRIVER,
FRONTPASSENGER
}
enum IntakeState {
UNKNOWN_START,
UNKNOWN_MOVE,
UNKNOWN_DETECT,
INTAKE,
FINDNEXT,
MOVING,
FULL,
SHOOTNEXT,
SHOOTMOVING,
SHOOTWAIT,
SHOOT_ALL_PREP,
SHOOT_ALL_READY,
SHOOT_PREP_CONTINOUS,
SHOOT_CONTINOUS
}
public enum BallColor {
UNKNOWN,
GREEN,
PURPLE
}
class spindexerBallRoatation {
int rearCenter = 0; // aka commanded Position
int frontDriver = 0;
int frontPassenger = 0;
}
class BallPosition {
boolean isEmpty = true;
int foundEmpty = 0;
BallColor ballColor = BallColor.UNKNOWN;
}
}

View File

@@ -9,10 +9,10 @@ import com.arcrobotics.ftclib.controller.PIDController;
import com.qualcomm.hardware.limelightvision.LLResult;
import com.qualcomm.hardware.limelightvision.LLResultTypes;
import com.qualcomm.hardware.limelightvision.Limelight3A;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import org.firstinspires.ftc.robotcore.external.navigation.Pose3D;
import org.firstinspires.ftc.teamcode.constants.Color;
import java.util.List;
@@ -51,6 +51,7 @@ public class Turret {
private int obeliskID = 0;
private double offset = 0.0;
private double currentTrackOffset = 0.0;
private double lightColor = Color.LightRed;
private int currentTrackCount = 0;
private double permanentOffset = 0.0;
private PIDController bearingPID;
@@ -62,6 +63,10 @@ public class Turret {
bearingPID = new PIDController(B_PID_P, B_PID_I, B_PID_D);
}
public double getLightColor() {
return lightColor;
}
public void zeroTurretEncoder() {
robot.intake.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.intake.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
@@ -153,11 +158,17 @@ public class Turret {
// LL has 54.5 degree total Horizontal FOV; very edges are not useful.
final double HORIZONTAL_FOV_RANGE = 26.0; // Total usable horizontal degrees from center +/-
final double DRIVE_POWER_REDUCTION = 2.0;
final double COLOR_OK_TOLERANCE = 2.5;
if (abs(targetTx) < TARGET_POSITION_TOLERANCE) {
bearingAligned = true;
lightColor = Color.LightBlue;
} else if (abs(targetTx) < COLOR_OK_TOLERANCE) {
bearingAligned = false;
lightColor = Color.LightPurple;
} else {
bearingAligned = false;
lightColor = Color.LightOrange;
}
// Only with valid data and if too far off target
@@ -246,6 +257,7 @@ public class Turret {
// if (currentTrackCount > 20) {
// offset = currentTrackOffset;
// }
lightColor = Color.LightRed;
currentTrackOffset = 0.0;
currentTrackCount = 0;
}