Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logic fixes #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import frc.lib.sensors.ColorSensor;
import frc.lib.sensors.ColorSensor.Colors;
import frc.lib.util.CrashTracker;
import frc.robot.auto.modes.AutoModeBase;
import frc.robot.auto.modes.ExampleMode;
import frc.robot.loops.Looper;
import frc.robot.subsystems.Drive;
import frc.robot.subsystems.Limelight;
Expand Down
51 changes: 48 additions & 3 deletions src/main/java/frc/robot/states/TimedLEDState.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
public interface TimedLEDState {
void getCurrentLEDState(LEDState desiredState, double timestamp);
int getPriority();
void upTimeout();
boolean isTimeout();

class FlashingLEDState implements TimedLEDState {
public class FlashingLEDState implements TimedLEDState {

public static FlashingLEDState kBootFault = new FlashingLEDState(LEDState.kRed, 0.75, 1);
public static FlashingLEDState kRuntimeFault = new FlashingLEDState(LEDState.kRed, 0.25, 1);
Expand All @@ -37,6 +39,8 @@ class FlashingLEDState implements TimedLEDState {
LEDState mStateTwo = new LEDState(0.0, 0.0, 0.0);
double mDuration;
int mPriority;
int mTimeoutMax;
int mCounter;

public FlashingLEDState(LEDState stateOne, LEDState stateTwo, double duration, int priority) {
mStateOne = stateOne;
Expand All @@ -47,6 +51,16 @@ public FlashingLEDState(LEDState stateOne, LEDState stateTwo, double duration, i

public FlashingLEDState(LEDState state, double duration, int priority) {
this(LEDState.kOff, state, duration, priority);
switch(priority) {
case 1:
mTimeoutMax = 5;
case 2:
mTimeoutMax = 5;
case 3:
mTimeoutMax = 1;
default:
mTimeoutMax = -1;
}
}

/**
Expand All @@ -72,24 +86,46 @@ public int getPriority() {
return mPriority - 1;
}

@Override
public void upTimeout() {
mCounter++;
}

@Override
public boolean isTimeout() {
return mTimeoutMax == -1 ? false : mCounter >= mTimeoutMax;
}

}

class StaticLEDState implements TimedLEDState {
public class StaticLEDState implements TimedLEDState {

public static StaticLEDState kEmergency = new StaticLEDState(LEDState.kRed, 1);
public static StaticLEDState kShooting = new StaticLEDState(LEDState.kGreen, 3);
public static StaticLEDState kAutoComplete = new StaticLEDState(LEDState.kOrange, 2);
public static StaticLEDState kHopperFull = new StaticLEDState(LEDState.kYellow, 3);
public static StaticLEDState kDisabled = new StaticLEDState(LEDState.kOff, 1);
public static StaticLEDState kEnabled = new StaticLEDState(LEDState.kWhite, 3);
public static StaticLEDState kBasic = new StaticLEDState(LEDState.kSpartaGreen, 3);
public static StaticLEDState kBasic = new StaticLEDState(LEDState.kSpartaGreen, 4);

LEDState mStaticState = new LEDState(0.0, 0.0, 0.0);
int mPriority;
int mTimeoutMax;
int mCounter;

public StaticLEDState(LEDState staticState, int priority) {
mStaticState.copyFrom(staticState);
mPriority = priority;
switch(priority) {
case 1:
mTimeoutMax = 5;
case 2:
mTimeoutMax = 10;
case 3:
mTimeoutMax = 15;
default:
mTimeoutMax = -1;
}
}

/**
Expand All @@ -109,6 +145,15 @@ public int getPriority() {
return mPriority - 1;
}

@Override
public void upTimeout() {
mCounter++;
}

@Override
public boolean isTimeout() {
return mTimeoutMax == -1 ? false : mCounter >= mTimeoutMax;
}
}


Expand Down
23 changes: 19 additions & 4 deletions src/main/java/frc/robot/subsystems/LED.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import frc.robot.loops.Loop;
import frc.robot.states.LEDState;
import frc.robot.states.TimedLEDState;
import frc.robot.states.TimedLEDState.StaticLEDState;

/**
* Add your docs here.
Expand All @@ -37,14 +38,16 @@ public static LED getInstance() {
return mInstance;
}

// hardware
//hardware
private final CANifier mCanifier;

// states
//states
private ArrayList<TimedLEDState> errorstates = new ArrayList<>();
private ArrayList<TimedLEDState> warningstates = new ArrayList<>();
private ArrayList<TimedLEDState> infostates = new ArrayList<>();
private List<ArrayList<TimedLEDState>> allStates = Arrays.asList(errorstates, warningstates, infostates);
private ArrayList<TimedLEDState> basicstate = new ArrayList<>();
private List<ArrayList<TimedLEDState>> allStates = Arrays.asList(errorstates, warningstates, infostates,
basicstate);
private LEDState mDesiredLEDState = new LEDState(0.0, 0.0, 0.0);
private int priorityLogic = 0;
private int cycleLogic = 0;
Expand Down Expand Up @@ -100,7 +103,11 @@ public void onStop(double timestamp) {
public void logics(double startTime) {
if (errorstates.size() == 0) {
if (warningstates.size() == 0) {
priorityLogic = 2;
if (infostates.size() == 0) {
priorityLogic = 3;
} else {
priorityLogic = 2;
}
} else {
priorityLogic = 1;
}
Expand All @@ -109,6 +116,7 @@ public void logics(double startTime) {
}
if (Timer.getFPGATimestamp() - startTime > cycletime) {
startTime = Timer.getFPGATimestamp();
removeLED();
cycleLogic++;
if (allStates.get(priorityLogic).size() < cycleLogic) {
cycleLogic = 0;
Expand All @@ -120,6 +128,13 @@ public void updateLED(double timestamp) {
allStates.get(priorityLogic).get(cycleLogic).getCurrentLEDState(mDesiredLEDState, timestamp);
}

public void removeLED() {
allStates.get(priorityLogic).get(cycleLogic).upTimeout();
if(allStates.get(priorityLogic).get(cycleLogic).isTimeout()) {
removeFromQueue(allStates.get(priorityLogic).get(cycleLogic));
}
}

@Override
public void writePeriodicOutputs() {
mCanifier.setLEDOutput(mDesiredLEDState.red, LEDChannel.LEDChannelA);
Expand Down