View Full Version : Communicating with the robot via Python?
Michael_Lee
24-11-2012, 18:13
Is there a way to communicate with the robot using Python?
I saw that you could use the NetworkTable.jar from the SmartDashboard project to talk to the robot, and I was wondering if there was a way to use NetworkTables in Python from the laptop. Or is there an alternate method I should try?
virtuald
26-11-2012, 02:02
RobotPy has an implementation of NetworkTables in python, which works on a normal computer running Python 3.x . You should be able to use it to communicate with the cRio.
Michael_Lee
17-12-2012, 18:06
But how? I first tried copying the contents of the site-packages folder from robotpy so my client-side folder structure looked like this:
my_code.py
robotpy
wpilib
__init__.py
Buttons.py
Commands.py
core.py
NetworkTables.py
Preferences.py
SmartDashboard.py
_nivision.pyd
_vision.pyd
_vision2009.pyd
_wpilib.pyd
nivision.py
README
sip.pyd
vision.py
vision2009.py
This failed, and gave the following stack trace:
Traceback (most recent call last):
File ".\my_code.py", line 14, in <module>
main()
File ".\my_code.py", line 4, in main
table = robotpy.wpilib.NetworkTable.GetTable(tableName)
AttributeError: 'module' object has no attribute 'wpilib'
I'm assuming that I'm setting up robotpy on the client end incorrectly. Am I supposed to copy the contents to my local site-packages folder? Or does robotpy come bundled with a python interpreter? If so, where can I find it?
----
Also, although I wasn't able to test this, this is the code I came up with. I'm basically trying to send data from my laptop to the computer, and confirm I received it on the robot by mirroring it to the SmartDashboard. Do I understand how to use robotpy and NetworkTables correctly? Are there any changes I should make?
my_code.py:
import robotpy
def main(tablename = "testTable"):
table = robotpy.wpilib.NetworkTable.GetTable(tableName)
while True:
test_int = int(raw_input("Test int:"))
test_string = raw_input("Test string:")
table.BeginTransaction()
table.PutInt("testInt", test_int)
table.PutString("testString", test_string)
table.EndTransaction()
if __name__ == '__main__':
main()
The code on the robot (in C++):
// snip
void MainRobot::OperatorControl(void) {
NetworkTable *table = NetworkTable::GetTable("testTable");
SmartDashboard *s = SmartDashboard::GetInstance();
while (IsOperatorControl()) {
table->BeginTransaction();
int testInt = table->GetInt("testInt");
std::string testString = table->GetString("testString");
table->EndTransaction();
s->Log(testInt, "testInt");
s->Log(testString.c_str(), "testString");
}
}
----
Also, the C# sample code I was looking through gave this:
(from http://www.chiefdelphi.com/forums/showthread.php?t=103352)
// 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();
However, while looking through the robotpy source code, I was unable to find a "setIPAddress" method in the NetworkTable class. Do I need this method? If so, where is it?
virtuald
17-12-2012, 18:16
If I were doing what you were doing, I would just take the NetworkTables.py file only and copy it to where your script was, and import that. To do what you're doing, you don't need any of the rest of the site-packages contents -- some of that requires a functioning wpilib, which is a binary module compiled for the cRio. I do have a 'fake' wpilib that will run on a PC (which is still in flux, but works) that contains the NetworkTables in it (https://github.com/robotpy/fake-wpilib)... of course, it's the same file, so nothing different there.
However, looking more closely, I don't know if the RobotPy implementation supports being the client. It does provide the server implementation (which is on the robot itself), but it may not provide the client implementation. You could probably use it as a starting point for a client implementation however, if it isn't already implemented. I'd accept patches for that if you get it working.
Looking at the ConnectionManager, it appears to have a mechanism to not be the server. And you can create a connection manually.. so perhaps that would be what you need to do.
I've used the NetworkTables successfully on a PC to be the server side, but I haven't tried using it as the client. Seems like it might be possible though.
virtuald
18-01-2013, 14:29
Not sure how much work you completed on this, but we have python wrappers of the NetworkTables library working now on a PC. See http://www.chiefdelphi.com/forums/showthread.php?t=111530. There is/will be sample programs in the repo too.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.