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
pinModeInput(pin)
pinModeOutput(pin)
(D)igital commands
setHigh(pin)
setLow(pin)
digitalRead(pin)
digitalWrite(pin, value)
(A)nalog commands
analogWrite(pin, value)
pwmRead(pin)
pwmPulseLow(pin, trigger)
(S)ervo commands
attachServo(servo, pin)
commandServo(servo, command)
detachServo(servo)
(H)ousekeeping and debugging commands
setDebugOn()
setTraceOn()
dumpTrace()
setDebugOff()
setTraceOff()
setLastTime()
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
compassInit()
compassBearing()
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