Getting Team color from the field

Hello.
I was trying to figure out how to get the team color from the field.
Since we re trying to use the color sensor to recognize the ball color and determine whether if it is our team color ball or the opposing team color ball.

I tried to dig through some documentation and api and I just couldn’t figure it out
send help

1 Like

DriverStation.getAlliance()

https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/DriverStation.html#getAlliance()

1 Like

DriverStation.getAlliance() (Java, C++) would do it.

1 Like

Here is the code we used this year. we just do it during disabled period and put it to a RobotContainer variable, so we don’t have to poll the FMS all the time.

	DriverStation.Alliance color;
	color = DriverStation.getAlliance();
	if(color == DriverStation.Alliance.Blue){
                     RobotContainer.isRedAlliance = false;
	}else {
                     RobotContainer.isRedAlliance = true;
	}

I would recommend reading the alliance color periodically. If you do it just once, if the DS wasn’t connected to the FMS you’ll be stuck with invalid or the wrong color alliance. We updated this every 1 second, so even in the case of a code reset (where disabledPeriodic() might not run), everything would be fine.

Here is what that code snippet would look like (goes in robotInit() in Robot.java):

// Update alliance color every second (just in case we lose comms or power).
addPeriodic(() -> robot_state_.setAlliance(DriverStation.getAlliance()), 1);
5 Likes

thx

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.