Get Alliance Color

The DriverStation class (Java, C++, Python) has many useful features for getting data from the Driver Station computer. One of the most important features is getAlliance (Java & Python) / GetAlliance (C++).

Getting your Alliance Color and Doing an Action

Optional<Alliance> ally = DriverStation.getAlliance();
if (ally.isPresent()) {
    if (ally.get() == Alliance.Red) {
        <RED ACTION>
    }
    if (ally.Get() == Alliance.Blue) {
        <BLUE ACTION>
    }
}
else {
    <NO COLOR YET ACTION>
}
using frc::DriverStation::Alliance;
if (auto ally = frc::DriverStation::GetAlliance()) {
    if (ally.value() == Alliance::kRed) {
        <RED ACTION>
    }
    if (ally.value() == Alliance::kBlue) {
        <BLUE ACTION>
    }
}
else {
    <NO COLOR YET ACTION>
}
from wpilib import DriverStation

self.ally = DriverStation.getAlliance()
if self.ally:
    if self.ally.value() == DriverStation.Alliance.kRed:
        <RED ACTION>
    if self.ally.value() == DriverStation.Alliance.kBlue:
        <BLUE ACTION>
else:
    <NO COLOR YET ACTION>