Thread: Network Tables?
View Single Post
  #9   Spotlight this post!  
Unread 03-29-2015, 12:00 AM
gegozi gegozi is offline
Registered User
no team
 
Join Date: Apr 2014
Rookie Year: 2012
Location: Bellaire, TX
Posts: 44
gegozi is an unknown quantity at this point
Re: Network Tables?

Quote:
Originally Posted by charrisTTI View Post
NetworkTable object can be used to pass data to/from robot code and another program running on another computer on the network. SmartDashboard uses it to pass data to/from robot.

We use it to pass data from image processing software running on the driver station back to the robot.

Here is example code:

Code:
//========================================
//Robot Code
// declare object
NetworkTable cameraTable;

// getinstance (table is a singleton, if one does not exist then one will
// be created otherwise returns reference to existing instance
cameraTable = NetworkTable.getTable("camera");

// get data element from the table
// default values returned if no data has been provided from another source yet

cameraTable.beginTransaction();
Boolean imageFound = cameraTable.getBoolean("found", false );
Double imageOffset = cameraTable.getDouble("offset", -1.0);
Double imageDistance = cameraTable.getDouble("distance", -1.0);
cameraTable.endTransaction();

System.out.println( "found = " + imageFound);
System.out.println( "offset = " + imageOffset);
System.out.println( "distance = " + imageDistance);

//=======================================
// Driver Station PC
// camera image processing is done in C# application on DS

// declare it
private NetworkTable table;

// Init NetworkTable
NetworkTable.setIPAddress("10.6.23.2");  // ip of crio
table = NetworkTable.getTable("camera");


// in the image processing loop

table.beginTransaction();
table.putBoolean("found", true);
table.putDouble("distance", distance);
table.putDouble("offset", offset);
table.endTransaction();
This doesn't work. I'm using eclipse on a windows 7 pc and the wpi lib.
Code:
package org.usfirst.frc.team2585.robot;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.networktables.NetworkTable;

public class VisionCalib implements ITableListener{
	private double xCoord;
	private double yCoord;
	private double distance;
	private NetworkTable nt;
	//private ITableListener table;
	/**
	 * 
	 */
	public VisionCalib() {
		nt = NetworkTable.getTable("SmartDashboard");
		nt.addTableListener(this);
	}
	
	private void update(){
		System.out.println("Hello");
		nt.beginTransaction();
		xCoord = nt.getNumber("COG_X", 1);
		yCoord = nt.getNumber("COG_Y", 0);
		distance = nt.getNumber("TL_TARGET_DISTANCE", 0);
		nt.endTransaction();
	}
	
	public void valueChanged(ITable itable, String key, Object obj, boolean isNew)
	{
		update();
	}
	public void setNums()
	{
		nt.putNumber("COG_X", 0.0);
		nt.putNumber("COG_Y", 0.0);
		nt.putNumber("TL_TARGET_DISTANCE", 0.0);
	}
	public double getDistance() {return distance;}
	public double getX() {return xCoord;}
	public double getY() {return yCoord;}
	public NetworkTable getNt() {
		return nt;
	}
	public void setNt(String table) {
		this.nt = NetworkTable.getTable(table);
	}
	@Override
	public String toString()
	{
		update();
		String s="";
		s+="xCoord = "+xCoord;
		s+="\n"+"yCoord = "+yCoord;
		s+="\n"+"Distance = "+distance;
		return s;
	}
}
Reply With Quote