Vision With GRIP

We have this code, but can’t figure out why it doesn’t work.
import wpilib
import networktables
from networktables import NetworkTable
class Vision():
def init(self):
# NetworkTable.setIPAddress(“10.26.5.73”)
# NetworkTable.setServerMode()
self.table = NetworkTable.getTable(‘GRIP/myContoursReport’)

def printCenter(self):
    self.centerX = networktables.NumberArray()
    self.center = self.table.retrieveValue("centerX", self.centerX)
    print("Center: " + str(self.center))

Please help.:confused:

->

NetworkTable.getTable("/GRIP/myContoursReport")

also, next time, use [noparse]


[/noparse] tags

We are suffering from the same problem with the exact same code. Here is a more verbose description of what is happening:


retrieveValue
    raise KeyError(name)
KeyError: '/GRIP/myContoursReport/centerX'
 
 
Locals at innermost frame:
 
{ 'entry': None,
  'externalData': ],
  'name': '/GRIP/myContoursReport/centerX',
  'self': <networktables2.server.NetworkTableServer object at 0x1027b6c18>}

Thanks!

The first thing that I would recommend is using table viewer to ensure that the value is actually available. And, even if it’s not available, the retrieveValue API doesn’t have functionality to set a default value, so you should do one of two things:

  • Set a default value when your program starts up
  • Surround the retrieveValue() call with a try…except block

Finally, I tested the functionality, and you can use pynetworktables to retrieve a NumberArray. However, the retrieveValue function returns None, not an array. Therefore, the code you have above won’t work – but remove the assignment, and it should be fine:


table = NetworkTable.getTable('/GRIP/myContoursReport')
na = NumberArray()

...

try:
    table.retrieveValue('centerX', na)
except KeyError:
    # do something else here...
else:
    print(na)

Oh, I didn’t see that quirk in the code (kids- always format your code in code blocks)

retrieveValue works like alot of C functions where it requests a pointer to put data into rather than returning the data itself (for reasons). The first argument is the name of the key (similar to how youd use getNumber(key,default)). The second argument isnt the argument but rather the variable that the return value will be placed into.

I now have the problem of connecting to GRIP. GRIP seems to be the server and in order to access the variables, you need to use the ClientMode. The problem with that is you need the IP of the server. GRIP is going to be running on our Driver Station and the IP will not stay the same.

How am I supposed to connect to GRIP?

Here is the working code(Just using GRIP locally by setting both to 127.0.0.1):


import sys
import time
from networktables import NetworkTable
import networktables
 
ip = "127.0.0.1"
 
NetworkTable.setIPAddress(ip)
NetworkTable.setClientMode()
NetworkTable.initialize()
 
table = NetworkTable.getTable('/GRIP/myContoursReport')
default = networktables.NumberArray()
 
while True:
    try:
        table.retrieveValue('centerX', default)
    except KeyError:
        pass
    else:
        print default
    time.sleep(1)

Thanks!

GRIP is a client. The server should be your roboRIO.

When I use Network Table Viewer to see the values, I need to click “Client” in order for it to work. “Server” never finds the table.

Correct, because the roboRio is the server. Clicking “Server” makes the Viewer its own server.