Go to Post Mmmmm, First Choice day. I can already taste the latency. - ttldomination [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-19-2014, 04:15 PM
Abhi_4505 Abhi_4505 is offline
Registered User
FRC #4505
 
Join Date: Jan 2014
Location: Maryland
Posts: 6
Abhi_4505 is an unknown quantity at this point
Help using Network Tables with Command Base Bot

Hello Chief Delphi,
Our software team is trying to find out how to use Network Tables with our Command Base Bot. Any help would be appreciated.

Thank you,
Abhi at 4505
Reply With Quote
  #2   Spotlight this post!  
Unread 01-20-2014, 10:30 AM
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
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:03 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi