NetworkTable addEntryListener

We are looking to implement an EntryListener that tracks the FMSInfo for the Alliance color. For some reason the following code works on the simulator but not on the real roboRio. Any ideas what the difference here is? Why doesn’t this work on the real roboRio?

@Override
public void robotInit() {
// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
m_robotContainer = new RobotContainer();

NetworkTableInstance inst = NetworkTableInstance.getDefault();
NetworkTable fmsInfoTable = inst.getTable("FMSInfo");

fmsInfoTable.addEntryListener("IsRedAlliance", (table, key, entry, value, flags) -> {
  var isRedAlliance = entry.getBoolean(false);
  System.out.format("INFO: Alliance Color: %s%n", (isRedAlliance ? "RED" : "BLUE"));
}, EntryListenerFlags.kNew | EntryListenerFlags.kUpdate);

}

1 Like

Could the problem be that the network table listener is only invoked on changes, and the way the drive station interacts with the roboRIO the “IsRedAlliance” entry doesn’t really change? It might have a value, the initial value, and that’s it? In that case, you should be able to just read it, instead of waiting for change. I think DriverStation.getAlliance() is another way to get the alliance info during for example start of autonomous.

2 Likes

My recommendation echos the above–just call DriverStation.getAlliance() directly. The FMSInfo NetworkTable is primarily there to facilitate dashboards/coprocessors getting that information; in robot code you can get the same info faster and easier directly from the DriverStation class (in fact, it’s the DriverStation class that’s setting those NetworkTable values). To answer your question, however, there are a couple of additional flags you need: kImmediate (so you get a callback even if the value is already set) and kLocal (because the value is being set local to robot code).

2 Likes

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