View Single Post
  #2   Spotlight this post!  
Unread 20-01-2014, 10:30
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 102
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Help using Network Tables with Command Base Bot

Hello:

I'm not entirely sure how you want to interact with your command based robot. The RobotComm class (at the end of this post) provides an example of firing off a command from a stand alone Java application running on the Driver Station.

Using this class involves some coordination on both the robot and your stand-lone Java program. For example, on the robot, if you add something like the following (NOTE: This should also add a button to your Smart Dashboard that you can click on to verify the command works):

Code:
// Install command button on dashboard to fire ball
SmartDashboard.putData("Fire Ball", new FireBallCommand());
You should then be able to start the "FireBallCommand()" from your stand alone Java application running on the driver station using something like the following:

Code:
class MyApp {
  private RobotComm robotComm;

  public MyApp(String ip) {
     robotComm = new RobotComm(ip);
  }

  public void run() {
     // Wait for target to go hot (you'll need to implement the decision)
     // waitForHotDetect();

     // Now that target is detected, fire the ball
     robotComm.startSmartDashboardCommand("Fire Ball");

     // Don't immediately exit the program as it may take a moment for the network tables to sync up.
     robotComm.sleepMillis(500);
  }

  public void static main(String[] args) {
     MyApp app = new MyApp((args.length == 1) ? args[0] : "10.8.68.2");
     app.run();
  }
}
In addition to firing off commands, you can also get/set values via the network tables. For example, if you had a stand alone application that just wanted to report the state of the left and right goals, you might add a method like the following:

Code:
public void reportGoalState(boolean leftHot, boolean rightHot) {
  NetworkTable sd = robotComm.getSmartDashboard();
  sd.putBoolean("Left Hot", leftHot);
  sd.putBoolean("Right Hot", rightHot);
}
The code running on the robot could then looks use something like the following to check: SmartDashboard.getBoolean("Left Hot", false);

Here is the source code for the RobotComm class used in the stand alone Java application running on the Driver Station. NOTE: To use this class in your Java projects, you will need to add the networktables-desktop.jar file found in the "sunspotfrcsdk/desktop-lib" folder under your home directory (this is placed here by the FRC Netbeans Java plug-ins).

Code:
/*
 * Copyright (c) 2013, pkb
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.techhounds.comm;

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

/**
 * A helper class to demonstrate how to use the {@link NetworkTable} class in a
 * stand alone Java application to communicate with the robot.
 *
 * @author Paul Blankenbaker
 */
public final class RobotComm {

    /**
     * Flag set if we happen to be interrupted.
     */
    private boolean _Interrupted;

    /**
     * Used when you want to communicate using the SmartDashboard fields.
     */
    private NetworkTable _SmartDashboard;

    /**
     * The key to use to control a smart dashboard command.
     */
    private static final String ENABLE_COMMAND_KEY = "running";

    /**
     * Constructs a new instance of the object associated with a specific IP
     * address.
     *
     * @param ipAddr The IP address of the cRIO unit on your robot (10.xx.xx.2
     * where xx.xx comes from your team number).
     */
    public RobotComm(String ipAddr) {
        _Interrupted = false;
        NetworkTable.setClientMode();
        NetworkTable.setIPAddress(ipAddr);
        _SmartDashboard = NetworkTable.getTable("SmartDashboard");
    }

    /**
     * Returns access to the NetworkTable associated with the SmartDashboard
     * values.
     *
     * <p>
     * You can use this table to set/get values within the SmartDashboard. NOTE:
     * Use the TableViewer utility to examine the keys/values currently
     * available from your robot. Look for the TableViewer JAR file to launch
     * this tool in your sunspotfrcsdk/tools directory.</p>
     *
     * @return The NetworkTable where the SmartDashboard holds it key/value
     * pairs.
     */
    public NetworkTable getSmartDashboard() {
        return _SmartDashboard;
    }

    /**
     * Starts a SmartDashboard command.
     *
     * <p>
     * This method attempts to start a smart dashboard command (as if user press
     * the button on the smart dashboard).</p>
     *
     * @param key The name of the SmartDashboard command to start (typically the
     * label that appears on the button on the smart dashboard).
     */
    public void startSmartDashboardCommand(String key) {
        getCommand(key).putBoolean(ENABLE_COMMAND_KEY, true);
    }

    /**
     * Cancels (interrupts) a SmartDashboard command.
     *
     * <p>
     * This method attempts to start a smart dashboard command (as if user press
     * the button in the cancel state on the smart dashboard).</p>
     *
     * @param key The name of the SmartDashboard command to start (typically the
     * label that appears on the button on the smart dashboard).
     */
    public void cancelSmartDashboardCommand(String key) {
        getCommand(key).putBoolean(ENABLE_COMMAND_KEY, false);
    }

    /**
     * Helper method to get the table of values associated with with a
     * SmartDashboard command.
     *
     * @param key The key (name) associated with the command.
     *
     * @return The table of values associated with the key.
     */
    private ITable getCommand(String key) {
        return getSmartDashboard().getSubTable(key);
    }

    /**
     * Returns true if we were ever interrupted while sleeping.
     *
     * @return true if we've been interrupted, false if not.
     */
    public boolean interrupted() {
        return _Interrupted;
    }

    /**
     * Sleeps for the specified amount of time (unless interrupted).
     *
     * @param millis Number of milliseconds to sleep/delay. Note, we may sleep
     * less than this if the thread is interrupted.
     */
    public void sleepMillis(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            _Interrupted = true;
        }
    }

}
Hope that helps,
Paul
Reply With Quote