Can't Set Encoder Frequency Using Python3

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
74ls04
 
Posts: 5
Joined: Mon Dec 17, 2018 7:37 pm

Can't Set Encoder Frequency Using Python3

Post by 74ls04 »

Test code:

Code: Select all

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()
Console Output

Code: Select all

debian@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
Python Version
3.5.3

System Version Info:

Code: Select all

git:/opt/scripts/:[e9bcff232834702c1c810710706ee815d77b080b]
eeprom:[A335BNLT00C05014BBBK12CA]
model:[TI_AM335x_BeagleBone_Black]
dogtag:[BeagleBoard.org Debian Image 2018-10-07]
bootloader:[microSD-(push-button)]:[/dev/mmcblk0]:[U-Boot 2018.09-00002-gd5b4c4b656]:[location: dd MBR]
bootloader:[eMMC-(default)]:[/dev/mmcblk1]:[U-Boot 2014.04-00014-g47880f5]:[location: fatfs /boot/uboot/MLO]
kernel:[4.14.71-ti-r80]
nodejs:[v6.15.0]
uboot_overlay_options:[enable_uboot_overlays=1]
uboot_overlay_options:[disable_uboot_overlay_video=1]
uboot_overlay_options:[uboot_overlay_pru=/lib/firmware/AM335X-PRU-RPROC-4-14-TI-00A0.dtbo]
uboot_overlay_options:[enable_uboot_cape_universal=1]
pkg check: to individually upgrade run: [sudo apt install --only-upgrade <pkg>]
pkg:[bb-cape-overlays]:[4.4.20181120.0-0rcnee0~stretch+20181120]
pkg:[bb-wl18xx-firmware]:[1.20180517-0rcnee0~stretch+20180517]
pkg:[kmod]:[23-2rcnee1~stretch+20171005]
pkg:[librobotcontrol]:[1.0.4-git20181123.0-0rcnee0~stretch+20181124]
pkg:[firmware-ti-connectivity]:[20170823-1rcnee1~stretch+20180328]
groups:[debian : debian adm kmem dialout cdrom floppy audio dip video plugdev users systemd-journal i2c bluetooth netdev cloud9ide gpio pwm eqep admin spi tisdk weston-launch xenomai]
cmdline:[console=ttyO0,115200n8 bone_capemgr.uboot_capemgr_enabled=1 root=/dev/mmcblk0p1 ro rootfstype=ext4 rootwait coherent_pool=1M net.ifnames=0 quiet]
dmesg | grep pinctrl-single
[    1.119815] pinctrl-single 44e10800.pinmux: 142 pins at pa f9e10800 size 568
dmesg | grep gpio-of-helper
[    1.131784] gpio-of-helper ocp:cape-universal: ready
END
strace Output
strace.log
(225.66 KiB) Downloaded 135 times

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Can't Set Encoder Frequency Using Python3

Post by drewfustini »

This appears to be where the error occurs:

Code: Select all

2831  open("/sys/devices/platform/ocp/48304000.epwmss/48304180.eqep/period", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE|O_CLOEXEC, 0666) = 3
2831  fstat64(3, {st_mode=S_IFREG|0664, st_size=4096, ...}) = 0
2831  ioctl(3, TCGETS, 0xbedad1f4)      = -1 ENOTTY (Inappropriate ioctl for device)
2831  _llseek(3, 0, [0], SEEK_CUR)      = 0
2831  ioctl(3, TCGETS, 0xbedad1a4)      = -1 ENOTTY (Inappropriate ioctl for device)
2831  _llseek(3, 0, [0], SEEK_CUR)      = 0
2831  _llseek(3, 0, [0], SEEK_CUR)      = 0
2831  write(3, "500000.0", 8)           = -1 EINVAL (Invalid argument)
2831  write(3, "500000.0", 8)           = -1 EINVAL (Invalid argument)
2831  close(3)                          = 0
Could you please try running just enable() and disable() and check if there is any error?

Code: Select all

from Adafruit_BBIO.Encoder import RotaryEncoder, eQEP2

test_encoder = RotaryEncoder(eQEP2)

# Enable
test_encoder.enable()

# Disable
test_encoder.disable()

User avatar
74ls04
 
Posts: 5
Joined: Mon Dec 17, 2018 7:37 pm

Re: Can't Set Encoder Frequency Using Python3

Post by 74ls04 »

That runs with no errors:

Code: Select all

debian@beaglebone:~$ python3 encoder_test.py
debian@beaglebone:~$

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Can't Set Encoder Frequency Using Python3

Post by drewfustini »

I'll have to investigate the eqep kernel driver.


It appears like Python is doing things correctly.

User avatar
74ls04
 
Posts: 5
Joined: Mon Dec 17, 2018 7:37 pm

Re: Can't Set Encoder Frequency Using Python3

Post by 74ls04 »

Sounds good.

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.
debug.PNG
debug.PNG (53.32 KiB) Viewed 859 times
strace2.log
(284.13 KiB) Downloaded 132 times

User avatar
74ls04
 
Posts: 5
Joined: Mon Dec 17, 2018 7:37 pm

Re: Can't Set Encoder Frequency Using Python3

Post by 74ls04 »

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/492 ... ng-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
period.PNG
period.PNG (11.33 KiB) Viewed 846 times

Code: Select all

debian@beaglebone:~$ python3 encoder_test.py
2000.0
Note how the output also shows up as a float due to the same behavior.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Can't Set Encoder Frequency Using Python3

Post by drewfustini »

Thanks very much!

I'll create a Pull Request and merge this fix into the library.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Can't Set Encoder Frequency Using Python3

Post by drewfustini »

FYI - I created this issue and will create a PR with the fix
https://github.com/adafruit/adafruit-be ... issues/299

User avatar
74ls04
 
Posts: 5
Joined: Mon Dec 17, 2018 7:37 pm

Re: Can't Set Encoder Frequency Using Python3

Post by 74ls04 »

Great, thanks!

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Can't Set Encoder Frequency Using Python3

Post by drewfustini »


Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”