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.

Sunday, October 14, 2012

Python to Arduino Interface - Part 2

This is the second part of a topic introduced a while ago here but not finished until now.  Note that Part 3 of this series of posts, including an updated library, is now available here.

One of the files packaged with the Python to Arduino Interface download is a short sample illustrating basic functionality.   Here is that sample with some additional commentary and a screen shot of it in operation:

Import the library

    import time     
    from Py2Ard import interfaceClass

The only argument the class needs is the port on which to find the Arduino

    # Specify the port as an argument     
    my_board = interfaceClass('/dev/ttyUSB0')

The two options below are useful for debugging.   Setting TraceOn will result in both sides of the interface making a log of what they are asked to do.   On the Python side this means writing to Py2Ard.log.   On the Arduino side it means caching the last 50 commands and then dumping them on command to the Python side.    Setting DebugOn causes the Python side of the interface to echo what is being done and the Arduino side to return long responses.    Obviously both of these options slow things down!

    print "---------------------------"
    my_board.setTraceOn()
    print "---------------------------"
    my_board.setDebugOn()
    print "---------------------------"

The following code causes the onboard LED to blink.    It is 1-1 analogous to what you would do in Arduino code directly.  Note that a -1 is returned from the Python side of the interface if there has been an error with the "returnDebug" attribute having the reason for the error.

    print "Pin Mode to Output" 
    if my_board.pinModeOutput(13) == -1: 
        print "Error:" + my_board.returnDebug
    print "Set High" 
    if my_board.setHigh(13) == -1:
         print "Error:" + my_board.returnDebug 
    time.sleep(1)
    print "Set Low" 
    if my_board.setLow(13) == -1:
         print "Error:" + my_board.returnDebug
    time.sleep(2)

Finally we dump the trace stuff that the Arduino side of the interface has been collecting.   if debug is currently on, as it is in this case, the dump will be shown on the screen as well as written to the log file.

    print "---------------------------"
    my_board.dumpTrace()
    print "---------------------------"

The screen shot of this sample running:

    ---------------------------
    ---------------------------
    returned False value Debug set to 1
    ---------------------------
    Pin Mode to Output
    @setLastTime
    returned False value Set last time for trace
    @pinModeOutput for pin 13
    returned False value Pin 13 set to Output
    Set High
    @setLastTime
    returned False value Set last time for trace
    @setHigh for pin 13
    returned False value Pin 13 set HIGH
    Set Low
    @setLastTime
    returned False value Set last time for trace
    @setLow for pin 13
    returned False value Pin 13 set LOW
    ---------------------------
    @setLastTime:09:25:26
    returned False value Set last time for trace
    @dumpTrace
    returned False value Trace dump follows
    Arduino -  - 0,Trace set to 1 - 12718 (12718)
    Arduino -  - 0,Set last time for trace - 12726 (8)
    Arduino - 09:25:23 - 0,Debug set to 1 - 12734 (8)
    Arduino - 09:25:23 - 0,Set last time for trace - 12742 (8)
    Arduino - 09:25:23 - 0,Pin 13 set to Output - 12750 (8)
    Arduino - 09:25:23 - 0,Set last time for trace - 12761 (11)
    Arduino - 09:25:23 - 0,Pin 13 set HIGH - 12770 (9)
    Arduino - 09:25:23 - 0,Set last time for trace - 13779 (1009)
    Arduino - 09:25:24 - 0,Pin 13 set LOW - 13788 (9)
    Arduino - 09:25:24 - 0,Set last time for trace - 15799 (2011)
    Arduino - 09:25:26 - 0,Trace dump follows - 15808 (9)
    Arduino -
    Arduino - #EOD#
    ---------------------------
 

The bottom of the screen shot (between the dashed lines) is the trace dump returned from the Arduino.   The second column is a time stamp set from the Python side to help you sync up what was happening.   The five digit number followed by the number in parenthesis is the execution time on the Arduino with the parenthesis being the time between the previous command and the one in parenthesis.

The trace functionality is a large part of the reason that I wrote my own interface.   If you are adding your own functionality to the Arduino side the ability to log stuff can be very useful.   Just call traceEntry with your own message for the trace log.

No comments:

Post a Comment