Translate

Asyncio is Coming to PyMata!

The next version of PyMata, scheduled for release later this summer, incorporates the Python 3 asyncio library. "Why?", you might. ask. Let's compare results of running two similar projects that control a servo motor

Both tests were run using Python 3.4.3 on Linux. The CPU used in the tests is an Intel® Core™ i5-2500K CPU running at 3.30GHz.

First, here is the PyMata 2.08 project code:

And now the system monitor results:

Application code for the new release:
Its system monitor results:


CPU utilization went down from 28% to 6% !


Comparing the code, you might be wondering why there is no import of the asyncio library. There are 3 APIs  included with the new release.

  • pymata_core
    • This is a pure asyncio API for those that wish to code directly in asyncio. It is a method based API.
  • pymata3
    • This is a "pymata plug-in api" that acts as a proxy for pymata_core. It shields the user from having to deal with asyncio. This is the API used for the sample code above. It is method based.
  • pymata_iot
    • This API provides an Autobahn based Websocket interface. It too is a "pymata plug-in" API that uses JSON messaging between client and server.
    • Here is a screen shot of a web application taking advantage of this API:

    • This rather busy screen shot shows a page that allows control of the digital inputs and the PyMata data latching feature. Pin 12 and 13 are enabled for reporting. Pin 12 is currently low and pin 13 is high. A latch was set for pin 12 that was triggered on 27 June 2015 at 13:27:24. Pin 13 also has a pending latch to fire when it goes low and then high again.
    • Here is a sample of the JavaScript code used in this screen shot for setting pin mode:


    • This code uses the browser's built-in Websocket client and therefore JavaScript was used. You can easily communicate using a Python Websocket client as well. First here is the JSON message template


    • And the Python code to set the pin mode via Websocket:


Here is a summary of the features of the new release, its current status and a "to-do" list.


Major Release Features

  • Utilizes the Python asyncio library for concurrency implementation.
  • Full implementation of the StandardFirmata version 2.4.3 protocol.
  • Continued support for FirmataPlus protocol(Sonar, Stepper Motor, Tone, and Encoder support).
  • 3 APIs provided for application flexibility.
  • Auto-detection of the Arduino COM port.
  • Built-in Control-C detection.

Current Status

  • pymata_core API is code complete and tested.
  • pymata3 API is code complete and tested.
  • pymata_iot is 70% complete and tested.

To Be Done

  • Finish pymata_iot.
  • Code cleanup.
  • Documentation.