Obtenir la couleur d’alliance

La classe DriverStation (Java, C++, Python) possède de nombreuses fonctionnalités utiles pour obtenir des données à partir de l’ordinateur de la station de pilotage. L’une des fonctionnalités les plus importantes est getAlliance (Java & Python) / GetAlliance (C++).

Note that there are three cases: red, blue, and no color yet. It is important that code handles the third case correctly because the alliance color will not be available until the Driver Station connects. In particular, code should not assume that the alliance color will be available during constructor methods or robotInit, but it should be available by the time autoInit or teleopInit is called. FMS will set the alliance color automatically; when not connected to FMS, the alliance color can be set from the Driver Station (see « Team Station » on the Operation Tab).

Obtenir votre couleur d’alliance et effectuer une 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

ally = DriverStation.getAlliance()
if ally is not None:
    if ally == DriverStation.Alliance.kRed:
        <RED ACTION>
    elif ally == DriverStation.Alliance.kBlue:
        <BLUE ACTION>
else:
    <NO COLOR YET ACTION>