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

I haven’t tried it, but seems like the getDouble() and the other get methods is what you’re looking for.

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:

// 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):

// 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!

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.

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?

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! :wink:

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.

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:


NetworkTable.getTable("SmartDashboard").putDouble("Pi", 3.14159);

Robot side:


double pi = NetworkTable.getTable("SmartDashboard").getDouble("Pi");

Slightly offtopic here; this is aimed towards Ginto8.

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.