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

btn=Button() gives error #193

Closed
ndward opened this issue Sep 4, 2016 · 39 comments
Closed

btn=Button() gives error #193

ndward opened this issue Sep 4, 2016 · 39 comments

Comments

@ndward
Copy link

ndward commented Sep 4, 2016

Hi. I've just made a website, ev3python.com, to help beginners make a start with EV3 Python (when I say 'beginners', I mean people who have a basic knowledge of standard Python but no knowledge of EV3 Python). I'm pretty much a Python beginner myself, despite being a teacher of computer science. I find EV3 Python to be quite neat and I've got most of the robot-specific functions worked out but not all of them. My biggest problems are with the EV3 buttons and the LCD. I've been advised to open one issue page per problem so let's start with the EV3 buttons.
I've come across many EV3 Python programs that include the detection of button presses but they all fail for me at the line btn=Button() . I get NameError: name 'Button' is not defined. Is it just me? Is there a bug? There is an example of button code on http://www.ev3dev.org/docs/tutorials/using-ev3-buttons/ but it's far too complicated: 25 lines of code just to detect a button press? There must be a better way? Please help! Can someone post a simple program showing good button code?

@ddemidov
Copy link
Member

ddemidov commented Sep 4, 2016

A similar question was asked ev3dev/ev3dev-lang not long ago, but it was buried in another issue, so I'll copy the answer here:

Currently you have to create an instance of the Button class and use it to query button states:

import ev3dev.auto as ev3

btn = ev3.Button()
# Is 'Left' button pressed?
print('yes' if btn.left else 'no')

# Check if any buttons are pressed:
print('yes' if btn.any() else 'no')

# Do something when state of 'Left' button changes:
def report(state):
    print('pressed' if state else 'not pressed')
btn.on_left = report

while True:  # This loop checks buttons state continuously, calls appropriate event handlers
  btn.process()

