Skip to content

Commit

Permalink
Merge pull request #84 from 4201VitruvianBots/LEDCode
Browse files Browse the repository at this point in the history
Pull Req. for the work I've done so far.
  • Loading branch information
gavinskycastle authored Feb 16, 2025
2 parents ef39a56 + 2fb6718 commit 3fb4d93
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/main/java/frc/robot/commands/led/GetSubsystemStates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands.led;

import edu.wpi.first.wpilibj2.command.Command;

/* You should consider using the more terse Command factories API instead https://docs.wpilib.org/en/stable/docs/software/commandbased/organizing-command-based.html#defining-commands */
public class GetSubsystemStates extends Command {
/** Creates a new GerSubsystemStates. */
public GetSubsystemStates() {
// Use addRequirements() here to declare subsystem dependencies.
}

// Called when the command is initially scheduled.
@Override
public void initialize() {}

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {}

// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {}

// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
1 change: 1 addition & 0 deletions src/main/java/frc/robot/constants/CAN.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class CAN {
public static final String rioCanbus = "rio";
public static String driveBaseCanbus = "drivebase";

public static final int CANdle = 8; // LED
public static final int pigeon = 9;

public static final int frontLeftCanCoder = 10;
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/frc/robot/constants/LED.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.constants;

import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.util.Color8Bit;

/** Add your docs here. */
public class LED {
public static final int LEDcount = 34; // TODO: change the amount od LEDs we have

public static final double LEDstripLength = Units.inchesToMeters(7); // meters

/** Different LED animation types */
public enum ANIMATION_TYPE {
ColorFlow,
ColorFlowLong,
Fire,
Larson,
Rainbow,
RgbFade,
SingleFade,
Strobe,
Twinkle,
TwinkleOff,
Solid
}

// These color are channels passed in the setPattern() method in the LED subsystem
public static final Color8Bit red = new Color8Bit(255, 0, 0); // Disabled
public static final Color8Bit orange = new Color8Bit(247, 116, 40);
public static final Color8Bit yellow = new Color8Bit(150, 120, 0); // Coral Mode
public static final Color8Bit green = new Color8Bit(0, 255, 0); // Enabled
public static final Color8Bit cyan = new Color8Bit(24, 94, 89); // Algae Mode
public static final Color8Bit blue = new Color8Bit(0, 0, 255); // Lined up for Reef
public static final Color8Bit purple = new Color8Bit(128, 0, 128);
public static final Color8Bit pink = new Color8Bit(190, 30, 35);
public static final Color8Bit white = new Color8Bit(125, 125, 125);

// They were in a different, probably more logical order, but I wanted them in rainbow order
// instead.

public enum SUBSYSTEM_STATES {
DISABLED, //
ENABLED, //
INTAKING, //
CORAL,
CORAL_OWNED,
ALGAE,
ALGAE_OWNED,
REEF_LINEUP,
ENDGAME;
}
}
167 changes: 166 additions & 1 deletion src/main/java/frc/robot/subsystems/LEDSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,176 @@

package frc.robot.subsystems;

import com.ctre.phoenix.led.Animation;
import com.ctre.phoenix.led.CANdle;
import com.ctre.phoenix.led.CANdle.LEDStripType;
import com.ctre.phoenix.led.CANdle.VBatOutputMode;
import com.ctre.phoenix.led.CANdleConfiguration;
import com.ctre.phoenix.led.CANdleStatusFrame;
import com.ctre.phoenix.led.ColorFlowAnimation;
import com.ctre.phoenix.led.ColorFlowAnimation.Direction;
import com.ctre.phoenix.led.FireAnimation;
import com.ctre.phoenix.led.LarsonAnimation;
import com.ctre.phoenix.led.LarsonAnimation.BounceMode;
import com.ctre.phoenix.led.RainbowAnimation;
import com.ctre.phoenix.led.RgbFadeAnimation;
import com.ctre.phoenix.led.SingleFadeAnimation;
import com.ctre.phoenix.led.StrobeAnimation;
import com.ctre.phoenix.led.TwinkleAnimation;
import com.ctre.phoenix.led.TwinkleAnimation.TwinklePercent;
import com.ctre.phoenix.led.TwinkleOffAnimation;
import com.ctre.phoenix.led.TwinkleOffAnimation.TwinkleOffPercent;
import edu.wpi.first.wpilibj.util.Color8Bit;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.CAN;
import frc.robot.constants.LED;
import frc.robot.constants.LED.*;

public class LEDSubsystem extends SubsystemBase {
/** Creates a new LEDSubsystem. */
public LEDSubsystem() {}
private final CANdle m_candle = new CANdle(CAN.CANdle);

private Color8Bit m_color = new Color8Bit();
private int m_red = 0;
private int m_green = 0; // setting all LED colors to none: there is no color when robot activates
private int m_blue = 0;
private int m_white = 0; // we have a seperate bulb in our LED strips for white
private double m_brightness = 0;
private double m_speed = 0;
private SUBSYSTEM_STATES currentRobotState = SUBSYSTEM_STATES.DISABLED;
private boolean setSolid; // this stops the animation
private Animation m_toAnimate = null;

public LEDSubsystem() {
m_candle.configFactoryDefault();
// sets up LED strip
CANdleConfiguration configAll = new CANdleConfiguration();
configAll.statusLedOffWhenActive = true; // sets lights of when the LEDs are activated
configAll.disableWhenLOS = false; // disables LEDs when there is no signal for control
configAll.stripType = LEDStripType.GRB;
configAll.brightnessScalar =
0.5; // 1 is highest we can go we don't want to blind everyone at the event
configAll.vBatOutputMode = VBatOutputMode.Modulated; // Modulate
m_candle.configAllSettings(configAll, 100);
m_candle.setStatusFramePeriod(CANdleStatusFrame.CANdleStatusFrame_Status_1_General, 255);
m_candle.setStatusFramePeriod(CANdleStatusFrame.CANdleStatusFrame_Status_2_Startup, 255);
m_candle.setStatusFramePeriod(
CANdleStatusFrame.CANdleStatusFrame_Status_3_FirmwareApiStatus, 255);
m_candle.setStatusFramePeriod(CANdleStatusFrame.CANdleStatusFrame_Status_4_ControlTelem, 255);
m_candle.setStatusFramePeriod(
CANdleStatusFrame.CANdleStatusFrame_Status_5_PixelPulseTrain, 255);
m_candle.setStatusFramePeriod(CANdleStatusFrame.CANdleStatusFrame_Status_6_BottomPixels, 255);
m_candle.setStatusFramePeriod(CANdleStatusFrame.CANdleStatusFrame_Status_7_TopPixels, 255);
}

// will create LED patterns
public void setPattern(Color8Bit color, int white, double speed, ANIMATION_TYPE toChange) {
m_color = color;
m_red = color.red;
m_green = color.green;
m_blue = color.blue;
m_white = white;
m_speed = speed;
switch (toChange) {
case ColorFlow: // stripe of color flowing through the LED strip
m_toAnimate =
new ColorFlowAnimation(
m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount, Direction.Forward);
break;
case ColorFlowLong: // stripe of color flowing through the LED strip
m_toAnimate =
new ColorFlowAnimation(
m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount + 15, Direction.Forward);
break;

case Fire: // red and orange LEDs flaming up and down the LED strip
m_brightness = 0.5;
m_speed = 0.7;
m_toAnimate = new FireAnimation(m_brightness, m_speed, LED.LEDcount, 0.7, 0.5);
break;
case Larson: // a line bouncing back and forth with its width determined by size
m_toAnimate =
new LarsonAnimation(
m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount, BounceMode.Front, 7);
break;
case Rainbow: // neon cat type beat
m_brightness = 1;
m_toAnimate = new RainbowAnimation(m_brightness, m_speed, LED.LEDcount);
break;
case RgbFade: // cycling between red, greed, and blue
m_brightness = 1;
m_toAnimate = new RgbFadeAnimation(m_brightness, m_speed, LED.LEDcount);
break;
case SingleFade: // slowly turn all LEDs from solid color to off
m_toAnimate =
new SingleFadeAnimation(m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount);
break;
case Strobe: // switching between solid color and full off at high speed
m_toAnimate = new StrobeAnimation(m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount);
break;
case Twinkle: // random LEDs turning on and off with certain color
m_toAnimate =
new TwinkleAnimation(
m_red, m_green, m_blue, m_white, m_speed, LED.LEDcount, TwinklePercent.Percent6);
break;
case TwinkleOff: // twinkle in reverse
m_toAnimate =
new TwinkleOffAnimation(
m_red,
m_green,
m_blue,
m_white,
m_speed,
LED.LEDcount,
TwinkleOffPercent.Percent100);
break;
case Solid:
m_toAnimate = null;
break;
default:
// System.out.println("Incorrect animation type provided to changeAnimation()
// method");
break;
}
}

// will set LEDs a coordinated color for an action
public void expressState(SUBSYSTEM_STATES state) {
if (state != currentRobotState) {
switch (state) {
case ENABLED:
setPattern(LED.green, 0, 0, ANIMATION_TYPE.Solid); // Solid Green
break;
case DISABLED:
setPattern(LED.red, 0, 0, ANIMATION_TYPE.Solid); // Solid Red
break;
case INTAKING:
setPattern(LED.pink, 0, 0, ANIMATION_TYPE.Strobe); // Unknown
break;
case CORAL:
setPattern(LED.yellow, 0, 0, ANIMATION_TYPE.Solid); // Solid Yellow
break;
case CORAL_OWNED:
setPattern(LED.yellow, 0, 0, ANIMATION_TYPE.ColorFlow); // Flashing Yellow
break;
case ALGAE:
setPattern(LED.cyan, 0, 0, ANIMATION_TYPE.Solid); // Solid Cyan
break;
case ALGAE_OWNED:
setPattern(LED.cyan, 0, 0, ANIMATION_TYPE.Rainbow); // Flashing Cyan
break;
case REEF_LINEUP:
setPattern(LED.blue, 0, 0, ANIMATION_TYPE.Solid); // Solid Blue
break;
case ENDGAME:
setPattern(LED.white, 0, 0, ANIMATION_TYPE.Rainbow); // Rainbow
break;
default:
break;
}
currentRobotState = state;
}
}

@Override
public void periodic() {
Expand Down
Loading

0 comments on commit 3fb4d93

Please sign in to comment.