|
ArmarX provides python bindings to conveniently access interfaces via Python.
If you did not install ArmarXCore and are using it from source, you have to register the ArmarX python module:
cd ${ArmarXCore_DIR}/../etc/python python setup.py develop --user
First, you need to load the interface defintion file. These are located in each package in the source/${PackageName}/interface folder. To load and compile these files for python you can use the load_armarx_slice
function from the module armarx.interface_helper
. The function expects the package name as the first paramter and a relative path to the interface folder (to the path given above) as the second parameter. Afterwards, the interface module is available and needs to be imported. An example looks like this:
from armarx.interface_helper import load_armarx_slice load_armarx_slice('VisionX', 'core/ImageProviderInterface.ice') from visionx import ImageProviderInterfacePrx
All ArmarX slice interfaces can be found here: armarx_documentation: Slice Documentation
This is a simple example to control the platform. The proxy to the PlatformUnit is obtained by calling get_proxy from the interface_helper module. In the next step, the platform is moved to postion 3000, 8000 with respect to the world coordinate system.
#!/usr/bin/env python from armarx.interface_helper import load_armarx_slice, get_proxy load_armarx_slice('RobotAPI', 'units/PlatformUnitInterface.ice') from armarx import PlatformUnitInterfacePrx def main(): platform_unit = get_proxy(PlatformUnitInterfacePrx, 'Armar3PlatformUnit') platform_unit.moveTo(3000, 8000, 0, 10.0, 0.1) if __name__ == '__main__': main()
The python interface can also be used to register a class to a topic. In this example the 'ExampleListenerInterface' is implemented and the function on_example_event is registered to the topic 'ExampleTopicName'
First, an instance of the class is created by
ice_object = ExampleListener()
. The instance is then registered to and connected to the topic via
proxy = register_object(ice_object, 'ExampleListener') topic = using_topic(proxy, 'ExampleTopicName')
The function on_example_event
is triggered now automatically by the middleware on each event. Please do not forget to unsubscribe from the topic via:
topic.unsubscribe(proxy)
The full example is as follows.
#!/usr/bin/env python import time import logging from armarx.interface_helper import load_armarx_slice, ice_helper from armarx.interface_helper import get_proxy, register_object, using_topic load_armarx_slice('YourArmarXPackageName', 'component/ExampleInterface.ice') from armarx import ExampleListenerInterface logger = logging.getLogger(__name__) class ExampleListener(ExampleListenerInterface): def __init__(self): super(ExampleListenerInterface, self).__init__() def on_example_event(self, current=None): logger.info('called') def main(): ice_object = ExampleListener() proxy = register_object(ice_object, 'ExampleListener') topic = using_topic(proxy, 'ExampleTopicName') try: while not ice_helper.iceCommunicator.isShutdown(): logger.debug('still alive') time.sleep(1) except KeyboardInterrupt: logger.info('shutting down') topic.unsubscribe(proxy) if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) main()