Does this work for you, or are there any errors? (I've just checked, it does work for me).

@ndward
Copy link
Author

ndward commented Sep 4, 2016

Sorry. Errors. Because I'm using Python3?

File "demidov-button.py", line 3, in
btn = ev3.Button()
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1851, in init
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1855, in _button_file
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 74, in file_handle
OSError: [Errno 29] Illegal seek

@dwalton76
Copy link
Collaborator

@ndward can you check if you are missing the shebang in your demidov-button.py? The fact that "btn = ev3.Button()" is on line 3 in the traceback suggests that is the case.

@dwalton76
Copy link
Collaborator

Closing...re-open if needed

@ndward
Copy link
Author

ndward commented Sep 22, 2016

I'm back from vacation. This is my program:

#!/usr/bin/env python3

from ev3dev.auto import *
# btn = Button()   Also tried this. Which is correct?
btn = ev3.Button()
# Is 'Left' button pressed?
print('yes' if btn.left else 'no')

# Check if any buttons are pressed:
print('yes' if btn.any() else 'no')

# Do something when state of 'Left' button changes:
def report(state):
    print('pressed' if state else 'not pressed')
btn.on_left = report

while True:  # This loop checks buttons state continuously, calls appropriate event handlers
    btn.process()

and this is the error message:
File "demidov-button.py", line 5, in
btn = ev3.Button()
NameError: name 'ev3' is not defined

If I use btn = Button() instead of btn = ev3.Button() (which is right?) I get this error message:
btn = Button() #Also tried this. Which is correct?
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1851, in init
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1855, in _button_file
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 74, in file_handle
OSError: [Errno 29] Illegal seek

I'm using kernel 3.16.7-ckt21-9-ev3dev-ev3

You asked about the shebang but I'm running the program from the command line so the shebang should make no difference(?)

@ddemidov
Copy link
Member

from ev3dev.auto import *
# btn = Button()   Also tried this. Which is correct?

This form is the correct one. import * imports everything defined in ev3dev.auto into global namespace. Can you please rerun with the change?

@ddemidov ddemidov reopened this Sep 22, 2016
@ndward
Copy link
Author

ndward commented Sep 22, 2016

Thanks for quick answer. As I said in my previous message, when I use btn = Button() I get this error message:
btn = Button()
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1851, in init
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1855, in _button_file
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 74, in file_handle
OSError: [Errno 29] Illegal seek

@ddemidov
Copy link
Member

I am trying to reflash my ev3 with the latest nightly build to see if there is a problem with the latest published version of the library.

@ddemidov
Copy link
Member

Sorry, I can not reproduce this with the version that is installed with the latest nightly build. This is what I get after fresh install of nightly-ev3dev-jessie-ev3-generic-2016-09-04.img.xz (whoops, its not actually the latest one anymore, but it was half an hour ago, I promise). I followed instructions at the Getting started page. By the way, congratulations to whoever picked Etcher as the default tool, it does the job with minimum fuss!

This is with python2 (python3 is not yet enforced in v0.6.0):

robot@ev3dev:~$ python
Python 2.7.9 (default, Mar  1 2015, 13:52:09) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ev3dev.auto import *
>>> btn = Button()
>>> btn.left
False
>>> btn.left # Now with the button pressed:
True
>>> 

And this is with python3:

robot@ev3dev:~$ python3
Python 3.4.2 (default, Oct  8 2014, 14:47:30) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ev3dev.auto import *
>>> btn = Button()
>>> btn.left
False
>>> btn.left
True
>>> 

@ndward
Copy link
Author

ndward commented Sep 22, 2016

Thanks for trying. This is what I get in Python3:

from ev3dev.auto import *
btn = Button()
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1851, in init
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1855, in _button_file
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 74, in file_handle
OSError: [Errno 29] Illegal seek

I'm reluctant to work with nightly builds because I'm trying to keep things as simple as possible for beginners.

@ddemidov
Copy link
Member

This looks like you have a problem with the input file that is used with the Button class. Its defined here: https://github.com/rhempel/ev3dev-lang-python/blob/0.6.0/ev3dev/ev3.py#L166-L173. The error seems to indicate that a seek operation was tried on a non-seekable object (e.g. a stream). I'll try to reflash with an 'official' image tomorrow and retry if nobody has better ideas.

@ndward
Copy link
Author

ndward commented Sep 23, 2016

I just upgraded the kernel from 9 to 15 (4.4.19-15-ev3dev-ev3) and have Brickman v.0.8.0. I used option 2 on http://www.ev3dev.org/docs/tutorials/upgrading-ev3dev/
Is that upgrade process exactly equivalent of downloading a nightly image? What are the disadvantages of using a nightly image?
When I run this script

#!/usr/bin/env python3

from ev3dev.ev3 import *

btn = Button()

# Is 'Left' button pressed?
print('yes' if btn.left else 'no')

# Check if any buttons are pressed:
print('yes' if btn.any() else 'no')

# Do something when state of 'Left' button changes:
def report(state):
    print('pressed' if state else 'not pressed')
btn.on_left = report

while True:  # This loop checks buttons state continuously, calls appropriate event handlers
    btn.process()

I still get
File "demidov-button.py", line 5, in
btn = Button()
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1851, in init
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 1855, in _button_file
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 74, in file_handle
OSError: [Errno 29] Illegal seek

Thanks for your patience!

@WasabiFan
Copy link
Member

It's worth noting that you are now using a version of this library that doesn't support the kernel version you upgraded to. You should follow the instructions at the top of the README on this repo to upgrade. Still, I doubt it will fix the button issue.

@ndward
Copy link
Author

ndward commented Sep 23, 2016

Do you mean I should run
sudo apt-get update
sudo apt-get install python3-ev3dev

I did that and it says 'python3-ev3dev is already the newest version.'

I've spent so much time doing updates and upgrades, I suppose I really need to try working with a nightly build. Again, are there any disadvantages or risks associated with that apart from having to backup my Python files from the card to the PC?

@WasabiFan
Copy link
Member

Do you mean I should run...

Yes.

I did that and it says 'python3-ev3dev is already the newest version.'

Odd. I suspect the problem is related to a separate installation of the module. Re-flashing (as discussed below) will probably fix it.

I've spent so much time doing updates and upgrades, I suppose I really need to try working with a nightly build. Again, are there any disadvantages or risks associated with that apart from having to backup my Python files from the card to the PC?

No, in general they've been pretty consistently functional, and there's practically no risk of it causing any other issues; worst-case is you have to reflash. Just make sure that you back up the required files.

@ndward
Copy link
Author

ndward commented Sep 23, 2016

Eureka (sigh). I've just copied the nightly build to a different SD card and the button code is working for me at last. I deduce that updating and upgrading does not have the same effect as using a fresh new nightly version, something that wasn't clear to me as I read the page http://www.ev3dev.org/docs/tutorials/upgrading-ev3dev/
Should I assume that my card had been corrupted in some way and that the updates and upgrades were not fixing the problem? If you can confirm that option 2 and option 1 on that page are not equivalent and that flashing is a better option than upgrading not only because it's faster but also because it can fix errors that may have developed then I guess this issue can be closed.
The problems that I have had suggest to me that the getting started page should not propose that pre-release file from Dec 2015 which is much too old now, but should propose instead the nightly build. Or simply designate a recent nightly build as a 'pre-release'?

@ddemidov
Copy link
Member

I tried to flash ev3-ev3dev-jessie-2015-12-30.img.xz which is linked to from the Getting Started page. The situation is a bit different there. On a clean system, python 2 still works:

robot@ev3dev:~$ python
Python 2.7.9 (default, Mar  1 2015, 13:52:09) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ev3dev.ev3 import *
>>> btn = Button()
>>> btn.left
False
>>> btn.left
True
>>> 

However, unlike in the nightly build, python3 version of the library is not installed out of the box.

sudo apt-get update
sudo apt-get install python3-ev3dev

installs version 0.7.0-rc1 for me. The Button class still works:

robot@ev3dev:~$ python3
Python 3.4.2 (default, Oct  8 2014, 14:47:30) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ev3dev.ev3 import *
>>> btn = Button()
>>> btn.left
False
>>> btn.left
True
>>> 

So I am at a loss. Looks like reflashing with a nightly build is the best option for now.

@WasabiFan
Copy link
Member

@ddemidov See @ndward's comment just above yours; he got it to work by re-flashing.

I deduce that updating and upgrading does not have the same effect as using a fresh new nightly version, something that wasn't clear to me as I read the page

Yes, re-flasing resets the card, so anything you did (good or bad) will be reset.

Should I assume that my card had been corrupted in some way and that the updates and upgrades were not fixing the problem?

I can't say what it was; when in doubt, reset it.

The problems that I have had suggest to me that the getting started page should not propose that pre-release file from Dec 2015 which is much too old now, but should propose instead the nightly build. Or simply designate a recent nightly build as a 'pre-release'?

We expect to release an official image (which will be pretty much exactly the same as the most recent nightly) within the next few days -- whenever we release v0.7.0 of this Python package. So I agree in general, but it won't be needed at this point.

@ndward
Copy link
Author

ndward commented Sep 23, 2016

No, wait - I've just realised that now that the button code works most of my scripts involving motors no longer work! Some scripts still work. The scripts that don't work don't crash but the motors do nothing. Has something been changed in the motors?

@WasabiFan
Copy link
Member

Did you follow the README instructions to install the right version of the library?

@ddemidov
Copy link
Member

This works for me on the fresh nightly build (without upgrading python3-ev3dev):

robot@ev3dev:~$ python3
Python 3.4.2 (default, Oct  8 2014, 14:47:30) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ev3dev.ev3 import *
>>> m = LargeMotor('outB')
>>> m.connected
True
>>> m.run_timed(time_sp=1000, speed_sp=600)
>>> 

(The result is that motor turns for 1 second).

@ddemidov
Copy link
Member

So may be the reason is that @ndward's scripts need to be updated to the latest motor API?

@WasabiFan
Copy link
Member

Yes, that's a good line of inquiry. @ndward Can you provide a copy of one of the scripts that isn't working?

@ndward
Copy link
Author

ndward commented Sep 23, 2016

I ran the code suggested by Denis and it worked, so I guess I need to update all my motor scripts (grrrr). When are the APIs going to stop changing? When the APIs change in such a way that people need to update their scripts could you make it more obvious? Especially for something as basic as motors. Is there something other than motors that has changed?

I'm not sure I understand WasabiFan's message about following the README instructions to install the right version of the library. I had assumed that the nightly build would simply be ready to use. Do you mean that after installing the nightly run to the card I need to do
sudo apt-get update
sudo apt-get install python3-ev3dev
I guess what I'm asking is whether the Python3 binding for ev3dev is installed by default in recent nightly builds.

For a while I thought that this was the page to get info on the Python binding:
http://ev3dev-lang-python.readthedocs.io/en/latest/index.html
but now it seems I was wrong and should be using:
http://python-ev3dev.readthedocs.io/en/latest/
(please confirm).

I think this is an essential document for users of EV3 Python and that it is not conspicuous enough on your site (it is on mine!). I suggest you add a 'Languages' menu header to your site with a sub-item for each of the main supported languages. The Python sub-item should not necessarily link straight to the repo page because that page is intimidating for newbies, with a lot of stuff that is not relevant to them, it seems to me. It's a shame that the important part of that page, the readme, appears at the bottom of the page. The repo page works better than it did a month ago but as I said, I don't think the link to the Read the docs page. Similarly the page http://www.ev3dev.org/docs/libraries/ is much better than it was but again, the Get started with Python link goes to the repo page which is not very newbie-friendly. I still don't see any advice for newbies about how they can get their Python scripts onto the brick, how to run their scripts either via SSH or Brickman, and how to download scripts for the brick to the computer.

I'm disappointed that you don't yet consider my site ev3python.com to be good enough to be worth mentioning near your 'get started with Python' link since that is exactly what my site is about!

@ndward
Copy link
Author

ndward commented Sep 23, 2016

I ran
sudo apt-get update
sudo apt-get install python3-ev3dev
and now my scripts seem to work so I guess I don't need to update them after all. But the motor on port C seems to be behaving erratically - I'm looking into it.
I'm assuming that Denis suggested trying m.run_timed(time_sp=1000, speed_sp=600) rather than m.run_timed(time_sp=1000, duty_cycle_sp=50) because speed_sp works even if python3 is not upgraded? And why did Denis mention upgrading Python3 when the readme says install python3? Do I need to install then upgrade?

@WasabiFan
Copy link
Member

When are the APIs going to stop changing? When the APIs change in such a way that people need to update their scripts could you make it more obvious?

They haven't changed in the last 7 months (aside from bug fixes and one obscure brand rename). How do you expect us to make it more clear? This is made a bit rocky by the fact that we are just getting around to releasing a new official image, but nonetheless we have a news feed which lists all breaking changes and you can subscribe to it to stay up-to-date. This will be better once we have gotten a new image released, which as I said before should be happening in the coming days.

Is there something other than motors that has changed?

http://www.ev3dev.org/news/2016/04/11/Kernel-Release-Cycle-10/

I'm not sure I understand WasabiFan's message about following the README instructions to install the right version of the library. I had assumed that the nightly build would simply be ready to use. Do you mean that after installing the nightly run to the card I need to do ... I guess what I'm asking is whether the Python3 binding for ev3dev is installed by default in recent nightly builds.

It is, but not at a version that is fully supported; so you do have to run those commands to get the newest version of the library. Why? I can't speak to this specific case, but in general the external libraries aren't synched with kernel releases, so having everything be packaged together and up-to-date is difficult for nightlies, and it isn't even a significant problem when the solution is a one-time command.

This is the only language library that is shipped with the image; if you are using JavaScript or C++ or Java or any of the other languages, you have to install it yourself -- that's just a required part of software development in general. We try to make Python easiest to use because it is the most popular, but at some point we have to accept that we won't be able to keep them in sync for nightly builds.

For a while I thought that this was the page to get info on the Python binding ... but now it seems I was wrong and should be using: ... (please confirm).

Yes, I believe the latter is correct (@ddemidov ?).

I think this is an essential document for users of EV3 Python and that it is not conspicuous enough on your site (it is on mine!).

What site? ev3dev.org is about ev3dev as a platform, not about Python specifically; once you get to this repository, however, you are referred to those docs. There's a link in the title and a link in the "User Resources" section, and that section is linked to by the getting started area and other places. What do you believe would make the README clearer?

I suggest you add a 'Languages' menu header to your site with a sub-item for each of the main supported languages. The Python sub-item should not necessarily link straight to the repo page because that page is intimidating for newbies, with a lot of stuff that is not relevant to them, it seems to me. It's a shame that the important part of that page, the readme, appears at the bottom of the page. The repo page works better than it did a month ago but as I said, I don't think the link to the Read the docs page.

That's an interesting idea. I'm hesitant to do that immediately, but I will definitely consider it; we're currently working on some major structural changes to the site, so that might be something we do as well.

As for the issue with the README, I don't think we can do much better. Everything in this README is relevant to a beginner; there is a getting started guide, some examples, documentation links, etc., and all of those are things that every user should see. I'm unsure of how we could make this easier for new users without compromising the overall usability and functionality, even if we created a dedicated page for it.

Similarly the page http://www.ev3dev.org/docs/libraries/ is much better than it was but again, the Get started with Python link goes to the repo page which is not very newbie-friendly. I still don't see any advice for newbies about how they can get their Python scripts onto the brick, how to run their scripts either via SSH or Brickman, and how to download scripts for the brick to the computer.

This doesn't happen over night. We have an SSH page that is currently awaiting review and a bunch of other changes in various stages of completion. We're working on it, but right now there are still holes as you saw a month ago.

I'm disappointed that you don't yet consider my site ev3python.com to be good enough to be worth mentioning near your 'get started with Python' link since that is exactly what my site is about!

I've discussed this via email. In short, I don't want to rely on your site because we can't be confident that it won't disappear or change drastically -- it's not because we don't like it. It might make sense to link to it from the repo README, though even then it's a significant risk.

And why did Denis mention upgrading Python3 when the readme says install python3? Do I need to install then upgrade?

I'm not sure exactly what you mean. Can you tell us the exact image you flashed from, the kernel version that it says you are using, and the output of the following commands?

dpkg -s python-ev3dev
dpkg -s python3-ev3dev

@ddemidov
Copy link
Member

I had assumed that the nightly build would simply be ready to use.

I can confirm this. The latest available version of python3-ev3dev is installed out of the box in the latest nightly builds, so there is no need to install/upgrade the package.

For a while I thought that this was the page to get info on the Python binding:
http://ev3dev-lang-python.readthedocs.io/en/latest/index.html
but now it seems I was wrong and should be using:
http://python-ev3dev.readthedocs.io/en/latest/
(please confirm).

That is correct, the latter link is the one that is autogenerated with every commit into this repo. @rhempel is the owner of the former, so I guess it was an experiment of his. @rhempel, do you think you could delete the project on readthedocs?

I ran
sudo apt-get update
sudo apt-get install python3-ev3dev
and now my scripts seem to work so I guess I don't need to update them after all.

My guess is the following happens: if you tried to run my example

m.run_timed(time_sp=1000, speed_sp=600)

it is equivalent to

m.time_sp=1000
m.speed_sp=600
m.run_timed()

The speed_sp attribute stays set to 600 after the command is finished, so if you do now

m.run_timed(time_sp=1000, duty_cycle_sp=100)

the motor will turn, but not because you've set duty_cycle_sp=100 (the attribute is only used with run_direct() command); it will still turn at the speed of 600 clicks per second. Further, we removed destructors from motor classes at some point in the past, so motors are not reset at program exit, and speed_sp=600 could still apply to subsequently launched scripts. You can confirm this by

>>> m.run_timed(time_sp=1000, speed_sp=600) # should turn
>>> m.reset()
>>> m.run_timed(time_sp=1000, duty_cycle_sp=100) # should silently fail

As for the issue with the README, I don't think we can do much better.

We could link to @ndward's site from the README. We could even incorporate the pages from that site into our documentation on readthedocs. Although I am not sure if @ndward would agree, or how much work it would involve (my guess is not that much). But I guess this would also answer @WasabiFan's concern about stability of @ndward's site. @ndward, what do you think?

And why did Denis mention upgrading Python3 when the readme says install python3? Do I need to install then upgrade?

The latest nightly images are good as is, there is no need to install/upgrade anything. I guess you are talking about my experiment with the officially released image, where I had to install python3-ev3dev? Also, my understanding is that apt-get install <package> installs the package when its not present in the system, and upgrades it to the latest available version otherwise. Could this be the reason for misunderstanding?

@ndward
Copy link
Author

ndward commented Sep 24, 2016

Hi, and thanks for your long comments. I downloaded nightly-ev3dev-jessie-ev3-generic-2016-09-22.img. I have kernel 4.4.19-15-ev3dev-ev3.
robot@ev3dev:$ dpkg -s python-ev3dev
Package: python-ev3dev
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 136
Maintainer: Ralph Hempel [email protected]
Architecture: all
Version: 0.6.0
Depends: python-pil, python:any (<< 2.8), python:any (>= 2.7.5-5
)
Description: Python language bindings for ev3dev
This package is a pure Python binding to the peripheral
devices on hardware that is supported by ev3dev.org - a
minimal Debian distribution optimized for the LEGO
MINDSTORMS EV3.

robot@ev3dev:$ dpkg -s python3-ev3dev
Package: python3-ev3dev
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 182
Maintainer: Ralph Hempel [email protected]
Architecture: all
Source: python-ev3dev
Version: 0.7.0
rc1
Depends: python3-pil, python3:any (>= 3.3.2-2~)
Description: Python language bindings for ev3dev
This package is a pure Python binding to the peripheral
devices on hardware that is supported by ev3dev.org - a
minimal Debian distribution optimized for the LEGO
MINDSTORMS EV3.

My EV3 motors are behaving strangely at the moment. Yesterday, for example, the standard remote control script (which worked perfectly a couple of days ago with the old card) would only make the motors turn forwards, not backwards. I restarted the brick and now the same script does not turn the motors at all. The script below

#!/usr/bin/env python3

from ev3dev.ev3 import *
from time   import sleep

mB = LargeMotor('outB')
mC = LargeMotor('outC')

mB.run_timed(time_sp=3000, duty_cycle_sp=50)

print("duty cycle= " + str(mB.duty_cycle))
sleep(5)

mC.run_timed(time_sp=1000, speed_sp=-600)
print("duty cycle= " + str(mC.duty_cycle))
sleep(5)

fails to turn the motor with turns the motor with duty_cycle_sp=50 but does turn the motor with speed_sp=-600. Output is as follows:
duty cycle= 0
duty cycle= -67

I don't really understand the reason you give for not wanting to link to my site: "I don't want to rely on your site because we can't be confident that it won't disappear or change drastically -- it's not because we don't like it. It might make sense to link to it from the repo README, though even then it's a significant risk." Surely if my site were to evolve in a way you didn't like you would just remove the link. Where is the "significant risk" and does it really override the potential benefit of giving EV3 Python beginners access to dozens of simple code samples, just what your own site is currently lacking? There isn't much risk of my site disappearing for it's hosted free by Google and if there is one company in the world that is unlikely to disappear then that could well be Google, wouldn't you agree?

@ddemidov
Copy link
Member

My EV3 motors are behaving strangely at the moment. Yesterday, for
example, the standard remote control script (which worked perfectly a
couple of days ago with the old card) would only make the motors turn
forwards, not backwards. I restarted the brick and now the same script does
not turn the motors at all.

The symptoms convince me in my earlier assumption: your duty_cycle_sp
parameter is ignored, and speed_sp value you set at some point is used
instead. Reset zeroes speed_sp and motors stop turning.

@ndward
Copy link
Author

ndward commented Sep 24, 2016

I think I understand what you are saying. I probably reset the motors at some point, in effect disabling the duty_cycle_sp argument until speed_sp is given a new non-zero value.
Is the speed argument new or was it always there? When I run this

from ev3dev.ev3 import *
mB = LargeMotor('outB')
mB.speed_sp=500
print (mB.speed_sp)
mB.reset()
print (mB.speed_sp)

I get this output
500
0
so reset() really does set speed_sp to zero. But isn't that like a trap for beginners because it means that after resetting the motor a simple command like mB.run_timed(time_sp=3000, duty_cycle_sp=50) will not have the expected effect? Shouldn't reset() set speed_sp to some non-zero value? What is the range of values that can be used for speed_sp?

@ndward
Copy link
Author

ndward commented Sep 24, 2016

(Off topic, I know). There is a section on the remote control on this page
http://ev3dev-lang-python.readthedocs.io/en/latest/index.html
but I can't find anything here:
http://python-ev3dev.readthedocs.io/en/latest/

@ndward
Copy link
Author

ndward commented Sep 24, 2016

I found the parameter max_speed and can thus answer my own question above about the range of values of speed_sp for the standard EV3 motors: Medium Motor has max_speed= 1560 and Large Motor has max_speed=1050. Since these motors are the standard motors perhaps it would be worth mentioning these values in the documentation for max_speed? Similarly it would perhaps be worth mentioning in the documentation for count_per_rot that count_per_rot=360 for both the standard EV3 medium motor and the standard EV3 large motor (thus one count = one degree of course)

I understand the values for max_speed given above are theoretical values so for convenience it might make sense to limit actual used values to 1500 and 1000?

@ddemidov
Copy link
Member

I probably reset the motors at some point, in effect disabling the duty_cycle_sp argument until speed_sp is given a new non-zero value.I probably reset the motors at some point, in effect disabling the duty_cycle_sp argument until speed_sp is given a new non-zero value.

duty_cycle_sp is ignored irregardless of what value speed_sp has. This has changed since kernel 10; @WasabiFan pointed to the kernel release announcement where the change is advertised, here it is again: http://www.ev3dev.org/news/2016/04/11/Kernel-Release-Cycle-10/.

So currently, motors are always controlled by speed_sp parameter except for the run_direct() command. Basically, you should never set duty_cycle_sp and always use speed_sp.

There is a section on the remote control on this page
http://ev3dev-lang-python.readthedocs.io/en/latest/index.html
but I can't find anything here:
http://python-ev3dev.readthedocs.io/en/latest/

Opened new issue: #214.

@ndward
Copy link
Author

ndward commented Sep 24, 2016

You're right, I missed that announcement about kernel 10. The getting started section of ev3dev.org still proposes the 2015-12-30 pre-release which includes version 9 of the kernel, I believe, which is what I was using until very recently. I probably won't be the only one getting confused over the changes to the motor commands until a new pre-release is proposed on the getting started page.

@ndward
Copy link
Author

ndward commented Sep 24, 2016

I found this tip very helpful: "So currently, motors are always controlled by speed_sp parameter except for the run_direct() command. Basically, you should never set duty_cycle_sp and always use speed_sp" and I'm surprised to see that that tip is not included on http://www.ev3dev.org/news/2016/04/11/Kernel-Release-Cycle-10/
Perhaps it should be?

@ndward
Copy link
Author

ndward commented Sep 24, 2016

I've studied the documentation for speed_sp but still have some questions.
Prior to kernel cycle 10 I encouraged my beginner audience(?) to use non-regulated i.e. duty_cycle_sp instead of speed_sp. This was largely because the range of values for duty_cycle_sp is -100 to 100, just like in EV3-G. As I recall, the units of duty_cycle_sp are rpm (at least for the EV3 motors), is that correct? (It doesn't seem to be indicated in the documentation). I also understand that for the EV3 motors the units of speed_sp are degrees per second. This would mean that a duty_cycle_sp of 100 (which I assume to be equivalent to a power of 100 in EV3-G) would be 100 rpm or 600 deg/s so that means that we should normally use a range of -600 to +600 for speed_sp. Again, I didn't find a statement in the documentation that speed_sp=600 is equivalent to duty_cycle_sp=100, at least for EV3 motors.
These values are very different from the values returned by max_speed, which are 1050 for EV3 large motor and 1560 for EV3 medium motor, but I do understand that these values are theoretical and in fact not attainable. My tests suggest that it is possible to use values for speed_sp up to about 900 before the motor starts to misbehave but I will recommend to my beginner audience that only values up to 600 should be used to maintain consistency with duty_cycle_sp and EV3-G. Does that make sense? While I used the script below to test different values of speed_sp I noticed that a value of -900 makes the motor runs forwards, not backwards, and a number of other scripts are also failing to run backwards for me. What could cause that?

from ev3dev.ev3 import *
from time import sleep
m = LargeMotor('outB')
m.run_to_rel_pos(position_sp=360, speed_sp=900, stop_action="hold")
sleep(5)

As I understand it, the difference between run_direct and run_forever is that run_direct is unregulated and uses duty_cycle_sp while run_forever is regulated and uses speed_sp? If I set a value for duty_cycle_sp (with or without actually running run_direct) this should not interfere with the other (regulated) commands, should it? Can you confirm that if you run the run_forever command then changing speed_sp while running will NOT take effect immediately?

@ndward
Copy link
Author

ndward commented Sep 24, 2016

I would like to make sure I give the right advice to users who have kernel version 9 and therefore need to upgrade. I will recommend that they use option 1 (reflash SD card) on http://www.ev3dev.org/docs/tutorials/upgrading-ev3dev/ but I understand this only gives an upgraded version of EV3dev and that they need to upgrade the Python binding also. Please confirm that all that needs to be done to get the latest Python binding is

sudo apt-get update
sudo apt-get install python3-ev3dev

Specifically I would like to be reassured that it is NOT necessary to run the code below:

sudo apt-get install python3-pil
sudo python3 -m easy_install python-ev3dev

@WasabiFan
Copy link
Member

I'm surprised to see that that tip is not included on http://www.ev3dev.org/news/2016/04/11/Kernel-Release-Cycle-10/ ... Perhaps it should be?

It is, although it is worded differently; it's the first "breaking changes" bullet point. You should read the motor tutorial if you want to make sure you understand the various properties (the guide explains using the command line, but it applies to what you are doing too). It also may be helpful to look at the tacho motor detailed docs.

As I recall, the units of duty_cycle_sp are rpm (at least for the EV3 motors), is that correct?

No. The duty_cycle_sp property is a duty cycle percentage; from Wikipedia: "A duty cycle is the percentage of one period in which a signal or system is active." What that means is that 100% duty cycle is the maximum amount of power that can be applied to the motor; it has no direct relation to physical speed.

This would mean that a duty_cycle_sp of 100 (which I assume to be equivalent to a power of 100 in EV3-G) would be 100 rpm or 600 deg/s so that means that we should normally use a range of -600 to +600 for speed_sp.

As mentioned above, the duty cycle and speed values do not do the same thing, and can't be compared like this. Additionally, if I remember correctly, the values in EV3-G correspond to the functionality of speed_sp, albeit scaled such that EV3-G's 100 is our 1050 for large motors (or something around there; it's not exact).

My tests suggest that it is possible to use values for speed_sp up to about 900 before the motor starts to misbehave but I will recommend to my beginner audience that only values up to 600 should be used to maintain consistency with duty_cycle_sp and EV3-G. Does that make sense?

Because the units don't correspond like that, I don't believe this makes sense. Additionally, I don't expect that it would "misbehave", per se. It would just apply full power (100% duty cycle) without being able to get to its target speed if you gave it one that was unattainable.

While I used the script below to test different values of speed_sp I noticed that a value of -900 makes the motor runs forwards, not backwards, and a number of other scripts are also failing to run backwards for me. What could cause that?

By giving it a negative speed there, you're telling it to go to forward 360 degrees by rotating backwards... that doesn't make much sense 😆 The negative sign is ignored with run_to_rel_pos; give it a negative distance to go in the opposite direction.

If I set a value for duty_cycle_sp (with or without actually running run_direct) this should not interfere with the other (regulated) commands, should it?

Correct; the duty cycle is completely ignored unless you use run_direct.

Can you confirm that if you run the run_forever command then changing speed_sp while running will NOT take effect immediately?

Correct.

I would like to make sure I give the right advice to users who have kernel version 9 and therefore need to upgrade. I will recommend that they use option 1 (reflash SD card) on http://www.ev3dev.org/docs/tutorials/upgrading-ev3dev/ but I understand this only gives an upgraded version of EV3dev and that they need to upgrade the Python binding also.

It turns out that I was incorrect; if you re-flash, you don't need to run either of those sets of commands. It should "just work" (correct, @ddemidov?).

Is it that speed_spis the requested speed and speedis the actual speed?

Yep, exactly. Same with duty_cycle and duty_cycle_sp. sp stands for "setpoint", which is the goal value that you tell it.

@ddemidov
Copy link
Member

It turns out that I was incorrect; if you re-flash, you don't need to run either of those sets of commands. It should "just work" (correct, @ddemidov?).

Correct, python3 version 0.7.0-rc1 of the library comes preinstalled.

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

4 participants