Skip to content

LED "delay_on"/"delay_off" broken? #234

Closed

Description

@WasabiFan
  • ev3dev version: 4.4.19-15-ev3dev-ev3
  • ev3dev-lang-python version: 0.7.0

I'm getting odd errors while attempting to set the delay_on/delay_off attributes for use with the timer trigger. For example:

>>> Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer", delay_on=2000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/ev3dev/ev3.py", line 90, in set
    setattr(led, k, kwargs[k])
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 2051, in delay_on
    self._delay_on = self.set_attr_int(self._delay_on, 'delay_on', value)
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 213, in set_attr_int
    return self._set_attribute(attribute, name, str(int(value)))
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 202, in _set_attribute
    attribute.write(value.encode())
io.UnsupportedOperation: File not open for writing

I have also seen a different error, which I can no longer reproduce:

>>> Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer", delay_off=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/ev3dev/ev3.py", line 90, in set
    setattr(led, k, kwargs[k])
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 2065, in delay_off
    self._delay_off = self.set_attr_int(self._delay_off, 'delay_off', value)
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 213, in set_attr_int
    return self._set_attribute(attribute, name, str(int(value)))
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 202, in _set_attribute
    attribute.write(value.encode())
OSError: [Errno 19] No such device

The first one (and likely the second) seem to be related to the file handle cache, but I can't quite tell. @ddemidov IIRC you have had a fair amount of experience with the caching system; thoughts?

I can write to these attributes without issue from Bash.

Activity

  1. ddemidov commented on Oct 21, 2016

    @ddemidov
    Member

    Would that work with

    Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer")
    time.sleep(0.1)
    Leds.set(Leds.LEFT, delay_on=2000)

    ? The delay_{on,off} attributes are only created when trigger is set to timer, and there is some delay in between.

  2. WasabiFan commented on Oct 21, 2016

    @WasabiFan
    MemberAuthor

    Unfortunately, that doesn't fix it; even giving it a few whole seconds manually doesn't resolve the problem.

    >>> Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer")
    # 10 seconds pass...
    >>> Leds.set(Leds.LEFT, delay_on=2000)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3/dist-packages/ev3dev/ev3.py", line 90, in set
        setattr(led, k, kwargs[k])
      File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 2051, in delay_on
        self._delay_on = self.set_attr_int(self._delay_on, 'delay_on', value)
      File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 213, in set_attr_int
        return self._set_attribute(attribute, name, str(int(value)))
      File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 202, in _set_attribute
        attribute.write(value.encode())
    OSError: [Errno 19] No such device
  3. ddemidov commented on Oct 21, 2016

    @ddemidov
    Member

    Ok, will look into it this weekend.

  4. self-assigned this
    on Oct 21, 2016
  5. ensonic commented on Oct 21, 2016

    @ensonic
    Contributor

    I am applying the same workaround:
    https://github.com/OpenRoberta/robertalab-ev3dev/blob/develop/roberta/ev3.py#L191-L200
    Let me re-test here too.

  6. ddemidov commented on Oct 21, 2016

    @ddemidov
    Member
    Leds.set(Leds.LEFT, brightness_pct=1, trigger="timer")
    time.sleep(0.1)
    Leds.set(Leds.LEFT, delay_on=2000)

    works for me. But, when I do

    Leds.set(Leds.RIGHT, brightness_pct=1, trigger="timer", delay_on=2000)

    I get the same error as you (File not open for writing). And after that, any attempt to change the delay_on attribute results in the same error. So it does look like a bug in cache implementation.

  7. ddemidov commented on Oct 21, 2016

    @ddemidov
    Member

    I think I know what happens. Device._attribute_file_open() checks permissions on the requested file and opens it in appropriate mode ('r' for readonly, 'w' for write-only, 'r+' for read-write). After trigger is set to 'timer', new attributes, 'delay_on'/'delay_off' are created, but initially those have read-only access, and after some delay the access is changed to read-write. So when you set both trigger and delay_on in the same call, the file is opened in incorrect access mode.
    I guess we could change the implementation of Device._set_attribute() to attempt a file reopen on failure, but since this should only be needed for the Led class, and should result in an error at first attempt anyway, I am inclined to mark this as 'wontfix'.

  8. WasabiFan commented on Oct 21, 2016

    @WasabiFan
    MemberAuthor

    Nice sleuthing! That cause makes sense.

    Honestly, I don't think we can call this wontfix and move on. We expect most beginners to be using Python and this library, and that demographic can't really handle having to work around special cases like this, Whereas more experienced programmers would probably be fine with just a note in the docs, I don't think that beginners would be able to use the functionality at all.

    I'd say we should aim for some sort of a one-statement solution. I would be fine with something like a set_timer(brightness_pct, delay_on, delay_off) method which handles this manually or some other special case for the set method. We'd need to make sure that it works on individual LEDs, LED groups and any other LED-related interfaces though.

  9. ddemidov commented on Oct 22, 2016

    @ddemidov
    Member

    ev3dev-lang-cpp has led::flash(on_ms, off_ms) that does this. We could port the function to python.

  10. WasabiFan commented on Oct 22, 2016

    @WasabiFan
    MemberAuthor

    That looks like a nice solution; I think that would work well.

  11. ensonic commented on Oct 30, 2016

    @ensonic
    Contributor

    The change of the permissions is strange though and we should have to work around it. We know that is takes a little while before the new attributes appear, but they should be created with the right permissions right away. @ddemidov did you wrote a little test to repro this? @dlech can you confirm this from the kernel side?
    Also adding special api is kind of nice, but the low-level access would still fail.

  12. ddemidov commented on Oct 30, 2016

    @ddemidov
    Member

    This was discussed in ev3dev/ev3dev#225

  13. ensonic commented on Oct 30, 2016

    @ensonic
    Contributor

    Ah, right. What if we do a led-blink thread? I could give this a try. This way we don't need to switch attributes at all and can also do various other blink patterns and pulses.

  14. dlech commented on Oct 30, 2016

    @dlech
    Member

    This comment is still accurate and sums up my views on the matter.

  15. ddemidov commented on Oct 30, 2016

    @ddemidov
    Member

    I am thinking of providing a custom version of Led.trigger property, so that the property writer only returns when the delay_on and delay_off are actually created and have the appropriate permissions.
    This should be more reliable than doing things in a separate thread.

  16. ensonic commented on Oct 30, 2016

    @ensonic
    Contributor

    @ddemidov the downside of the custom code is that it will be less deterministic. It can wait for quite some time and applications might prefer a non-blocking behavior. At least that is one thing that I don't like about the code that I have in the openroberta codebase.

  17. ddemidov commented on Oct 31, 2016

    @ddemidov
    Member

    We can easily provide both ways. trigger/delay_on/delay_off are standard linux attributes, so we can support those with the workaround I mentioned. And if you provide a reliable thread-based interface for pattern blinking, we could have that as well.

    the downside of the custom code is that it will be less deterministic

    If an application needs to be deterministic (although I am not sure that low EV3 resources combined with python may be deterministic), one could set the trigger to timer and delay_on to 0 during program initialization, and only set delay_on/delay_off as needed later.

  18. added this to the 0.8.0 milestone on Oct 31, 2016
  19. added a commit that references this issue on Oct 31, 2016
    273cd41
  20. added a commit that references this issue on Oct 31, 2016
    a36879b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions