|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Communicating with the robot via Python?
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? |
|
#2
|
||||
|
||||
|
Re: Communicating with the robot via Python?
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.
|
|
#3
|
|||
|
|||
|
Re: Communicating with the robot via Python?
But how? I first tried copying the contents of the site-packages folder from robotpy so my client-side folder structure looked like this:
This failed, and gave the following stack trace: Code:
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'
---- 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: Code:
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()
Code:
// 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/sh...d.php?t=103352) Code:
// 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();
|
|
#4
|
||||
|
||||
|
Re: Communicating with the robot via Python?
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. Last edited by virtuald : 17-12-2012 at 18:18. |
|
#5
|
||||
|
||||
|
Re: Communicating with the robot via Python?
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/sh...d.php?t=111530. There is/will be sample programs in the repo too.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|