Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timing issue with analog reads #16

Closed
ghost opened this issue May 26, 2013 · 6 comments
Closed

Timing issue with analog reads #16

ghost opened this issue May 26, 2013 · 6 comments
Labels

Comments

@ghost
Copy link

ghost commented May 26, 2013

Hi,

I could not manage to setup a reliable connection between my pc and arduino (standard firmata sketch). The confusing part is that it all seems to work in the python command line, but when running the example code in a small script, analog reads return 'None'. After inserting a little pause (emulating manual execution in the python shell), analog read does output some values.

Here's my test-script:

#!/usr/bin/env python
import time
from pyfirmata import Arduino, util
board = Arduino('/dev/ttyUSB0')
it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()
print board.analog[0].read()
print board.analog[0].read()
print board.analog[0].read()
print board.analog[0].read()
time.sleep(1)
print board.analog[0].read()
print board.analog[0].read()
print board.analog[0].read()
print board.analog[0].read()
board.exit()

...and it's output:

None
None
None
None
0.6354
0.6354
0.6354
0.6354

As mentioned, when just typing the statements in the python shell it just seems to work fine:

>>> import time
>>> from pyfirmata import Arduino, util
>>> board = Arduino('/dev/ttyUSB0')
>>> it = util.Iterator(board)
>>> it.start()
>>> board.analog[0].enable_reporting()
>>> print board.analog[0].read()
0.6129
>>> print board.analog[0].read()
0.567
>>> print board.analog[0].read()
0.5787
>>> print board.analog[0].read()
0.607
.... etc

Used hard-/software:
pyFirmata 0.95 in python 2.7.3
Firmata 2.3.5 on an Arduino duemilanova (ATmega328 version)

Any clue?

Bests,

Bram

@tino
Copy link
Owner

tino commented May 26, 2013

The iterator thread updates the values of the pins, and has a little sleep time in there (https://github.com/tino/pyFirmata/blob/master/pyfirmata/util.py#L42). It is probably that that update has not run yet when you read the values. Could you try this instead of your reads and time.sleep(1):

start = time.time()
while board.analog[0].read() is None:
    print "nothing after {0}".format(time.time() - start)
print "first value after {0}".format(time.time() - start)

That will probably tell you how long you have to wait.

@ghost
Copy link
Author

ghost commented May 27, 2013

Hi Tino,

Thanks! The first value is returned after around 22ms.
I can do this in my code.

If I try to fix it in util.py, by changing the little sleep time of 1ms to 50ms, I get:

...
...
nothing after 0.0590040683746
first value after 0.0655460357666
0.739
0.739
0.739
0.739
0.739
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/dist-packages/pyfirmata/util.py", line 40, in run
    while self.board.bytes_available():
  File "/usr/local/lib/python2.7/dist-packages/pyfirmata/pyfirmata.py", line 225, in bytes_available
    return self.sp.inWaiting()
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 435, in inWaiting
    s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
TypeError: argument must be an int, or have a fileno() method.

That's obviously not the right place to wait.

At least I can produce some working code now!

Groetjes,

Bram

@tino
Copy link
Owner

tino commented May 29, 2013

The easiest is probably to just read and test for None

@ghost
Copy link
Author

ghost commented May 29, 2013

I've done exactly that. But when reading digital ports at the same time, it gives me nothing but None's ;-(
I'll post a short script later.
Have people been using pyFirmata successfully with firmata 2.3.5 on a Arduino Duemilanova?
BTW, do you have some example code available?

@tino
Copy link
Owner

tino commented May 29, 2013

What do you mean by "reading digital ports at the same time"?

This is the only example I could find quickly. It is not that easy though...
https://gist.github.com/tino/5673806

@tino
Copy link
Owner

tino commented Oct 29, 2016

Closing, as I can't move forward with this. Hopefully it's not a problem anymore!

@tino tino closed this as completed Oct 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant