Using Python to interface with the PM

Post questions and issues with Concept2 PM3 SDK
gambrell
Paddler
Posts: 5
Joined: December 6th, 2011, 6:06 pm
Location: Shelton, CT
Contact:

Using Python to interface with the PM

Post by gambrell » December 6th, 2011, 6:16 pm

I've been working on a python script to allow for easier communication between the computer and the Concept 2 PM. It has been used successfully with a single erg setup but has not been extensively tested. I've included a link below in hope that other people may have some use for it too.

http://www.newhavenrowingclub.org/pyrow/

zaf
Paddler
Posts: 12
Joined: January 10th, 2012, 5:57 am

Re: Using Python to interface with the PM

Post by zaf » January 10th, 2012, 6:02 am

I'm stuck at trying to get my system updated with the requirments need to run the script.

I'm using an ubuntu linux machine (after failing harder on a mac) and after installing libusb, pyusb, setting permissions on 'udev' rules, I finally hit the wall with "usb.core.USBError: Resource busy":

Code: Select all

python statshow.py 
Traceback (most recent call last):
  File "statshow.py", line 18, in <module>
    erg = pyrow.pyrow(ergs[0])
  File "/home/zaf/tmp/pyrow/pyrow.py", line 32, in __init__
    erg.set_configuration() #required to configure USB connection
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 514, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 85, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/usr/local/lib/python2.7/dist-packages/usb/_debug.py", line 53, in do_trace
    return f(*args, **named_args)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb10.py", line 451, in set_configuration
    _check(_lib.libusb_set_configuration(dev_handle, config_value))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb10.py", line 353, in _check
    raise USBError(_str_error[retval.value])
usb.core.USBError: Resource busy

I really need some help because I want to do some cool stuff.

gambrell
Paddler
Posts: 5
Joined: December 6th, 2011, 6:06 pm
Location: Shelton, CT
Contact:

Re: Using Python to interface with the PM

Post by gambrell » January 10th, 2012, 5:22 pm

Zaf,

After doing some searching around it appears that pyusb may require an extra step when connecting to a device in linux. I've updated the code and reposted it at the above link. I do not have access to a linux machine that could be easily located next to my erg so I have not had a chance to test the new code in linux. If you have a chance please give it a try and let me know how it goes.

zaf
Paddler
Posts: 12
Joined: January 10th, 2012, 5:57 am

Re: Using Python to interface with the PM

Post by zaf » January 11th, 2012, 5:39 am

Thanks for the reply!

I'm downloading now and testing.

zaf
Paddler
Posts: 12
Joined: January 10th, 2012, 5:57 am

Re: Using Python to interface with the PM

Post by zaf » January 11th, 2012, 5:43 am

Same error. Heres the dump:

Code: Select all

python statshow.py 
Traceback (most recent call last):
  File "statshow.py", line 18, in <module>
    erg = pyrow.pyrow(ergs[0])
  File "/home/zaf/tmp/pyrow/pyrow.py", line 39, in __init__
    erg.set_configuration() #required to configure USB connection
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 514, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 85, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/usr/local/lib/python2.7/dist-packages/usb/_debug.py", line 53, in do_trace
    return f(*args, **named_args)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb10.py", line 451, in set_configuration
    _check(_lib.libusb_set_configuration(dev_handle, config_value))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb10.py", line 353, in _check
    raise USBError(_str_error[retval.value])
usb.core.USBError: Resource busy

gambrell
Paddler
Posts: 5
Joined: December 6th, 2011, 6:06 pm
Location: Shelton, CT
Contact:

Re: Using Python to interface with the PM

Post by gambrell » January 11th, 2012, 6:47 pm

Zaf,

I have a few more theories and ideas as to your usb issues with linux. However I would need some testing and feedback from you in regards to each idea. Instead of flooding Concept 2's forum with trial and error I figured it would make more sense to try working this through email then posting a solution if we find one.

If you're willing please shot me an email at: pyrow [at] newhavenrowingclub [dot] org

zaf
Paddler
Posts: 12
Joined: January 10th, 2012, 5:57 am

Re: Using Python to interface with the PM

Post by zaf » January 12th, 2012, 4:36 am

Email shot :)

zaf
Paddler
Posts: 12
Joined: January 10th, 2012, 5:57 am

Re: Using Python to interface with the PM

Post by zaf » January 16th, 2012, 6:21 am

In case any reads this - we solved the issues and I have a linux machine communicating with the PM3 on the rowing machine using python. And its really cool ;)

I'd like to thank gambrell for creating the pyrow library and for helping out to get this to work.

