Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Getting values from SmartDashboard (http://www.chiefdelphi.com/forums/showthread.php?t=103108)

cooltext 17-02-2012 20:32

Getting values from SmartDashboard
 
how do I use the SmartDashboard to control values while the robot is running, so say I want to change a variable's value while the robot is running, I could type the new value into the SmartDashboard and the robot would read that new value?

thanks

neal 17-02-2012 23:04

Re: Getting values from SmartDashboard
 
I haven't tried it, but seems like the getDouble() and the other get methods is what you're looking for.

Ginto8 17-02-2012 23:31

Re: Getting values from SmartDashboard
 
Well, the SmartDasboard's entire communication interface is run via NetworkTables, so you have to work with those. On the SmartDashboard end, you probably want to make some sort of extension, with those options possibly as properties for the extension. Then, when these properties change, you change a value in a NetworkTable, then retrieve it on the robot end with a get<Type>(<key>).

That was a terribly generic description, so let's spice it up with an example! Let's say you have a SmartDashboard extension (what it does doesn't matter for the scope of this example), and it has a property called "speed". On the extension's end, you have 2 ways to do this, and each has a corresponding way of accessing it on the robot.
The first way:
Code:

// SmartDashboard end
NetworkTable table = Robot.getTable();
table.beginTransaction();
table.putDouble("speed",SpeedProperty.getValue()),
table.endTransaction();

// Robot end
double speed = NetworkTable.getTable("SmartDashboard").getDouble("speed");

Note that on the SmartDashboard end, you could replace Robot.getTable() with NetworkTable.getTable("SmartDashboard") and it would do the exact same thing.
Now for the second method (which is really the same thing, just put together slightly differently):
Code:

// SmartDashboard
NetworkTable table = NetworkTable.getTable("Super awesome extension of awesome coolness XTREME!"); // note the custom table name
table.beginTransaction();
table.putDouble("speed",SpeedProperty.getValue()),
table.endTransaction();

// Robot
double speed = NetworkTable.getTable("Super awesome extension of awesome coolness XTREME!").getDouble("speed"); // if the table name is the same on both ends, it will work as though it's really just one table.

Just keep in mind the basic rules of NetworkTables: surround all puts with begin/endTransaction bits, and don't expect instant transfer.

Happy SmartDashboarding!

Sunstroke 19-02-2012 00:26

Re: Getting values from SmartDashboard
 
Quote:

Originally Posted by Ginto8 (Post 1129219)
Just keep in mind the basic rules of NetworkTables: surround all puts with begin/endTransaction bits, and don't expect instant transfer.

Happy SmartDashboarding!

I'm excited someone understands the SmartDashboard and stuff so well, but I would like to correct you on this point.

It is actually worse if you surround things with begin and end transactions. If all you care about is sending one value, don't use a transaction.

If you want two or more values to be linked together, that is when you want to use a transaction.

F22Rapture 19-08-2012 11:41

Re: Getting values from SmartDashboard
 
I'm a bit of a newb at this, so if you don't mind me asking where do I need to put the code on the SmartDashboard end?

Ginto8 19-08-2012 18:02

Re: Getting values from SmartDashboard
 
Quote:

Originally Posted by Sunstroke (Post 1129970)
I'm excited someone understands the SmartDashboard and stuff so well, but I would like to correct you on this point.

It is actually worse if you surround things with begin and end transactions. If all you care about is sending one value, don't use a transaction.

If you want two or more values to be linked together, that is when you want to use a transaction.

Thanks for the correction! I wasn't entirely sure how the whole transaction situation was handled when I wrote that post, but now I understand it a bit better.

However, I'd like to point out that, in most situations, the overhead of the beginTransaction/endTransaction cycle would be mostly negligible. Transactions have a few extra steps (and a few more synchronized() calls), but unless you're doing an insane amount of transactions, the performance difference should be minimal - the main difference is the use of a NetworkQueue, which is just a specialized linked list.

That said, don't use transactions unless you're trying to send more than one entry at a time! ;)


Quote:

Originally Posted by F22Rapture (Post 1182162)
I'm a bit of a newb at this, so if you don't mind me asking where do I need to put the code on the SmartDashboard end?

You would put the code wherever you want to send or receive a new value - for example, if you have an extension that retrieves the state of the robot's drive train, you could have a TimerTask which periodically refreshes it, and the data retrieval would go in there.

I'm pretty sure that there's also some way to have the data automatically refreshed when it changes, but I haven't figured out how to use it yet - though I'm guessing it's possible by implementing NetworkListener or NetworkAdditionListener.

Djur 20-08-2012 22:00

Re: Getting values from SmartDashboard
 
To answer OP's question, you could call NetworkTable.putDouble() in a widget to send a number and then call NetworkTable.getDouble() in the robot's code to retrieve it.

Driver's side:
Code:

NetworkTable.getTable("SmartDashboard").putDouble("Pi", 3.14159);
Robot side:
Code:

double pi = NetworkTable.getTable("SmartDashboard").getDouble("Pi");
Slightly offtopic here; this is aimed towards Ginto8.
Quote:

Originally Posted by Ginto8 (Post 1182192)
You would put the code wherever you want to send or receive a new value - for example, if you have an extension that retrieves the state of the robot's drive train, you could have a TimerTask which periodically refreshes it, and the data retrieval would go in there.

I'm pretty sure that there's also some way to have the data automatically refreshed when it changes, but I haven't figured out how to use it yet - though I'm guessing it's possible by implementing NetworkListener or NetworkAdditionListener.

Don't bother with using a TimerTask; the new NetworkTables will use listeners to update values when they change in the table.

Ginto8 21-08-2012 03:11

Re: Getting values from SmartDashboard
 
Quote:

Originally Posted by Djur (Post 1182412)
Don't bother with using a TimerTask; the new NetworkTables will use listeners to update values when they change in the table.

I wasn't saying to update values in the table, I was saying to update values from the table. But Looking at the NetworkTable library, I realize that a NetworkListener would be a much better choice in this context.


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

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