Arduino Forum

Forum 2005-2010 (read only) => Software => Interfacing => Topic started by: dylpkls91 on Dec 29, 2007, 09:53 pm

Title: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Dec 29, 2007, 09:53 pm
Has anyone had luck using Firmata + Pyduino, the Python library?

The Firmata sketch is located here: http://www.arduino.cc/playground/Interfacing/Firmata but the download link is broken so I had to copy/paste from the CVS site. Only later did I find that Firmata is already located in a folder in the Arduino IDE (I'm on WinXP).

I've had no luck getting the Pyduino library and example to work. PySerial can connect to the Arduino's serial port without errors, but any attempt at one- or two-way communication (for example, getting the firmata version number stored in the sketch on the arduino) produces nothing. Other serial communication to my board using other sketches works fine.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 02, 2008, 06:31 pm
Hi, I wrote pyduino, so this is almost certainly my fault  :)
Can you post the python you were using (if it isn't just the example), and any errors you're getting.  I've not got my arduino with me for a few days, but last time I used it I had it working fine.  One possibility is that firmata is now working at the 115200 bitrate - I need to check properly, but it's either happened or happening soon.  I'm so behind with my arduino news  :)
If thats the problem you could try changing line 51 of pyduino.py from
Code: [Select]
self.sp = serial.Serial(port, 57600, timeout=0.02)
to
Code: [Select]
self.sp = serial.Serial(port, 115200, timeout=0.02)

I'll be able to have a proper look in a few days time, but any more info would be much appreciated.
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 02, 2008, 11:17 pm
Thanks for the response, sounds like you're the person to talk to.

I'm not trying to do anything fancy, just turn on the LED at pin 13 of my Diecimila. This is the code I was using after changing the serial rate in pyduino.py like you suggested:

Code: [Select]

import pyduino
a = pyduino.Arduino("com5")
a.digital[13].set_active(1)
a.digital[13].set_mode(pyduino.DIGITAL_OUTPUT)
a.digital[13].write(1)


The firmata sketch uploads fine, the rx/tx lights blink while it runs, and I don't get any errors in Python, but nothing else happens. I know it's not a communication issue; I have sent and received serial data from my Arduino in several other instances. PySerial and the FTDI drivers are installed (I'm on Python 2.5). Any idea as to what's going wrong? Thanks again for your help.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: mellis on Jan 02, 2008, 11:27 pm
dylpkls91: are you using a Diecimila?  orphans: have you tested pyduino on a Diecimila?  

The auto-reset feature of the Diecimila means that you might need to pause 2 seconds or so after opening the serial port but before sending any data over it.  That gives the bootloader on the Diecimila a chance to time out and start the firmata sketch.  Otherwise, the first few bytes sent by pyduino will go to the bootloader, not firmata.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 03, 2008, 12:13 am
Ohhh... yep, I've got a Diecimila. I'll try putting a time.sleep(2) in pyduino.py and see if that works. I'll let you know how it goes. Thanks for the tip!  :)
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 03, 2008, 12:24 am
Well, I just added a sleep of two seconds right after pyduino initializes the serial connection, but nothing changed in the behavior of my arduino. I even fixed the mistake in my test code where I forgot to call board.iterate() before writing to the pin, but that didn't change anything either.  :'(
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 03, 2008, 01:08 pm
Mellis: No I hadn't tested with a Diecimila - there was a post on the mailing list saying that Firmata worked better with the automatic reset, but I think that sending a couple of bytes to get the version straight after initializing the connection could be a problem.  But not this problem evidently - I think it might be a bug on top of a bug :D  I'll stick a sleep in a new version once I know what's causing this.

dylpkls91: Ok, could you try running this - it's about as basic as it can get.

Code: [Select]
import pyduino
a = pyduino.Arduino("com5")
print a.get_firmata_version()


You should see the status LED strobe a pattern at the start (giving the major and minor version of firmata).  Then there should be another quick blip or two from the status pin, and you should get a version number back.  If this works then then serial communication with firmata is OK, and it's probably something in my digital code which is a little sketchy  :)  If you could try this at both bitrates it'd be much appreciated too.

Also if none of that works could you give it a go using rev 1.3 (http://pure-data.cvs.sourceforge.net/*checkout*/pure-data/externals/hardware/arduino/Pd_firmware/Pd_firmware.pde?revision=1.30) of firmata.  I think there's a chance that the serial initiation code has gone amiss in the change to using firmata as a library (I can't find it in the 0.10 arduino code anyway...)

Sorry I can't debug this myself at the moment, and cheers for looking into it.
Joe

Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: mellis on Jan 03, 2008, 05:56 pm
Yea, the firmata that comes with Arduino 0010 doesn't work - I'm not sure it even compiles.  You might also try increasing the delay from 2 seconds to 3 or 4 (or more).
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 04, 2008, 02:16 am
Thank you both again for your help, but I regret to say that pyduino still isn't working.

Here's what I was able to find out:
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 04, 2008, 03:33 pm
Right, I think that I almost know what's going on now...maybe
The iterate and sleep should definitely be in __init__ - that's my fault.  The iterate after get_firmata_version() grabs the bytes sending the version, everything I've done before has been looping over iterate so I didn't notice.
I think that the problem setting the digital pin is in the 0010 version of firmata, although it does compile.  I'm going to take a guess that the compile errors in rev 1.3 are to do with port_to_input_PGM not being found.  If so try putting
Code: [Select]
#include <pins_arduino.h> after the EEPROM include in firmata.  I think it should compile then and (hopefully) work with 57600 bitrate.  If it compiles and doesn't work then we're back to it being my fault.  If it doesn't compile then can you post up the errors - I remember I had to do a little tweaking to get 1.30 to compile with 0010 (probably just the include), but I thought it was my fault botching the new versions of everything.
Cheers
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: mellis on Jan 04, 2008, 05:13 pm
You might want to try the version of Firmata in the processing-arduino library (http://www.arduino.cc/playground/uploads/Interfacing/processing-arduino.zip) from the Interfacing w/ Processing (http://www.arduino.cc/playground/Interfacing/Processing) page on the playground.  That's the one I've been using (I created the Processing library), and I believe it's the most recent stable version.

BTW, dylpkls91: when you upload a sketch (e.g. led blink), do you have to wait about 6 seconds or so after uploading it before it starts running?  You may have gotten a Diecimila that was accidently burned with the NG bootloader, which works fine, but has a longer initial delay (so requires a longer sleep in the pyduino code).
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 04, 2008, 07:29 pm
Here's where I'm at right now.

I just tried Firmata 1.30, applying orphans' suggestion of including pins_arduino.h. At first, it didn't compile and I got the same error, but it worked after I poked around and changed the method calls in lines 164 and 165 of Firmata from "port_to_input" to "port_to_input_PGM".

I'm using pyduino.py modified in the following ways: I added a six-second sleep and self.iterate() to __init__, and set the baudrate to 57600, which matches the speed set in Firmata's setup() function.

But I'm still at the same point: Python can connect to the board via serial, pin 13 verifies being set active, I can get the version number, but any attempt to write a 1 to the pin does nothing. The rx light flickers, but that's it.  :'(

Mellis: Thanks, I'll try using that Firmata and let you know how it goes. No, when I upload a regular sketch (like the hello world led test) the board starts the program only a couple seconds after the transfer is complete. I put a six-second sleep in pyduino.py so that no data would be sent until after Firmata does its initial pin13-version-blink thing. Thank goodness my Arduino doesn't suffer from bootloader issues!

Thank you both again for all your help. It just goes to show that open-source communities have more than one advantage over the big guys.  ;D
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 04, 2008, 07:58 pm
Breaking:

I am now using Firmata 1.29 and Pyduino (with sleep and iterate in __init__ and a baudrate of 57600).

The Processing examples work (input, output, and pwm).

Better yet, as far as I can tell, Pyduino is now working just fine!!!

Thank you, thank you, thank you, both!

;D ;D ;D
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 04, 2008, 08:01 pm
Brilliant!  Over the next couple of days I'll put up a new version of Pyduino which has your changes in, and contains the working firmware so we don't have this problem again.  Thanks for all of your help dylpkls91 and mellis.
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 04, 2008, 08:33 pm
Great! Why don't I just e-mail you the two files or something?

Through trial and error, I found that the shortest possible sleep time without data/command loss is 1.5 seconds for Diecimilas.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 04, 2008, 08:38 pm
Yeah, that'd be great if you could.  My email's in the linky thing at the bottom of the posts.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 05, 2008, 09:25 pm
Hmmm...

I've been playing around some more, and it seems like reading analog and digital inputs only works in a "while True" loop like the one in the pyduino example. I can't just grab an input's value once, it returns a 0.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 07, 2008, 11:49 am
Yeah, I get this too - I've got a fix for analog, just need to get a free hour to look at digital.  What happens is that there is a delay between activating a pin and getting values out, so it just returns zero (a default value).  I think I should be able to have a look this afternoon.
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 07, 2008, 07:10 pm
Great, once that's fixed, it'll be set!  ;D
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 07, 2008, 08:24 pm
Right - half success...
Analog works fine afaik, and is not too bad code wise.  Digital is however pretty sketchy I'm afraid.  Anyway, here's a new version of pyduino: http://rafb.net/p/u9u2QP86.html
...and a rough idea of how it should work: http://rafb.net/p/FjOkxU38.html

As you can see, the analog iterates until it does not return -1 (i.e. a value has been assigned to the analog pin).  This would be OK to do with digital, but there is no way of knowing whether the digital pin has been activated or not, as it only signals on a change.  This is in the TODO for firmata, so will hopefully be in the next version.  For now you can work around it by iterating for a while after the pin is activated so it has time to get a value.

On the plus side this has helped me find a big old bug in the digital handling, so it's all been good and worthwhile  :)
Let me know how this one goes
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 07, 2008, 09:10 pm
Thanks, I'll let you know how it goes.

I almost forgot, do you want to add the extra Diecimila pwm pins 3, 5, and 6 to the constant PWM_PINS? You may not want to, though, because AFAIK there's no way to tell which type of board Firmata is running on.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: dylpkls91 on Jan 07, 2008, 09:14 pm
P.S. - Hopefully the next version of Firmata is out soon, to fix the digital input problem. Do you have any idea how active Firmata development is?
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: wrybread on Jan 17, 2008, 11:56 pm
Links are broken...

http://rafb.net/p/FjOkxU38.html and http://rafb.net/p/u9u2QP86.html are both 404 for me....
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jan 18, 2008, 02:26 am
Sorry, they were just up for a day or so for testing.  I'll get a proper tarball out over the next couple of days.  For now here's the links:
http://pyduino.pastebin.com/f2b76d85e
http://pyduino.pastebin.com/m69d01f2d

Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: eighthave on Jul 23, 2008, 05:20 pm
Hey

Did that tarball ever get released?  If so, please update the link on the Firmata page, under Host Implementations:

http://www.arduino.cc/playground/Interfacing/Firmata

Also, let me know if you want any help updating pyduino to work with Firmata v2.0.  It is slated to be included in Arduino 12 and we are working towards making it the default firmware that Arduinos ship with, so it would be good to have pyduino working with it.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: orphans on Jul 24, 2008, 10:19 am
Oh gawd, it never did.  I'll have a look this weekend and see what sort of state it's in and give a go at making a version which works with 2.0.  If I have any problems I'll drop an email to the ML.
Cheers
Joe
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: ballagas on Dec 11, 2008, 08:17 pm
The link on the firmata site is still code from 2007.04.09.  Any chance you could put the pyduino code on berlios or sourceforge so that we can help you get it up and running.
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: kap on Jan 19, 2009, 07:21 pm
Today i wrote orphans a mail asking if he can send me his newest pyduino version.
He was super-reactive and published it.
The link is in the next post from me, since this is my first, and some spam eater in this forum software doesnt want a link to show up in a first post.

This works very well with Firmata 2.0, wow!
I am using the Firmata sketchbook included in arduino-012, btw.

So, huge thanks to orphans!
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: kap on Jan 19, 2009, 07:22 pm
The link to pyduino:

http://code.google.com/p/pyduino/
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: rferrazz on May 12, 2010, 10:26 pm
I've modified the pyduino library to work with firmata 2.1, everything work except i2c comunication
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: rferrazz on May 12, 2010, 10:27 pm
You can read about my version here http://www.f24h.netsons.org/2010/05/pyduino-firmata-2-1/
and you can download it here http://www.f24h.netsons.org/wp-content/uploads/2010/05/pyduino.tar.gz
The archive contain a patch that can be applied to the original pyduino.
Any help is appreciated :D
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: Samarth on Sep 23, 2010, 03:10 pm
where do we download pyduino from I can't find it on google code
please help!
Title: Re: Arduino + Firmata + Pyduino (Python)
Post by: JimmyDB on Nov 18, 2010, 07:51 pm
hello,

look in the above post...  the file you want is pyduino.tar.gz