Skip to content

Can't Set Encoder Frequency Using Python3 #299

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

Closed
pdp7 opened this issue Dec 18, 2018 · 4 comments
Closed

Can't Set Encoder Frequency Using Python3 #299

pdp7 opened this issue Dec 18, 2018 · 4 comments

Comments

@pdp7
Copy link
Collaborator

pdp7 commented Dec 18, 2018

From the Adafruit forum:
https://forums.adafruit.com/viewtopic.php?f=49&t=145110&p=716829#p716829

Test:

from Adafruit_BBIO.Encoder import RotaryEncoder, eQEP2

test_encoder = RotaryEncoder(eQEP2)

# Enable
test_encoder.enable()

# Attempt to set frequency
test_encoder.frequency = 2000

print(test_encoder.frequency)

# Disable
test_encoder.disable()

Error:

ebian@beaglebone:~$ python encoder_test.py
2000
debian@beaglebone:~$ python3 encoder_test.py
OSError: [Errno 22] Invalid argument

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "encoder_test.py", line 9, in <module>
    test_encoder.frequency = 2000
  File "/usr/local/lib/python3.5/dist-packages/Adafruit_BBIO/Encoder.py", line 355, in frequency
    self._eqep.node.period = str(period)
  File "/usr/local/lib/python3.5/dist-packages/Adafruit_BBIO/sysfs.py", line 93, in __setattr__
    fp.write(val)
OSError: [Errno 22] Invalid argument
@pdp7
Copy link
Collaborator Author

pdp7 commented Dec 18, 2018

update:

If it's of any help I've attached the strace output of the initial code executed with python 2 for comparison.

Looking at that write() call, the main difference I see is that Python3 is trying to write a float ("500000.0") while Python2 is not ("500000"). Not sure if the driver expects a certain type.

@pdp7
Copy link
Collaborator Author

pdp7 commented Dec 18, 2018

update:

I've been able to confirm that that's the issue. It stems from how Python3 handles division as discussed here https://stackoverflow.com/questions/49282799/python-3-int-division-operator-is-returning-a-float.

I tested this by explicitly casting the period to an int in the frequency setter of Encoder.py and the program executed successfully

@pdp7
Copy link
Collaborator Author

pdp7 commented Dec 18, 2018

Link to image with the code fix:
https://forums.adafruit.com/download/file.php?id=63277

pdp7 added a commit that referenced this issue Dec 19, 2018
@pdp7
Copy link
Collaborator Author

pdp7 commented Dec 19, 2018

Fixed by commit b2a6be5

test299.py

from Adafruit_BBIO.Encoder import RotaryEncoder, eQEP2

test_encoder = RotaryEncoder(eQEP2)

# Enable
test_encoder.enable()

# Attempt to set frequency
test_encoder.frequency = 2000

print(test_encoder.frequency)

# Disable
test_encoder.disable()
debian@beaglebone:~$ python3 test299.py 
2000.0

@pdp7 pdp7 closed this as completed Dec 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant