View Single Post
  #7   Spotlight this post!  
Unread 29-02-2016, 20:15
AWoL's Avatar
AWoL AWoL is offline
Lvl. 225 Dark Code Mage (Java Type)
AKA: Adam Wolnikowski
FRC #0225 (TechFire)
Team Role: Programmer
 
Join Date: Mar 2014
Rookie Year: 2014
Location: York, Pennsylvania
Posts: 116
AWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond reputeAWoL has a reputation beyond repute
Re: Network tables has already been initialized

Quote:
Originally Posted by pblankenbaker View Post
Here is an stand alone example program I have been using (run as a Java application, not as a Robot program) that connects to our network tables, monitors and then clears the "SmartDashboard" entries.

If you are running your program outside of a robot project, then maybe the way we do our set up would apply to you as well.

Code:
package com.techhounds.dashboard;

import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;

/**
 * A utility to delete (clear) the SmartDashboard keys from the NetworkTable.
 * 
 * @author pkb
 */
public class ClearDashboard implements ITableListener {
	
	/**
	 * Entry point into the application.
	 * 
	 * @param args You can specify the IP address of the NetworkTable server to connect to on the command line. 
	 * 
	 * @throws InterruptedException If there is a problem accessing the SmartDashboard table.
	 */
	public static void main(String args[]) throws InterruptedException {
		ClearDashboard cd = new ClearDashboard();
		String defaultIp = "10.8.68.2";
		//String defaultIp = "127.0.0.1";
		String ipAddr = (args.length > 0) ? args[0] : defaultIp;
		
		NetworkTable.setIPAddress(ipAddr);
		NetworkTable.setClientMode();
		NetworkTable.initialize();
		NetworkTable table = NetworkTable.getTable("SmartDashboard");
		table.addTableListener(cd);
		table.addSubTableListener(cd);
		table.putBoolean("Cleared", false);
		// Give a bit of time to allow network fetch of keys to show up
		Thread.sleep(1000);
		deleteTableKeys(table, "", true);
		// Give a bit of time for network operations to flush out
		Thread.sleep(500);
		
		// Put value back (for testing)
		// table.putBoolean("Cleared", true);
	}

	/**
	 * Helper method to recursively delete NetworkTable key/value pairs.
	 * 
	 * @param table The NetworkTable to clear key/value pairs from.
	 * @param indent The leading indent (like: "  ") for printing out info.
	 * @param deleteTable Should we delete the table after clearing its children.
	 */
	private static void deleteTableKeys(ITable table, String indent, boolean deleteTable) {
		System.out.println(indent + table);
		indent += "  ";
		
		// Delete key/value pairs from table
		for (String key : table.getKeys()) {
			Object value = table.getValue(key, null);
			System.out.println(indent + key + " : " + value);
			table.delete(key);
		}
		
		// Process any sub-tables
		for (String key : table.getSubTables()) {
			ITable subTable = table.getSubTable(key);
			System.out.println(indent + key + " {");
			// Is it save to delete table entries (and if so should we delete children first?)
			deleteTableKeys(subTable, indent, true);
			System.out.println(indent + "}");
			if (deleteTable) {
			  table.delete(key);			  
			}
		}
	}

	/**
	 * Let's us know as values come in.
	 */
	@Override
	public void valueChanged(ITable table, String key, Object value, boolean flag) {
		System.out.println("table: " + table + "  key:" + key + "  value: " + value + "  flag: " + flag);
	}

}
Hope that helps.
I'm also trying to make a NetworkTables desktop client for a SmartDashboard vision widget, and I'm doing the exact same initialization steps that you are, in the same order, but I'm still getting a "Network tables has already been initialized" error. Any idea on what this might be?

EDIT: So it turns out that the source of the error is NetworkTable.setIPAddress(), whether it's called before or after NetworkTable.getTable().
__________________
2016 Competition Results (Co-captain, Driver, and Lead Programmer; Junior)
Springside-Chestnut Hill District Event - WINNER / #2 Seed, #1 Seed's First Pick / Gracious Professionalism Award
Westtown District Event - WINNER / #1 Seed / Industrial Design Award
MAR District Championship - WINNER / #1 Seed / Industrial Design Award / Dean's List Finalist Award (Me)
World Championship, Carson Subdivision - QUARTERFINALIST / #3 Seed, #2 Seed's First Pick
Indiana Robotics Invitational - FINALIST / #14 Seed, #2 Seed's Second Pick

Last edited by AWoL : 29-02-2016 at 20:36. Reason: More Info
Reply With Quote