Skip to content
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
23 changes: 11 additions & 12 deletions source/docs/software/basic-programming/alliancecolor.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Get Alliance Color

The ``DriverStation`` class ([Java](https://github.wpilib.org/allwpilib/docs/beta/java/org/wpilib/driverstation/DriverStation.html), [C++](https://github.wpilib.org/allwpilib/docs/beta/cpp/classwpi_1_1_driver_station.html), :py:class:`Python <robotpy:wpilib.DriverStation>`) has many useful features for getting data from the Driver Station computer. One of the most important features is ``getAlliance`` (Java & Python) / ``GetAlliance`` (C++).
The ``MatchState`` class ([Java](https://github.wpilib.org/allwpilib/docs/beta/java/org/wpilib/driverstation/MatchState.html), [C++](https://github.wpilib.org/allwpilib/docs/beta/cpp/classwpi_1_1_match_state.html), :py:class:`Python <robotpy:wpilib.MatchState>`) has many useful features for getting data from the Driver Station computer. One of the most important features is ``getAlliance`` (Java) / ``GetAlliance`` (C++) / ``get_alliance`` (Python).

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, 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 :ref:`"Team Station" on the Operation Tab <docs/software/driverstation/driver-station:Operation Tab>`).

Expand All @@ -9,12 +9,12 @@ Note that there are three cases: red, blue, and no color yet. It is important t
.. tab-set-code::

```java
Optional<Alliance> ally = DriverStation.getAlliance();
Optional<Alliance> ally = MatchState.getAlliance();
if (ally.isPresent()) {
if (ally.get() == Alliance.Red) {
if (ally.get() == Alliance.RED) {
<RED ACTION>
}
if (ally.get() == Alliance.Blue) {
if (ally.get() == Alliance.BLUE) {
<BLUE ACTION>
}
}
Expand All @@ -24,12 +24,11 @@ Note that there are three cases: red, blue, and no color yet. It is important t
```

```c++
using wpi::DriverStation::Alliance;
if (auto ally = wpi::DriverStation::GetAlliance()) {
if (ally.value() == Alliance::kRed) {
if (auto ally = wpi::MatchState::GetAlliance()) {
if (ally.value() == wpi::Alliance::RED) {
<RED ACTION>
}
if (ally.value() == Alliance::kBlue) {
if (ally.value() == wpi::Alliance::BLUE) {
<BLUE ACTION>
}
}
Expand All @@ -39,12 +38,12 @@ Note that there are three cases: red, blue, and no color yet. It is important t
```

```Python
from wpilib import DriverStation
ally = DriverStation.get_alliance()
from wpilib import MatchState
ally = MatchState.get_alliance()
if ally is not None:
if ally == DriverStation.Alliance.kRed:
if ally == MatchState.Alliance.RED:
<RED ACTION>
elif ally == DriverStation.Alliance.kBlue:
elif ally == MatchState.Alliance.BLUE:
<BLUE ACTION>
else:
<NO COLOR YET ACTION>
Expand Down
Loading
Loading