dhhagan / py-opc

Python wrapper for the Alphasense OPC-N2 built around py-spidev
MIT License
30 stars 26 forks source link

SPI-USB Adapter with OPC-N2 example does not work with any python version #70

Closed tshu closed 6 years ago

tshu commented 6 years ago

Related to #69, running the example script for the device using python3 results in

Traceback (most recent call last):
  File "reviewTest.py", line 1, in <module>
    from usbiss.spi import SPI
ImportError: No module named 'usbiss'

Attempting to use python results in

ERROR:opc:Could not parse the fimrware version from ????????????????????????????????????????????????????????????
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
WARNING:opc:Data transfer was incomplete

in addition to the device's fan continually running until unplugged.

DancingQuanta commented 6 years ago

The first error showed you haven't installed pyusbiss. Did you execute pip install pyusbiss?

On Sat, 23 Jun 2018, 00:16 Tony Shu, notifications@github.com wrote:

Related to #69 https://github.com/dhhagan/py-opc/issues/69, running the example script for the device using python3 results in

Traceback (most recent call last): File "reviewTest.py", line 1, in from usbiss.spi import SPI ImportError: No module named 'usbiss'

Attempting to use python results in

ERROR:opc:Could not parse the fimrware version from ???????????????????????????????????????????????????????????? Traceback (most recent call last): File "/home/pi/py-opc/opc/init.py", line 77, in init self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1]) IndexError: list index out of range ERROR:opc:Could not parse the fimrware version from ? Traceback (most recent call last): File "/home/pi/py-opc/opc/init.py", line 77, in init self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1]) IndexError: list index out of range WARNING:opc:Data transfer was incomplete

in addition to the device's fan continually running until unplugged.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dhhagan/py-opc/issues/70, or mute the thread https://github.com/notifications/unsubscribe-auth/AIB3VVIpINi6fPQKWpr8he_zooeBCZH6ks5t_XregaJpZM4U0iJK .

DancingQuanta commented 6 years ago

Ah. If you wanted python 3 to work then execute pip3 install pyusbiss. Both Python 2 and python 3 have different site-packages and consequently different pip.

DancingQuanta commented 6 years ago

Your second issue after executing python2 appears to be a bad communication. Can you connect your sensor to the adaptor and wait for a minute before executing the test-device.py script? We have numerous Github issues regarding the connection problems and one of the solutions was to wait between physically connecting the sensor and executing any commands.

DancingQuanta commented 6 years ago

Oh, your traceback does show that you successfully connected to your sensor. The py-opc library attempts to communicate with your sensor a number of times before it succeeded. Your traceback shows two failed attempts and third attempt is successful. You can see the same index out of range error two times but it does not stop here because it managed to connect properly. Thus the test-device.py continues to run until it did something that triggers a WARNING.

Your real problem is the last line in the traceback WARNING:opc:Data transfer was incomplete. This was due to test-device.py executing the histogram command at https://github.com/dhhagan/py-opc/blob/8eba6bb460d5ba4c6de8d5d779b9f8e8cd9f5dd6/test-device.py#L27

This histogram can have issues because you have to wait a bit between executing histogram.

Debugging this problem is difficult because the traceback for the failed attempts pollutes the traceback of other exceptions and so made it hard to see what is the problem. There must be a way to not show the traceback from the failed connection attempts after successful connection. Lastly the warning and error messages from the logger does not show where they comes from.

dhhagan commented 6 years ago

@tshu Can you let me know which version of py-opc is installed (opc.__version__)? I thought I fixed several of these in the newest release after speaking with Alphasense on some of the issues. Thanks!

tshu commented 6 years ago

Great, so both python2 and python3 work with test-device.py after using pip3 and waiting after USB connection. I will occasionally fail to connect 2-3 times, but it will always run the script in the end. If there can be a note about the wait time in the readme, that would be great.

However, referring back to the original problem in this issue of running the example code from the readme, both python2 and python3 will attempt to connect 0-3 times before terminating with `WARNING:opc:Data transfer was incomplete'. Fan remains on. I can still run test-device.py to regain control of the device and have the fans turn off at the end of the script.

@dhhagan currently running 1.6.0

DancingQuanta commented 6 years ago

What is output of the following script (modified from readme)

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alphasense.on()

# Read the information string
print (alphasense.read_info_string())

# Turn the opc OFF
alphasense.off()
tshu commented 6 years ago
pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
Traceback (most recent call last):
  File "reviewTest2.py", line 14, in <module>
    alphasense.on()
NameError: name 'alphasense' is not defined
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
Traceback (most recent call last):
  File "reviewTest2.py", line 14, in <module>
    alphasense.on()
NameError: name 'alphasense' is not defined
dhhagan commented 6 years ago

@DancingQuanta @tshu It would appear the script/code above is incorrect - it should be alpha.on() since that is what the OPCN2 class instance was initiated as. I have just merged the previous readme changes into v1.6.1. Care to check?

DancingQuanta commented 6 years ago

Opps, I made a name mistake. Some corrections here

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alpha.on()

# Read the information string
print (alpha.read_info_string())

# Turn the opc OFF
alpha.off()
tshu commented 6 years ago
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range

pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range


Getting some garbage outputs.

DancingQuanta commented 6 years ago

Could you replace print(alpha.read_info_string()) with print(repr(alpha.read_info_string())) and try again please? The function read_info_string() should return a string. repr() will show what type of data it is.

tshu commented 6 years ago
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
DancingQuanta commented 6 years ago

Your output shows that it is getting blank data from your sensor. Can you add sleep(1) before each line containing alpha and run the script? Also make sure to update your py-opc to v1.6.1.

tshu commented 6 years ago

That did the trick.

pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
'OPC-N2 FirmwareVer=OPC-018.2..............................BD'
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
'OPC-N2 FirmwareVer=OPC-018.2..............................BD'

The only necessary sleep is between turnon and the read. The following code works:

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alpha.on()

sleep(1)
# Read the information string
print(repr(alpha.read_info_string()))

# Turn the opc OFF
alpha.off()

@dhhagan These edits can go in the readme examples.

DancingQuanta commented 6 years ago

We probably need to improve alpha.on(). A sleep could be added to end of alpha.on(). Or have a timer in the library that track which command have been sent. So that the script itself can work without being slowed down until it execute another alpha command which then will invoke sleep with time period equal to alphasense's response then execute. Like a debt.

dhhagan commented 6 years ago

@DancingQuanta Yea, I tried to avoid doing anything other than wrapping the SPI commands, but it seems that it is causing so much trouble for most users (myself included). I went ahead and added sleeps in the necessary places and will push a bit later on today as I have some free time.

DancingQuanta commented 6 years ago

Can I suggest a decorator to wrap some methods to add sleep to beginning of a method if the method was called in the specified time period since last method called. Like a cool down. We know that a method such as on takes a while to make the device ready. Every time a method such as on is called the time of this call is recorded. Next time a method (any method) is called, the new time is compared to old time and the difference is used to delay execution of called method.

This way the time spent waiting for sleep in each method to work would be less if we could use the time in between calls as part of waiting.

dhhagan commented 6 years ago

@DancingQuanta I'll go ahead and merge a few PR's and push right now - maybe then take a look and see what you think is best? A decorator would be fine as you suggest - I think I just added a few sleeps. Give me 30 min or so.

dhhagan commented 6 years ago

@tshu I've added some notes to the readme. Should be okay now?