Reading Data on YARP with Python

YARP is another robot development platform, similar to ROS. I had to code up a simple data reader in Python (operating over YARP ports) and couldn’t find any good examples. After some experimenting, I found a solution that worked for me. The following is a simple code snippet for other YARP Python newbies:

class ExampleReader:
    def __init__(self):
        #create a new input port and open it
        self.in_port = yarp.BufferedPortBottle()
        self.in_port.open("/example/data:i")

        #connect up the output port to our input port
        yarp.Network.connect("/example/data:o", "/example/data:i")

        return

    def getData(self):
        #in this example, I assume the data is a single integer
        #we use read() where the parameter determines if it is 
        #blocking (True) or not.

        btl = self.in_port.read(True)

        my_data = btl.get(0).asInt()

        #if you have doubles, you can use asDouble()
        #or strings can be obtained using asString() 

        return my_data
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: