Evolution of a Blog

This blog has evolved as I have as a maker. It starts at the beginning of my journey where I began to re-tread my tires in the useful lore of micro electronics and the open-source software that can drive them. While building solutions around micro-electronics are still an occasional topic my more recent focus has been on the 3D Printing side of making.

Thursday, August 23, 2012

Python to Arduino Interface - Part 1

Once I started thinking about my project I realized that I was going to need to find or develop an interface library to connect the RPi running Python with the Arduino.  I found some samples on the web but not the complete library that I was looking for so decided to write my own.

I envisioned something that would be reasonably bullet proof, support the 'standard' Arduino functions while leaving room for custom expansion, use a minimum of time for communications (e.g. small command/response size), and have an optional mode of operation for debugging.

The library that I developed (Py2Ard Interface Library) is available with no express or implied warranty if you wish to experiment with it yourself.

The interface is driven from the Python side so the Arduino loop basically waits for serial input.  Handshake is initiated by a "%" to which a "#" is sent in response.  The command is then sent terminated by a "@".  Results are returned to Python with a prefix of "0" for OK or "1" for error followed a return value (0 if not appropriate) and then a narrative response (if debug is on):

    '%' -->                                from Python to Arduino
    <-- '#'                                from Arduino to Python
    'DR13@' -->                            from Python to Arduino
    <-- '0,1,Digital read from pin 13'     from Arduino to Python

Commands that provide two arguments would have a comma separating the first and second argument (e.g. DW13,20).

The standard commands implemented in the first version of this library are shown below:

    (P)in mode commands
    (D)igital commands
    digitalWrite(pin, value)

    (A)nalog commands
    analogWrite(pin, value)
    pwmPulseLow(pin, trigger)

    (S)ervo commands
    attachServo(servo, pin)
    commandServo(servo, command)

    (H)ousekeeping and debugging commands

The custom commands implemented in the first version of this library are shown below (my Robot needed a compass and that requires a wire interface):

    (C)ompass commands

A sample of how this library is used will be in the next post on this topic.   Part 2 of this post covers more about the library.

No comments:

Post a Comment