Go to Post "Fair and professional" needs to be two-directional. - John Neun [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
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 24-11-2012, 18:13
Michael_Lee Michael_Lee is offline
Registered User
FRC #2976
 
Join Date: Jan 2012
Location: Issaquah, WA
Posts: 21
Michael_Lee is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 26-11-2012, 02:02
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,040
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #3   Spotlight this post!  
Unread 17-12-2012, 18:06
Michael_Lee Michael_Lee is offline
Registered User
FRC #2976
 
Join Date: Jan 2012
Location: Issaquah, WA
Posts: 21
Michael_Lee is an unknown quantity at this point
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:
  • 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:

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'
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:

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()
The code on the robot (in C++):

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();
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?
Reply With Quote
  #4   Spotlight this post!  
Unread 17-12-2012, 18:16
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,040
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff

Last edited by virtuald : 17-12-2012 at 18:18.
Reply With Quote
  #5   Spotlight this post!  
Unread 18-01-2013, 14:29
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,040
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
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 19:53.

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