Finally, if there is a Concept2 employee reading this, please let your management know that they really need to change the hardware/software on the future PM5 regarding the USB communications with the outside world. It would be less headache for your engineers and developers by using current trends in hardware and software. Seriously, this could determine the success or failure for your future products.

ChrisRey
Paddler
Posts: 2
Joined: August 21st, 2012, 10:20 am

Re: Using Python to interface with the PM

Post by ChrisRey » December 13th, 2013, 4:58 pm

And a thanks from me for the PyRow library - I'm giving it a try on the Raspberry Pi.

Cheers

Chris

gilga
Paddler
Posts: 2
Joined: December 18th, 2013, 11:44 am

Re: Using Python to interface with the PM

Post by gilga » January 5th, 2014, 9:58 am

zaf wrote:In case any reads this - we solved the issues and I have a linux machine communicating with the PM3 on the rowing machine using python. And its really cool ;)

I'd like to thank gambrell for creating the pyrow library and for helping out to get this to work.

Finally, if there is a Concept2 employee reading this, please let your management know that they really need to change the hardware/software on the future PM5 regarding the USB communications with the outside world. It would be less headache for your engineers and developers by using current trends in hardware and software. Seriously, this could determine the success or failure for your future products.
May I know how you solve this issue? I've got exactly the same problem and it's a bit frustrating, so close from the goal ! :)

Streph
Paddler
Posts: 3
Joined: February 24th, 2014, 11:52 am

Re: Using Python to interface with the PM

Post by Streph » February 24th, 2014, 12:02 pm

Hi, All,

I've been waiting years for adevelopment like this. Thanks! I'm having trouble, unfortunately. On my Debian Wheezy machine (Python 2.7.3 with non-Debian pyusb 1.0 installed), I get the following error:

~/pyrow-beta-01-18-2012$ python pyrow.py
File "pyrow.py", line 39
else:
^
SyntaxError: invalid syntax

I don't use python often, so I may be making a stupid mistake. I'd appreciate any suggestions.

Streph

User avatar
Citroen
SpamTeam
Posts: 8062
Joined: March 16th, 2006, 3:28 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Using Python to interface with the PM

Post by Citroen » February 24th, 2014, 6:31 pm

Python being an awful language (I hate semantic whitespace) has two active versions (because the entirely stupid developers made python3 so that it is not 100% compatible with python2). When you get inexplicable errors the first thing to do is try it with the other flavour of python. So in this case try python3.

There are words to describe the omnishambles created by the python2 vs python3 debacle, but they are mostly not for use in a family forum.

Streph
Paddler
Posts: 3
Joined: February 24th, 2014, 11:52 am

Re: Using Python to interface with the PM

Post by Streph » February 24th, 2014, 8:09 pm

Thanks, Citroen,

Unfortunately, I get the same error with python3.

Streph

User avatar
Citroen
SpamTeam
Posts: 8062
Joined: March 16th, 2006, 3:28 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Using Python to interface with the PM

Post by Citroen » February 24th, 2014, 8:58 pm

Looks like all the python files need to be unmangled with "dos2unix".

Code: Select all

 find . -name "*py" | xargs dos2unix
Also there's a coding problem in lines 37 through to 43 in pyrow.py.
Here's an untested patch file

Code: Select all

--- pyrow.py    2014-02-25 01:13:46.272848657 +0000
+++ pyrow.py~~  2014-02-25 01:13:44.132615324 +0000
@@ -27,22 +27,24 @@
        return ergs

 class pyrow:
-
        def __init__(this, erg):
                #Configures usb connection and sets erg value
-
                if sys.platform != 'win32':
                        try:
                                #Check to see if driver is attached to kernel (linux)
                                if erg.is_kernel_driver_active(interface):
                                        erg.detach_kernel_driver(interface)
-                       else:
-                               print "DEBUG: usb kernel driver not on " + sys.platform
+                               else:
+                                       print "DEBUG: usb kernel driver not on " + sys.platform

-               #Claim interface (Needs Testing To See If Necessary)
+                               #Claim interface (Needs Testing To See If Necessary)
+                       except:
+                               pass
+
                usb.util.claim_interface(erg, interface)

                #Linux throws error, reason unknown
+
                try:
                        erg.set_configuration() #required to configure USB connection
                        #Ubuntu Linux returns 'usb.core.USBError: Resource busy' but rest of code still works

Streph
Paddler
Posts: 3
Joined: February 24th, 2014, 11:52 am

Re: Using Python to interface with the PM

Post by Streph » February 26th, 2014, 12:17 pm

Thanks for the patch.

I have statshow.py working now. Enclosing all of the print statements in parentheses got rid of the errors, and I have to run it as root, but it's working! It's exciting to contemplate finally having an interface between my PM3 and a Linux machine.

Locked