Reading Array Values Published by NetworkTables

This article describes how to read values published by NetworkTables using a program running on the robot. This is useful when using computer vision where the images are processed on your driver station laptop and the results stored into NetworkTables possibly using a separate vision processor like a raspberry pi, or a tool on the robot like a python program to do the image processing.

通常,这些值是针对一个或多个感兴趣区域(例如目标或游戏片段)的,并且会返回多个实例。在下面的示例中,图像处理器返回了多个x,y,宽度,高度和区域,并且机器人程序可以通过进一步的处理来找出返回的值中哪些是有趣的。

Verify the NetworkTables Topics Being Published

Image of OutlineViewer with the NetworkTables topics

You can verify the names of the NetworkTables topics used for publishing the values by using the Outline Viewer application. It is a C++ program in your user directory in the wpilib/<YEAR>/tools folder. The application is started by selecting the “WPILib” menu in Visual Studio Code then Start Tool then “OutlineViewer”. In this example, with the image processing program running (GRIP) you can see the values being put into NetworkTables.

In this case the values are stored in a table called GRIP and a sub-table called myContoursReport. You can see that the values are in brackets and there are 2 values in this case for each topic. The NetworkTables topic names are centerX, centerY, area, height and width.

下面的两个示例都是高度简化的程序,仅说明了网络表的用法。所有代码都在robotInit()方法中,因此仅在程序启动时运行。在您的程序中,这些值的获取大多会在比赛的自动阶段或手动阶段,使机器人瞄向一个方向的指令或控制循环中进行。

Writing a Program to Access the Topics

DoubleArraySubscriber areasSub;

@Override
public void robotInit() {
  NetworkTable table = NetworkTableInstance.getDefault().getTable("GRIP/mycontoursReport");
  areasSub = table.getDoubleArrayTopic("area").subscribe(new double[] {});
}

@Override
public void teleopPeriodic() {
    double[] areas = areasSub.get();

    System.out.print("areas: " );

    for (double area : areas) {
      System.out.print(area + " ");
    }

    System.out.println();
}
nt::DoubleArraySubscriber areasSub;

void Robot::RobotInit() override {
  auto table = nt::NetworkTableInstance::GetDefault().GetTable("GRIP/myContoursReport");
  areasSub = table->GetDoubleArrayTopic("area").Subscribe({});
}

void Robot::TeleopPeriodic() override {
  std::cout << "Areas: ";

  std::vector<double> arr = areasSub.Get();

  for (double val : arr) {
    std::cout << val << " ";
  }

  std::cout << std::endl;
}
def robotInit(self):
    table = ntcore.NetworkTableInstance.getDefault().getTable("GRIP/mycontoursReport")
    self.areasSub = table.getDoubleArrayTopic("area").subscribe([])

def teleopPeriodic(self):
    areas = self.areasSub.get()
    print("Areas:", areas)

获取值并在此程序中打印它们的步骤是:

  1. 声明表变量,该变量将保存具有值的子表的实例。

  2. 初始化子表实例,以便以后可用于检索值。

  3. Read the array of values from NetworkTables. In the case of a communicating programs, it’s possible that the program producing the output being read here might not yet be available when the robot program starts up. To avoid issues of the data not being ready, a default array of values is supplied. This default value will be returned if the NetworkTables topic hasn’t yet been published. This code will loop over the value of areas every 20ms.

Program Output

Image of Riolog showing the values

在这种情况下,程序仅查看关于areas数组,但在实际示例中,更可能所有值都会被使用。使用VS Code中的Riolog或Driver Station日志,您可以看到被检索的值。该程序使用的是示例静态图像,因此区域数值不会改变。但是您可以想象在机器人上安装摄像头后,值会不断变化。